Skip to content

bpo-30879: os.listdir() and os.scandir() now emit bytes names when #2634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3420,6 +3420,22 @@ def test_bytes(self):
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)

@unittest.skipUnless(os.listdir in os.supports_fd,
'fd support for listdir required for this test.')
def test_fd(self):
Expand Down
14 changes: 11 additions & 3 deletions Lib/test/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,17 +643,25 @@ def test_chdir(self):
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()")
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ Extension Modules
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*()``.

Expand Down
8 changes: 5 additions & 3 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,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 : "",
Expand Down Expand Up @@ -3588,8 +3590,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 = ".";
Expand Down Expand Up @@ -11842,7 +11844,7 @@ DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
goto error;
}

if (!path->narrow || !PyBytes_Check(path->object)) {
if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
if (joined_path)
entry->path = PyUnicode_DecodeFSDefault(joined_path);
Expand Down