[Python-checkins] cpython (3.5): Issue #25860: os.fwalk() no longer skips remaining directories when error

serhiy.storchaka python-checkins at python.org
Tue Dec 22 17:09:23 EST 2015


https://hg.python.org/cpython/rev/767262c149ca
changeset:   99662:767262c149ca
branch:      3.5
parent:      99660:0a2ea08fcebe
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Dec 23 00:08:24 2015 +0200
summary:
  Issue #25860: os.fwalk() no longer skips remaining directories when error occurs.
Original patch by Samson Lee.

files:
  Lib/os.py           |   2 +-
  Lib/test/test_os.py |  31 ++++++++++++++++++++-----------
  Misc/NEWS           |   3 +++
  3 files changed, 24 insertions(+), 12 deletions(-)


diff --git a/Lib/os.py b/Lib/os.py
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -514,7 +514,7 @@
             except OSError as err:
                 if onerror is not None:
                     onerror(err)
-                return
+                continue
             try:
                 if follow_symlinks or path.samestat(orig_st, stat(dirfd)):
                     dirpath = path.join(toppath, name)
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -791,12 +791,8 @@
 
     # Wrapper to hide minor differences between os.walk and os.fwalk
     # to tests both functions with the same code base
-    def walk(self, directory, topdown=True, follow_symlinks=False):
-        walk_it = os.walk(directory,
-                          topdown=topdown,
-                          followlinks=follow_symlinks)
-        for root, dirs, files in walk_it:
-            yield (root, dirs, files)
+    def walk(self, directory, **kwargs):
+        return os.walk(directory, **kwargs)
 
     def setUp(self):
         join = os.path.join
@@ -926,16 +922,29 @@
                     os.remove(dirname)
         os.rmdir(support.TESTFN)
 
+    def test_walk_bad_dir(self):
+        # Walk top-down.
+        errors = []
+        walk_it = self.walk(self.walk_path, onerror=errors.append)
+        root, dirs, files = next(walk_it)
+        self.assertFalse(errors)
+        dir1 = dirs[0]
+        dir1new = dir1 + '.new'
+        os.rename(os.path.join(root, dir1), os.path.join(root, dir1new))
+        roots = [r for r, d, f in walk_it]
+        self.assertTrue(errors)
+        self.assertNotIn(os.path.join(root, dir1), roots)
+        self.assertNotIn(os.path.join(root, dir1new), roots)
+        for dir2 in dirs[1:]:
+            self.assertIn(os.path.join(root, dir2), roots)
+
 
 @unittest.skipUnless(hasattr(os, 'fwalk'), "Test needs os.fwalk()")
 class FwalkTests(WalkTests):
     """Tests for os.fwalk()."""
 
-    def walk(self, directory, topdown=True, follow_symlinks=False):
-        walk_it = os.fwalk(directory,
-                           topdown=topdown,
-                           follow_symlinks=follow_symlinks)
-        for root, dirs, files, root_fd in walk_it:
+    def walk(self, directory, **kwargs):
+        for root, dirs, files, root_fd in os.fwalk(directory, **kwargs):
             yield (root, dirs, files)
 
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -28,6 +28,9 @@
 Library
 -------
 
+- Issue #25860: os.fwalk() no longer skips remaining directories when error
+  occurs.  Original patch by Samson Lee.
+
 - Issue #25914: Fixed and simplified OrderedDict.__sizeof__.
 
 - Issue #25902: Fixed various refcount issues in ElementTree iteration.

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


More information about the Python-checkins mailing list