-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Avoid calling isatty() for most open() calls #90102
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
Comments
isatty() 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. |
What if change FileIO.isatty() instead? If make it returning False without invoking a system call if the file size is non-zero it will eliminate the need to expose _size. |
I suggest using the file type instead of the size. There's no reason to call isatty() if it's not an S_IFCHR file. This will avoid calling isatty() on regular files that happen to be empty. In Windows, isatty(fd) is based solely on the file type, which is flagged in the fd record when a file descriptor is opened for a native file handle. It's not a system call, but it's also nearly worthless for how isatty() is typically used, since it's true for any S_IFCHR file (e.g. con, nul, com1). |
Only invoke the isatty() system call for character devices and cache the result.
One test fails with #112495 if the stdin is not a terminal. For example:
This is because the result of #29870 uses the cached value only in |
PR gh-112495 was closed. @serhiy-storchaka wrote:
Should we close the issue? |
I think it's okay to rely on the |
Co-authored-by: Victor Stinner <vstinner@python.org>
Spotted by @ngnpope. `isatty` returns False to indicate the file is not a TTY. The C implementation of _io does that (`Py_RETURN_FALSE`) but I got the bool backwards in the _pyio implementaiton.
Optimization implemented by change cc9b9be. |
Co-authored-by: Victor Stinner <vstinner@python.org>
Spotted by @ngnpope. `isatty` returns False to indicate the file is not a TTY. The C implementation of _io does that (`Py_RETURN_FALSE`) but I got the bool backwards in the _pyio implementaiton.
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
The text was updated successfully, but these errors were encountered: