mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Issue #8397: Raise an error when attempting to mix iteration and regular
reads on a BZ2File object, rather than returning incorrect results.
This commit is contained in:
parent
7fb6f5121a
commit
7ffa196dce
3 changed files with 49 additions and 0 deletions
|
@ -138,6 +138,22 @@ typedef struct {
|
|||
/* ===================================================================== */
|
||||
/* Utility functions. */
|
||||
|
||||
/* Refuse regular I/O if there's data in the iteration-buffer.
|
||||
* Mixing them would cause data to arrive out of order, as the read*
|
||||
* methods don't use the iteration buffer. */
|
||||
static int
|
||||
check_iterbuffered(BZ2FileObject *f)
|
||||
{
|
||||
if (f->f_buf != NULL &&
|
||||
(f->f_bufend - f->f_bufptr) > 0 &&
|
||||
f->f_buf[0] != '\0') {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"Mixing iteration and read methods would lose data");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
Util_CatchBZ2Error(int bzerror)
|
||||
{
|
||||
|
@ -427,6 +443,10 @@ BZ2File_read(BZ2FileObject *self, PyObject *args)
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
/* refuse to mix with f.next() */
|
||||
if (check_iterbuffered(self))
|
||||
goto cleanup;
|
||||
|
||||
if (bytesrequested < 0)
|
||||
buffersize = Util_NewBufferSize((size_t)0);
|
||||
else
|
||||
|
@ -516,6 +536,10 @@ BZ2File_readline(BZ2FileObject *self, PyObject *args)
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
/* refuse to mix with f.next() */
|
||||
if (check_iterbuffered(self))
|
||||
goto cleanup;
|
||||
|
||||
if (sizehint == 0)
|
||||
ret = PyBytes_FromStringAndSize("", 0);
|
||||
else
|
||||
|
@ -573,6 +597,10 @@ BZ2File_readlines(BZ2FileObject *self, PyObject *args)
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
/* refuse to mix with f.next() */
|
||||
if (check_iterbuffered(self))
|
||||
goto cleanup;
|
||||
|
||||
if ((list = PyList_New(0)) == NULL)
|
||||
goto cleanup;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue