[Python-checkins] cpython (3.6): Issue #27998: Removed workarounds for supporting bytes paths on Windows in

serhiy.storchaka python-checkins at python.org
Wed Oct 5 16:18:56 EDT 2016


https://hg.python.org/cpython/rev/7c36e6fd0232
changeset:   104297:7c36e6fd0232
branch:      3.6
parent:      104295:de0fa478c22e
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Oct 05 23:17:10 2016 +0300
summary:
  Issue #27998: Removed workarounds for supporting bytes paths on Windows in
os.walk() function and glob module since os.scandir() now directly supports
them.

files:
  Lib/glob.py |  23 ++++---------
  Lib/os.py   |  70 +---------------------------------------
  2 files changed, 10 insertions(+), 83 deletions(-)


diff --git a/Lib/glob.py b/Lib/glob.py
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -118,22 +118,13 @@
         else:
             dirname = os.curdir
     try:
-        if os.name == 'nt' and isinstance(dirname, bytes):
-            names = os.listdir(dirname)
-            if dironly:
-                for name in names:
-                    if os.path.isdir(os.path.join(dirname, name)):
-                        yield name
-            else:
-                yield from names
-        else:
-            with os.scandir(dirname) as it:
-                for entry in it:
-                    try:
-                        if not dironly or entry.is_dir():
-                            yield entry.name
-                    except OSError:
-                        pass
+        with os.scandir(dirname) as it:
+            for entry in it:
+                try:
+                    if not dironly or entry.is_dir():
+                        yield entry.name
+                except OSError:
+                    pass
     except OSError:
         return
 
diff --git a/Lib/os.py b/Lib/os.py
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -343,12 +343,9 @@
     # minor reason when (say) a thousand readable directories are still
     # left to visit.  That logic is copied here.
     try:
-        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)
+        # 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)
@@ -417,67 +414,6 @@
         # Yield after recursion if going bottom up
         yield top, dirs, nondirs
 
-class _DummyDirEntry:
-    """Dummy implementation of DirEntry
-
-    Only used internally by os.walk(bytes). Since os.walk() doesn't need the
-    follow_symlinks parameter: don't implement it, always follow symbolic
-    links.
-    """
-
-    def __init__(self, dir, name):
-        self.name = name
-        self.path = path.join(dir, name)
-        # Mimick FindFirstFile/FindNextFile: we should get file attributes
-        # while iterating on a directory
-        self._stat = None
-        self._lstat = None
-        try:
-            self.stat(follow_symlinks=False)
-        except OSError:
-            pass
-
-    def stat(self, *, follow_symlinks=True):
-        if follow_symlinks:
-            if self._stat is None:
-                self._stat = stat(self.path)
-            return self._stat
-        else:
-            if self._lstat is None:
-                self._lstat = stat(self.path, follow_symlinks=False)
-            return self._lstat
-
-    def is_dir(self):
-        if self._lstat is not None and not self.is_symlink():
-            # use the cache lstat
-            stat = self.stat(follow_symlinks=False)
-            return st.S_ISDIR(stat.st_mode)
-
-        stat = self.stat()
-        return st.S_ISDIR(stat.st_mode)
-
-    def is_symlink(self):
-        stat = self.stat(follow_symlinks=False)
-        return st.S_ISLNK(stat.st_mode)
-
-class _dummy_scandir:
-    # listdir-based implementation for bytes patches on Windows
-    def __init__(self, dir):
-        self.dir = dir
-        self.it = iter(listdir(dir))
-
-    def __iter__(self):
-        return self
-
-    def __next__(self):
-        return _DummyDirEntry(self.dir, next(self.it))
-
-    def __enter__(self):
-        return self
-
-    def __exit__(self, *args):
-        self.it = iter(())
-
 __all__.append("walk")
 
 if {open, stat} <= supports_dir_fd and {listdir, stat} <= supports_fd:

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list