Skip to content

Commit d18bfb9

Browse files
committed
pythonGH-119169: Slightly speed up os.fwalk()
Handle "disappearing" files as in `walk()`: add them to the `nondirs` list rather than omitting them entirely.
1 parent 6239d41 commit d18bfb9

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

Lib/os.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -548,19 +548,16 @@ def _fwalk(stack, isbytes, topdown, onerror, follow_symlinks):
548548
if isbytes:
549549
name = fsencode(name)
550550
try:
551-
if entry.is_dir():
552-
dirs.append(name)
553-
if entries is not None:
554-
entries.append(entry)
555-
else:
556-
nondirs.append(name)
551+
is_dir = entry.is_dir()
557552
except OSError:
558-
try:
559-
# Add dangling symlinks, ignore disappeared files
560-
if entry.is_symlink():
561-
nondirs.append(name)
562-
except OSError:
563-
pass
553+
is_dir = False
554+
555+
if is_dir:
556+
dirs.append(name)
557+
if entries is not None:
558+
entries.append(entry)
559+
else:
560+
nondirs.append(name)
564561

565562
if topdown:
566563
yield toppath, dirs, nondirs, topfd
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Slightly speed up :func:`os.fwalk` by simplifying handling of inaccessible
2+
files.

0 commit comments

Comments
 (0)