diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst index 22d6dba9e1a9c6..18aa12be96fc8e 100644 --- a/Doc/library/shutil.rst +++ b/Doc/library/shutil.rst @@ -286,7 +286,7 @@ Directory and files operations .. versionadded:: 3.8 The *dirs_exist_ok* parameter. -.. function:: rmtree(path, ignore_errors=False, onerror=None) +.. function:: rmtree(path, ignore_errors=False, onerror=None, dir_fd=None) .. index:: single: directory; deleting diff --git a/Lib/shutil.py b/Lib/shutil.py index 949e024853c1d2..ab4134fba0cc23 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -680,7 +680,7 @@ def _rmtree_safe_fd(topfd, path, onerror): os.scandir in os.supports_fd and os.stat in os.supports_follow_symlinks) -def rmtree(path, ignore_errors=False, onerror=None): +def rmtree(path, ignore_errors=False, onerror=None, dir_fd=None): """Recursively delete a directory tree. If ignore_errors is set, errors are ignored; otherwise, if onerror @@ -691,7 +691,7 @@ def rmtree(path, ignore_errors=False, onerror=None): is false and onerror is None, an exception is raised. """ - sys.audit("shutil.rmtree", path) + sys.audit("shutil.rmtree", path, dir_fd) if ignore_errors: def onerror(*args): pass @@ -705,12 +705,12 @@ def onerror(*args): # Note: To guard against symlink races, we use the standard # lstat()/open()/fstat() trick. try: - orig_st = os.lstat(path) + orig_st = os.lstat(path, dir_fd=dir_fd) except Exception: onerror(os.lstat, path, sys.exc_info()) return try: - fd = os.open(path, os.O_RDONLY) + fd = os.open(path, os.O_RDONLY, dir_fd=dir_fd) except Exception: onerror(os.open, path, sys.exc_info()) return @@ -718,7 +718,7 @@ def onerror(*args): if os.path.samestat(orig_st, os.fstat(fd)): _rmtree_safe_fd(fd, path, onerror) try: - os.rmdir(path) + os.rmdir(path, dir_fd=dir_fd) except OSError: onerror(os.rmdir, path, sys.exc_info()) else: