Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
merged 10 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions 1 Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(_initializing)
STRUCT_FOR_ID(_io)
STRUCT_FOR_ID(_is_text_encoding)
STRUCT_FOR_ID(_isatty_openonly)
STRUCT_FOR_ID(_length_)
STRUCT_FOR_ID(_limbo)
STRUCT_FOR_ID(_lock_unlock_module)
Expand Down
1 change: 1 addition & 0 deletions 1 Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions 4 Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion 15 Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -1794,6 +1794,19 @@ def isatty(self):
self._checkClosed()
return os.isatty(self._fd)

def _isatty_openonly(self):
cmaloney marked this conversation as resolved.
Show resolved Hide resolved
"""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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be:

Suggested change
return True
return False

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, Py_RETURN_FALSE; I got right in the C but not the pyio. Making a new PR to update....

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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()."""
Expand Down
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.
2 changes: 1 addition & 1 deletion 2 Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,

/* buffering */
if (buffering < 0) {
PyObject *res = PyObject_CallMethodNoArgs(raw, &_Py_ID(isatty));
PyObject *res = PyObject_CallMethodNoArgs(raw, &_Py_ID(_isatty_openonly));
if (res == NULL)
goto error;
isatty = PyObject_IsTrue(res);
Expand Down
19 changes: 16 additions & 3 deletions 19 Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
vstinner marked this conversation as resolved.
Show resolved Hide resolved
# include <sys/stat.h>
#endif
#ifdef HAVE_IO_H
# include <io.h>
#endif
Expand Down Expand Up @@ -1208,6 +1205,21 @@ _io_FileIO_isatty_impl(fileio *self)
return PyBool_FromLong(res);
}

/* 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
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). */
static PyObject *
_io_FileIO_isatty_openonly(fileio *self, void *Py_UNUSED(ignored))
{
if (self->stat_atopen != NULL && !S_ISCHR(self->stat_atopen->st_mode)) {
Py_RETURN_FALSE;
}
return _io_FileIO_isatty_impl(self);
}

#include "clinic/fileio.c.h"

static PyMethodDef fileio_methods[] = {
Expand All @@ -1224,6 +1236,7 @@ static PyMethodDef fileio_methods[] = {
_IO_FILEIO_WRITABLE_METHODDEF
_IO_FILEIO_FILENO_METHODDEF
_IO_FILEIO_ISATTY_METHODDEF
{"_isatty_openonly", (PyCFunction)_io_FileIO_isatty_openonly, METH_NOARGS},
{"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
{"__reduce__", _PyIOBase_cannot_pickle, METH_NOARGS},
{"__reduce_ex__", _PyIOBase_cannot_pickle, METH_O},
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.