_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(_initializing));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(_io));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(_is_text_encoding));
+ _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(_isatty_open_only));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(_length_));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(_limbo));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(_lock_unlock_module));
STRUCT_FOR_ID(_initializing)
STRUCT_FOR_ID(_io)
STRUCT_FOR_ID(_is_text_encoding)
+ STRUCT_FOR_ID(_isatty_open_only)
STRUCT_FOR_ID(_length_)
STRUCT_FOR_ID(_limbo)
STRUCT_FOR_ID(_lock_unlock_module)
INIT_ID(_initializing), \
INIT_ID(_io), \
INIT_ID(_is_text_encoding), \
+ INIT_ID(_isatty_open_only), \
INIT_ID(_length_), \
INIT_ID(_limbo), \
INIT_ID(_lock_unlock_module), \
_PyUnicode_InternStatic(interp, &string);
assert(_PyUnicode_CheckConsistency(string, 1));
assert(PyUnicode_GET_LENGTH(string) != 1);
+ string = &_Py_ID(_isatty_open_only);
+ _PyUnicode_InternStatic(interp, &string);
+ assert(_PyUnicode_CheckConsistency(string, 1));
+ assert(PyUnicode_GET_LENGTH(string) != 1);
string = &_Py_ID(_length_);
_PyUnicode_InternStatic(interp, &string);
assert(_PyUnicode_CheckConsistency(string, 1));
result = raw
try:
line_buffering = False
- if buffering == 1 or buffering < 0 and raw.isatty():
+ if buffering == 1 or buffering < 0 and raw._isatty_open_only():
buffering = -1
line_buffering = True
if buffering < 0:
self._checkClosed()
return os.isatty(self._fd)
+ def _isatty_open_only(self):
+ """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).
+ """
+ if (self._stat_atopen is not None
+ and not stat.S_ISCHR(self._stat_atopen.st_mode)):
+ return True
+ return os.isatty(self._fd)
+
@property
def closefd(self):
"""True if the file descriptor will be closed by close()."""
--- /dev/null
+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.
/* buffering */
if (buffering < 0) {
- PyObject *res = PyObject_CallMethodNoArgs(raw, &_Py_ID(isatty));
+ PyObject *res = PyObject_CallMethodNoArgs(raw, &_Py_ID(_isatty_open_only));
if (res == NULL)
goto error;
isatty = PyObject_IsTrue(res);
#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
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[] = {
_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},
_IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF
_IO__WINDOWSCONSOLEIO_FILENO_METHODDEF
_IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
+ {"_isatty_open_only", (PyCFunction)_io__WindowsConsoleIO_isatty, METH_NOARGS},
{NULL, NULL} /* sentinel */
};