4343import shutil as _shutil
4444import errno as _errno
4545from random import Random as _Random
46- import stat as _stat
4746import sys as _sys
4847import types as _types
4948import weakref as _weakref
@@ -277,68 +276,15 @@ def _dont_follow_symlinks(func, path, *args):
277276 elif _os .name == 'nt' or not _os .path .islink (path ):
278277 func (path , * args )
279278
280- def _resetflags (path ):
279+ def _resetperms (path ):
281280 try :
282281 chflags = _os .chflags
283282 except AttributeError :
284283 pass
285284 else :
286285 _dont_follow_symlinks (chflags , path , 0 )
287-
288- def _resetperms (path ):
289- _resetflags (path )
290286 _dont_follow_symlinks (_os .chmod , path , 0o700 )
291287
292- # True if TemporaryDirectory._rmtree() can work relative to open directories
293- # instead of resolving paths again.
294- _rmtree_use_dir_fd = (
295- {_os .chmod , _os .unlink , _os .lstat } <= _os .supports_dir_fd
296- and _os .chmod in _os .supports_fd
297- )
298-
299- def _resetperms_fd (dir_fd , path ):
300- # Same as _resetperms(), but for the directory referred to by dir_fd.
301- if dir_fd is None :
302- _resetperms (path )
303- return
304- _resetflags (path )
305- _os .chmod (dir_fd , 0o700 )
306-
307- try :
308- _nofollow_mode = _os .O_RDONLY | _os .O_NONBLOCK | _os .O_NOFOLLOW
309- except AttributeError :
310- _nofollow_mode = None
311-
312- def _resetperms_at (name , dir_fd , path ):
313- # Same as _resetperms(), but name is resolved relative to the directory
314- # file descriptor dir_fd. path is only used for os.chflags(), which
315- # doesn't support dir_fd or file descriptors.
316- if dir_fd is None :
317- _resetperms (path )
318- return
319- _resetflags (path )
320- if _os .chmod in _os .supports_follow_symlinks :
321- _os .chmod (name , 0o700 , dir_fd = dir_fd , follow_symlinks = False )
322- else :
323- # dir_fd & follow_symlinks is not supported on this platform.
324- # Try chmod opening the file with O_NOFOLLOW.
325- if _nofollow_mode is not None :
326- try :
327- fd = _os .open (name , _nofollow_mode , dir_fd = dir_fd )
328- except OSError :
329- pass
330- else :
331- try :
332- _os .chmod (fd , 0o700 )
333- finally :
334- _os .close (fd )
335- return
336- # If that did not work, we change by name, which is subject to a race
337- # condition.
338- stat = _os .lstat (name , dir_fd = dir_fd )
339- if not _stat .S_ISLNK (stat .st_mode ):
340- _os .chmod (name , 0o700 , dir_fd = dir_fd )
341-
342288
343289# User visible interfaces.
344290
@@ -946,42 +892,23 @@ def __init__(self, suffix=None, prefix=None, dir=None,
946892 ignore_errors = self ._ignore_cleanup_errors , delete = self ._delete )
947893
948894 @classmethod
949- def _rmtree (cls , name , ignore_errors = False , repeated = False , dir_fd = None ,
950- fullname = None ):
951- if fullname is None :
952- fullname = name
953-
954- def onexc (func , path , exc , direntry = None , dir_fd = None ):
895+ def _rmtree (cls , name , ignore_errors = False , repeated = False ):
896+ def onexc (func , path , exc ):
955897 if isinstance (exc , PermissionError ):
956898 if repeated and path == name :
957899 if ignore_errors :
958900 return
959901 raise
960902
961- # fullpath is path as seen from the working directory
962- fullpath = fullname + path [len (name ):]
963- # base is path relative to dir_fd, the directory rmtree()
964- # reached it through, or the whole path when there is none
965- if dir_fd is None or not _rmtree_use_dir_fd :
966- base , dir_fd = path , None
967- elif direntry is None :
968- base = path
969- else :
970- base = direntry .name
971-
972903 try :
973904 if path != name :
974- # The parent directory of path is the one referred to
975- # by dir_fd.
976- _resetperms_fd (dir_fd , _os .path .dirname (fullpath ))
977- _resetperms_at (base , dir_fd , fullpath )
905+ _resetperms (_os .path .dirname (path ))
906+ _resetperms (path )
978907
979908 try :
980- _os .unlink (base , dir_fd = dir_fd )
909+ _os .unlink (path )
981910 except IsADirectoryError :
982- cls ._rmtree (base , ignore_errors = ignore_errors ,
983- repeated = (path == name ),
984- dir_fd = dir_fd , fullname = fullpath )
911+ cls ._rmtree (path , ignore_errors = ignore_errors )
985912 except PermissionError :
986913 # The PermissionError handler was originally added for
987914 # FreeBSD in directories, but it seems that it is raised
@@ -990,27 +917,21 @@ def onexc(func, path, exc, direntry=None, dir_fd=None):
990917 # raise NotADirectoryError and mask the PermissionError.
991918 # So we must re-raise the current PermissionError if
992919 # path is not a directory.
993- if (not _os .path .isdir (fullpath )
994- or _os .path .isjunction (fullpath )):
920+ if not _os .path .isdir (path ) or _os .path .isjunction (path ):
995921 if ignore_errors :
996922 return
997923 raise
998- cls ._rmtree (base , ignore_errors = ignore_errors ,
999- repeated = (path == name ),
1000- dir_fd = dir_fd , fullname = fullpath )
924+ cls ._rmtree (path , ignore_errors = ignore_errors ,
925+ repeated = (path == name ))
1001926 except FileNotFoundError :
1002927 pass
1003- except OSError :
1004- if ignore_errors :
1005- return
1006- raise
1007928 elif isinstance (exc , FileNotFoundError ):
1008929 pass
1009930 else :
1010931 if not ignore_errors :
1011932 raise
1012933
1013- _shutil .rmtree (name , onexc = onexc , dir_fd = dir_fd , _onexc_kwargs = True )
934+ _shutil .rmtree (name , onexc = onexc )
1014935
1015936 @classmethod
1016937 def _cleanup (cls , name , warn_message , ignore_errors = False , delete = True ):
0 commit comments