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

AlexWaygood webhook-mailer at python.org
Tue Apr 25 12:06:07 EDT 2023


https://github.com/python/cpython/commit/32bea69b89e32a8c673db3315e1ea3de48ea7702
commit: 32bea69b89e32a8c673db3315e1ea3de48ea7702
branch: main
author: Samuel Sloniker <sam at kj7rrv.com>
committer: AlexWaygood <Alex.Waygood at Gmail.com>
date: 2023-04-25T16:05:59Z
summary:

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

Co-authored-by: Éric <merwok at netwok.org>
Co-authored-by: AlexWaygood <alex.waygood at gmail.com>

files:
A Misc/NEWS.d/next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst
M Doc/library/tempfile.rst
M Doc/whatsnew/3.12.rst
M Lib/tempfile.py
M Lib/test/test_tempfile.py

diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst
index 61358eb76925..fd4c294613fd 100644
--- a/Doc/library/tempfile.rst
+++ b/Doc/library/tempfile.rst
@@ -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()
 
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index 373e31b37cd9..9a32f985c6a0 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -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:
 
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index 4732eb0efe1f..2b4f43132471 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -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")
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index 11a43aca17e8..db08fb1c7f2a 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -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()."""
diff --git a/Misc/NEWS.d/next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst b/Misc/NEWS.d/next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst
new file mode 100644
index 000000000000..50a3d6a46291
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst
@@ -0,0 +1,2 @@
+Make :func:`tempfile.mkdtemp` return absolute paths when its *dir*
+parameter is relative.



More information about the Python-checkins mailing list