Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 9b566c3

Browse files
committed
Issue python#25064: Adjust documentation according to new mkstemp signature
The mkstemp() and mkdtemp() signatures have already been updated, but the higher-level functions still suggest that the default values are forced to text strings. Also merged some paragraphs describing the "suffix" and "prefix" parameters, and pointed out that mktemp() does not support the new changes.
1 parent 97f46db commit 9b566c3

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

Doc/library/tempfile.rst

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ is recommended to use keyword arguments for clarity.
3333

3434
The module defines the following user-callable items:
3535

36-
.. function:: TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None)
36+
.. function:: TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None)
3737

3838
Return a :term:`file-like object` that can be used as a temporary storage area.
3939
The file is created securely, using the same rules as :func:`mkstemp`. It will be destroyed as soon
@@ -54,8 +54,8 @@ The module defines the following user-callable items:
5454
stored. *buffering*, *encoding* and *newline* are interpreted as for
5555
:func:`open`.
5656

57-
The *dir*, *prefix* and *suffix* parameters have the same meaning
58-
as with :func:`mkstemp`.
57+
The *dir*, *prefix* and *suffix* parameters have the same meaning and
58+
defaults as with :func:`mkstemp`.
5959

6060
The returned object is a true file object on POSIX platforms. On other
6161
platforms, it is a file-like object whose :attr:`!file` attribute is the
@@ -69,7 +69,7 @@ The module defines the following user-callable items:
6969
The :py:data:`os.O_TMPFILE` flag is now used if available.
7070

7171

72-
.. function:: NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None, delete=True)
72+
.. function:: NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True)
7373

7474
This function operates exactly as :func:`TemporaryFile` does, except that
7575
the file is guaranteed to have a visible name in the file system (on
@@ -84,7 +84,7 @@ The module defines the following user-callable items:
8484
be used in a :keyword:`with` statement, just like a normal file.
8585

8686

87-
.. function:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None)
87+
.. function:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None)
8888

8989
This function operates exactly as :func:`TemporaryFile` does, except that
9090
data is spooled in memory until the file size exceeds *max_size*, or
@@ -106,7 +106,7 @@ The module defines the following user-callable items:
106106
the truncate method now accepts a ``size`` argument.
107107

108108

109-
.. function:: TemporaryDirectory(suffix='', prefix='tmp', dir=None)
109+
.. function:: TemporaryDirectory(suffix=None, prefix=None, dir=None)
110110

111111
This function securely creates a temporary directory using the same rules as :func:`mkdtemp`.
112112
The resulting object can be used as a context manager (see
@@ -138,32 +138,29 @@ The module defines the following user-callable items:
138138
Unlike :func:`TemporaryFile`, the user of :func:`mkstemp` is responsible
139139
for deleting the temporary file when done with it.
140140

141-
If *suffix* is specified, the file name will end with that suffix,
141+
If *suffix* is not ``None``, the file name will end with that suffix,
142142
otherwise there will be no suffix. :func:`mkstemp` does not put a dot
143143
between the file name and the suffix; if you need one, put it at the
144144
beginning of *suffix*.
145145

146-
If *prefix* is specified, the file name will begin with that prefix;
147-
otherwise, a default prefix is used.
146+
If *prefix* is not ``None``, the file name will begin with that prefix;
147+
otherwise, a default prefix is used. The default is the return value of
148+
:func:`gettempprefix` or :func:`gettempprefixb`, as appropriate.
148149

149-
If *dir* is specified, the file will be created in that directory;
150+
If *dir* is not ``None``, the file will be created in that directory;
150151
otherwise, a default directory is used. The default directory is chosen
151152
from a platform-dependent list, but the user of the application can
152153
control the directory location by setting the *TMPDIR*, *TEMP* or *TMP*
153154
environment variables. There is thus no guarantee that the generated
154155
filename will have any nice properties, such as not requiring quoting
155156
when passed to external commands via ``os.popen()``.
156157

157-
*suffix*, *prefix*, and *dir* must all contain the same type, if specified.
158+
If any of *suffix*, *prefix*, and *dir* are not
159+
``None``, they must be the same type.
158160
If they are bytes, the returned name will be bytes instead of str.
159161
If you want to force a bytes return value with otherwise default behavior,
160162
pass ``suffix=b''``.
161163

162-
A *prefix* value of ``None`` means use the return value of
163-
:func:`gettempprefix` or :func:`gettempprefixb` as appropriate.
164-
165-
A *suffix* value of ``None`` means use an appropriate empty value.
166-
167164
If *text* is specified, it indicates whether to open the file in binary
168165
mode (the default) or text mode. On some platforms, this makes no
169166
difference.
@@ -241,7 +238,7 @@ The module defines the following user-callable items:
241238

242239
.. function:: gettempprefixb()
243240

244-
Same as :func:`gettempprefixb` but the return value is in bytes.
241+
Same as :func:`gettempprefix` but the return value is in bytes.
245242

246243
.. versionadded:: 3.5
247244

@@ -314,8 +311,9 @@ other functions described above.
314311
Use :func:`mkstemp` instead.
315312

316313
Return an absolute pathname of a file that did not exist at the time the
317-
call is made. The *prefix*, *suffix*, and *dir* arguments are the same
318-
as for :func:`mkstemp`.
314+
call is made. The *prefix*, *suffix*, and *dir* arguments are similar
315+
to those of :func:`mkstemp`, except that bytes file names, ``suffix=None``
316+
and ``prefix=None`` are not supported.
319317

320318
.. warning::
321319

Lib/tempfile.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,22 @@ def mkstemp(suffix=None, prefix=None, dir=None, text=False):
307307
file. The return value is a pair (fd, name) where fd is the
308308
file descriptor returned by os.open, and name is the filename.
309309
310-
If 'suffix' is specified, the file name will end with that suffix,
310+
If 'suffix' is not None, the file name will end with that suffix,
311311
otherwise there will be no suffix.
312312
313-
If 'prefix' is specified, the file name will begin with that prefix,
313+
If 'prefix' is not None, the file name will begin with that prefix,
314314
otherwise a default prefix is used.
315315
316-
If 'dir' is specified, the file will be created in that directory,
316+
If 'dir' is not None, the file will be created in that directory,
317317
otherwise a default directory is used.
318318
319319
If 'text' is specified and true, the file is opened in text
320320
mode. Else (the default) the file is opened in binary mode. On
321321
some operating systems, this makes no difference.
322322
323-
suffix, prefix and dir must all contain the same type if specified.
324-
If they are bytes, the returned name will be bytes; str otherwise.
325-
A value of None will cause an appropriate default to be used.
323+
If any of 'suffix', 'prefix' and 'dir' are not None, they must be the
324+
same type. If they are bytes, the returned name will be bytes; str
325+
otherwise.
326326
327327
The file is readable and writable only by the creating user ID.
328328
If the operating system uses permission bits to indicate whether a
@@ -385,8 +385,9 @@ def mktemp(suffix="", prefix=template, dir=None):
385385
"""User-callable function to return a unique temporary file name. The
386386
file is not created.
387387
388-
Arguments are as for mkstemp, except that the 'text' argument is
389-
not accepted.
388+
Arguments are similar to mkstemp, except that the 'text' argument is
389+
not accepted, and suffix=None, prefix=None and bytes file names are not
390+
supported.
390391
391392
THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED. The file name may
392393
refer to a file that did not exist at some point, but by the time

0 commit comments

Comments
 (0)