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

Skip to content
Prev Previous commit
Next Next commit
Simplify exception handling in fwalk()
  • Loading branch information
barneygale committed Jul 6, 2024
commit b32c1c0641f9cec2e6788b0ae49ba61d9b9b3d75
49 changes: 21 additions & 28 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,35 +505,28 @@ def _fwalk(stack, isbytes, topdown, onerror, follow_symlinks):
stack.append((_fwalk_yield, (toppath, dirs, nondirs, topfd)))

topprefix = path.join(toppath, toppath[:0]) # Add trailing slash.
try:
for entry in scandir(topfd):
name = entry.name
if isbytes:
name = fsencode(name)

try:
is_dir = entry.is_dir()
except OSError:
# If is_dir() raises an OSError, consider the entry not to
# be a directory, same behaviour as os.path.isdir().
is_dir = False
for entry in scandir(topfd):
name = entry.name
if isbytes:
name = fsencode(name)

if is_dir:
if not topdown:
# Bottom-up: traverse into sub-directory.
stack.append(
(_fwalk_walk, (
False, topfd, topprefix + name, name,
None if follow_symlinks else entry)))
dirs.append(name)
else:
nondirs.append(name)
except:
# Undo additions to stack in bottom-up mode.
if not topdown:
while stack.pop()[0] != _fwalk_yield:
pass
raise
try:
is_dir = entry.is_dir()
except OSError:
# If is_dir() raises an OSError, consider the entry not to
# be a directory, same behaviour as os.path.isdir().
is_dir = False

if is_dir:
if not topdown:
# Bottom-up: traverse into sub-directory.
stack.append(
(_fwalk_walk, (
False, topfd, topprefix + name, name,
None if follow_symlinks else entry)))
dirs.append(name)
else:
nondirs.append(name)

if topdown:
# Yield before sub-directory traversal if going top down
Expand Down