Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1180e5a5
Commit
1180e5a5
authored
Jul 11, 2017
by
Serhiy Storchaka
Committed by
GitHub
Jul 11, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-30879: os.listdir() and os.scandir() now emit bytes names when (#2634)
called with bytes-like argument.
parent
4f9a446f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
6 deletions
+35
-6
Lib/test/test_os.py
Lib/test/test_os.py
+16
-0
Lib/test/test_posix.py
Lib/test/test_posix.py
+11
-3
Misc/NEWS
Misc/NEWS
+3
-0
Modules/posixmodule.c
Modules/posixmodule.c
+5
-3
No files found.
Lib/test/test_os.py
View file @
1180e5a5
...
...
@@ -3420,6 +3420,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
)
@
unittest
.
skipUnless
(
os
.
listdir
in
os
.
supports_fd
,
'fd support for listdir required for this test.'
)
def
test_fd
(
self
):
...
...
Lib/test/test_posix.py
View file @
1180e5a5
...
...
@@ -643,17 +643,25 @@ class PosixTester(unittest.TestCase):
self
.
assertRaises
(
OSError
,
posix
.
chdir
,
support
.
TESTFN
)
def
test_listdir
(
self
):
self
.
assert
True
(
support
.
TESTFN
in
posix
.
listdir
(
os
.
curdir
))
self
.
assert
In
(
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
.
assert
True
(
support
.
TESTFN
in
posix
.
listdir
())
self
.
assert
In
(
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()"
)
...
...
Misc/NEWS
View file @
1180e5a5
...
...
@@ -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*()``.
...
...
Modules/posixmodule.c
View file @
1180e5a5
...
...
@@ -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
:
""
,
...
...
@@ -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
=
"."
;
...
...
@@ -11842,7 +11844,7 @@ DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
goto
error
;
}
if
(
!
path
->
narrow
||
!
Py
Bytes_Check
(
path
->
object
))
{
if
(
!
path
->
narrow
||
!
Py
Object_CheckBuffer
(
path
->
object
))
{
entry
->
name
=
PyUnicode_DecodeFSDefaultAndSize
(
name
,
name_len
);
if
(
joined_path
)
entry
->
path
=
PyUnicode_DecodeFSDefault
(
joined_path
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment