Skip to content

gh-136156: Allow using linkat() with TemporaryFile #136281

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

Merged
merged 2 commits into from
Jul 8, 2025

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jul 4, 2025

tempfile.TemporaryFile() no longer uses os.O_EXCL with os.O_TMPFILE, so it's possible to use linkat() on the file descriptor.

tempfile.TemporaryFile() no longer uses os.O_EXCL with os.O_TMPFILE,
so it's possible to use linkat() on the file descriptor.
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

@serhiy-storchaka
Copy link
Member

Could you add a test using the proc filesystem? I tried to implement AT_EMPTY_PATH support (see #89341), but it only works for root.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

@@ -1594,6 +1594,29 @@ def test_unexpected_error(self):
mock_close.assert_called()
self.assertEqual(os.listdir(dir), [])

@unittest.skipUnless(tempfile._O_TMPFILE_WORKS, 'need os.O_TMPFILE')
@unittest.skipUnless(os.path.exists('/proc/self/fd'),
Copy link
Member

Choose a reason for hiding this comment

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

Maybe even isdir()?

@serhiy-storchaka
Copy link
Member

!buildbot BSD

@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @serhiy-storchaka for commit c912963 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F136281%2Fmerge

The command will test the builders whose names match following regular expression: BSD

The builders matched are:

  • AMD64 FreeBSD PR
  • AMD64 FreeBSD14 PR
  • AMD64 FreeBSD Refleaks PR
  • AMD64 FreeBSD15 PR

@vstinner
Copy link
Member Author

vstinner commented Jul 8, 2025

!buildbot BSD

I tested manually: the test is skipped since os.O_TMPFILE is missing.

@vstinner vstinner merged commit 6c81e8c into python:main Jul 8, 2025
43 of 44 checks passed
@vstinner vstinner deleted the tempfile branch July 8, 2025 16:39
@vstinner
Copy link
Member Author

vstinner commented Jul 8, 2025

Merged, thanks for review @serhiy-storchaka.

@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot aarch64 Android 3.x (tier-3) has failed when building commit 6c81e8c.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1594/builds/2820) and take a look at the build logs.
  4. Check if the failure is related to this commit (6c81e8c) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1594/builds/2820

Failed tests:

  • test_tempfile

Failed subtests:

  • test_link_tmpfile - test.test_tempfile.TestTemporaryFile.test_link_tmpfile

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.15/test/test_tempfile.py", line 1614, in test_link_tmpfile
    os.link(f'/proc/self/fd/{fd}',
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
            filename,
            ^^^^^^^^^
            follow_symlinks=True)
            ^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/proc/self/fd/82' -> '/data/user/0/org.python.testbed/cache/tmpd0x9o66s/link'


Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.15/test/test_tempfile.py", line 1614, in test_link_tmpfile
    os.link(f'/proc/self/fd/{fd}',
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
            filename,
            ^^^^^^^^^
            follow_symlinks=True)
            ^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/proc/self/fd/82' -> '/data/user/0/org.python.testbed/cache/tmpmjq3eb8x/link'

@vstinner
Copy link
Member Author

vstinner commented Jul 8, 2025

Oh, the test fails with PermissionError on Android:

ERROR: test_link_tmpfile (test.test_tempfile.TestTemporaryFile.test_link_tmpfile)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.15/test/test_tempfile.py", line 1614, in test_link_tmpfile
    os.link(f'/proc/self/fd/{fd}',
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
            filename,
            ^^^^^^^^^
            follow_symlinks=True)
            ^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/proc/self/fd/82' -> '/data/user/0/org.python.testbed/cache/tmpmjq3eb8x/link'

@vstinner
Copy link
Member Author

vstinner commented Jul 8, 2025

I wrote #136430 to fix the test on Android.

@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Android 3.x (tier-3) has failed when building commit 6c81e8c.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1591/builds/2548) and take a look at the build logs.
  4. Check if the failure is related to this commit (6c81e8c) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1591/builds/2548

Failed tests:

  • test_tempfile

Failed subtests:

  • test_link_tmpfile - test.test_tempfile.TestTemporaryFile.test_link_tmpfile

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.15/test/test_tempfile.py", line 1614, in test_link_tmpfile
    os.link(f'/proc/self/fd/{fd}',
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
            filename,
            ^^^^^^^^^
            follow_symlinks=True)
            ^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/proc/self/fd/83' -> '/data/user/0/org.python.testbed/cache/tmprqz0o6wv/link'


Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.15/test/test_tempfile.py", line 1614, in test_link_tmpfile
    os.link(f'/proc/self/fd/{fd}',
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
            filename,
            ^^^^^^^^^
            follow_symlinks=True)
            ^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/proc/self/fd/83' -> '/data/user/0/org.python.testbed/cache/tmpocwzcriy/link'

@vstinner
Copy link
Member Author

Oh. The test fails on Arch Linux buildbots: https://buildbot.python.org/#/builders/582/builds/6857

ERROR: test_link_tmpfile (test.test_tempfile.TestTemporaryFile.test_link_tmpfile)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/buildbot/buildarea/3.x.pablogsal-arch-x86_64.asan/build/Lib/test/test_tempfile.py", line 1614, in test_link_tmpfile
    os.link(f'/proc/self/fd/{fd}',
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
            filename,
            ^^^^^^^^^
            follow_symlinks=True)
            ^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/proc/self/fd/3' -> '/tmp/test_python_712__99i/tmpgkq51fdc/link'

AndPuQing pushed a commit to AndPuQing/cpython that referenced this pull request Jul 11, 2025
tempfile.TemporaryFile() no longer uses os.O_EXCL with os.O_TMPFILE,
so it's possible to use linkat() on the file descriptor.
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
tempfile.TemporaryFile() no longer uses os.O_EXCL with os.O_TMPFILE,
so it's possible to use linkat() on the file descriptor.
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy