• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Temporary files.
2
3This module provides generic, low- and high-level interfaces for
4creating temporary files and directories.  All of the interfaces
5provided by this module can be used without fear of race conditions
6except for 'mktemp'.  'mktemp' is subject to race conditions and
7should not be used; it is provided for backward compatibility only.
8
9The default path names are returned as str.  If you supply bytes as
10input, all return values will be in bytes.  Ex:
11
12    >>> tempfile.mkstemp()
13    (4, '/tmp/tmptpu9nin8')
14    >>> tempfile.mkdtemp(suffix=b'')
15    b'/tmp/tmppbi8f0hy'
16
17This module also provides some data items to the user:
18
19  TMP_MAX  - maximum number of names that will be tried before
20             giving up.
21  tempdir  - If this is set to a string before the first use of
22             any routine from this module, it will be considered as
23             another candidate location to store temporary files.
24"""
25
26__all__ = [
27    "NamedTemporaryFile", "TemporaryFile", # high level safe interfaces
28    "SpooledTemporaryFile", "TemporaryDirectory",
29    "mkstemp", "mkdtemp",                  # low level safe interfaces
30    "mktemp",                              # deprecated unsafe interface
31    "TMP_MAX", "gettempprefix",            # constants
32    "tempdir", "gettempdir",
33    "gettempprefixb", "gettempdirb",
34   ]
35
36
37# Imports.
38
39import functools as _functools
40import warnings as _warnings
41import io as _io
42import os as _os
43import shutil as _shutil
44import errno as _errno
45from random import Random as _Random
46import weakref as _weakref
47import _thread
48_allocate_lock = _thread.allocate_lock
49
50_text_openflags = _os.O_RDWR | _os.O_CREAT | _os.O_EXCL
51if hasattr(_os, 'O_NOFOLLOW'):
52    _text_openflags |= _os.O_NOFOLLOW
53
54_bin_openflags = _text_openflags
55if hasattr(_os, 'O_BINARY'):
56    _bin_openflags |= _os.O_BINARY
57
58if hasattr(_os, 'TMP_MAX'):
59    TMP_MAX = _os.TMP_MAX
60else:
61    TMP_MAX = 10000
62
63# This variable _was_ unused for legacy reasons, see issue 10354.
64# But as of 3.5 we actually use it at runtime so changing it would
65# have a possibly desirable side effect...  But we do not want to support
66# that as an API.  It is undocumented on purpose.  Do not depend on this.
67template = "tmp"
68
69# Internal routines.
70
71_once_lock = _allocate_lock()
72
73if hasattr(_os, "lstat"):
74    _stat = _os.lstat
75elif hasattr(_os, "stat"):
76    _stat = _os.stat
77else:
78    # Fallback.  All we need is something that raises OSError if the
79    # file doesn't exist.
80    def _stat(fn):
81        fd = _os.open(fn, _os.O_RDONLY)
82        _os.close(fd)
83
84def _exists(fn):
85    try:
86        _stat(fn)
87    except OSError:
88        return False
89    else:
90        return True
91
92
93def _infer_return_type(*args):
94    """Look at the type of all args and divine their implied return type."""
95    return_type = None
96    for arg in args:
97        if arg is None:
98            continue
99        if isinstance(arg, bytes):
100            if return_type is str:
101                raise TypeError("Can't mix bytes and non-bytes in "
102                                "path components.")
103            return_type = bytes
104        else:
105            if return_type is bytes:
106                raise TypeError("Can't mix bytes and non-bytes in "
107                                "path components.")
108            return_type = str
109    if return_type is None:
110        return str  # tempfile APIs return a str by default.
111    return return_type
112
113
114def _sanitize_params(prefix, suffix, dir):
115    """Common parameter processing for most APIs in this module."""
116    output_type = _infer_return_type(prefix, suffix, dir)
117    if suffix is None:
118        suffix = output_type()
119    if prefix is None:
120        if output_type is str:
121            prefix = template
122        else:
123            prefix = _os.fsencode(template)
124    if dir is None:
125        if output_type is str:
126            dir = gettempdir()
127        else:
128            dir = gettempdirb()
129    return prefix, suffix, dir, output_type
130
131
132class _RandomNameSequence:
133    """An instance of _RandomNameSequence generates an endless
134    sequence of unpredictable strings which can safely be incorporated
135    into file names.  Each string is eight characters long.  Multiple
136    threads can safely use the same instance at the same time.
137
138    _RandomNameSequence is an iterator."""
139
140    characters = "abcdefghijklmnopqrstuvwxyz0123456789_"
141
142    @property
143    def rng(self):
144        cur_pid = _os.getpid()
145        if cur_pid != getattr(self, '_rng_pid', None):
146            self._rng = _Random()
147            self._rng_pid = cur_pid
148        return self._rng
149
150    def __iter__(self):
151        return self
152
153    def __next__(self):
154        c = self.characters
155        choose = self.rng.choice
156        letters = [choose(c) for dummy in range(8)]
157        return ''.join(letters)
158
159def _candidate_tempdir_list():
160    """Generate a list of candidate temporary directories which
161    _get_default_tempdir will try."""
162
163    dirlist = []
164
165    # First, try the environment.
166    for envname in 'TMPDIR', 'TEMP', 'TMP':
167        dirname = _os.getenv(envname)
168        if dirname: dirlist.append(dirname)
169
170    # Failing that, try OS-specific locations.
171    if _os.name == 'nt':
172        dirlist.extend([ _os.path.expanduser(r'~\AppData\Local\Temp'),
173                         _os.path.expandvars(r'%SYSTEMROOT%\Temp'),
174                         r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ])
175    else:
176        dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ])
177
178    # As a last resort, the current directory.
179    try:
180        dirlist.append(_os.getcwd())
181    except (AttributeError, OSError):
182        dirlist.append(_os.curdir)
183
184    return dirlist
185
186def _get_default_tempdir():
187    """Calculate the default directory to use for temporary files.
188    This routine should be called exactly once.
189
190    We determine whether or not a candidate temp dir is usable by
191    trying to create and write to a file in that directory.  If this
192    is successful, the test file is deleted.  To prevent denial of
193    service, the name of the test file must be randomized."""
194
195    namer = _RandomNameSequence()
196    dirlist = _candidate_tempdir_list()
197
198    for dir in dirlist:
199        if dir != _os.curdir:
200            dir = _os.path.abspath(dir)
201        # Try only a few names per directory.
202        for seq in range(100):
203            name = next(namer)
204            filename = _os.path.join(dir, name)
205            try:
206                fd = _os.open(filename, _bin_openflags, 0o600)
207                try:
208                    try:
209                        with _io.open(fd, 'wb', closefd=False) as fp:
210                            fp.write(b'blat')
211                    finally:
212                        _os.close(fd)
213                finally:
214                    _os.unlink(filename)
215                return dir
216            except FileExistsError:
217                pass
218            except PermissionError:
219                # This exception is thrown when a directory with the chosen name
220                # already exists on windows.
221                if (_os.name == 'nt' and _os.path.isdir(dir) and
222                    _os.access(dir, _os.W_OK)):
223                    continue
224                break   # no point trying more names in this directory
225            except OSError:
226                break   # no point trying more names in this directory
227    raise FileNotFoundError(_errno.ENOENT,
228                            "No usable temporary directory found in %s" %
229                            dirlist)
230
231_name_sequence = None
232
233def _get_candidate_names():
234    """Common setup sequence for all user-callable interfaces."""
235
236    global _name_sequence
237    if _name_sequence is None:
238        _once_lock.acquire()
239        try:
240            if _name_sequence is None:
241                _name_sequence = _RandomNameSequence()
242        finally:
243            _once_lock.release()
244    return _name_sequence
245
246
247def _mkstemp_inner(dir, pre, suf, flags, output_type):
248    """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
249
250    names = _get_candidate_names()
251    if output_type is bytes:
252        names = map(_os.fsencode, names)
253
254    for seq in range(TMP_MAX):
255        name = next(names)
256        file = _os.path.join(dir, pre + name + suf)
257        try:
258            fd = _os.open(file, flags, 0o600)
259        except FileExistsError:
260            continue    # try again
261        except PermissionError:
262            # This exception is thrown when a directory with the chosen name
263            # already exists on windows.
264            if (_os.name == 'nt' and _os.path.isdir(dir) and
265                _os.access(dir, _os.W_OK)):
266                continue
267            else:
268                raise
269        return (fd, _os.path.abspath(file))
270
271    raise FileExistsError(_errno.EEXIST,
272                          "No usable temporary file name found")
273
274
275# User visible interfaces.
276
277def gettempprefix():
278    """The default prefix for temporary directories."""
279    return template
280
281def gettempprefixb():
282    """The default prefix for temporary directories as bytes."""
283    return _os.fsencode(gettempprefix())
284
285tempdir = None
286
287def gettempdir():
288    """Accessor for tempfile.tempdir."""
289    global tempdir
290    if tempdir is None:
291        _once_lock.acquire()
292        try:
293            if tempdir is None:
294                tempdir = _get_default_tempdir()
295        finally:
296            _once_lock.release()
297    return tempdir
298
299def gettempdirb():
300    """A bytes version of tempfile.gettempdir()."""
301    return _os.fsencode(gettempdir())
302
303def mkstemp(suffix=None, prefix=None, dir=None, text=False):
304    """User-callable function to create and return a unique temporary
305    file.  The return value is a pair (fd, name) where fd is the
306    file descriptor returned by os.open, and name is the filename.
307
308    If 'suffix' is not None, the file name will end with that suffix,
309    otherwise there will be no suffix.
310
311    If 'prefix' is not None, the file name will begin with that prefix,
312    otherwise a default prefix is used.
313
314    If 'dir' is not None, the file will be created in that directory,
315    otherwise a default directory is used.
316
317    If 'text' is specified and true, the file is opened in text
318    mode.  Else (the default) the file is opened in binary mode.  On
319    some operating systems, this makes no difference.
320
321    If any of 'suffix', 'prefix' and 'dir' are not None, they must be the
322    same type.  If they are bytes, the returned name will be bytes; str
323    otherwise.
324
325    The file is readable and writable only by the creating user ID.
326    If the operating system uses permission bits to indicate whether a
327    file is executable, the file is executable by no one. The file
328    descriptor is not inherited by children of this process.
329
330    Caller is responsible for deleting the file when done with it.
331    """
332
333    prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
334
335    if text:
336        flags = _text_openflags
337    else:
338        flags = _bin_openflags
339
340    return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
341
342
343def mkdtemp(suffix=None, prefix=None, dir=None):
344    """User-callable function to create and return a unique temporary
345    directory.  The return value is the pathname of the directory.
346
347    Arguments are as for mkstemp, except that the 'text' argument is
348    not accepted.
349
350    The directory is readable, writable, and searchable only by the
351    creating user.
352
353    Caller is responsible for deleting the directory when done with it.
354    """
355
356    prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
357
358    names = _get_candidate_names()
359    if output_type is bytes:
360        names = map(_os.fsencode, names)
361
362    for seq in range(TMP_MAX):
363        name = next(names)
364        file = _os.path.join(dir, prefix + name + suffix)
365        try:
366            _os.mkdir(file, 0o700)
367        except FileExistsError:
368            continue    # try again
369        except PermissionError:
370            # This exception is thrown when a directory with the chosen name
371            # already exists on windows.
372            if (_os.name == 'nt' and _os.path.isdir(dir) and
373                _os.access(dir, _os.W_OK)):
374                continue
375            else:
376                raise
377        return file
378
379    raise FileExistsError(_errno.EEXIST,
380                          "No usable temporary directory name found")
381
382def mktemp(suffix="", prefix=template, dir=None):
383    """User-callable function to return a unique temporary file name.  The
384    file is not created.
385
386    Arguments are similar to mkstemp, except that the 'text' argument is
387    not accepted, and suffix=None, prefix=None and bytes file names are not
388    supported.
389
390    THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED.  The file name may
391    refer to a file that did not exist at some point, but by the time
392    you get around to creating it, someone else may have beaten you to
393    the punch.
394    """
395
396##    from warnings import warn as _warn
397##    _warn("mktemp is a potential security risk to your program",
398##          RuntimeWarning, stacklevel=2)
399
400    if dir is None:
401        dir = gettempdir()
402
403    names = _get_candidate_names()
404    for seq in range(TMP_MAX):
405        name = next(names)
406        file = _os.path.join(dir, prefix + name + suffix)
407        if not _exists(file):
408            return file
409
410    raise FileExistsError(_errno.EEXIST,
411                          "No usable temporary filename found")
412
413
414class _TemporaryFileCloser:
415    """A separate object allowing proper closing of a temporary file's
416    underlying file object, without adding a __del__ method to the
417    temporary file."""
418
419    file = None  # Set here since __del__ checks it
420    close_called = False
421
422    def __init__(self, file, name, delete=True):
423        self.file = file
424        self.name = name
425        self.delete = delete
426
427    # NT provides delete-on-close as a primitive, so we don't need
428    # the wrapper to do anything special.  We still use it so that
429    # file.name is useful (i.e. not "(fdopen)") with NamedTemporaryFile.
430    if _os.name != 'nt':
431        # Cache the unlinker so we don't get spurious errors at
432        # shutdown when the module-level "os" is None'd out.  Note
433        # that this must be referenced as self.unlink, because the
434        # name TemporaryFileWrapper may also get None'd out before
435        # __del__ is called.
436
437        def close(self, unlink=_os.unlink):
438            if not self.close_called and self.file is not None:
439                self.close_called = True
440                try:
441                    self.file.close()
442                finally:
443                    if self.delete:
444                        unlink(self.name)
445
446        # Need to ensure the file is deleted on __del__
447        def __del__(self):
448            self.close()
449
450    else:
451        def close(self):
452            if not self.close_called:
453                self.close_called = True
454                self.file.close()
455
456
457class _TemporaryFileWrapper:
458    """Temporary file wrapper
459
460    This class provides a wrapper around files opened for
461    temporary use.  In particular, it seeks to automatically
462    remove the file when it is no longer needed.
463    """
464
465    def __init__(self, file, name, delete=True):
466        self.file = file
467        self.name = name
468        self.delete = delete
469        self._closer = _TemporaryFileCloser(file, name, delete)
470
471    def __getattr__(self, name):
472        # Attribute lookups are delegated to the underlying file
473        # and cached for non-numeric results
474        # (i.e. methods are cached, closed and friends are not)
475        file = self.__dict__['file']
476        a = getattr(file, name)
477        if hasattr(a, '__call__'):
478            func = a
479            @_functools.wraps(func)
480            def func_wrapper(*args, **kwargs):
481                return func(*args, **kwargs)
482            # Avoid closing the file as long as the wrapper is alive,
483            # see issue #18879.
484            func_wrapper._closer = self._closer
485            a = func_wrapper
486        if not isinstance(a, int):
487            setattr(self, name, a)
488        return a
489
490    # The underlying __enter__ method returns the wrong object
491    # (self.file) so override it to return the wrapper
492    def __enter__(self):
493        self.file.__enter__()
494        return self
495
496    # Need to trap __exit__ as well to ensure the file gets
497    # deleted when used in a with statement
498    def __exit__(self, exc, value, tb):
499        result = self.file.__exit__(exc, value, tb)
500        self.close()
501        return result
502
503    def close(self):
504        """
505        Close the temporary file, possibly deleting it.
506        """
507        self._closer.close()
508
509    # iter() doesn't use __getattr__ to find the __iter__ method
510    def __iter__(self):
511        # Don't return iter(self.file), but yield from it to avoid closing
512        # file as long as it's being used as iterator (see issue #23700).  We
513        # can't use 'yield from' here because iter(file) returns the file
514        # object itself, which has a close method, and thus the file would get
515        # closed when the generator is finalized, due to PEP380 semantics.
516        for line in self.file:
517            yield line
518
519
520def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
521                       newline=None, suffix=None, prefix=None,
522                       dir=None, delete=True):
523    """Create and return a temporary file.
524    Arguments:
525    'prefix', 'suffix', 'dir' -- as for mkstemp.
526    'mode' -- the mode argument to io.open (default "w+b").
527    'buffering' -- the buffer size argument to io.open (default -1).
528    'encoding' -- the encoding argument to io.open (default None)
529    'newline' -- the newline argument to io.open (default None)
530    'delete' -- whether the file is deleted on close (default True).
531    The file is created as mkstemp() would do it.
532
533    Returns an object with a file-like interface; the name of the file
534    is accessible as its 'name' attribute.  The file will be automatically
535    deleted when it is closed unless the 'delete' argument is set to False.
536    """
537
538    prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
539
540    flags = _bin_openflags
541
542    # Setting O_TEMPORARY in the flags causes the OS to delete
543    # the file when it is closed.  This is only supported by Windows.
544    if _os.name == 'nt' and delete:
545        flags |= _os.O_TEMPORARY
546
547    (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
548    try:
549        file = _io.open(fd, mode, buffering=buffering,
550                        newline=newline, encoding=encoding)
551
552        return _TemporaryFileWrapper(file, name, delete)
553    except BaseException:
554        _os.unlink(name)
555        _os.close(fd)
556        raise
557
558if _os.name != 'posix' or _os.sys.platform == 'cygwin':
559    # On non-POSIX and Cygwin systems, assume that we cannot unlink a file
560    # while it is open.
561    TemporaryFile = NamedTemporaryFile
562
563else:
564    # Is the O_TMPFILE flag available and does it work?
565    # The flag is set to False if os.open(dir, os.O_TMPFILE) raises an
566    # IsADirectoryError exception
567    _O_TMPFILE_WORKS = hasattr(_os, 'O_TMPFILE')
568
569    def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
570                      newline=None, suffix=None, prefix=None,
571                      dir=None):
572        """Create and return a temporary file.
573        Arguments:
574        'prefix', 'suffix', 'dir' -- as for mkstemp.
575        'mode' -- the mode argument to io.open (default "w+b").
576        'buffering' -- the buffer size argument to io.open (default -1).
577        'encoding' -- the encoding argument to io.open (default None)
578        'newline' -- the newline argument to io.open (default None)
579        The file is created as mkstemp() would do it.
580
581        Returns an object with a file-like interface.  The file has no
582        name, and will cease to exist when it is closed.
583        """
584        global _O_TMPFILE_WORKS
585
586        prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
587
588        flags = _bin_openflags
589        if _O_TMPFILE_WORKS:
590            try:
591                flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT
592                fd = _os.open(dir, flags2, 0o600)
593            except IsADirectoryError:
594                # Linux kernel older than 3.11 ignores the O_TMPFILE flag:
595                # O_TMPFILE is read as O_DIRECTORY. Trying to open a directory
596                # with O_RDWR|O_DIRECTORY fails with IsADirectoryError, a
597                # directory cannot be open to write. Set flag to False to not
598                # try again.
599                _O_TMPFILE_WORKS = False
600            except OSError:
601                # The filesystem of the directory does not support O_TMPFILE.
602                # For example, OSError(95, 'Operation not supported').
603                #
604                # On Linux kernel older than 3.11, trying to open a regular
605                # file (or a symbolic link to a regular file) with O_TMPFILE
606                # fails with NotADirectoryError, because O_TMPFILE is read as
607                # O_DIRECTORY.
608                pass
609            else:
610                try:
611                    return _io.open(fd, mode, buffering=buffering,
612                                    newline=newline, encoding=encoding)
613                except:
614                    _os.close(fd)
615                    raise
616            # Fallback to _mkstemp_inner().
617
618        (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
619        try:
620            _os.unlink(name)
621            return _io.open(fd, mode, buffering=buffering,
622                            newline=newline, encoding=encoding)
623        except:
624            _os.close(fd)
625            raise
626
627class SpooledTemporaryFile:
628    """Temporary file wrapper, specialized to switch from BytesIO
629    or StringIO to a real file when it exceeds a certain size or
630    when a fileno is needed.
631    """
632    _rolled = False
633
634    def __init__(self, max_size=0, mode='w+b', buffering=-1,
635                 encoding=None, newline=None,
636                 suffix=None, prefix=None, dir=None):
637        if 'b' in mode:
638            self._file = _io.BytesIO()
639        else:
640            # Setting newline="\n" avoids newline translation;
641            # this is important because otherwise on Windows we'd
642            # get double newline translation upon rollover().
643            self._file = _io.StringIO(newline="\n")
644        self._max_size = max_size
645        self._rolled = False
646        self._TemporaryFileArgs = {'mode': mode, 'buffering': buffering,
647                                   'suffix': suffix, 'prefix': prefix,
648                                   'encoding': encoding, 'newline': newline,
649                                   'dir': dir}
650
651    def _check(self, file):
652        if self._rolled: return
653        max_size = self._max_size
654        if max_size and file.tell() > max_size:
655            self.rollover()
656
657    def rollover(self):
658        if self._rolled: return
659        file = self._file
660        newfile = self._file = TemporaryFile(**self._TemporaryFileArgs)
661        del self._TemporaryFileArgs
662
663        newfile.write(file.getvalue())
664        newfile.seek(file.tell(), 0)
665
666        self._rolled = True
667
668    # The method caching trick from NamedTemporaryFile
669    # won't work here, because _file may change from a
670    # BytesIO/StringIO instance to a real file. So we list
671    # all the methods directly.
672
673    # Context management protocol
674    def __enter__(self):
675        if self._file.closed:
676            raise ValueError("Cannot enter context with closed file")
677        return self
678
679    def __exit__(self, exc, value, tb):
680        self._file.close()
681
682    # file protocol
683    def __iter__(self):
684        return self._file.__iter__()
685
686    def close(self):
687        self._file.close()
688
689    @property
690    def closed(self):
691        return self._file.closed
692
693    @property
694    def encoding(self):
695        try:
696            return self._file.encoding
697        except AttributeError:
698            if 'b' in self._TemporaryFileArgs['mode']:
699                raise
700            return self._TemporaryFileArgs['encoding']
701
702    def fileno(self):
703        self.rollover()
704        return self._file.fileno()
705
706    def flush(self):
707        self._file.flush()
708
709    def isatty(self):
710        return self._file.isatty()
711
712    @property
713    def mode(self):
714        try:
715            return self._file.mode
716        except AttributeError:
717            return self._TemporaryFileArgs['mode']
718
719    @property
720    def name(self):
721        try:
722            return self._file.name
723        except AttributeError:
724            return None
725
726    @property
727    def newlines(self):
728        try:
729            return self._file.newlines
730        except AttributeError:
731            if 'b' in self._TemporaryFileArgs['mode']:
732                raise
733            return self._TemporaryFileArgs['newline']
734
735    def read(self, *args):
736        return self._file.read(*args)
737
738    def readline(self, *args):
739        return self._file.readline(*args)
740
741    def readlines(self, *args):
742        return self._file.readlines(*args)
743
744    def seek(self, *args):
745        self._file.seek(*args)
746
747    @property
748    def softspace(self):
749        return self._file.softspace
750
751    def tell(self):
752        return self._file.tell()
753
754    def truncate(self, size=None):
755        if size is None:
756            self._file.truncate()
757        else:
758            if size > self._max_size:
759                self.rollover()
760            self._file.truncate(size)
761
762    def write(self, s):
763        file = self._file
764        rv = file.write(s)
765        self._check(file)
766        return rv
767
768    def writelines(self, iterable):
769        file = self._file
770        rv = file.writelines(iterable)
771        self._check(file)
772        return rv
773
774
775class TemporaryDirectory(object):
776    """Create and return a temporary directory.  This has the same
777    behavior as mkdtemp but can be used as a context manager.  For
778    example:
779
780        with TemporaryDirectory() as tmpdir:
781            ...
782
783    Upon exiting the context, the directory and everything contained
784    in it are removed.
785    """
786
787    def __init__(self, suffix=None, prefix=None, dir=None):
788        self.name = mkdtemp(suffix, prefix, dir)
789        self._finalizer = _weakref.finalize(
790            self, self._cleanup, self.name,
791            warn_message="Implicitly cleaning up {!r}".format(self))
792
793    @classmethod
794    def _cleanup(cls, name, warn_message):
795        _shutil.rmtree(name)
796        _warnings.warn(warn_message, ResourceWarning)
797
798    def __repr__(self):
799        return "<{} {!r}>".format(self.__class__.__name__, self.name)
800
801    def __enter__(self):
802        return self.name
803
804    def __exit__(self, exc, value, tb):
805        self.cleanup()
806
807    def cleanup(self):
808        if self._finalizer.detach():
809            _shutil.rmtree(self.name)
810