[Python-checkins] peps: Update PEP 519 as per

brett.cannon python-checkins at python.org
Wed Jun 15 15:56:42 EDT 2016


https://hg.python.org/peps/rev/92feff129ee4
changeset:   6380:92feff129ee4
user:        Brett Cannon <brett at python.org>
date:        Wed Jun 15 12:56:31 2016 -0700
summary:
  Update PEP 519 as per https://mail.python.org/pipermail/python-dev/2016-June/145219.html

files:
  pep-0519.txt |  25 +++++++++++++++++++++----
  1 files changed, 21 insertions(+), 4 deletions(-)


diff --git a/pep-0519.txt b/pep-0519.txt
--- a/pep-0519.txt
+++ b/pep-0519.txt
@@ -149,7 +149,10 @@
     def fspath(path: t.Union[PathLike, str, bytes]) -> t.Union[str, bytes]:
         """Return the string representation of the path.
 
-        If str or bytes is passed in, it is returned unchanged.
+        If str or bytes is passed in, it is returned unchanged. If __fspath__()
+        returns something other than str or bytes then TypeError is raised. If
+        this function is given something that is not str, bytes, or os.PathLike
+        then TypeError is raised.
         """
         if isinstance(path, (str, bytes)):
             return path
@@ -158,13 +161,19 @@
         # methods.
         path_type = type(path)
         try:
-            return path_type.__fspath__(path)
+            path = path_type.__fspath__(path)
         except AttributeError:
             if hasattr(path_type, '__fspath__'):
                 raise
+        else:
+            if isinstance(path, (str, bytes)):
+                return path
+            else:
+                raise TypeError("expected __fspath__() to return str or bytes, "
+                                "not " + type(path).__name__)
 
-            raise TypeError("expected str, bytes or os.PathLike object, not "
-                            + path_type.__name__)
+        raise TypeError("expected str, bytes or os.PathLike object, not "
+                        + path_type.__name__)
 
 The ``os.fsencode()`` [#os-fsencode]_ and
 ``os.fsdecode()`` [#os-fsdecode]_ functions will be updated to accept
@@ -294,6 +303,14 @@
 
         path_repr = PyObject_CallFunctionObjArgs(func, NULL);
         Py_DECREF(func);
+        if (!PyUnicode_Check(path_repr) && !PyBytes_Check(path_repr)) {
+            Py_DECREF(path_repr);
+            return PyErr_Format(PyExc_TypeError,
+                                "expected __fspath__() to return str or bytes, "
+                                "not %S",
+                                path_repr->ob_type);
+        }
+
         return path_repr;
     }
 

-- 
Repository URL: https://hg.python.org/peps


More information about the Python-checkins mailing list