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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
fd = None
def opener(*args):
nonlocal fd
flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT
flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT & ~_os.O_EXCL
fd = _os.open(dir, flags2, 0o600)
return fd
try:
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()?

'need /proc/self/fd')
def test_link_tmpfile(self):
dir = tempfile.mkdtemp()
self.addCleanup(os_helper.rmtree, dir)
filename = os.path.join(dir, "link")

with tempfile.TemporaryFile('w', dir=dir) as tmp:
# the flag can become False on Linux <= 3.11
if not tempfile._O_TMPFILE_WORKS:
self.skipTest("O_TMPFILE doesn't work")

tmp.write("hello")
tmp.flush()
fd = tmp.fileno()

os.link(f'/proc/self/fd/{fd}',
filename,
follow_symlinks=True)
with open(filename) as fp:
self.assertEqual(fp.read(), "hello")


# Helper for test_del_on_shutdown
class NulledModules:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`tempfile.TemporaryFile` no longer uses :data:`os.O_EXCL` with
:data:`os.O_TMPFILE`, so it's possible to use ``linkat()`` on the file
descriptor. Patch by Victor Stinner.
Loading
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