diff options
author | Eric L <ericzolf@users.noreply.github.com> | 2021-03-03 20:36:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-03 20:36:22 (GMT) |
commit | 9c7927400cd8f1d283bf7915b6b33fea81b8655d (patch) | |
tree | a1cfe04df8fd6569f55d8d352b9c5137563c441d /Doc/library/tempfile.rst | |
parent | 62e3b6370cf9aa990485d9c7c3ea3f6f150daa47 (diff) | |
download | cpython-9c7927400cd8f1d283bf7915b6b33fea81b8655d.zip cpython-9c7927400cd8f1d283bf7915b6b33fea81b8655d.tar.gz cpython-9c7927400cd8f1d283bf7915b6b33fea81b8655d.tar.bz2 |
bpo-40701: tempfile mixes str and bytes in an inconsistent manner (GH-20442)
The case of tempfile.tempdir variable being bytes is now handled consistently.
The getters return the right type and no more error of mixing str and bytes unless explicitly caused by the user.
Adds a regression test.
Expands the documentation to clarify the behavior.
Co-authored-by: Eric L <ewl+git@lavar.de>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Doc/library/tempfile.rst')
-rw-r--r-- | Doc/library/tempfile.rst | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index f9421da..2b8a35e 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -248,6 +248,11 @@ The module defines the following user-callable items: The result of this search is cached, see the description of :data:`tempdir` below. + .. versionchanged:: 3.10 + + Always returns a str. Previously it would return any :data:`tempdir` + value regardless of type so long as it was not ``None``. + .. function:: gettempdirb() Same as :func:`gettempdir` but the return value is in bytes. @@ -269,18 +274,30 @@ The module uses a global variable to store the name of the directory used for temporary files returned by :func:`gettempdir`. It can be set directly to override the selection process, but this is discouraged. All functions in this module take a *dir* argument which can be used -to specify the directory and this is the recommended approach. +to specify the directory. This is the recommended approach that does +not surprise other unsuspecting code by changing global API behavior. .. data:: tempdir When set to a value other than ``None``, this variable defines the default value for the *dir* argument to the functions defined in this - module. + module, including its type, bytes or str. It cannot be a + :term:`path-like object`. If ``tempdir`` is ``None`` (the default) at any call to any of the above functions except :func:`gettempprefix` it is initialized following the algorithm described in :func:`gettempdir`. + .. note:: + + Beware that if you set ``tempdir`` to a bytes value, there is a + nasty side effect: The global default return type of + :func:`mkstemp` and :func:`mkdtemp` changes to bytes when no + explicit ``prefix``, ``suffix``, or ``dir`` arguments of type + str are supplied. Please do not write code expecting or + depending on this. This awkward behavior is maintained for + compatibility with the historcal implementation. + .. _tempfile-examples: Examples |