Repository navigation
Avoid calling isatty() for most open() calls #90102
Description
Activity
- added3.7 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usage
on Dec 1, 2021 collinanderson commented
on Dec 1, 2021 collinandersonmannequinMannequinAuthorMore actionsisatty() is a system call on linux. Most open()s are files, and we're already getting the size of the file. If it has a size, then we know it's not a atty, and can avoid calling it.
17 remaining items
One test fails with #112495 if the stdin is not a terminal. For example:
$ ./python -m test -v test_builtin -m test_input_no_stdout_fileno <python ... test test_builtin failed -- Traceback (most recent call last): File "/home/serhiy/py/cpython/Lib/test/test_builtin.py", line 2393, in test_input_no_stdout_fileno self.assertSequenceEqual(lines, expected) ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ AssertionError: Sequences differ: ['stdin.isatty(): False', "captured: 'prompt'"] != ('stdin.isatty(): True', "captured: 'prompt'") First differing element 0: 'stdin.isatty(): False' 'stdin.isatty(): True' - ['stdin.isatty(): False', "captured: 'prompt'"] ? ^ ^^^^ ^ + ('stdin.isatty(): True', "captured: 'prompt'") ? ^ ^^^ ^This is because the result of
isatty()is not constant during the lifetime of the file object. The file descriptor can be reassigned to other file or device, so it should not be cached.#29870 uses the cached value only in
open(), immediately after creating the FileIO object, so it is not affected by this. But it is more complex, and making it more portable will make it even more complex. How much time it saves?PR gh-112495 was closed. @serhiy-storchaka wrote:
It does not work, because the file descriptor can be reassigned from tty to non-tty and vice versa. The result of isatty() can not be cached.
Should we close the issue?
I think it's okay to rely on the
fdnot being modified inside ofopen()which is what #121593 does. It doesn't cache isatty generally (any future call still does aisattylibc call), just says "we can skip this call/check during open if we already have enough information fromstatthat the fd isn't a TTY".Reacted by Victor Stinner- added a commit that references this issue
on Oct 8, 2024 - added a commit that references this issue
on Oct 8, 2024 - added a commit that references this issue
on Oct 8, 2024 Optimization implemented by change cc9b9be.
Reacted by Collin Anderson- added a commit that references this issue
on Oct 8, 2024
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs