Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3c890b5

Browse files
authored
GH-89727: Fixos.fwalk() recursion error on deep trees (#119638)
Implement `os.fwalk()` using a list as a stack to avoid emitting recursionerrors on deeply nested trees.
1 parent2cc3502 commit3c890b5

File tree

3 files changed

+56
-40
lines changed

3 files changed

+56
-40
lines changed

‎Lib/os.py‎

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -478,24 +478,52 @@ def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=
478478
"""
479479
sys.audit("os.fwalk",top,topdown,onerror,follow_symlinks,dir_fd)
480480
top=fspath(top)
481-
# Note: To guard against symlink races, we use the standard
482-
# lstat()/open()/fstat() trick.
483-
ifnotfollow_symlinks:
484-
orig_st=stat(top,follow_symlinks=False,dir_fd=dir_fd)
485-
topfd=open(top,O_RDONLY|O_NONBLOCK,dir_fd=dir_fd)
486-
try:
487-
if (follow_symlinksor (st.S_ISDIR(orig_st.st_mode)and
488-
path.samestat(orig_st,stat(topfd)))):
489-
yieldfrom_fwalk(topfd,top,isinstance(top,bytes),
490-
topdown,onerror,follow_symlinks)
491-
finally:
492-
close(topfd)
493-
494-
def_fwalk(topfd,toppath,isbytes,topdown,onerror,follow_symlinks):
481+
stack= [(_fwalk_walk, (True,dir_fd,top,top,None))]
482+
isbytes=isinstance(top,bytes)
483+
whilestack:
484+
yieldfrom_fwalk(stack,isbytes,topdown,onerror,follow_symlinks)
485+
486+
# Each item in the _fwalk() stack is a pair (action, args).
487+
_fwalk_walk=0# args: (isroot, dirfd, toppath, topname, entry)
488+
_fwalk_yield=1# args: (toppath, dirnames, filenames, topfd)
489+
_fwalk_close=2# args: dirfd
490+
491+
def_fwalk(stack,isbytes,topdown,onerror,follow_symlinks):
495492
# Note: This uses O(depth of the directory tree) file descriptors: if
496493
# necessary, it can be adapted to only require O(1) FDs, see issue
497494
# #13734.
498495

496+
action,value=stack.pop()
497+
ifaction==_fwalk_close:
498+
close(value)
499+
return
500+
elifaction==_fwalk_yield:
501+
yieldvalue
502+
return
503+
assertaction==_fwalk_walk
504+
isroot,dirfd,toppath,topname,entry=value
505+
try:
506+
ifnotfollow_symlinks:
507+
# Note: To guard against symlink races, we use the standard
508+
# lstat()/open()/fstat() trick.
509+
ifentryisNone:
510+
orig_st=stat(topname,follow_symlinks=False,dir_fd=dirfd)
511+
else:
512+
orig_st=entry.stat(follow_symlinks=False)
513+
topfd=open(topname,O_RDONLY|O_NONBLOCK,dir_fd=dirfd)
514+
exceptOSErroraserr:
515+
ifisroot:
516+
raise
517+
ifonerrorisnotNone:
518+
onerror(err)
519+
return
520+
stack.append((_fwalk_close,topfd))
521+
ifnotfollow_symlinks:
522+
ifisrootandnotst.S_ISDIR(orig_st.st_mode):
523+
return
524+
ifnotpath.samestat(orig_st,stat(topfd)):
525+
return
526+
499527
scandir_it=scandir(topfd)
500528
dirs= []
501529
nondirs= []
@@ -521,31 +549,18 @@ def _fwalk(topfd, toppath, isbytes, topdown, onerror, follow_symlinks):
521549

522550
iftopdown:
523551
yieldtoppath,dirs,nondirs,topfd
552+
else:
553+
stack.append((_fwalk_yield, (toppath,dirs,nondirs,topfd)))
524554

525-
fornameindirsifentriesisNoneelsezip(dirs,entries):
526-
try:
527-
ifnotfollow_symlinks:
528-
iftopdown:
529-
orig_st=stat(name,dir_fd=topfd,follow_symlinks=False)
530-
else:
531-
assertentriesisnotNone
532-
name,entry=name
533-
orig_st=entry.stat(follow_symlinks=False)
534-
dirfd=open(name,O_RDONLY|O_NONBLOCK,dir_fd=topfd)
535-
exceptOSErroraserr:
536-
ifonerrorisnotNone:
537-
onerror(err)
538-
continue
539-
try:
540-
iffollow_symlinksorpath.samestat(orig_st,stat(dirfd)):
541-
dirpath=path.join(toppath,name)
542-
yieldfrom_fwalk(dirfd,dirpath,isbytes,
543-
topdown,onerror,follow_symlinks)
544-
finally:
545-
close(dirfd)
546-
547-
ifnottopdown:
548-
yieldtoppath,dirs,nondirs,topfd
555+
toppath=path.join(toppath,toppath[:0])# Add trailing slash.
556+
ifentriesisNone:
557+
stack.extend(
558+
(_fwalk_walk, (False,topfd,toppath+name,name,None))
559+
fornameindirs[::-1])
560+
else:
561+
stack.extend(
562+
(_fwalk_walk, (False,topfd,toppath+name,name,entry))
563+
forname,entryinzip(dirs[::-1],entries[::-1]))
549564

550565
__all__.append("fwalk")
551566

‎Lib/test/test_os.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,8 +1687,6 @@ def test_fd_leak(self):
16871687

16881688
# fwalk() keeps file descriptors open
16891689
test_walk_many_open_files=None
1690-
# fwalk() still uses recursion
1691-
test_walk_above_recursion_limit=None
16921690

16931691

16941692
classBytesWalkTests(WalkTests):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix issue with:func:`os.fwalk` where a:exc:`RecursionError` was raised on
2+
deep directory trees by adjusting the implementation to be iterative instead
3+
of recursive.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp