diff options
Diffstat (limited to 'Objects/fileobject.c')
-rw-r--r-- | Objects/fileobject.c | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 565bf5c..5b76b0d 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -861,16 +861,16 @@ file_read(PyFileObject *f, PyObject *args) buffersize - bytesread, f->f_fp, (PyObject *)f); Py_END_ALLOW_THREADS if (chunksize == 0) { - if (!ferror(f->f_fp)) + if (!PyErr_ExceptionMatches(PyExc_IOError)) break; - clearerr(f->f_fp); /* When in non-blocking mode, data shouldn't * be discarded if a blocking signal was * received. That will also happen if * chunksize != 0, but bytesread < buffersize. */ - if (bytesread > 0 && BLOCKED_ERRNO(errno)) + if (bytesread > 0 && BLOCKED_ERRNO(errno)) { + PyErr_Clear(); break; - PyErr_SetFromErrno(PyExc_IOError); + } Py_DECREF(v); return NULL; } @@ -917,10 +917,8 @@ file_readinto(PyFileObject *f, PyObject *args) (PyObject *)f); Py_END_ALLOW_THREADS if (nnow == 0) { - if (!ferror(f->f_fp)) + if (!PyErr_ExceptionMatches(PyExc_IOError)) break; - PyErr_SetFromErrno(PyExc_IOError); - clearerr(f->f_fp); return NULL; } ndone += nnow; @@ -1412,10 +1410,8 @@ file_readlines(PyFileObject *f, PyObject *args) } if (nread == 0) { sizehint = 0; - if (!ferror(f->f_fp)) + if (!PyErr_ExceptionMatches(PyExc_IOError)) break; - PyErr_SetFromErrno(PyExc_IOError); - clearerr(f->f_fp); error: Py_DECREF(list); list = NULL; @@ -1863,9 +1859,7 @@ readahead(PyFileObject *f, int bufsize) f->f_buf, bufsize, f->f_fp, (PyObject *)f); Py_END_ALLOW_THREADS if (chunksize == 0) { - if (ferror(f->f_fp)) { - PyErr_SetFromErrno(PyExc_IOError); - clearerr(f->f_fp); + if (PyErr_ExceptionMatches(PyExc_IOError)) { drop_readahead(f); return -1; } @@ -2416,6 +2410,7 @@ Py_UniversalNewlineFread(char *buf, size_t n, char *dst = buf; PyFileObject *f = (PyFileObject *)fobj; int newlinetypes, skipnextlf; + size_t nread; assert(buf != NULL); assert(stream != NULL); @@ -2424,22 +2419,35 @@ Py_UniversalNewlineFread(char *buf, size_t n, errno = ENXIO; /* What can you do... */ return 0; } - if (!f->f_univ_newline) - return fread(buf, 1, n, stream); + if (!f->f_univ_newline) { + nread = fread(buf, 1, n, stream); + if (nread == 0) { + if (ferror(stream)) + PyErr_SetFromErrno(PyExc_IOError); + clearerr(stream); + } + return nread; + } newlinetypes = f->f_newlinetypes; skipnextlf = f->f_skipnextlf; /* Invariant: n is the number of bytes remaining to be filled * in the buffer. */ while (n) { - size_t nread; int shortread; char *src = dst; nread = fread(dst, 1, n, stream); assert(nread <= n); - if (nread == 0) + if (nread == 0) { + if (ferror(stream)) { + clearerr(stream); + PyErr_SetFromErrno(PyExc_IOError); + return 0; + } + clearerr(stream); break; + } n -= nread; /* assuming 1 byte out for each in; will adjust */ shortread = n != 0; /* true iff EOF or error */ |