Skip to content
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

[cpython/Lib/tempfile.py] mkdtemp - canonicalise directory name #114545

Open
SamuelMarks opened this issue Jan 25, 2024 · 2 comments
Open

[cpython/Lib/tempfile.py] mkdtemp - canonicalise directory name #114545

SamuelMarks opened this issue Jan 25, 2024 · 2 comments
Labels
type-feature A feature request or enhancement

Comments

@SamuelMarks
Copy link
Contributor

SamuelMarks commented Jan 25, 2024

Feature or enhancement

Proposal:

I just solved an annoying bug where my temporary directory worked on Windows and Linux but not macOS; because mkdtemp doesn't canonicalise its output; causing "/private/" to be prefixed to some of my paths but not the first path out of with TemporaryDirectory() as directory.

I found where the one line solution can be applied:
https://github.com/python/cpython/blob/3.12/Lib/tempfile.py#L395

diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index cbfc172a789b25..2ded1af58b73dd 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -392,7 +392,7 @@ def mkdtemp(suffix=None, prefix=None, dir=None):
                 continue
             else:
                 raise
-        return _os.path.abspath(file)
+        return _os.path.realpath(_os.path.abspath(file))
 
     raise FileExistsError(_errno.EEXIST,
                           "No usable temporary directory name found")

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

This was discussed on the Node.js project nodejs/node#11422

Linked PRs

@SamuelMarks SamuelMarks added the type-feature A feature request or enhancement label Jan 25, 2024
@Ravencentric
Copy link

I encountered this a while back

# MacOS
>>> from tempfile import TemporaryDirectory
>>> from pathlib import Path
>>> TemporaryDirectory().name
'/var/folders/kj/5xf97h1x0t741q334hgxxf_r0000gn/T/tmpcqfpeiwq'
>>> Path(TemporaryDirectory().name).resolve()
PosixPath('/private/var/folders/kj/5xf97h1x0t741q334hgxxf_r0000gn/T/tmp0sfpzcnh')

Was also brought up in this discussion https://discuss.python.org/t/tempfile-temporarydirectory-name-should-return-pathlib-path-instead-of-str/40900/10

@aisk
Copy link
Contributor

aisk commented Jan 26, 2024

Hi, I think this (with additional tests) deserves a PR, and PRs are welcome in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants