Skip to content

gh-136156: Remove tempfile test_link_tmpfile() #136534

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 1 commit into from
Jul 11, 2025

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jul 11, 2025

It's not always possible to guarantee that the file was opened with O_TMPFILE even if tempfile._O_TMPFILE_WORKS is true.

It's not always possible to guarantee that the file was opened with
O_TMPFILE even if tempfile._O_TMPFILE_WORKS is true.
@vstinner
Copy link
Member Author

The test fails on Arch Linux: #136281 (comment)

cc @serhiy-storchaka

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.

Is removing the test the only way? Can we check _O_TMPFILE_WORKS after calling TemporaryFile()?

@vstinner
Copy link
Member Author

vstinner commented Jul 11, 2025

Is removing the test the only way? Can we check _O_TMPFILE_WORKS after calling TemporaryFile()?

The test already checks _O_TMPFILE_WORKS after TemporaryFile():

            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")

The problem is that TemporaryFile() doesn't set _O_TMPFILE_WORKS to False on OSError (other than IsADirectoryError), it only ignores the error silently.

@vstinner vstinner merged commit cbf007b into python:main Jul 11, 2025
45 checks passed
@vstinner vstinner deleted the remove_test_link_tmpfile branch July 11, 2025 14:45
@vstinner
Copy link
Member Author

I removed the test. It can be added back if someone finds a clever trick to check if O_TMPFILE was used or not. I don't know how to query open flags from a file descriptor.

@serhiy-storchaka
Copy link
Member

What was that error? Why _O_TMPFILE_WORKS was not set here?

@vstinner
Copy link
Member Author

What was that error? Why _O_TMPFILE_WORKS was not set here?

I don't have access to the buildbot, so I don't know. But I suspect that we hit this code path:

            except OSError:
                # The filesystem of the directory does not support O_TMPFILE.
                # For example, OSError(95, 'Operation not supported').
                #
                # On Linux kernel older than 3.11, trying to open a regular
                # file (or a symbolic link to a regular file) with O_TMPFILE
                # fails with NotADirectoryError, because O_TMPFILE is read as
                # O_DIRECTORY.
                pass
            # Fallback to _mkstemp_inner().

Later, linkat() fails with FileNotFoundError: #136281 (comment)

@vstinner
Copy link
Member Author

Example trying to call linkat() whereas O_TMPFILE is not used:

import os, tempfile
dir = '.'
flags = os.O_RDWR | os.O_CREAT | os.O_EXCL
fd, name = tempfile._mkstemp_inner(dir, "tmp", "", flags, str)
os.unlink(name)
try:
    os.link(f'/proc/self/fd/{fd}', 'link', follow_symlinks=True)
finally:
    os.close(fd)

It fails with:

Traceback (most recent call last):
  File "/home/vstinner/python/main/x.py", line 7, in <module>
    os.link(f'/proc/self/fd/{fd}', 'link', follow_symlinks=True)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/proc/self/fd/3' -> 'link'

@serhiy-storchaka
Copy link
Member

There may be a bug in TemporaryFile.

Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
It's not always possible to guarantee that the file was opened with
O_TMPFILE even if tempfile._O_TMPFILE_WORKS is true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 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