Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f6a0b4

Browse files
Issue #25911: Restored support of bytes paths in os.walk() on Windows.
1 parent 4439148 commit 5f6a0b4

3 files changed

Lines changed: 41 additions & 10 deletions

File tree

Lib/os.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,12 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
363363
# minor reason when (say) a thousand readable directories are still
364364
# left to visit. That logic is copied here.
365365
try:
366-
# Note that scandir is global in this module due
367-
# to earlier import-*.
368-
scandir_it = scandir(top)
366+
if name == 'nt' and isinstance(top, bytes):
367+
scandir_it = _dummy_scandir(top)
368+
else:
369+
# Note that scandir is global in this module due
370+
# to earlier import-*.
371+
scandir_it = scandir(top)
369372
except OSError as error:
370373
if onerror is not None:
371374
onerror(error)
@@ -418,8 +421,8 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
418421

419422
# Recurse into sub-directories
420423
islink, join = path.islink, path.join
421-
for name in dirs:
422-
new_path = join(top, name)
424+
for dirname in dirs:
425+
new_path = join(top, dirname)
423426
# Issue #23605: os.path.islink() is used instead of caching
424427
# entry.is_symlink() result during the loop on os.scandir() because
425428
# the caller can replace the directory entry during the "yield"
@@ -430,6 +433,20 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
430433
# Yield after recursion if going bottom up
431434
yield top, dirs, nondirs
432435

436+
class _DummyDirEntry:
437+
def __init__(self, dir, name):
438+
self.name = name
439+
self.path = path.join(dir, name)
440+
def is_dir(self):
441+
return path.isdir(self.path)
442+
def is_symlink(self):
443+
return path.islink(self.path)
444+
445+
def _dummy_scandir(dir):
446+
# listdir-based implementation for bytes patches on Windows
447+
for name in listdir(dir):
448+
yield _DummyDirEntry(dir, name)
449+
433450
__all__.append("walk")
434451

435452
if {open, stat} <= supports_dir_fd and {listdir, stat} <= supports_fd:

Lib/test/test_os.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,10 @@ class WalkTests(unittest.TestCase):
791791

792792
# Wrapper to hide minor differences between os.walk and os.fwalk
793793
# to tests both functions with the same code base
794-
def walk(self, directory, **kwargs):
794+
def walk(self, top, **kwargs):
795795
if 'follow_symlinks' in kwargs:
796796
kwargs['followlinks'] = kwargs.pop('follow_symlinks')
797-
return os.walk(directory, **kwargs)
797+
return os.walk(top, **kwargs)
798798

799799
def setUp(self):
800800
join = os.path.join
@@ -945,11 +945,10 @@ def test_walk_bad_dir(self):
945945
class FwalkTests(WalkTests):
946946
"""Tests for os.fwalk()."""
947947

948-
def walk(self, directory, **kwargs):
949-
for root, dirs, files, root_fd in os.fwalk(directory, **kwargs):
948+
def walk(self, top, **kwargs):
949+
for root, dirs, files, root_fd in os.fwalk(top, **kwargs):
950950
yield (root, dirs, files)
951951

952-
953952
def _compare_to_walk(self, walk_kwargs, fwalk_kwargs):
954953
"""
955954
compare with walk() results.
@@ -1020,6 +1019,19 @@ def tearDown(self):
10201019
os.unlink(name, dir_fd=rootfd)
10211020
os.rmdir(support.TESTFN)
10221021

1022+
class BytesWalkTests(WalkTests):
1023+
"""Tests for os.walk() with bytes."""
1024+
def walk(self, top, **kwargs):
1025+
if 'follow_symlinks' in kwargs:
1026+
kwargs['followlinks'] = kwargs.pop('follow_symlinks')
1027+
for broot, bdirs, bfiles in os.walk(os.fsencode(top), **kwargs):
1028+
root = os.fsdecode(broot)
1029+
dirs = list(map(os.fsdecode, bdirs))
1030+
files = list(map(os.fsdecode, bfiles))
1031+
yield (root, dirs, files)
1032+
bdirs[:] = list(map(os.fsencode, dirs))
1033+
bfiles[:] = list(map(os.fsencode, files))
1034+
10231035

10241036
class MakedirTests(unittest.TestCase):
10251037
def setUp(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Core and Builtins
7373
Library
7474
-------
7575

76+
- Issue #25911: Restored support of bytes paths in os.walk() on Windows.
77+
7678
- Issue #26045: Add UTF-8 suggestion to error message when posting a
7779
non-Latin-1 string with http.client.
7880

0 commit comments

Comments
 (0)