cpython: cbe7560d4443 (original) (raw)
Mercurial > cpython
changeset 76951:cbe7560d4443
#14773: Fix os.fwalk() failing on dangling symlinks [#14773]
Hynek Schlawack hs@ox.cx | |
---|---|
date | Tue, 15 May 2012 16:32:21 +0200 |
parents | d4c590cee68b |
children | cdea40514623 |
files | Lib/os.py Lib/test/test_os.py Misc/NEWS |
diffstat | 3 files changed, 24 insertions(+), 8 deletions(-)[+] [-] Lib/os.py 24 Lib/test/test_os.py 6 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/os.py +++ b/Lib/os.py @@ -353,13 +353,23 @@ if _exists("openat"): names = flistdir(topfd) dirs, nondirs = [], [] for name in names:
# Here, we don't use AT_SYMLINK_NOFOLLOW to be consistent with[](#l1.7)
# walk() which reports symlinks to directories as directories. We do[](#l1.8)
# however check for symlinks before recursing into a subdirectory.[](#l1.9)
if st.S_ISDIR(fstatat(topfd, name).st_mode):[](#l1.10)
dirs.append(name)[](#l1.11)
else:[](#l1.12)
nondirs.append(name)[](#l1.13)
try:[](#l1.14)
# Here, we don't use AT_SYMLINK_NOFOLLOW to be consistent with[](#l1.15)
# walk() which reports symlinks to directories as directories.[](#l1.16)
# We do however check for symlinks before recursing into[](#l1.17)
# a subdirectory.[](#l1.18)
if st.S_ISDIR(fstatat(topfd, name).st_mode):[](#l1.19)
dirs.append(name)[](#l1.20)
else:[](#l1.21)
nondirs.append(name)[](#l1.22)
except FileNotFoundError:[](#l1.23)
try:[](#l1.24)
# Add dangling symlinks, ignore disappeared files[](#l1.25)
if st.S_ISLNK(fstatat(topfd, name, AT_SYMLINK_NOFOLLOW)[](#l1.26)
.st_mode):[](#l1.27)
nondirs.append(name)[](#l1.28)
except FileNotFoundError:[](#l1.29)
continue[](#l1.30)
if topdown: yield toppath, dirs, nondirs, topfd
--- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -651,6 +651,7 @@ class WalkTests(unittest.TestCase): # SUB2/ a file kid and a dirsymlink kid # tmp3 # link/ a symlink to TESTFN.2
# broken_link[](#l2.7) # TEST2/[](#l2.8) # tmp4 a lone file[](#l2.9) walk_path = join(support.TESTFN, "TEST1")[](#l2.10)
@@ -663,6 +664,8 @@ class WalkTests(unittest.TestCase): link_path = join(sub2_path, "link") t2_path = join(support.TESTFN, "TEST2") tmp4_path = join(support.TESTFN, "TEST2", "tmp4")
link_path = join(sub2_path, "link")[](#l2.15)
broken_link_path = join(sub2_path, "broken_link")[](#l2.16)
# Create stuff. os.makedirs(sub11_path) @@ -679,7 +682,8 @@ class WalkTests(unittest.TestCase): else: symlink_to_dir = os.symlink symlink_to_dir(os.path.abspath(t2_path), link_path)
sub2_tree = (sub2_path, ["link"], ["tmp3"])[](#l2.24)
symlink_to_dir('broken', broken_link_path)[](#l2.25)
sub2_tree = (sub2_path, ["link"], ["broken_link", "tmp3"])[](#l2.26) else:[](#l2.27) sub2_tree = (sub2_path, [], ["tmp3"])[](#l2.28)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -31,6 +31,8 @@ Core and Builtins Library ------- +- Issue 14773: Fix os.fwalk() failing on dangling symlinks. +