summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Sloniker <sam@kj7rrv.com>2023-04-25 16:05:59 (GMT)
committerGitHub <noreply@github.com>2023-04-25 16:05:59 (GMT)
commit32bea69b89e32a8c673db3315e1ea3de48ea7702 (patch)
tree6dfc98400b736b23c0f809794e017589fce5c5f9
parentc8c3956d905e019101038b018129a4c90c9c9b8f (diff)
downloadcpython-32bea69b89e32a8c673db3315e1ea3de48ea7702.zip
cpython-32bea69b89e32a8c673db3315e1ea3de48ea7702.tar.gz
cpython-32bea69b89e32a8c673db3315e1ea3de48ea7702.tar.bz2
gh-51574: Make tempfile.mkdtemp() always return absolute paths (#94612)
Co-authored-by: Éric <merwok@netwok.org> Co-authored-by: AlexWaygood <alex.waygood@gmail.com>
-rw-r--r--Doc/library/tempfile.rst3
-rw-r--r--Doc/whatsnew/3.12.rst6
-rw-r--r--Lib/tempfile.py2
-rw-r--r--Lib/test/test_tempfile.py9
-rw-r--r--Misc/NEWS.d/next/Library/2022-07-06-11-10-37.gh-issue-51574.sveUeD.rst2
5 files changed, 19 insertions, 3 deletions
diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst
index 61358eb..fd4c294 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 373e31b..9a32f98 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 4732eb0..2b4f431 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 11a43ac..db08fb1 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 0000000..50a3d6a
--- /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.