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

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

wants to merge 2 commits into from

Conversation

alphavector
Copy link
Contributor

This is a fix for old problems (gh-51574, gh-64466) where tempfile.mkdtemp() would not return an absolute path if a relative directory was specified.

This doesn't solve any global problem, but it does make the result consistent
Judging by grep.app this case occurred in a small number of projects and shouldn't break everything after the upgrade

As @serhiy-storchaka mentioned #64466 (comment)
the temporary directory will not be deleted if you rename the parent directory

Python 3.8.12 (default, Nov 23 2021, 17:00:15)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, tempfile
>>> os.mkdir('parent')
>>> os.chdir('parent')
>>> t = tempfile.TemporaryDirectory(dir=".")
>>> os.rename('../parent', '../parent2')
>>> t.cleanup()
>>> os.path.exists("../parent2/" + t.name.split("/")[-1])
False
Python 3.11.0a7+ (heads/main-dirty:e61330b44f, May  4 2022, 23:41:57) [Clang 13.1.6 (clang-1316.0.21.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, tempfile
>>> os.mkdir('parent')
>>> os.chdir('parent')
>>> t = tempfile.TemporaryDirectory(dir=".")
>>> os.rename('../parent', '../parent2')
>>> t.cleanup()
>>> os.path.exists("../parent2/" + t.name.split("/")[-1])
True

But it seems normal - now the behavior of TemporaryDirectory and NamedTemporaryFile is the same, in the sense that both classes do not delete the temporary directory/file if it no longer exists in the old path

Python 3.8.12 (default, Nov 23 2021, 17:00:15)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
>>> import os, tempfile
>>> os.mkdir('parent')
>>> os.chdir('parent')
>>> t = tempfile.TemporaryDirectory(dir=".")
>>> os.rename('../parent', '../parent2')
>>> t.__exit__(None, None, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/marat/.pyenv/versions/3.8.12/lib/python3.8/tempfile.py", line 492, in __exit__
    self.close()
  File "/Users/marat/.pyenv/versions/3.8.12/lib/python3.8/tempfile.py", line 499, in close
    self._closer.close()
  File "/Users/marat/.pyenv/versions/3.8.12/lib/python3.8/tempfile.py", line 436, in close
    unlink(self.name)
FileNotFoundError: [Errno 2] No such file or directory: '/private/tmp/parent/tmp4ztw557q'

(It might be worth adding the same error for TemporaryDirectory, that the directory does not exist)

@ghost
Copy link

ghost commented May 4, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-bot
Copy link

Every change to Python requires a NEWS entry.

Please, add it using the blurb_it Web app or the blurb command-line tool.

@alphavector
Copy link
Contributor Author

Hmm, a bad test that does not take into account the peculiarities of the location of Windows directories. How can it be improved?

@@ -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.

@alphavector
Copy link
Contributor Author

Closed in favor of #94612

@alphavector alphavector closed this Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants