Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 25, 2023
3 changes: 3 additions & 0 deletions Doc/library/tempfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ The module defines the following user-callable items:
.. versionchanged:: 3.6
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()

Expand Down
6 changes: 4 additions & 2 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,10 @@ uuid
tempfile
--------

The :class:`tempfile.NamedTemporaryFile` function has a new optional parameter
*delete_on_close* (Contributed by Evgeny Zorin in :gh:`58451`.)
* The :class:`tempfile.NamedTemporaryFile` function has a new optional parameter
*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:

Expand Down
2 changes: 1 addition & 1 deletion Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def mkdtemp(suffix=None, prefix=None, dir=None):
continue
else:
raise
return file
return _os.path.abspath(file)

raise FileExistsError(_errno.EEXIST,
"No usable temporary directory name found")
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,15 @@ def test_for_tempdir_is_bytes_issue40701_api_warts(self):
finally:
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):
"""Test mktemp()."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make :func:`tempfile.mkdtemp` return absolute paths when its *dir*
parameter is relative.