]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-51574: Make tempfile.mkdtemp() always return absolute paths (#94612)
authorSamuel Sloniker <sam@kj7rrv.com>
Tue, 25 Apr 2023 16:05:59 +0000 (09:05 -0700)
committerGitHub <noreply@github.com>
Tue, 25 Apr 2023 16:05:59 +0000 (16:05 +0000)
Co-authored-by: Éric <merwok@netwok.org>
Co-authored-by: AlexWaygood <alex.waygood@gmail.com>
Doc/library/tempfile.rst
Doc/whatsnew/3.12.rst
Lib/tempfile.py
Lib/test/test_tempfile.py
Misc/NEWS.d/next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst [new file with mode: 0644]

index 61358eb76925b23db79f44aecf70fecfb21d67b8..fd4c294613fd315f92c7b48f7ea50ce014e41b64 100644 (file)
@@ -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()
 
index 373e31b37cd9dc33c746fa63a9c507925e4244ab..9a32f985c6a05a9ddb65f056d2bd092ad165764a 100644 (file)
@@ -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:
 
index 4732eb0efe1f76d4e12d9f5dd0270576dda429bc..2b4f43132471285e975ac3f2b93fe81539b346a1 100644 (file)
@@ -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")
index 11a43aca17e88a5a01f55599c66dc957e28c2d1b..db08fb1c7f2a42f5bed8f5d4169891d9d7b16ff5 100644 (file)
@@ -850,6 +850,15 @@ class TestMkdtemp(TestBadTempdir, BaseTestCase):
         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 (file)
index 0000000..50a3d6a
--- /dev/null
@@ -0,0 +1,2 @@
+Make :func:`tempfile.mkdtemp` return absolute paths when its *dir*
+parameter is relative.