diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index 61358eb7692..fd4c294613f 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 373e31b37cd..9a32f985c6a 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 4732eb0efe1..2b4f4313247 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 11a43aca17e..db08fb1c7f2 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -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 index 00000000000..50a3d6a4629 --- /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.