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
Title: Avoid calling isatty() for most open() calls
Type: performance Stage: patch review
Components: IO Versions: Python 3.11
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).
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90102
2021-12-01 11:53:13eryksunsetnosy: + eryksun
messages: + msg407444
2021-12-01 09:24:45serhiy.storchakasetnosy: + pitrou, benjamin.peterson, stutzbach

versions: - Python 3.6, Python 3.7, Python 3.8, Python 3.9, Python 3.10
2021-12-01 09:22:53serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg407434
2021-12-01 05:12:39collinandersonsetmessages: + msg407427
2021-12-01 05:11:58collinandersonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28096
2021-12-01 05:08:15collinandersoncreate