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

Skip to content

Commit b32c1c0

Browse files
committed
Simplify exception handling in fwalk()
1 parent 91b5a78 commit b32c1c0

1 file changed

Lines changed: 21 additions & 28 deletions

File tree

Lib/os.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -505,35 +505,28 @@ def _fwalk(stack, isbytes, topdown, onerror, follow_symlinks):
505505
stack.append((_fwalk_yield, (toppath, dirs, nondirs, topfd)))
506506

507507
topprefix = path.join(toppath, toppath[:0]) # Add trailing slash.
508-
try:
509-
for entry in scandir(topfd):
510-
name = entry.name
511-
if isbytes:
512-
name = fsencode(name)
513-
514-
try:
515-
is_dir = entry.is_dir()
516-
except OSError:
517-
# If is_dir() raises an OSError, consider the entry not to
518-
# be a directory, same behaviour as os.path.isdir().
519-
is_dir = False
508+
for entry in scandir(topfd):
509+
name = entry.name
510+
if isbytes:
511+
name = fsencode(name)
520512

521-
if is_dir:
522-
if not topdown:
523-
# Bottom-up: traverse into sub-directory.
524-
stack.append(
525-
(_fwalk_walk, (
526-
False, topfd, topprefix + name, name,
527-
None if follow_symlinks else entry)))
528-
dirs.append(name)
529-
else:
530-
nondirs.append(name)
531-
except:
532-
# Undo additions to stack in bottom-up mode.
533-
if not topdown:
534-
while stack.pop()[0] != _fwalk_yield:
535-
pass
536-
raise
513+
try:
514+
is_dir = entry.is_dir()
515+
except OSError:
516+
# If is_dir() raises an OSError, consider the entry not to
517+
# be a directory, same behaviour as os.path.isdir().
518+
is_dir = False
519+
520+
if is_dir:
521+
if not topdown:
522+
# Bottom-up: traverse into sub-directory.
523+
stack.append(
524+
(_fwalk_walk, (
525+
False, topfd, topprefix + name, name,
526+
None if follow_symlinks else entry)))
527+
dirs.append(name)
528+
else:
529+
nondirs.append(name)
537530

538531
if topdown:
539532
# Yield before sub-directory traversal if going top down

0 commit comments

Comments
 (0)