Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Defer DirEntry.stat()
  • Loading branch information
barneygale committed May 29, 2024
commit c9d0134656f6cbe9899b866ec2222a1cab5f3284
27 changes: 12 additions & 15 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,15 @@ def _fwalk(stack, isbytes, topdown, onerror, follow_symlinks):
yield value
return
assert action == _fwalk_walk
isroot, toppath, dirfd, topname, orig_st = value
isroot, toppath, dirfd, topname, entry = value
try:
if orig_st is None and not follow_symlinks:
if not follow_symlinks:
# Note: To guard against symlink races, we use the standard
# lstat()/open()/fstat() trick.
orig_st = stat(topname, follow_symlinks=False, dir_fd=dirfd)
if entry is None:
orig_st = stat(topname, follow_symlinks=False, dir_fd=dirfd)
else:
orig_st = entry.stat(follow_symlinks=False)
topfd = open(topname, O_RDONLY | O_NONBLOCK, dir_fd=dirfd)
except OSError as err:
if isroot:
Expand Down Expand Up @@ -544,19 +547,13 @@ def _fwalk(stack, isbytes, topdown, onerror, follow_symlinks):
stack.append((_fwalk_yield, (toppath, dirs, nondirs, topfd)))

if entries is None:
for name in reversed(dirs):
args = (False, path.join(toppath, name), topfd, name, None)
stack.append((_fwalk_walk, args))
stack.extend(
(_fwalk_walk, (False, path.join(toppath, name), topfd, name, None))
for name in reversed(dirs))
else:
for name, entry in zip(dirs, entries):
try:
orig_st = entry.stat(follow_symlinks=False)
except OSError as err:
if onerror is not None:
onerror(err)
continue
args = (False, path.join(toppath, name), topfd, name, orig_st)
stack.append((_fwalk_walk, args))
stack.extend(
(_fwalk_walk, (False, path.join(toppath, name), topfd, name, entry))
for name, entry in zip(dirs, entries))

__all__.append("fwalk")

Expand Down