Issue 45944: Avoid calling isatty() for most open() calls - Python tracker

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, collinanderson, eryksun, pitrou, serhiy.storchaka, stutzbach
Priority: normal Keywords: patch

Created on 2021-12-01 05:08 by collinanderson, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 29870 open collinanderson, 2021-12-01 05:11
Messages (3)
msg407427 - (view) Author: Collin Anderson (collinanderson) * Date: 2021-12-01 05:12
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.
msg407434 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-12-01 09:22
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.
msg407444 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-12-01 11:53
> 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).