.. 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()
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:
continue
else:
raise
- return file
+ return _os.path.abspath(file)
raise FileExistsError(_errno.EEXIST,
"No usable temporary directory name found")
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()."""
--- /dev/null
+Make :func:`tempfile.mkdtemp` return absolute paths when its *dir*
+parameter is relative.