Skip to content

Commit 32bea69

Browse files
kj7rrvmerwokAlexWaygood
authored
gh-51574: Make tempfile.mkdtemp() always return absolute paths (#94612)
Co-authored-by: Éric <[email protected]> Co-authored-by: AlexWaygood <[email protected]>
1 parent c8c3956 commit 32bea69

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

Doc/library/tempfile.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ The module defines the following user-callable items:
292292
.. versionchanged:: 3.6
293293
The *dir* parameter now accepts a :term:`path-like object`.
294294

295+
.. versionchanged:: 3.12
296+
:func:`mkdtemp` now always returns an absolute path, even if *dir* is relative.
297+
295298

296299
.. function:: gettempdir()
297300

Doc/whatsnew/3.12.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,10 @@ uuid
457457
tempfile
458458
--------
459459

460-
The :class:`tempfile.NamedTemporaryFile` function has a new optional parameter
461-
*delete_on_close* (Contributed by Evgeny Zorin in :gh:`58451`.)
460+
* The :class:`tempfile.NamedTemporaryFile` function has a new optional parameter
461+
*delete_on_close* (Contributed by Evgeny Zorin in :gh:`58451`.)
462+
* :func:`tempfile.mkdtemp` now always returns an absolute path, even if the
463+
argument provided to the *dir* parameter is a relative path.
462464

463465
.. _whatsnew-typing-py312:
464466

Lib/tempfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def mkdtemp(suffix=None, prefix=None, dir=None):
376376
continue
377377
else:
378378
raise
379-
return file
379+
return _os.path.abspath(file)
380380

381381
raise FileExistsError(_errno.EEXIST,
382382
"No usable temporary directory name found")

Lib/test/test_tempfile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,15 @@ def test_for_tempdir_is_bytes_issue40701_api_warts(self):
850850
finally:
851851
tempfile.tempdir = orig_tempdir
852852

853+
def test_path_is_absolute(self):
854+
# Test that the path returned by mkdtemp with a relative `dir`
855+
# argument is absolute
856+
try:
857+
path = tempfile.mkdtemp(dir=".")
858+
self.assertTrue(os.path.isabs(path))
859+
finally:
860+
os.rmdir(path)
861+
853862

854863
class TestMktemp(BaseTestCase):
855864
"""Test mktemp()."""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make :func:`tempfile.mkdtemp` return absolute paths when its *dir*
2+
parameter is relative.

0 commit comments

Comments
 (0)