Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-51574: Return the absolute path in tempfile.mkdtemp() #92317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The absolute path is returned, just like [mkdtemp()](https://docs.python.org/3/library/tempfile.html#tempfile.mkdtemp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean just like mkstemp? Also, if you say at the beginning what this is about (tempfile.mkdtemp()) it will read more clearly.