Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3.6
Changeset: r93389:10f89fd70107
Date: 2017-12-12 18:51 +0100
http://bitbucket.org/pypy/pypy/changeset/10f89fd70107/

Log:    Most file operations now accept a PathLike object with a __fspath__
        method.

diff --git a/pypy/module/_io/interp_io.py b/pypy/module/_io/interp_io.py
--- a/pypy/module/_io/interp_io.py
+++ b/pypy/module/_io/interp_io.py
@@ -6,6 +6,7 @@
     TypeDef, interp_attrproperty, generic_new_descr)
 from pypy.module._io.interp_fileio import W_FileIO
 from pypy.module._io.interp_textio import W_TextIOWrapper
+from pypy.module.posix import interp_posix
 
 
 class Cache:
@@ -25,7 +26,7 @@
     if not (space.isinstance_w(w_file, space.w_unicode) or
             space.isinstance_w(w_file, space.w_bytes) or
             space.isinstance_w(w_file, space.w_int)):
-        raise oefmt(space.w_TypeError, "invalid file: %R", w_file)
+        w_file = interp_posix.fspath(space, w_file)
 
     reading = writing = creating = appending = updating = text = binary = 
universal = False
 
diff --git a/pypy/module/posix/interp_posix.py 
b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -151,12 +151,26 @@
     except OperationError as e:
         if not e.match(space, space.w_TypeError):
             raise
-        if allow_fd:
-            fd = unwrap_fd(space, w_value, "string, bytes or integer")
-            return Path(fd, None, None, w_value)
-    raise oefmt(space.w_TypeError,
-                "illegal type for path parameter (expected "
-                "string or bytes, got %T)", w_value)
+    if allow_fd and space.index_CHECK(w_value):
+        fd = unwrap_fd(space, w_value, "string, bytes or integer")
+        return Path(fd, None, None, w_value)
+
+    # Inline fspath() for better error messages.
+    w_fspath_method = space.lookup(w_value, '__fspath__')
+    if w_fspath_method:
+        w_result = space.get_and_call_function(w_fspath_method, w_value)
+        if (space.isinstance_w(w_result, space.w_text) or
+            space.isinstance_w(w_result, space.w_bytes)):
+            return _unwrap_path(space, w_result, allow_fd=False)
+
+    if allow_fd:
+        raise oefmt(space.w_TypeError,
+                    "illegal type for path parameter (expected "
+                    "string, bytes, os.PathLike or integer, got %T)", w_value)
+    else:
+        raise oefmt(space.w_TypeError,
+                    "illegal type for path parameter (expected "
+                    "string, bytes or os.PathLike, got %T)", w_value)
 
 class _PathOrFd(Unwrapper):
     def unwrap(self, space, w_value):
diff --git a/pypy/module/posix/test/test_scandir.py 
b/pypy/module/posix/test/test_scandir.py
--- a/pypy/module/posix/test/test_scandir.py
+++ b/pypy/module/posix/test/test_scandir.py
@@ -207,3 +207,10 @@
             del iterator
             gc.collect()
         assert not l
+
+    def test_lstat(self):
+        posix = self.posix
+        d = next(posix.scandir())
+        with open(d) as fp:
+            length = len(fp.read())
+        assert posix.lstat(d).st_size == length
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to