Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af4e474

Browse files
Issue #28353: os.fwalk() no longer fails on broken links.
2 parents 179111b + 42babab commit af4e474

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

Lib/os.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,13 @@ def _fwalk(topfd, toppath, topdown, onerror, follow_symlinks):
481481
dirs.append(name)
482482
else:
483483
nondirs.append(name)
484-
except FileNotFoundError:
484+
except OSError:
485485
try:
486486
# Add dangling symlinks, ignore disappeared files
487487
if st.S_ISLNK(stat(name, dir_fd=topfd, follow_symlinks=False)
488488
.st_mode):
489489
nondirs.append(name)
490-
except FileNotFoundError:
490+
except OSError:
491491
continue
492492

493493
if topdown:

Lib/test/test_os.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,38 +853,54 @@ def setUp(self):
853853
# SUB11/ no kids
854854
# SUB2/ a file kid and a dirsymlink kid
855855
# tmp3
856+
# SUB21/ not readable
857+
# tmp5
856858
# link/ a symlink to TESTFN.2
857859
# broken_link
860+
# broken_link2
861+
# broken_link3
858862
# TEST2/
859863
# tmp4 a lone file
860864
self.walk_path = join(support.TESTFN, "TEST1")
861865
self.sub1_path = join(self.walk_path, "SUB1")
862866
self.sub11_path = join(self.sub1_path, "SUB11")
863867
sub2_path = join(self.walk_path, "SUB2")
868+
sub21_path = join(sub2_path, "SUB21")
864869
tmp1_path = join(self.walk_path, "tmp1")
865870
tmp2_path = join(self.sub1_path, "tmp2")
866871
tmp3_path = join(sub2_path, "tmp3")
872+
tmp5_path = join(sub21_path, "tmp3")
867873
self.link_path = join(sub2_path, "link")
868874
t2_path = join(support.TESTFN, "TEST2")
869875
tmp4_path = join(support.TESTFN, "TEST2", "tmp4")
870876
broken_link_path = join(sub2_path, "broken_link")
877+
broken_link2_path = join(sub2_path, "broken_link2")
878+
broken_link3_path = join(sub2_path, "broken_link3")
871879

872880
# Create stuff.
873881
os.makedirs(self.sub11_path)
874882
os.makedirs(sub2_path)
883+
os.makedirs(sub21_path)
875884
os.makedirs(t2_path)
876885

877-
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
886+
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path, tmp5_path:
878887
with open(path, "x") as f:
879888
f.write("I'm " + path + " and proud of it. Blame test_os.\n")
880889

881890
if support.can_symlink():
882891
os.symlink(os.path.abspath(t2_path), self.link_path)
883892
os.symlink('broken', broken_link_path, True)
884-
self.sub2_tree = (sub2_path, ["link"], ["broken_link", "tmp3"])
893+
os.symlink(join('tmp3', 'broken'), broken_link2_path, True)
894+
os.symlink(join('SUB21', 'tmp5'), broken_link3_path, True)
895+
self.sub2_tree = (sub2_path, ["link", "SUB21"],
896+
["broken_link", "broken_link2", "broken_link3",
897+
"tmp3"])
885898
else:
886899
self.sub2_tree = (sub2_path, [], ["tmp3"])
887900

901+
os.chmod(sub21_path, 0)
902+
self.addCleanup(os.chmod, sub21_path, stat.S_IRWXU)
903+
888904
def test_walk_topdown(self):
889905
# Walk top-down.
890906
all = list(self.walk(self.walk_path))

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Core and Builtins
2929
Library
3030
-------
3131

32+
- Issue #28353: os.fwalk() no longer fails on broken links.
33+
3234
- Issue #28430: Fix iterator of C implemented asyncio.Future doesn't accept
3335
non-None value is passed to it.send(val).
3436

0 commit comments

Comments
 (0)