Skip to content

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

Open
@SamuelMarks

Description

@SamuelMarks

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions