summaryrefslogtreecommitdiffstats
path: root/Lib/os.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-02-08 14:23:28 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-02-08 14:23:28 (GMT)
commit5f6a0b4eb26695be759cd32e49e83f38b5123ce6 (patch)
tree64b45f0d4437b0e829dfa6f7e38b4e3bfb805cba /Lib/os.py
parent44391481d7d302bbe1c9c9eb0518b6a45f21e0b9 (diff)
downloadcpython-5f6a0b4eb26695be759cd32e49e83f38b5123ce6.zip
cpython-5f6a0b4eb26695be759cd32e49e83f38b5123ce6.tar.gz
cpython-5f6a0b4eb26695be759cd32e49e83f38b5123ce6.tar.bz2
Issue #25911: Restored support of bytes paths in os.walk() on Windows.
Diffstat (limited to 'Lib/os.py')
-rw-r--r--Lib/os.py27
1 files changed, 22 insertions, 5 deletions
diff --git a/Lib/os.py b/Lib/os.py
index 13a42c3..674a7d7 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -363,9 +363,12 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
# 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
- # to earlier import-*.
- scandir_it = scandir(top)
+ if name == 'nt' and isinstance(top, bytes):
+ scandir_it = _dummy_scandir(top)
+ else:
+ # Note that scandir is global in this module due
+ # to earlier import-*.
+ scandir_it = scandir(top)
except OSError as error:
if onerror is not None:
onerror(error)
@@ -418,8 +421,8 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
# Recurse into sub-directories
islink, join = path.islink, path.join
- for name in dirs:
- new_path = join(top, name)
+ for dirname in dirs:
+ new_path = join(top, dirname)
# Issue #23605: os.path.islink() is used instead of caching
# entry.is_symlink() result during the loop on os.scandir() because
# the caller can replace the directory entry during the "yield"
@@ -430,6 +433,20 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
# Yield after recursion if going bottom up
yield top, dirs, nondirs
+class _DummyDirEntry:
+ def __init__(self, dir, name):
+ self.name = name
+ self.path = path.join(dir, name)
+ def is_dir(self):
+ return path.isdir(self.path)
+ def is_symlink(self):
+ return path.islink(self.path)
+
+def _dummy_scandir(dir):
+ # listdir-based implementation for bytes patches on Windows
+ for name in listdir(dir):
+ yield _DummyDirEntry(dir, name)
+
__all__.append("walk")
if {open, stat} <= supports_dir_fd and {listdir, stat} <= supports_fd: