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

Skip to content

Commit 42babab

Browse files
Issue #28353: os.fwalk() no longer fails on broken links.
1 parent 036fb15 commit 42babab

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

Lib/os.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,13 +537,13 @@ def _fwalk(topfd, toppath, topdown, onerror, follow_symlinks):
537537
dirs.append(name)
538538
else:
539539
nondirs.append(name)
540-
except FileNotFoundError:
540+
except OSError:
541541
try:
542542
# Add dangling symlinks, ignore disappeared files
543543
if st.S_ISLNK(stat(name, dir_fd=topfd, follow_symlinks=False)
544544
.st_mode):
545545
nondirs.append(name)
546-
except FileNotFoundError:
546+
except OSError:
547547
continue
548548

549549
if topdown:

Lib/test/test_os.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -830,39 +830,53 @@ def setUp(self):
830830
# SUB11/ no kids
831831
# SUB2/ a file kid and a dirsymlink kid
832832
# tmp3
833+
# SUB21/ not readable
834+
# tmp5
833835
# link/ a symlink to TESTFN.2
834836
# broken_link
837+
# broken_link2
838+
# broken_link3
835839
# TEST2/
836840
# tmp4 a lone file
837841
self.walk_path = join(support.TESTFN, "TEST1")
838842
self.sub1_path = join(self.walk_path, "SUB1")
839843
self.sub11_path = join(self.sub1_path, "SUB11")
840844
sub2_path = join(self.walk_path, "SUB2")
845+
self.sub21_path = join(sub2_path, "SUB21")
841846
tmp1_path = join(self.walk_path, "tmp1")
842847
tmp2_path = join(self.sub1_path, "tmp2")
843848
tmp3_path = join(sub2_path, "tmp3")
849+
tmp5_path = join(self.sub21_path, "tmp3")
844850
self.link_path = join(sub2_path, "link")
845851
t2_path = join(support.TESTFN, "TEST2")
846852
tmp4_path = join(support.TESTFN, "TEST2", "tmp4")
847853
broken_link_path = join(sub2_path, "broken_link")
854+
broken_link2_path = join(sub2_path, "broken_link2")
855+
broken_link3_path = join(sub2_path, "broken_link3")
848856

849857
# Create stuff.
850858
os.makedirs(self.sub11_path)
851859
os.makedirs(sub2_path)
860+
os.makedirs(self.sub21_path)
852861
os.makedirs(t2_path)
853862

854-
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
855-
f = open(path, "w")
856-
f.write("I'm " + path + " and proud of it. Blame test_os.\n")
857-
f.close()
863+
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path, tmp5_path:
864+
with open(path, "x") as f:
865+
f.write("I'm " + path + " and proud of it. Blame test_os.\n")
858866

859867
if support.can_symlink():
860868
os.symlink(os.path.abspath(t2_path), self.link_path)
861869
os.symlink('broken', broken_link_path, True)
862-
self.sub2_tree = (sub2_path, ["link"], ["broken_link", "tmp3"])
870+
os.symlink(join('tmp3', 'broken'), broken_link2_path, True)
871+
os.symlink(join('SUB21', 'tmp5'), broken_link3_path, True)
872+
self.sub2_tree = (sub2_path, ["link", "SUB21"],
873+
["broken_link", "broken_link2", "broken_link3",
874+
"tmp3"])
863875
else:
864876
self.sub2_tree = (sub2_path, [], ["tmp3"])
865877

878+
os.chmod(self.sub21_path, 0)
879+
866880
def test_walk_topdown(self):
867881
# Walk top-down.
868882
all = list(self.walk(self.walk_path))
@@ -935,6 +949,7 @@ def tearDown(self):
935949
# Windows, which doesn't have a recursive delete command. The
936950
# (not so) subtlety is that rmdir will fail unless the dir's
937951
# kids are removed first, so bottom up is essential.
952+
os.chmod(self.sub21_path, stat.S_IRWXU)
938953
for root, dirs, files in os.walk(support.TESTFN, topdown=False):
939954
for name in files:
940955
os.remove(os.path.join(root, name))
@@ -1030,6 +1045,7 @@ def test_fd_leak(self):
10301045

10311046
def tearDown(self):
10321047
# cleanup
1048+
os.chmod(self.sub21_path, stat.S_IRWXU)
10331049
for root, dirs, files, rootfd in os.fwalk(support.TESTFN, topdown=False):
10341050
for name in files:
10351051
os.unlink(name, dir_fd=rootfd)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ Core and Builtins
113113
Library
114114
-------
115115

116+
- Issue #28353: os.fwalk() no longer fails on broken links.
117+
116118
- Issue #25464: Fixed HList.header_exists() in tkinter.tix module by addin
117119
a workaround to Tix library bug.
118120

0 commit comments

Comments
 (0)