-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
|
||||||
PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL; | ||||||
|
||||||
|
@@ -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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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; | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -72,6 +72,7 @@ typedef struct { | |||||
unsigned int closefd : 1; | ||||||
char finalizing; | ||||||
unsigned int blksize; | ||||||
__int64_t size; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( |
||||||
PyObject *weakreflist; | ||||||
PyObject *dict; | ||||||
} fileio; | ||||||
|
@@ -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; | ||||||
} | ||||||
|
@@ -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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
#if defined(MS_WINDOWS) || defined(__CYGWIN__) | ||||||
|
@@ -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}, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{"_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}, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.