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

gh-114545: [Lib/tempfile.py] mkdtemp - canonicalise directory name #114691

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

SamuelMarks
Copy link
Contributor

@SamuelMarks SamuelMarks commented Jan 28, 2024

On macOS—and probably other operating systems—the temporary directory /tmp is not canonical. This causes weird issues in ones codebase, e.g., /var/folders != /private/var/folders. This additional function call solves the problem.

PS: I'm not sure if the os.path.abspath call can be removed…

PPS: See #114545 for discussion; also see nodejs/node/issues/11422 for when this was discussed for Node.js.

…t/test_tempfile.py] Add test for `mkdtemp` creating a `realpath`
@aisk
Copy link
Contributor

aisk commented Jan 29, 2024

I found a strange thing. After this change, the test_tempfile failed on my machine:

./python.exe -m test test_tempfile -m test_basic
Using random seed: 967898022
Raised RLIMIT_NOFILE: 256 -> 1024
0:00:00 load avg: 2.25 Run 1 test sequentially
0:00:00 load avg: 2.25 [1/1] test_tempfile
test test_tempfile failed -- Traceback (most recent call last):
  File "/Users/xxxxx/Codes/cpython/Lib/test/test_tempfile.py", line 747, in test_basic
    os.rmdir(self.do_create())
             ~~~~~~~~~~~~~~^^
  File "/Users/xxxxx/Codes/cpython/Lib/test/test_tempfile.py", line 739, in do_create
    self.nameCheck(name, dir, pre, suf)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxxx/Codes/cpython/Lib/test/test_tempfile.py", line 122, in nameCheck
    self.assertEqual(os.path.abspath(ndir), os.path.abspath(dir),
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                     "file %r not in directory %r" % (name, dir))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: '/private/var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T' != '/var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T'
- /private/var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T
? --------
+ /var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T
 : file '/private/var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T/eu7jqves' not in directory '/var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T'

test_tempfile failed (1 failure)

== Tests result: FAILURE ==

1 test failed:
    test_tempfile

Total duration: 338 ms
Total tests: run=7 (filtered) failures=1
Total test files: run=1/1 (filtered) failed=1
Result: FAILURE

I see that the nameCheck

def nameCheck(self, name, dir, pre, suf):
checks if the result of tempfile.mkdtemp is in the temp directory, which is determined by tempfile.gettempdir.

On my machine, the result of tempfile.gettempdir is not canonicalized (without the /private prefix), so the check fails. But I don't know why it passed on CI.

I'm using a Intel mac with OS version 12.7.3.

@aisk
Copy link
Contributor

aisk commented Jan 30, 2024

The test_basic passed after latest change, but there are two tests still failed:

======================================================================
FAIL: test_same_thing (test.test_tempfile.TestGetTempDir.test_same_thing)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/xxxxx/Codes/cpython/Lib/test/test_tempfile.py", line 606, in test_same_thing
    self.assertTrue(a is b)
    ~~~~~~~~~~~~~~~^^^^^^^^
AssertionError: False is not true

======================================================================
FAIL: test_basic_with_bytes_names (test.test_tempfile.TestMkdtemp.test_basic_with_bytes_names)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/xxxxx/Codes/cpython/Lib/test/test_tempfile.py", line 756, in test_basic_with_bytes_names
    os.rmdir(self.do_create(dir=d))
             ~~~~~~~~~~~~~~^^^^^^^
  File "/Users/xxxxx/Codes/cpython/Lib/test/test_tempfile.py", line 739, in do_create
    self.nameCheck(name, dir, pre, suf)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxxx/Codes/cpython/Lib/test/test_tempfile.py", line 122, in nameCheck
    self.assertEqual(os.path.abspath(ndir), os.path.abspath(dir),
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                     "file %r not in directory %r" % (name, dir))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: b'/private/var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T' != b'/var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T' : file b'/private/var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T/_rzdrbad' not in directory b'/var/folders/f3/y148281n5m73g6npw8l12lnw0000gn/T'

----------------------------------------------------------------------

The macOS CI failed with the same error as the previous run, despite appearing unrelated to the change. However, I suspect that a connection might not be easily detectable.

@erlend-aasland erlend-aasland marked this pull request as draft January 30, 2024 21:01
@erlend-aasland
Copy link
Contributor

Marking this as draft, as it is work in progress.

@SamuelMarks
Copy link
Contributor Author

Oh interesting; this just hit #93852

…un_realpath` around ; [Lib/multiprocessing/util.py] Resolve python#93852
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants