From b731bfbd9dc15f315c7d5d5dcb1d1b5d7aa21ff9 Mon Sep 17 00:00:00 2001 From: Marat Idrisov Date: Thu, 5 May 2022 01:19:29 +0300 Subject: [PATCH 1/2] gh-51574: return the absolute path of the temporary directory --- Lib/tempfile.py | 2 +- Lib/test/test_tempfile.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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 From 8ad8a9d5af88e1daaa753d125241020003bf0ced Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 4 May 2022 23:49:21 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-05-04-23-49-20.gh-issue-51574.8zvexM.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-05-04-23-49-20.gh-issue-51574.8zvexM.rst 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)