cpython: 5310f94772f4 (original) (raw)
Mercurial > cpython
changeset 100185:5310f94772f4 3.5
Issue #25911: Restored support of bytes paths in os.walk() on Windows. [#25911]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Mon, 08 Feb 2016 16:23:28 +0200 |
parents | 966bd147ccb5 |
children | b060af2a58b6 8ec721bb3027 |
files | Lib/os.py Lib/test/test_os.py Misc/NEWS |
diffstat | 3 files changed, 41 insertions(+), 10 deletions(-)[+] [-] Lib/os.py 27 Lib/test/test_os.py 22 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/os.py +++ b/Lib/os.py @@ -363,9 +363,12 @@ def walk(top, topdown=True, onerror=None # minor reason when (say) a thousand readable directories are still # left to visit. That logic is copied here. try:
# Note that scandir is global in this module due[](#l1.7)
# to earlier import-*.[](#l1.8)
scandir_it = scandir(top)[](#l1.9)
if name == 'nt' and isinstance(top, bytes):[](#l1.10)
scandir_it = _dummy_scandir(top)[](#l1.11)
else:[](#l1.12)
# Note that scandir is global in this module due[](#l1.13)
# to earlier import-*.[](#l1.14)
except OSError as error: if onerror is not None: onerror(error)scandir_it = scandir(top)[](#l1.15)
@@ -418,8 +421,8 @@ def walk(top, topdown=True, onerror=None # Recurse into sub-directories islink, join = path.islink, path.join
for name in dirs:[](#l1.23)
new_path = join(top, name)[](#l1.24)
for dirname in dirs:[](#l1.25)
new_path = join(top, dirname)[](#l1.26) # Issue #23605: os.path.islink() is used instead of caching[](#l1.27) # entry.is_symlink() result during the loop on os.scandir() because[](#l1.28) # the caller can replace the directory entry during the "yield"[](#l1.29)
@@ -430,6 +433,20 @@ def walk(top, topdown=True, onerror=None # Yield after recursion if going bottom up yield top, dirs, nondirs +class _DummyDirEntry:
- def init(self, dir, name):
self.name = name[](#l1.36)
self.path = path.join(dir, name)[](#l1.37)
- def is_dir(self):
return path.isdir(self.path)[](#l1.39)
- def is_symlink(self):
return path.islink(self.path)[](#l1.41)
listdir-based implementation for bytes patches on Windows
- for name in listdir(dir):
yield _DummyDirEntry(dir, name)[](#l1.46)
+ all.append("walk") if {open, stat} <= supports_dir_fd and {listdir, stat} <= supports_fd:
--- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -791,10 +791,10 @@ class WalkTests(unittest.TestCase): # Wrapper to hide minor differences between os.walk and os.fwalk # to tests both functions with the same code base
- def walk(self, top, **kwargs): if 'follow_symlinks' in kwargs: kwargs['followlinks'] = kwargs.pop('follow_symlinks')
return os.walk(directory, **kwargs)[](#l2.11)
return os.walk(top, **kwargs)[](#l2.12)
def setUp(self): join = os.path.join @@ -945,11 +945,10 @@ class WalkTests(unittest.TestCase): class FwalkTests(WalkTests): """Tests for os.fwalk()."""
- def walk(self, directory, **kwargs):
for root, dirs, files, root_fd in os.fwalk(directory, **kwargs):[](#l2.21)
- def walk(self, top, **kwargs):
for root, dirs, files, root_fd in os.fwalk(top, **kwargs):[](#l2.23) yield (root, dirs, files)[](#l2.24)
- def _compare_to_walk(self, walk_kwargs, fwalk_kwargs): """ compare with walk() results. @@ -1020,6 +1019,19 @@ class FwalkTests(WalkTests): os.unlink(name, dir_fd=rootfd) os.rmdir(support.TESTFN) +class BytesWalkTests(WalkTests):
- """Tests for os.walk() with bytes."""
- def walk(self, top, **kwargs):
if 'follow_symlinks' in kwargs:[](#l2.37)
kwargs['followlinks'] = kwargs.pop('follow_symlinks')[](#l2.38)
for broot, bdirs, bfiles in os.walk(os.fsencode(top), **kwargs):[](#l2.39)
root = os.fsdecode(broot)[](#l2.40)
dirs = list(map(os.fsdecode, bdirs))[](#l2.41)
files = list(map(os.fsdecode, bfiles))[](#l2.42)
yield (root, dirs, files)[](#l2.43)
bdirs[:] = list(map(os.fsencode, dirs))[](#l2.44)
bfiles[:] = list(map(os.fsencode, files))[](#l2.45)
+ class MakedirTests(unittest.TestCase): def setUp(self):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -73,6 +73,8 @@ Core and Builtins Library ------- +- Issue #25911: Restored support of bytes paths in os.walk() on Windows. +