Skip to content

gh-90102: Optimize io.FileIO.isatty() #112495

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Optimize :meth:`io.FileIO.isatty`. Only invoke the ``isatty()`` system call
for character devices and cache the result. It also makes :func:`open` a bit faster.
25 changes: 17 additions & 8 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ typedef struct {
unsigned int writable : 1;
unsigned int appending : 1;
signed int seekable : 2; /* -1 means unknown */
int isatty : 2; /* -1 means unknown */
unsigned int closefd : 1;
char finalizing;
unsigned int blksize;
Expand Down Expand Up @@ -195,6 +196,7 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self->writable = 0;
self->appending = 0;
self->seekable = -1;
self->isatty = -1;
self->blksize = 0;
self->closefd = 1;
self->weakreflist = NULL;
Expand Down Expand Up @@ -475,6 +477,11 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
if (fdfstat.st_blksize > 1)
self->blksize = fdfstat.st_blksize;
#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
#ifdef S_ISCHR
if (!S_ISCHR(fdfstat.st_mode)) {
self->isatty = 0;
}
#endif /* S_ISCHR */
}

#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Expand Down Expand Up @@ -1141,16 +1148,18 @@ static PyObject *
_io_FileIO_isatty_impl(fileio *self)
/*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
{
long res;

if (self->fd < 0)
return err_closed();
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
res = isatty(self->fd);
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
return PyBool_FromLong(res);
if (self->isatty < 0) {
int res;
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
res = isatty(self->fd);
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
self->isatty = res;
}
return PyBool_FromLong(self->isatty);
}

#include "clinic/fileio.c.h"
Expand Down