diff --git a/Lib/tempfile.py b/Lib/tempfile.py index eb870c998c251a..7a2bdd7ec94234 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 07a54028ec6979..0359242f340027 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1467,6 +1467,25 @@ def do_create2(self, path, recurse=1, dirs=1, files=1): with open(os.path.join(path, "test%d.txt" % i), "wb") as f: f.write(b"Hello world!") + def test_absolute_path(self): + """Test that the absolute path to the temporary directory is returned""" + with tempfile.TemporaryDirectory() as working_dir, contextlib.chdir(working_dir): + with tempfile.TemporaryDirectory(dir=".") as temp_dir: + self.assertEqual(os.path.dirname(temp_dir), os.path.realpath(working_dir)) + + def test_mkdtemp_rename_dir_failure(self): + """Test that after renaming the parent directory, the temporary directory is not deleted + See: https://github.com/python/cpython/issues/64466#issuecomment-1093642375 + """ + with tempfile.TemporaryDirectory() as working_dir, contextlib.chdir(working_dir): + parent_path = pathlib.Path(working_dir) / "parent" + parent_path.mkdir() + os.chdir(parent_path) + with self.do_create(dir=".") as temp_dir: + parent_path.rename("../parent2") + self.assertFalse(os.path.exists(temp_dir)) + self.assertTrue((parent_path.parent / "parent2").exists()) + def test_mkdtemp_failure(self): # Check no additional exception if mkdtemp fails # Previously would raise AttributeError instead diff --git a/Misc/NEWS.d/next/Library/2022-05-04-23-49-20.gh-issue-51574.8zvexM.rst b/Misc/NEWS.d/next/Library/2022-05-04-23-49-20.gh-issue-51574.8zvexM.rst new file mode 100644 index 00000000000000..a93eac27972289 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-04-23-49-20.gh-issue-51574.8zvexM.rst @@ -0,0 +1 @@ +The absolute path is returned, just like [mkdtemp()](https://docs.python.org/3/library/tempfile.html#tempfile.mkdtemp)