Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-45944: Avoid calling isatty() for most open() calls #29870

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
wants to merge 2 commits into from
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
1 change: 1 addition & 0 deletions 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 Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(_restype_)
STRUCT_FOR_ID(_showwarnmsg)
STRUCT_FOR_ID(_shutdown)
STRUCT_FOR_ID(_size)
STRUCT_FOR_ID(_slotnames)
STRUCT_FOR_ID(_strptime_datetime)
STRUCT_FOR_ID(_swappedbytes_)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

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

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

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

3 changes: 2 additions & 1 deletion 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 getattr(raw, '_size', 0) == 0 and raw.isatty():
buffering = -1
line_buffering = True
if buffering < 0:
Expand Down Expand Up @@ -1571,6 +1571,7 @@ def __init__(self, file, mode='r', closefd=True, opener=None):
self._blksize = getattr(fdfstat, 'st_blksize', 0)
if self._blksize <= 1:
self._blksize = DEFAULT_BUFFER_SIZE
self._size = fdfstat.st_size

if _setmode:
# don't translate newlines (\r\n <=> \n)
Expand Down
11 changes: 11 additions & 0 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,

char rawmode[6], *m;
int line_buffering, is_number, isatty = 0;
__int64_t size = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
__int64_t size = 0;
Py_ssize_t size = 0;


PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL;

Expand Down Expand Up @@ -346,6 +347,16 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,

/* buffering */
if (buffering < 0) {
PyObject *size_obj;
size_obj = PyObject_GetAttr(raw, &_Py_ID(_size));
if (size_obj == NULL)
goto error;
size = PyLong_AsLongLong(size_obj);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
size = PyLong_AsLongLong(size_obj);
size = PyLong_AsSsize_t(size_obj);

Py_DECREF(size_obj);
if (size == -1 && PyErr_Occurred())
goto error;
}
if (buffering < 0 && size == 0) {
PyObject *res = PyObject_CallMethodNoArgs(raw, &_Py_ID(isatty));
if (res == NULL)
goto error;
Expand Down
4 changes: 4 additions & 0 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ typedef struct {
unsigned int closefd : 1;
char finalizing;
unsigned int blksize;
__int64_t size;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
__int64_t size;
Py_ssize_t size;

Copy link
Member

Choose a reason for hiding this comment

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

It should be off_t. The file size can exceed the Py_ssize_t range.

Copy link
Member

Choose a reason for hiding this comment

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

You are right! size handling depends on platform and support for large file support. You can copy the approach from posixmodule.c:

#ifdef MS_WINDOWS
    typedef long long Py_off_t;
#else
    typedef off_t Py_off_t;
#endif
#ifdef HAVE_LARGEFILE_SUPPORT
    size = (Py_off_t)PyLong_AsLongLong(arg);
#else
    size = (Py_off_t)PyLong_AsLong(arg);
#endif

Copy link
Member

Choose a reason for hiding this comment

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

It is too complicated, we need to duplicate this in several files. But it may be simpler if only cache the boolean result (st_size == 0 or S_IFCHR()) as _maybeatty. Then the size of off_t does not matter.

PyObject *weakreflist;
PyObject *dict;
} fileio;
Expand Down Expand Up @@ -196,6 +197,7 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self->appending = 0;
self->seekable = -1;
self->blksize = 0;
self->size = 0;
self->closefd = 1;
self->weakreflist = NULL;
}
Expand Down Expand Up @@ -475,6 +477,7 @@ _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 */
self->size = fdfstat.st_size;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self->size = fdfstat.st_size;
self->size = fdfstat.st_size;

}

#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Expand Down Expand Up @@ -1205,6 +1208,7 @@ static PyGetSetDef fileio_getsetlist[] = {

static PyMemberDef fileio_members[] = {
{"_blksize", Py_T_UINT, offsetof(fileio, blksize), 0},
{"_size", T_UINT, offsetof(fileio, size), 0},
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{"_size", T_UINT, offsetof(fileio, size), 0},
{"_size", T_PYSSIZET, offsetof(fileio, size), 0},

{"_finalizing", Py_T_BOOL, offsetof(fileio, finalizing), 0},
{"__weaklistoffset__", Py_T_PYSSIZET, offsetof(fileio, weakreflist), Py_READONLY},
{"__dictoffset__", Py_T_PYSSIZET, offsetof(fileio, dict), Py_READONLY},
Expand Down