Commit ad577b93 authored by Gregory P. Smith's avatar Gregory P. Smith

Issue 24230: The tempfile module now accepts bytes for prefix, suffix and dir

parameters and returns bytes in such situations (matching the os module APIs).
parent 4a7fe7e3
...@@ -119,7 +119,7 @@ The module defines the following user-callable items: ...@@ -119,7 +119,7 @@ The module defines the following user-callable items:
.. versionadded:: 3.2 .. versionadded:: 3.2
.. function:: mkstemp(suffix='', prefix='tmp', dir=None, text=False) .. function:: mkstemp(suffix=None, prefix=None, dir=None, text=False)
Creates a temporary file in the most secure manner possible. There are Creates a temporary file in the most secure manner possible. There are
no race conditions in the file's creation, assuming that the platform no race conditions in the file's creation, assuming that the platform
...@@ -148,6 +148,16 @@ The module defines the following user-callable items: ...@@ -148,6 +148,16 @@ The module defines the following user-callable items:
filename will have any nice properties, such as not requiring quoting filename will have any nice properties, such as not requiring quoting
when passed to external commands via ``os.popen()``. when passed to external commands via ``os.popen()``.
*suffix*, *prefix*, and *dir* must all contain the same type, if specified.
If they are bytes, the returned name will be bytes instead of str.
If you want to force a bytes return value with otherwise default behavior,
pass ``suffix=b''``.
A *prefix* value of ``None`` means use the return value of
:func:`gettempprefix` or :func:`gettempprefixb` as appropriate.
A *suffix* value of ``None`` means use an appropriate empty value.
If *text* is specified, it indicates whether to open the file in binary If *text* is specified, it indicates whether to open the file in binary
mode (the default) or text mode. On some platforms, this makes no mode (the default) or text mode. On some platforms, this makes no
difference. difference.
...@@ -156,8 +166,14 @@ The module defines the following user-callable items: ...@@ -156,8 +166,14 @@ The module defines the following user-callable items:
file (as would be returned by :func:`os.open`) and the absolute pathname file (as would be returned by :func:`os.open`) and the absolute pathname
of that file, in that order. of that file, in that order.
.. versionchanged:: 3.5
*suffix*, *prefix*, and *dir* may now be supplied in bytes in order to
obtain a bytes return value. Prior to this, only str was allowed.
*suffix* and *prefix* now accept and default to ``None`` to cause
an appropriate default value to be used.
.. function:: mkdtemp(suffix='', prefix='tmp', dir=None) .. function:: mkdtemp(suffix=None, prefix=None, dir=None)
Creates a temporary directory in the most secure manner possible. There Creates a temporary directory in the most secure manner possible. There
are no race conditions in the directory's creation. The directory is are no race conditions in the directory's creation. The directory is
...@@ -171,6 +187,12 @@ The module defines the following user-callable items: ...@@ -171,6 +187,12 @@ The module defines the following user-callable items:
:func:`mkdtemp` returns the absolute pathname of the new directory. :func:`mkdtemp` returns the absolute pathname of the new directory.
.. versionchanged:: 3.5
*suffix*, *prefix*, and *dir* may now be supplied in bytes in order to
obtain a bytes return value. Prior to this, only str was allowed.
*suffix* and *prefix* now accept and default to ``None`` to cause
an appropriate default value to be used.
.. function:: mktemp(suffix='', prefix='tmp', dir=None) .. function:: mktemp(suffix='', prefix='tmp', dir=None)
...@@ -239,12 +261,23 @@ the appropriate function arguments, instead. ...@@ -239,12 +261,23 @@ the appropriate function arguments, instead.
:data:`tempdir` is not ``None``, this simply returns its contents; otherwise, :data:`tempdir` is not ``None``, this simply returns its contents; otherwise,
the search described above is performed, and the result returned. the search described above is performed, and the result returned.
.. function:: gettempdirb()
Same as :func:`gettempdir` but the return value is in bytes.
.. versionadded:: 3.5
.. function:: gettempprefix() .. function:: gettempprefix()
Return the filename prefix used to create temporary files. This does not Return the filename prefix used to create temporary files. This does not
contain the directory component. contain the directory component.
.. function:: gettempprefixb()
Same as :func:`gettempprefixb` but the return value is in bytes.
.. versionadded:: 3.5
Examples Examples
-------- --------
......
...@@ -96,7 +96,12 @@ Implementation improvements: ...@@ -96,7 +96,12 @@ Implementation improvements:
Significantly Improved Library Modules: Significantly Improved Library Modules:
* None yet. * You may now pass bytes to the :mod:`tempfile` module's APIs and it will
return the temporary pathname as bytes instead of str. It also accepts
a value of ``None`` on parameters where only str was accepted in the past to
do the right thing based on the types of the other inputs. Two functions,
:func:`gettempdirb` and :func:`gettempprefixb`, have been added to go along
with this. This behavior matches that of the :mod:`os` APIs.
Security improvements: Security improvements:
......
This diff is collapsed.
This diff is collapsed.
...@@ -61,6 +61,9 @@ Core and Builtins ...@@ -61,6 +61,9 @@ Core and Builtins
Library Library
------- -------
- Issue 24230: The tempfile module now accepts bytes for prefix, suffix and dir
parameters and returns bytes in such situations (matching the os module APIs).
- Issue 24244: Prevents termination when an invalid format string is - Issue 24244: Prevents termination when an invalid format string is
encountered on Windows in strftime. encountered on Windows in strftime.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment