gh-51574: Make tempfile.mkdtemp() always return absolute paths (#94612)

Co-authored-by: Éric <merwok@netwok.org>
Co-authored-by: AlexWaygood <alex.waygood@gmail.com>
This commit is contained in:
Samuel Sloniker 2023-04-25 09:05:59 -07:00 committed by GitHub
parent c8c3956d90
commit 32bea69b89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 3 deletions

View File

@ -292,6 +292,9 @@ The module defines the following user-callable items:
.. versionchanged:: 3.6 .. versionchanged:: 3.6
The *dir* parameter now accepts a :term:`path-like object`. The *dir* parameter now accepts a :term:`path-like object`.
.. versionchanged:: 3.12
:func:`mkdtemp` now always returns an absolute path, even if *dir* is relative.
.. function:: gettempdir() .. function:: gettempdir()

View File

@ -457,8 +457,10 @@ uuid
tempfile tempfile
-------- --------
The :class:`tempfile.NamedTemporaryFile` function has a new optional parameter * The :class:`tempfile.NamedTemporaryFile` function has a new optional parameter
*delete_on_close* (Contributed by Evgeny Zorin in :gh:`58451`.) *delete_on_close* (Contributed by Evgeny Zorin in :gh:`58451`.)
* :func:`tempfile.mkdtemp` now always returns an absolute path, even if the
argument provided to the *dir* parameter is a relative path.
.. _whatsnew-typing-py312: .. _whatsnew-typing-py312:

View File

@ -376,7 +376,7 @@ def mkdtemp(suffix=None, prefix=None, dir=None):
continue continue
else: else:
raise raise
return file return _os.path.abspath(file)
raise FileExistsError(_errno.EEXIST, raise FileExistsError(_errno.EEXIST,
"No usable temporary directory name found") "No usable temporary directory name found")

View File

@ -850,6 +850,15 @@ class TestMkdtemp(TestBadTempdir, BaseTestCase):
finally: finally:
tempfile.tempdir = orig_tempdir tempfile.tempdir = orig_tempdir
def test_path_is_absolute(self):
# Test that the path returned by mkdtemp with a relative `dir`
# argument is absolute
try:
path = tempfile.mkdtemp(dir=".")
self.assertTrue(os.path.isabs(path))
finally:
os.rmdir(path)
class TestMktemp(BaseTestCase): class TestMktemp(BaseTestCase):
"""Test mktemp().""" """Test mktemp()."""

View File

@ -0,0 +1,2 @@
Make :func:`tempfile.mkdtemp` return absolute paths when its *dir*
parameter is relative.