From: Serhiy Storchaka Date: Tue, 11 Jul 2017 04:16:11 +0000 (+0300) Subject: [3.6] bpo-30879: os.listdir() and os.scandir() now emit bytes names when (GH-2634... X-Git-Tag: v3.6.3rc1~245 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ecfe4f678bfb0e3c19c90fd7db79c5f3c76023e4;p=python [3.6] bpo-30879: os.listdir() and os.scandir() now emit bytes names when (GH-2634) (#2656) called with bytes-like argument.. (cherry picked from commit 1180e5a51871fa53ca6892e83fd2e69dc2600447) --- diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index f74c199c84..e4951b44ce 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3402,6 +3402,22 @@ class TestScandir(unittest.TestCase): self.assertEqual(entry.path, os.fsencode(os.path.join(self.path, 'file.txt'))) + def test_bytes_like(self): + self.create_file("file.txt") + + for cls in bytearray, memoryview: + path_bytes = cls(os.fsencode(self.path)) + with self.assertWarns(DeprecationWarning): + entries = list(os.scandir(path_bytes)) + self.assertEqual(len(entries), 1, entries) + entry = entries[0] + + self.assertEqual(entry.name, b'file.txt') + self.assertEqual(entry.path, + os.fsencode(os.path.join(self.path, 'file.txt'))) + self.assertIs(type(entry.name), bytes) + self.assertIs(type(entry.path), bytes) + def test_empty_path(self): self.assertRaises(FileNotFoundError, os.scandir, '') diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 4ca623050b..a6e2fed302 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -581,17 +581,25 @@ class PosixTester(unittest.TestCase): self.assertRaises(OSError, posix.chdir, support.TESTFN) def test_listdir(self): - self.assertTrue(support.TESTFN in posix.listdir(os.curdir)) + self.assertIn(support.TESTFN, posix.listdir(os.curdir)) def test_listdir_default(self): # When listdir is called without argument, # it's the same as listdir(os.curdir). - self.assertTrue(support.TESTFN in posix.listdir()) + self.assertIn(support.TESTFN, posix.listdir()) def test_listdir_bytes(self): # When listdir is called with a bytes object, # the returned strings are of type bytes. - self.assertTrue(os.fsencode(support.TESTFN) in posix.listdir(b'.')) + self.assertIn(os.fsencode(support.TESTFN), posix.listdir(b'.')) + + def test_listdir_bytes_like(self): + for cls in bytearray, memoryview: + with self.assertWarns(DeprecationWarning): + names = posix.listdir(cls(b'.')) + self.assertIn(os.fsencode(support.TESTFN), names) + for name in names: + self.assertIs(type(name), bytes) @unittest.skipUnless(posix.listdir in os.supports_fd, "test needs fd support for posix.listdir()") diff --git a/Misc/NEWS b/Misc/NEWS index 3c16a66e6f..3cdacc2b63 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -18,6 +18,9 @@ Core and Builtins Library ------- +- bpo-30879: os.listdir() and os.scandir() now emit bytes names when called + with bytes-like argument. + - bpo-30746: Prohibited the '=' character in environment variable names in ``os.putenv()`` and ``os.spawn*()``. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index c205dd9b05..97b57198cd 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -949,6 +949,8 @@ path_converter(PyObject *o, void *p) Py_INCREF(bytes); } else if (is_buffer) { + /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code + after removing suport of non-bytes buffer objects. */ if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, "%s%s%s should be %s, not %.200s", path->function_name ? path->function_name : "", @@ -3511,8 +3513,8 @@ _posix_listdir(path_t *path, PyObject *list) const char *name; if (path->narrow) { name = path->narrow; - /* only return bytes if they specified a bytes object */ - return_str = !(PyBytes_Check(path->object)); + /* only return bytes if they specified a bytes-like object */ + return_str = !PyObject_CheckBuffer(path->object); } else { name = "."; @@ -11708,7 +11710,7 @@ DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len, if (!joined_path) goto error; - if (!path->narrow || !PyBytes_Check(path->object)) { + if (!path->narrow || !PyObject_CheckBuffer(path->object)) { entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); entry->path = PyUnicode_DecodeFSDefault(joined_path); }