summaryrefslogtreecommitdiffstats
path: root/Modules/_io/fileio.c
diff options
context:
space:
mode:
authorCody Maloney <cmaloney@users.noreply.github.com>2024-10-08 06:50:42 (GMT)
committerGitHub <noreply@github.com>2024-10-08 06:50:42 (GMT)
commitcc9b9bebb2c09c475cb0f278268cce03c115eaea (patch)
tree0eef6d302bcf6922b4664e1feb3e4aa41161c8b5 /Modules/_io/fileio.c
parent6e3c70c61bf961e55e9912a31ca11f61c8e2cd0c (diff)
downloadcpython-cc9b9bebb2c09c475cb0f278268cce03c115eaea.zip
cpython-cc9b9bebb2c09c475cb0f278268cce03c115eaea.tar.gz
cpython-cc9b9bebb2c09c475cb0f278268cce03c115eaea.tar.bz2
gh-90102: Remove isatty call during regular open (#124922)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Modules/_io/fileio.c')
-rw-r--r--Modules/_io/fileio.c22
1 files changed, 19 insertions, 3 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index d9597f8..f374592 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -12,9 +12,6 @@
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
-#ifdef HAVE_SYS_STAT_H
-# include <sys/stat.h>
-#endif
#ifdef HAVE_IO_H
# include <io.h>
#endif
@@ -1218,6 +1215,24 @@ _io_FileIO_isatty_impl(fileio *self)
return PyBool_FromLong(res);
}
+/* Checks whether the file is a TTY using an open-only optimization.
+
+ TTYs are always character devices. If the interpreter knows a file is
+ not a character device when it would call ``isatty``, can skip that
+ call. Inside ``open()`` there is a fresh stat result that contains that
+ information. Use the stat result to skip a system call. Outside of that
+ context TOCTOU issues (the fd could be arbitrarily modified by
+ surrounding code). */
+static PyObject *
+_io_FileIO_isatty_open_only(PyObject *op, PyObject *Py_UNUSED(ignored))
+{
+ fileio *self = _PyFileIO_CAST(op);
+ 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[] = {
@@ -1234,6 +1249,7 @@ static PyMethodDef fileio_methods[] = {
_IO_FILEIO_WRITABLE_METHODDEF
_IO_FILEIO_FILENO_METHODDEF
_IO_FILEIO_ISATTY_METHODDEF
+ {"_isatty_open_only", _io_FileIO_isatty_open_only, METH_NOARGS},
{"_dealloc_warn", fileio_dealloc_warn, METH_O, NULL},
{"__reduce__", _PyIOBase_cannot_pickle, METH_NOARGS},
{"__reduce_ex__", _PyIOBase_cannot_pickle, METH_O},