cpython: ea7b6a7827a4 (original) (raw)

Mercurial > cpython

changeset 102160:ea7b6a7827a4

Issue #27186: Update os.fspath()/PyOS_FSPath() to check the return type of __fspath__(). As part of this change, also make sure that the pure Python implementation of os.fspath() is tested. [#27186]

Brett Cannon brett@python.org
date Fri, 24 Jun 2016 12:03:43 -0700
parents 7d5bc8a3c7fc
children 9c57178f13dc
files Doc/c-api/sys.rst Doc/library/os.rst Lib/os.py Lib/test/test_io.py Lib/test/test_os.py Misc/NEWS Modules/posixmodule.c
diffstat 7 files changed, 103 insertions(+), 73 deletions(-)[+] [-] Doc/c-api/sys.rst 5 Doc/library/os.rst 14 Lib/os.py 71 Lib/test/test_io.py 2 Lib/test/test_os.py 68 Misc/NEWS 3 Modules/posixmodule.c 13

line wrap: on

line diff

--- a/Doc/c-api/sys.rst +++ b/Doc/c-api/sys.rst @@ -10,8 +10,9 @@ Operating System Utilities Return the file system representation for path. If the object is a :class:str or :class:bytes object, then its reference count is incremented. If the object implements the :class:os.PathLike interface,

--- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -179,7 +179,8 @@ process and user. .. versionadded:: 3.2 .. versionchanged:: 3.6

.. function:: fsdecode(filename) @@ -192,17 +193,18 @@ process and user. .. versionadded:: 3.2 .. versionchanged:: 3.6

.. function:: fspath(path) Return the file system representation of the path.

--- a/Lib/os.py +++ b/Lib/os.py @@ -881,14 +881,11 @@ def _fscodec(): On Windows, use 'strict' error handler if the file system encoding is 'mbcs' (which is the default encoding). """

def fsdecode(filename): """Decode filename (an os.PathLike, bytes, or str) from the filesystem @@ -896,14 +893,11 @@ def _fscodec(): Windows, use 'strict' error handler if the file system encoding is 'mbcs' (which is the default encoding). """

return fsencode, fsdecode @@ -1102,27 +1096,44 @@ def fdopen(fd, *args, **kwargs): import io return io.open(fd, *args, **kwargs) -# Supply os.fspath() if not defined in C -if not _exists('fspath'):

+ +# For testing purposes, make sure the function is available when the C +# implementation exists. +def _fspath(path):

+# If there is no C implementation, make the pure Python version the +# implementation as transparently as possible. +if not _exists('fspath'):

+ class PathLike(abc.ABC):

--- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -879,7 +879,7 @@ class IOTest(unittest.TestCase): check_path_succeeds(PathLike(support.TESTFN.encode('utf-8'))) bad_path = PathLike(TypeError)

# ensure that refcounting is correct with some error conditions

--- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3112,55 +3112,59 @@ class TestScandir(unittest.TestCase): class TestPEP519(unittest.TestCase):

+

+

def test_return_bytes(self): for b in b'hello', b'goodbye', b'some/path/and/file':

def test_return_string(self): for s in 'hello', 'goodbye', 'some/path/and/file':

-

-

+

-

+

-

-

+

def test_garbage_in_exception_out(self): vapor = type('blah', (), {}) for o in int, type, os, vapor():

def test_argument_required(self): with self.assertRaises(TypeError):

+ + +# Only test if the C version is provided, otherwise TestPEP519 already tested +# the pure Python implementation. +if hasattr(os, "_fspath"):

+

+

if name == "main":

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.6.0 alpha 3 Library ------- +- Issue #27186: Update os.fspath()/PyOS_FSPath() to check the return value of

--- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -12317,12 +12317,21 @@ PyOS_FSPath(PyObject *path) if (NULL == func) { return PyErr_Format(PyExc_TypeError, "expected str, bytes or os.PathLike object, "

+ return path_repr; }