summaryrefslogtreecommitdiffstats
path: root/Lib/tempfile.py
diff options
context:
space:
mode:
authorJakobDev <jakobdev@gmx.de>2023-03-24 21:52:06 (GMT)
committerGitHub <noreply@github.com>2023-03-24 21:52:06 (GMT)
commit64cb1a4f0f0bc733a33ad7a6520e749ca1cdd43f (patch)
tree8565787413edcbc7a0475d7f67726b1938507c56 /Lib/tempfile.py
parentded9a7fc194a1d5c0e38f475a45f8f77dbe9c6bc (diff)
downloadcpython-64cb1a4f0f0bc733a33ad7a6520e749ca1cdd43f.zip
cpython-64cb1a4f0f0bc733a33ad7a6520e749ca1cdd43f.tar.gz
cpython-64cb1a4f0f0bc733a33ad7a6520e749ca1cdd43f.tar.bz2
gh-100131: Add optional delete parameter to tempfile.TemporaryDirectory() (#100132)
Add optional delete parameter to tempfile.TemporaryDirectory(). Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r--Lib/tempfile.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index cf06092..4732eb0 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -850,17 +850,26 @@ class TemporaryDirectory:
...
Upon exiting the context, the directory and everything contained
- in it are removed.
+ in it are removed (unless delete=False is passed or an exception
+ is raised during cleanup and ignore_cleanup_errors is not True).
+
+ Optional Arguments:
+ suffix - A str suffix for the directory name. (see mkdtemp)
+ prefix - A str prefix for the directory name. (see mkdtemp)
+ dir - A directory to create this temp dir in. (see mkdtemp)
+ ignore_cleanup_errors - False; ignore exceptions during cleanup?
+ delete - True; whether the directory is automatically deleted.
"""
def __init__(self, suffix=None, prefix=None, dir=None,
- ignore_cleanup_errors=False):
+ ignore_cleanup_errors=False, *, delete=True):
self.name = mkdtemp(suffix, prefix, dir)
self._ignore_cleanup_errors = ignore_cleanup_errors
+ self._delete = delete
self._finalizer = _weakref.finalize(
self, self._cleanup, self.name,
warn_message="Implicitly cleaning up {!r}".format(self),
- ignore_errors=self._ignore_cleanup_errors)
+ ignore_errors=self._ignore_cleanup_errors, delete=self._delete)
@classmethod
def _rmtree(cls, name, ignore_errors=False):
@@ -894,9 +903,10 @@ class TemporaryDirectory:
_shutil.rmtree(name, onexc=onexc)
@classmethod
- def _cleanup(cls, name, warn_message, ignore_errors=False):
- cls._rmtree(name, ignore_errors=ignore_errors)
- _warnings.warn(warn_message, ResourceWarning)
+ def _cleanup(cls, name, warn_message, ignore_errors=False, delete=True):
+ if delete:
+ cls._rmtree(name, ignore_errors=ignore_errors)
+ _warnings.warn(warn_message, ResourceWarning)
def __repr__(self):
return "<{} {!r}>".format(self.__class__.__name__, self.name)
@@ -905,7 +915,8 @@ class TemporaryDirectory:
return self.name
def __exit__(self, exc, value, tb):
- self.cleanup()
+ if self._delete:
+ self.cleanup()
def cleanup(self):
if self._finalizer.detach() or _os.path.exists(self.name):