@@ -606,37 +606,21 @@ def _rmtree_islink(st):
606606
607607# version vulnerable to race conditions
608608def _rmtree_unsafe (path , onexc ):
609- try :
610- with os .scandir (path ) as scandir_it :
611- entries = list (scandir_it )
612- except FileNotFoundError :
613- return
614- except OSError as err :
615- onexc (os .scandir , path , err )
616- entries = []
617- for entry in entries :
618- fullname = entry .path
619- try :
620- is_dir = entry .is_dir (follow_symlinks = False )
621- except FileNotFoundError :
622- continue
623- except OSError :
624- is_dir = False
625-
626- if is_dir and not entry .is_junction ():
609+ def onerror (err ):
610+ if not isinstance (err , FileNotFoundError ):
611+ onexc (os .scandir , err .filename , err )
612+ results = os .walk (path , topdown = False , onerror = onerror , followlinks = os ._walk_symlinks_as_files )
613+ for dirpath , dirnames , filenames in results :
614+ for name in dirnames :
615+ fullname = os .path .join (dirpath , name )
627616 try :
628- if entry .is_symlink ():
629- # This can only happen if someone replaces
630- # a directory with a symlink after the call to
631- # os.scandir or entry.is_dir above.
632- raise OSError ("Cannot call rmtree on a symbolic link" )
617+ os .rmdir (fullname )
633618 except FileNotFoundError :
634619 continue
635620 except OSError as err :
636- onexc (os .path .islink , fullname , err )
637- continue
638- _rmtree_unsafe (fullname , onexc )
639- else :
621+ onexc (os .rmdir , fullname , err )
622+ for name in filenames :
623+ fullname = os .path .join (dirpath , name )
640624 try :
641625 os .unlink (fullname )
642626 except FileNotFoundError :
0 commit comments