From 2fc9fab789e6d9d8aeb9fbada0be77f8b1540cd5 Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Wed, 6 Jul 2022 11:10:40 -0700 Subject: [PATCH 1/8] Make tempfile.mkdtemp() always return absolute paths Closes #51574 Issue: gh-51574 --- Lib/tempfile.py | 2 +- .../next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 480c17232e9f09..88f7bef5f54e22 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -362,7 +362,7 @@ def mkdtemp(suffix=None, prefix=None, dir=None): for seq in range(TMP_MAX): name = next(names) - file = _os.path.join(dir, prefix + name + suffix) + file = _os.path.abspath(_os.path.join(dir, prefix + name + suffix)) _sys.audit("tempfile.mkdtemp", file) try: _os.mkdir(file, 0o700) 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 00000000000000..c1ee164b6bbadf --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst @@ -0,0 +1 @@ +Make tempfile.mkdtemp() return absolute paths when dir is relative From 23656934dc1392f61bd68de04251923d1cf6eef8 Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Wed, 6 Jul 2022 15:11:13 -0700 Subject: [PATCH 2/8] Document change --- Doc/library/tempfile.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index b7e604c1b70acb..2a02e63972a0ad 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -239,6 +239,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() From a9098beeaf1bbe415cf7ef4df11b5cb248f0569d Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Wed, 6 Jul 2022 16:13:06 -0700 Subject: [PATCH 3/8] Add test for tempfile.mkdtemp output being absolute --- Lib/test/test_tempfile.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index ccf7ea072de276..d9921469467422 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -848,6 +848,12 @@ def test_for_tempdir_is_bytes_issue40701_api_warts(self): finally: tempfile.tempdir = orig_tempdir + def test_absolute_path(self): + """Test that the path returned is absolute""" + path = tempfile.mkdtemp(dir=".") + self.assertEqual(os.path.isabs(path), True) + os.rmdir(path) + class TestMktemp(BaseTestCase): """Test mktemp().""" From a65f15236073e7211b8e795fb4e01548392fd421 Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Thu, 7 Jul 2022 07:43:54 -0700 Subject: [PATCH 4/8] Update Doc/library/tempfile.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric --- Doc/library/tempfile.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index 2a02e63972a0ad..0b0e305b676e91 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -239,8 +239,8 @@ 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. + .. versionchanged:: 3.12 + :func:`mkdtemp` now always returns an absolute path, even if *dir* is relative. .. function:: gettempdir() From ff507d9002bb0689ea2e3cfbc80976a49b7a1f96 Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Thu, 7 Jul 2022 07:47:31 -0700 Subject: [PATCH 5/8] Fix test --- Lib/test/test_tempfile.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index d9921469467422..a51938bccd6486 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -848,11 +848,14 @@ def test_for_tempdir_is_bytes_issue40701_api_warts(self): finally: tempfile.tempdir = orig_tempdir - def test_absolute_path(self): - """Test that the path returned is absolute""" - path = tempfile.mkdtemp(dir=".") - self.assertEqual(os.path.isabs(path), True) - os.rmdir(path) + 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): From 8be2bf28fe00de2c826084ac72aaa4517d7e62b7 Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Mon, 24 Apr 2023 12:58:32 -0700 Subject: [PATCH 6/8] Update Misc/NEWS.d/next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Suggested by @merwok Co-authored-by: Éric --- .../next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index c1ee164b6bbadf..50a3d6a4629182 100644 --- 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 @@ -1 +1,2 @@ -Make tempfile.mkdtemp() return absolute paths when dir is relative +Make :func:`tempfile.mkdtemp` return absolute paths when its *dir* +parameter is relative. From 9085b35801931d8f1bb17f734ce087b788df021a Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 25 Apr 2023 09:32:35 -0600 Subject: [PATCH 7/8] Minor perf optimisation: only call abspath when necessary --- Lib/tempfile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/tempfile.py b/Lib/tempfile.py index fe72fa290708fe..2b4f4313247128 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -362,7 +362,7 @@ def mkdtemp(suffix=None, prefix=None, dir=None): for seq in range(TMP_MAX): name = next(names) - file = _os.path.abspath(_os.path.join(dir, prefix + name + suffix)) + file = _os.path.join(dir, prefix + name + suffix) _sys.audit("tempfile.mkdtemp", file) try: _os.mkdir(file, 0o700) @@ -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") From 8e8967a236ca4db864d5595d9d1144fcc6635897 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 25 Apr 2023 09:39:26 -0600 Subject: [PATCH 8/8] Add note to whatsnew --- Doc/whatsnew/3.12.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 373e31b37cd9dc..9a32f985c6a05a 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: