-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-90102: Remove isatty call during regular open #124922
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
Changes from 3 commits
a1a1cb7
b07dac7
346517e
cba68bc
110c490
de661d1
1f96a9f
1b12723
1783237
882867c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -238,7 +238,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None, | |||
result = raw | ||||
try: | ||||
line_buffering = False | ||||
if buffering == 1 or buffering < 0 and raw.isatty(): | ||||
if buffering == 1 or buffering < 0 and raw._isatty_openonly(): | ||||
buffering = -1 | ||||
line_buffering = True | ||||
if buffering < 0: | ||||
|
@@ -1794,6 +1794,19 @@ def isatty(self): | |||
self._checkClosed() | ||||
return os.isatty(self._fd) | ||||
|
||||
def _isatty_openonly(self): | ||||
"""Checks whether the file is a TTY using an open-only optimization. | ||||
Normally isatty always makes a system call. In the case of open() there | ||||
is a _inside the same python call_ stat result which we can use to | ||||
cmaloney marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
skip that system call for non-character files. Outside of that context | ||||
this is subject to TOCTOU issues (the FD has been returned to user code | ||||
and arbitrary syscalls could have happened). | ||||
""" | ||||
if (self._stat_atopen is not None and | ||||
not stat.S_ISCHR(self._stat_atopen.st_mode)): | ||||
cmaloney marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
return True | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
return os.isatty(self._fd) | ||||
|
||||
@property | ||||
def closefd(self): | ||||
"""True if the file descriptor will be closed by close().""" | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Skip the ``isatty`` system call during open() when the file is known to not | ||
be a character device. This provides a slight performance improvement when | ||
reading whole files. |
Uh oh!
There was an error while loading. Please reload this page.