|
14 | 14 |
|
15 | 15 | This module creates temporary files and directories. It works on all |
16 | 16 | supported platforms. :class:`TemporaryFile`, :class:`NamedTemporaryFile`, |
17 | | -:class:`TemporaryDirectory`, and :class:`SpooledTemporaryFile` are high-level |
18 | | -interfaces which provide automatic cleanup and can be used as |
19 | | -:term:`context managers <context manager>`. :func:`mkstemp` and |
20 | | -:func:`mkdtemp` are lower-level functions which require manual cleanup. |
| 17 | +:class:`TemporaryFileWrapper`, :class:`TemporaryDirectory`, and |
| 18 | +:class:`SpooledTemporaryFile` are high-level interfaces which provide |
| 19 | +automatic cleanup and can be used as :term:`context managers |
| 20 | +<context manager>`. :func:`mkstemp` and :func:`mkdtemp` are lower-level |
| 21 | +functions which require manual cleanup. |
21 | 22 |
|
22 | 23 | All the user-callable functions and constructors take additional arguments which |
23 | 24 | allow direct control over the location and name of temporary files and |
@@ -84,13 +85,13 @@ The module defines the following user-callable items: |
84 | 85 | :func:`TemporaryFile` with *delete* and *delete_on_close* parameters that |
85 | 86 | determine whether and how the named file should be automatically deleted. |
86 | 87 |
|
87 | | - The returned object is always a :term:`file-like object` whose :attr:`!file` |
88 | | - attribute is the underlying true file object. This file-like object |
89 | | - can be used in a :keyword:`with` statement, just like a normal file. The |
90 | | - name of the temporary file can be retrieved from the :attr:`!name` attribute |
91 | | - of the returned file-like object. On Unix, unlike with the |
92 | | - :func:`TemporaryFile`, the directory entry does not get unlinked immediately |
93 | | - after the file creation. |
| 88 | + The returned object is always a :class:`TemporaryFileWrapper` instance |
| 89 | + (a :term:`file-like object`) whose :attr:`~TemporaryFileWrapper.file` attribute is the underlying |
| 90 | + true file object. This file-like object can be used in a :keyword:`with` |
| 91 | + statement, just like a normal file. The name of the temporary file can be |
| 92 | + retrieved from the :attr:`!name` attribute of the returned file-like object. |
| 93 | + On Unix, unlike with the :func:`TemporaryFile`, the directory entry does not |
| 94 | + get unlinked immediately after the file creation. |
94 | 95 |
|
95 | 96 | If *delete* is true (the default) and *delete_on_close* is true (the |
96 | 97 | default), the file is deleted as soon as it is closed. If *delete* is true |
@@ -140,6 +141,34 @@ The module defines the following user-callable items: |
140 | 141 | .. versionchanged:: 3.12 |
141 | 142 | Added *delete_on_close* parameter. |
142 | 143 |
|
| 144 | +.. class:: TemporaryFileWrapper(file, name, delete=True, delete_on_close=True) |
| 145 | + |
| 146 | + A mutable wrapper returned by :func:`NamedTemporaryFile`. It wraps the |
| 147 | + underlying file object, delegating attribute access to it, and ensures |
| 148 | + the temporary file is deleted when appropriate. |
| 149 | + |
| 150 | + .. attribute:: file |
| 151 | + |
| 152 | + The underlying :term:`file-like object`. |
| 153 | + |
| 154 | + .. attribute:: name |
| 155 | + |
| 156 | + The file name of the temporary file. |
| 157 | + |
| 158 | + .. method:: close() |
| 159 | + |
| 160 | + Close the temporary file, possibly deleting it depending on the |
| 161 | + *delete* and *delete_on_close* arguments passed to |
| 162 | + :func:`NamedTemporaryFile`. |
| 163 | + |
| 164 | + .. note:: |
| 165 | + |
| 166 | + ``tempfile._TemporaryFileWrapper`` is kept as a backwards compatible |
| 167 | + deprecated alias for this class. |
| 168 | + It will be removed in Python 3.21 |
| 169 | + |
| 170 | + .. versionadded:: next |
| 171 | + |
143 | 172 |
|
144 | 173 | .. class:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None) |
145 | 174 |
|
|
0 commit comments