Kaydet (Commit) 51359921 authored tarafından Gregory P. Smith's avatar Gregory P. Smith

Fixes issue #12268: File readline, readlines and read() or readall() methods

no longer lose data when an underlying read system call is interrupted.
IOError is no longer raised due to a read system call returning EINTR
from within these methods.
üst 8150492f
This diff is collapsed.
...@@ -10,6 +10,11 @@ What's New in Python 3.2.4 ...@@ -10,6 +10,11 @@ What's New in Python 3.2.4
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #12268: File readline, readlines and read() or readall() methods
no longer lose data when an underlying read system call is interrupted.
IOError is no longer raised due to a read system call returning EINTR
from within these methods.
- Issue #15142: Fix reference leak when deallocating instances of types - Issue #15142: Fix reference leak when deallocating instances of types
created using PyType_FromSpec(). created using PyType_FromSpec().
......
...@@ -57,6 +57,11 @@ extern Py_ssize_t _PyIO_find_line_ending( ...@@ -57,6 +57,11 @@ extern Py_ssize_t _PyIO_find_line_ending(
int translated, int universal, PyObject *readnl, int translated, int universal, PyObject *readnl,
Py_UNICODE *start, Py_UNICODE *end, Py_ssize_t *consumed); Py_UNICODE *start, Py_UNICODE *end, Py_ssize_t *consumed);
/* Return 1 if an EnvironmentError with errno == EINTR is set (and then
clears the error indicator), 0 otherwise.
Should only be called when PyErr_Occurred() is true.
*/
extern int _PyIO_trap_eintr(void);
#define DEFAULT_BUFFER_SIZE (8 * 1024) /* bytes */ #define DEFAULT_BUFFER_SIZE (8 * 1024) /* bytes */
......
...@@ -730,8 +730,8 @@ _buffered_init(buffered *self) ...@@ -730,8 +730,8 @@ _buffered_init(buffered *self)
clears the error indicator), 0 otherwise. clears the error indicator), 0 otherwise.
Should only be called when PyErr_Occurred() is true. Should only be called when PyErr_Occurred() is true.
*/ */
static int int
_trap_eintr(void) _PyIO_trap_eintr(void)
{ {
static PyObject *eintr_int = NULL; static PyObject *eintr_int = NULL;
PyObject *typ, *val, *tb; PyObject *typ, *val, *tb;
...@@ -1314,7 +1314,7 @@ _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len) ...@@ -1314,7 +1314,7 @@ _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len)
*/ */
do { do {
res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readinto, memobj, NULL); res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readinto, memobj, NULL);
} while (res == NULL && _trap_eintr()); } while (res == NULL && _PyIO_trap_eintr());
Py_DECREF(memobj); Py_DECREF(memobj);
if (res == NULL) if (res == NULL)
return -1; return -1;
...@@ -1742,7 +1742,7 @@ _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len) ...@@ -1742,7 +1742,7 @@ _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len)
errno = 0; errno = 0;
res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_write, memobj, NULL); res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_write, memobj, NULL);
errnum = errno; errnum = errno;
} while (res == NULL && _trap_eintr()); } while (res == NULL && _PyIO_trap_eintr());
Py_DECREF(memobj); Py_DECREF(memobj);
if (res == NULL) if (res == NULL)
return -1; return -1;
......
...@@ -605,6 +605,13 @@ fileio_readall(fileio *self) ...@@ -605,6 +605,13 @@ fileio_readall(fileio *self)
if (n == 0) if (n == 0)
break; break;
if (n < 0) { if (n < 0) {
if (errno == EINTR) {
if (PyErr_CheckSignals()) {
Py_DECREF(result);
return NULL;
}
continue;
}
if (total > 0) if (total > 0)
break; break;
if (errno == EAGAIN) { if (errno == EAGAIN) {
......
...@@ -482,8 +482,14 @@ iobase_readline(PyObject *self, PyObject *args) ...@@ -482,8 +482,14 @@ iobase_readline(PyObject *self, PyObject *args)
if (has_peek) { if (has_peek) {
PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1); PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1);
if (readahead == NULL) if (readahead == NULL) {
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
when EINTR occurs so we needn't do it ourselves. */
if (_PyIO_trap_eintr()) {
continue;
}
goto fail; goto fail;
}
if (!PyBytes_Check(readahead)) { if (!PyBytes_Check(readahead)) {
PyErr_Format(PyExc_IOError, PyErr_Format(PyExc_IOError,
"peek() should have returned a bytes object, " "peek() should have returned a bytes object, "
...@@ -516,8 +522,14 @@ iobase_readline(PyObject *self, PyObject *args) ...@@ -516,8 +522,14 @@ iobase_readline(PyObject *self, PyObject *args)
} }
b = PyObject_CallMethod(self, "read", "n", nreadahead); b = PyObject_CallMethod(self, "read", "n", nreadahead);
if (b == NULL) if (b == NULL) {
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
when EINTR occurs so we needn't do it ourselves. */
if (_PyIO_trap_eintr()) {
continue;
}
goto fail; goto fail;
}
if (!PyBytes_Check(b)) { if (!PyBytes_Check(b)) {
PyErr_Format(PyExc_IOError, PyErr_Format(PyExc_IOError,
"read() should have returned a bytes object, " "read() should have returned a bytes object, "
...@@ -826,6 +838,11 @@ rawiobase_readall(PyObject *self, PyObject *args) ...@@ -826,6 +838,11 @@ rawiobase_readall(PyObject *self, PyObject *args)
PyObject *data = PyObject_CallMethod(self, "read", PyObject *data = PyObject_CallMethod(self, "read",
"i", DEFAULT_BUFFER_SIZE); "i", DEFAULT_BUFFER_SIZE);
if (!data) { if (!data) {
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
when EINTR occurs so we needn't do it ourselves. */
if (_PyIO_trap_eintr()) {
continue;
}
Py_DECREF(chunks); Py_DECREF(chunks);
return NULL; return NULL;
} }
......
...@@ -1541,8 +1541,14 @@ textiowrapper_read(textio *self, PyObject *args) ...@@ -1541,8 +1541,14 @@ textiowrapper_read(textio *self, PyObject *args)
/* Keep reading chunks until we have n characters to return */ /* Keep reading chunks until we have n characters to return */
while (remaining > 0) { while (remaining > 0) {
res = textiowrapper_read_chunk(self); res = textiowrapper_read_chunk(self);
if (res < 0) if (res < 0) {
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
when EINTR occurs so we needn't do it ourselves. */
if (_PyIO_trap_eintr()) {
continue;
}
goto fail; goto fail;
}
if (res == 0) /* EOF */ if (res == 0) /* EOF */
break; break;
if (chunks == NULL) { if (chunks == NULL) {
...@@ -1701,8 +1707,14 @@ _textiowrapper_readline(textio *self, Py_ssize_t limit) ...@@ -1701,8 +1707,14 @@ _textiowrapper_readline(textio *self, Py_ssize_t limit)
while (!self->decoded_chars || while (!self->decoded_chars ||
!PyUnicode_GET_SIZE(self->decoded_chars)) { !PyUnicode_GET_SIZE(self->decoded_chars)) {
res = textiowrapper_read_chunk(self); res = textiowrapper_read_chunk(self);
if (res < 0) if (res < 0) {
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
when EINTR occurs so we needn't do it ourselves. */
if (_PyIO_trap_eintr()) {
continue;
}
goto error; goto error;
}
if (res == 0) if (res == 0)
break; break;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment