Skip to content

Commit 0bddc9e

Browse files
Issue #25860: os.fwalk() no longer skips remaining directories when error occurs.
Original patch by Samson Lee.
1 parent 0ce7a3a commit 0bddc9e

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

Lib/os.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def _fwalk(topfd, toppath, topdown, onerror, follow_symlinks):
514514
except OSError as err:
515515
if onerror is not None:
516516
onerror(err)
517-
return
517+
continue
518518
try:
519519
if follow_symlinks or path.samestat(orig_st, stat(dirfd)):
520520
dirpath = path.join(toppath, name)

Lib/test/test_os.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -791,12 +791,8 @@ 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, topdown=True, follow_symlinks=False):
795-
walk_it = os.walk(directory,
796-
topdown=topdown,
797-
followlinks=follow_symlinks)
798-
for root, dirs, files in walk_it:
799-
yield (root, dirs, files)
794+
def walk(self, directory, **kwargs):
795+
return os.walk(directory, **kwargs)
800796

801797
def setUp(self):
802798
join = os.path.join
@@ -926,16 +922,29 @@ def tearDown(self):
926922
os.remove(dirname)
927923
os.rmdir(support.TESTFN)
928924

925+
def test_walk_bad_dir(self):
926+
# Walk top-down.
927+
errors = []
928+
walk_it = self.walk(self.walk_path, onerror=errors.append)
929+
root, dirs, files = next(walk_it)
930+
self.assertFalse(errors)
931+
dir1 = dirs[0]
932+
dir1new = dir1 + '.new'
933+
os.rename(os.path.join(root, dir1), os.path.join(root, dir1new))
934+
roots = [r for r, d, f in walk_it]
935+
self.assertTrue(errors)
936+
self.assertNotIn(os.path.join(root, dir1), roots)
937+
self.assertNotIn(os.path.join(root, dir1new), roots)
938+
for dir2 in dirs[1:]:
939+
self.assertIn(os.path.join(root, dir2), roots)
940+
929941

930942
@unittest.skipUnless(hasattr(os, 'fwalk'), "Test needs os.fwalk()")
931943
class FwalkTests(WalkTests):
932944
"""Tests for os.fwalk()."""
933945

934-
def walk(self, directory, topdown=True, follow_symlinks=False):
935-
walk_it = os.fwalk(directory,
936-
topdown=topdown,
937-
follow_symlinks=follow_symlinks)
938-
for root, dirs, files, root_fd in walk_it:
946+
def walk(self, directory, **kwargs):
947+
for root, dirs, files, root_fd in os.fwalk(directory, **kwargs):
939948
yield (root, dirs, files)
940949

941950

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Core and Builtins
2828
Library
2929
-------
3030

31+
- Issue #25860: os.fwalk() no longer skips remaining directories when error
32+
occurs. Original patch by Samson Lee.
33+
3134
- Issue #25914: Fixed and simplified OrderedDict.__sizeof__.
3235

3336
- Issue #25902: Fixed various refcount issues in ElementTree iteration.

0 commit comments

Comments
 (0)