/* This is a hacked version of Python's fileobject.c:file_read(). */static PyObject *BZ2File_read(BZ2FileObject *self, PyObject *args){ long bytesrequested = -1; size_t bytesread, buffersize, chunksize; int bzerror; PyObject *ret = NULL; if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested)) return NULL; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_READ: break; case MODE_READ_EOF: ret = PyString_FromString(""); goto cleanup; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto cleanup; default: PyErr_SetString(PyExc_IOError, "file is not ready for reading"); goto cleanup; } if (bytesrequested < 0) buffersize = Util_NewBufferSize((size_t)0); else buffersize = bytesrequested; if (buffersize > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "requested number of bytes is " "more than a Python string can hold"); goto cleanup; } ret = PyString_FromStringAndSize((char *)NULL, buffersize); if (ret == NULL) goto cleanup; bytesread = 0; for (;;) { Py_BEGIN_ALLOW_THREADS chunksize = Util_UnivNewlineRead(&bzerror, self->fp, BUF(ret)+bytesread, buffersize-bytesread, self); self->pos += chunksize; Py_END_ALLOW_THREADS bytesread += chunksize; if (bzerror == BZ_STREAM_END) { self->size = self->pos; self->mode = MODE_READ_EOF; break; } else if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); Py_DECREF(ret); ret = NULL; goto cleanup; } if (bytesrequested < 0) { buffersize = Util_NewBufferSize(buffersize); if (_PyString_Resize(&ret, buffersize) < 0) goto cleanup; } else { break; } } if (bytesread != buffersize) _PyString_Resize(&ret, bytesread); cleanup: RELEASE_LOCK(self); return ret;} PyDoc_STRVAR(BZ2File_readline__doc__,"readline([size]) -> string\n\\n\Return the next line from the file, as a string, retaining newline.\n\A non-negative size argument will limit the maximum number of bytes to\n\return (an incomplete line may be returned then). Return an empty\n\string at EOF.\n\"); static PyObject *BZ2File_readline(BZ2FileObject *self, PyObject *args){ PyObject *ret = NULL; int sizehint = -1; if (!PyArg_ParseTuple(args, "|i:readline", &sizehint)) return NULL; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_READ: break; case MODE_READ_EOF: ret = PyString_FromString(""); goto cleanup; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto cleanup; default: PyErr_SetString(PyExc_IOError, "file is not ready for reading"); goto cleanup; } if (sizehint == 0) ret = PyString_FromString(""); else ret = Util_GetLine(self, (sizehint < 0) ? 0 : sizehint); cleanup: RELEASE_LOCK(self); return ret;} PyDoc_STRVAR(BZ2File_readlines__doc__,"readlines([size]) -> list\n\\n\Call readline() repeatedly and return a list of lines read.\n\The optional size argument, if given, is an approximate bound on the\n\total number of bytes in the lines returned.\n\"); /* This is a hacked version of Python's fileobject.c:file_readlines(). */static PyObject *BZ2File_readlines(BZ2FileObject *self, PyObject *args){ long sizehint = 0; PyObject *list = NULL; PyObject *line; char small_buffer[SMALLCHUNK]; char *buffer = small_buffer; size_t buffersize = SMALLCHUNK; PyObject *big_buffer = NULL; size_t nfilled = 0; size_t nread; size_t totalread = 0; char *p, *q, *end; int err; int shortread = 0; int bzerror; if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint)) return NULL; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_READ: break; case MODE_READ_EOF: list = PyList_New(0); goto cleanup; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto cleanup; default: PyErr_SetString(PyExc_IOError, "file is not ready for reading"); goto cleanup; } if ((list = PyList_New(0)) == NULL) goto cleanup; for (;;) { Py_BEGIN_ALLOW_THREADS nread = Util_UnivNewlineRead(&bzerror, self->fp, buffer+nfilled, buffersize-nfilled, self); self->pos += nread; Py_END_ALLOW_THREADS if (bzerror == BZ_STREAM_END) { self->size = self->pos; self->mode = MODE_READ_EOF; if (nread == 0) { sizehint = 0; break; } shortread = 1; } else if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); error: Py_DECREF(list); list = NULL; goto cleanup; } totalread += nread; p = memchr(buffer+nfilled, '\n', nread); if (!shortread && p == NULL) { /* Need a larger buffer to fit this line */ nfilled += nread; buffersize *= 2; if (buffersize > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "line is longer than a Python string can hold"); goto error; } if (big_buffer == NULL) { /* Create the big buffer */ big_buffer = PyString_FromStringAndSize( NULL, buffersize); if (big_buffer == NULL) goto error; buffer = PyString_AS_STRING(big_buffer); memcpy(buffer, small_buffer, nfilled); } else { /* Grow the big buffer */ _PyString_Resize(&big_buffer, buffersize); buffer = PyString_AS_STRING(big_buffer); } continue; } end = buffer+nfilled+nread; q = buffer; while (p != NULL) { /* Process complete lines */ p++; line = PyString_FromStringAndSize(q, p-q); if (line == NULL) goto error; err = PyList_Append(list, line); Py_DECREF(line); if (err != 0) goto error; q = p; p = memchr(q, '\n', end-q); } /* Move the remaining incomplete line to the start */ nfilled = end-q; memmove(buffer, q, nfilled); if (sizehint > 0) if (totalread >= (size_t)sizehint) break; if (shortread) { sizehint = 0; break; } } if (nfilled != 0) { /* Partial last line */ line = PyString_FromStringAndSize(buffer, nfilled); if (line == NULL) goto error; if (sizehint > 0) { /* Need to complete the last line */ PyObject *rest = Util_GetLine(self, 0); if (rest == NULL) { Py_DECREF(line); goto error; } PyString_Concat(&line, rest); Py_DECREF(rest); if (line == NULL) goto error; } err = PyList_Append(list, line); Py_DECREF(line); if (err != 0) goto error; } cleanup: RELEASE_LOCK(self); if (big_buffer) { Py_DECREF(big_buffer); } return list;} PyDoc_STRVAR(BZ2File_xreadlines__doc__,"xreadlines() -> self\n\\n\For backward compatibility. BZ2File objects now include the performance\n\optimizations previously implemented in the xreadlines module.\n\"); PyDoc_STRVAR(BZ2File_write__doc__,"write(data) -> None\n\\n\Write the 'data' string to file. Note that due to buffering, close() may\n\be needed before the file on disk reflects the data written.\n\"); /* This is a hacked version of Python's fileobject.c:file_write(). */static PyObject *BZ2File_write(BZ2FileObject *self, PyObject *args){ PyObject *ret = NULL; char *buf; int len; int bzerror; if (!PyArg_ParseTuple(args, "s#:write", &buf, &len)) return NULL; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_WRITE: break; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto cleanup; default: PyErr_SetString(PyExc_IOError, "file is not ready for writing"); goto cleanup; } self->f_softspace = 0; Py_BEGIN_ALLOW_THREADS BZ2_bzWrite (&bzerror, self->fp, buf, len); self->pos += len; Py_END_ALLOW_THREADS if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); goto cleanup; } Py_INCREF(Py_None); ret = Py_None; cleanup: RELEASE_LOCK(self); return ret;} PyDoc_STRVAR(BZ2File_writelines__doc__,"writelines(sequence_of_strings) -> None\n\\n\Write the sequence of strings to the file. Note that newlines are not\n\added. The sequence can be any iterable object producing strings. This is\n\equivalent to calling write() for each string.\n\"); /* This is a hacked version of Python's fileobject.c:file_writelines(). */static PyObject *BZ2File_writelines(BZ2FileObject *self, PyObject *seq){#define CHUNKSIZE 1000 PyObject *list = NULL; PyObject *iter = NULL; PyObject *ret = NULL; PyObject *line; int i, j, index, len, islist; int bzerror; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_WRITE: break; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto error; default: PyErr_SetString(PyExc_IOError, "file is not ready for writing"); goto error; } islist = PyList_Check(seq); if (!islist) { iter = PyObject_GetIter(seq); if (iter == NULL) { PyErr_SetString(PyExc_TypeError, "writelines() requires an iterable argument"); goto error; } list = PyList_New(CHUNKSIZE); if (list == NULL) goto error; } /* Strategy: slurp CHUNKSIZE lines into a private list, checking that they are all strings, then write that list without holding the interpreter lock, then come back for more. */ for (index = 0; ; index += CHUNKSIZE) { if (islist) { Py_XDECREF(list); list = PyList_GetSlice(seq, index, index+CHUNKSIZE); if (list == NULL) goto error; j = PyList_GET_SIZE(list); } else { for (j = 0; j < CHUNKSIZE; j++) { line = PyIter_Next(iter); if (line == NULL) { if (PyErr_Occurred()) goto error; break; } PyList_SetItem(list, j, line); } } if (j == 0) break; /* Check that all entries are indeed strings. If not, apply the same rules as for file.write() and convert the rets to strings. This is slow, but seems to be the only way since all conversion APIs could potentially execute Python code. */ for (i = 0; i < j; i++) { PyObject *v = PyList_GET_ITEM(list, i); if (!PyString_Check(v)) { const char *buffer; Py_ssize_t len; if (PyObject_AsCharBuffer(v, &buffer, &len)) { PyErr_SetString(PyExc_TypeError, "writelines() " "argument must be " "a sequence of " "strings"); goto error; } line = PyString_FromStringAndSize(buffer, len); if (line == NULL) goto error; Py_DECREF(v); PyList_SET_ITEM(list, i, line); } } self->f_softspace = 0; /* Since we are releasing the global lock, the following code may *not* execute Python code. */ Py_BEGIN_ALLOW_THREADS for (i = 0; i < j; i++) { line = PyList_GET_ITEM(list, i); len = PyString_GET_SIZE(line); BZ2_bzWrite (&bzerror, self->fp, PyString_AS_STRING(line), len); if (bzerror != BZ_OK) { Py_BLOCK_THREADS Util_CatchBZ2Error(bzerror); goto error; } } Py_END_ALLOW_THREADS if (j < CHUNKSIZE) break; } Py_INCREF(Py_None); ret = Py_None; error: RELEASE_LOCK(self); Py_XDECREF(list); Py_XDECREF(iter); return ret;#undef CHUNKSIZE} PyDoc_STRVAR(BZ2File_seek__doc__,"seek(offset [, whence]) -> None\n\\n\Move to new file position. Argument offset is a byte count. Optional\n\argument whence defaults to 0 (offset from start of file, offset\n\should be >= 0); other values are 1 (move relative to current position,\n\positive or negative), and 2 (move relative to end of file, usually\n\negative, although many platforms allow seeking beyond the end of a file).\n\\n\Note that seeking of bz2 files is emulated, and depending on the parameters\n\the operation may be extremely slow.\n\"); static PyObject *BZ2File_seek(BZ2FileObject *self, PyObject *args){ int where = 0; PyObject *offobj; Py_off_t offset; char small_buffer[SMALLCHUNK]; char *buffer = small_buffer; size_t buffersize = SMALLCHUNK; Py_off_t bytesread = 0; size_t readsize;Lines 501–1000 of 2232. Previous lines Load more
/* This is a hacked version of Python's fileobject.c:file_read(). */ static PyObject * BZ2File_read(BZ2FileObject *self, PyObject *args) { long bytesrequested = -1; size_t bytesread, buffersize, chunksize; int bzerror; PyObject *ret = NULL; if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested)) return NULL; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_READ: break; case MODE_READ_EOF: ret = PyString_FromString(""); goto cleanup; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto cleanup; default: PyErr_SetString(PyExc_IOError, "file is not ready for reading"); goto cleanup; } if (bytesrequested < 0) buffersize = Util_NewBufferSize((size_t)0); else buffersize = bytesrequested; if (buffersize > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "requested number of bytes is " "more than a Python string can hold"); goto cleanup; } ret = PyString_FromStringAndSize((char *)NULL, buffersize); if (ret == NULL) goto cleanup; bytesread = 0; for (;;) { Py_BEGIN_ALLOW_THREADS chunksize = Util_UnivNewlineRead(&bzerror, self->fp, BUF(ret)+bytesread, buffersize-bytesread, self); self->pos += chunksize; Py_END_ALLOW_THREADS bytesread += chunksize; if (bzerror == BZ_STREAM_END) { self->size = self->pos; self->mode = MODE_READ_EOF; break; } else if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); Py_DECREF(ret); ret = NULL; goto cleanup; } if (bytesrequested < 0) { buffersize = Util_NewBufferSize(buffersize); if (_PyString_Resize(&ret, buffersize) < 0) goto cleanup; } else { break; } } if (bytesread != buffersize) _PyString_Resize(&ret, bytesread); cleanup: RELEASE_LOCK(self); return ret; } PyDoc_STRVAR(BZ2File_readline__doc__, "readline([size]) -> string\n\ \n\ Return the next line from the file, as a string, retaining newline.\n\ A non-negative size argument will limit the maximum number of bytes to\n\ return (an incomplete line may be returned then). Return an empty\n\ string at EOF.\n\ "); static PyObject * BZ2File_readline(BZ2FileObject *self, PyObject *args) { PyObject *ret = NULL; int sizehint = -1; if (!PyArg_ParseTuple(args, "|i:readline", &sizehint)) return NULL; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_READ: break; case MODE_READ_EOF: ret = PyString_FromString(""); goto cleanup; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto cleanup; default: PyErr_SetString(PyExc_IOError, "file is not ready for reading"); goto cleanup; } if (sizehint == 0) ret = PyString_FromString(""); else ret = Util_GetLine(self, (sizehint < 0) ? 0 : sizehint); cleanup: RELEASE_LOCK(self); return ret; } PyDoc_STRVAR(BZ2File_readlines__doc__, "readlines([size]) -> list\n\ \n\ Call readline() repeatedly and return a list of lines read.\n\ The optional size argument, if given, is an approximate bound on the\n\ total number of bytes in the lines returned.\n\ "); /* This is a hacked version of Python's fileobject.c:file_readlines(). */ static PyObject * BZ2File_readlines(BZ2FileObject *self, PyObject *args) { long sizehint = 0; PyObject *list = NULL; PyObject *line; char small_buffer[SMALLCHUNK]; char *buffer = small_buffer; size_t buffersize = SMALLCHUNK; PyObject *big_buffer = NULL; size_t nfilled = 0; size_t nread; size_t totalread = 0; char *p, *q, *end; int err; int shortread = 0; int bzerror; if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint)) return NULL; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_READ: break; case MODE_READ_EOF: list = PyList_New(0); goto cleanup; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto cleanup; default: PyErr_SetString(PyExc_IOError, "file is not ready for reading"); goto cleanup; } if ((list = PyList_New(0)) == NULL) goto cleanup; for (;;) { Py_BEGIN_ALLOW_THREADS nread = Util_UnivNewlineRead(&bzerror, self->fp, buffer+nfilled, buffersize-nfilled, self); self->pos += nread; Py_END_ALLOW_THREADS if (bzerror == BZ_STREAM_END) { self->size = self->pos; self->mode = MODE_READ_EOF; if (nread == 0) { sizehint = 0; break; } shortread = 1; } else if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); error: Py_DECREF(list); list = NULL; goto cleanup; } totalread += nread; p = memchr(buffer+nfilled, '\n', nread); if (!shortread && p == NULL) { /* Need a larger buffer to fit this line */ nfilled += nread; buffersize *= 2; if (buffersize > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "line is longer than a Python string can hold"); goto error; } if (big_buffer == NULL) { /* Create the big buffer */ big_buffer = PyString_FromStringAndSize( NULL, buffersize); if (big_buffer == NULL) goto error; buffer = PyString_AS_STRING(big_buffer); memcpy(buffer, small_buffer, nfilled); } else { /* Grow the big buffer */ _PyString_Resize(&big_buffer, buffersize); buffer = PyString_AS_STRING(big_buffer); } continue; } end = buffer+nfilled+nread; q = buffer; while (p != NULL) { /* Process complete lines */ p++; line = PyString_FromStringAndSize(q, p-q); if (line == NULL) goto error; err = PyList_Append(list, line); Py_DECREF(line); if (err != 0) goto error; q = p; p = memchr(q, '\n', end-q); } /* Move the remaining incomplete line to the start */ nfilled = end-q; memmove(buffer, q, nfilled); if (sizehint > 0) if (totalread >= (size_t)sizehint) break; if (shortread) { sizehint = 0; break; } } if (nfilled != 0) { /* Partial last line */ line = PyString_FromStringAndSize(buffer, nfilled); if (line == NULL) goto error; if (sizehint > 0) { /* Need to complete the last line */ PyObject *rest = Util_GetLine(self, 0); if (rest == NULL) { Py_DECREF(line); goto error; } PyString_Concat(&line, rest); Py_DECREF(rest); if (line == NULL) goto error; } err = PyList_Append(list, line); Py_DECREF(line); if (err != 0) goto error; } cleanup: RELEASE_LOCK(self); if (big_buffer) { Py_DECREF(big_buffer); } return list; } PyDoc_STRVAR(BZ2File_xreadlines__doc__, "xreadlines() -> self\n\ \n\ For backward compatibility. BZ2File objects now include the performance\n\ optimizations previously implemented in the xreadlines module.\n\ "); PyDoc_STRVAR(BZ2File_write__doc__, "write(data) -> None\n\ \n\ Write the 'data' string to file. Note that due to buffering, close() may\n\ be needed before the file on disk reflects the data written.\n\ "); /* This is a hacked version of Python's fileobject.c:file_write(). */ static PyObject * BZ2File_write(BZ2FileObject *self, PyObject *args) { PyObject *ret = NULL; char *buf; int len; int bzerror; if (!PyArg_ParseTuple(args, "s#:write", &buf, &len)) return NULL; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_WRITE: break; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto cleanup; default: PyErr_SetString(PyExc_IOError, "file is not ready for writing"); goto cleanup; } self->f_softspace = 0; Py_BEGIN_ALLOW_THREADS BZ2_bzWrite (&bzerror, self->fp, buf, len); self->pos += len; Py_END_ALLOW_THREADS if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); goto cleanup; } Py_INCREF(Py_None); ret = Py_None; cleanup: RELEASE_LOCK(self); return ret; } PyDoc_STRVAR(BZ2File_writelines__doc__, "writelines(sequence_of_strings) -> None\n\ \n\ Write the sequence of strings to the file. Note that newlines are not\n\ added. The sequence can be any iterable object producing strings. This is\n\ equivalent to calling write() for each string.\n\ "); /* This is a hacked version of Python's fileobject.c:file_writelines(). */ static PyObject * BZ2File_writelines(BZ2FileObject *self, PyObject *seq) { #define CHUNKSIZE 1000 PyObject *list = NULL; PyObject *iter = NULL; PyObject *ret = NULL; PyObject *line; int i, j, index, len, islist; int bzerror; ACQUIRE_LOCK(self); switch (self->mode) { case MODE_WRITE: break; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); goto error; default: PyErr_SetString(PyExc_IOError, "file is not ready for writing"); goto error; } islist = PyList_Check(seq); if (!islist) { iter = PyObject_GetIter(seq); if (iter == NULL) { PyErr_SetString(PyExc_TypeError, "writelines() requires an iterable argument"); goto error; } list = PyList_New(CHUNKSIZE); if (list == NULL) goto error; } /* Strategy: slurp CHUNKSIZE lines into a private list, checking that they are all strings, then write that list without holding the interpreter lock, then come back for more. */ for (index = 0; ; index += CHUNKSIZE) { if (islist) { Py_XDECREF(list); list = PyList_GetSlice(seq, index, index+CHUNKSIZE); if (list == NULL) goto error; j = PyList_GET_SIZE(list); } else { for (j = 0; j < CHUNKSIZE; j++) { line = PyIter_Next(iter); if (line == NULL) { if (PyErr_Occurred()) goto error; break; } PyList_SetItem(list, j, line); } } if (j == 0) break; /* Check that all entries are indeed strings. If not, apply the same rules as for file.write() and convert the rets to strings. This is slow, but seems to be the only way since all conversion APIs could potentially execute Python code. */ for (i = 0; i < j; i++) { PyObject *v = PyList_GET_ITEM(list, i); if (!PyString_Check(v)) { const char *buffer; Py_ssize_t len; if (PyObject_AsCharBuffer(v, &buffer, &len)) { PyErr_SetString(PyExc_TypeError, "writelines() " "argument must be " "a sequence of " "strings"); goto error; } line = PyString_FromStringAndSize(buffer, len); if (line == NULL) goto error; Py_DECREF(v); PyList_SET_ITEM(list, i, line); } } self->f_softspace = 0; /* Since we are releasing the global lock, the following code may *not* execute Python code. */ Py_BEGIN_ALLOW_THREADS for (i = 0; i < j; i++) { line = PyList_GET_ITEM(list, i); len = PyString_GET_SIZE(line); BZ2_bzWrite (&bzerror, self->fp, PyString_AS_STRING(line), len); if (bzerror != BZ_OK) { Py_BLOCK_THREADS Util_CatchBZ2Error(bzerror); goto error; } } Py_END_ALLOW_THREADS if (j < CHUNKSIZE) break; } Py_INCREF(Py_None); ret = Py_None; error: RELEASE_LOCK(self); Py_XDECREF(list); Py_XDECREF(iter); return ret; #undef CHUNKSIZE } PyDoc_STRVAR(BZ2File_seek__doc__, "seek(offset [, whence]) -> None\n\ \n\ Move to new file position. Argument offset is a byte count. Optional\n\ argument whence defaults to 0 (offset from start of file, offset\n\ should be >= 0); other values are 1 (move relative to current position,\n\ positive or negative), and 2 (move relative to end of file, usually\n\ negative, although many platforms allow seeking beyond the end of a file).\n\ \n\ Note that seeking of bz2 files is emulated, and depending on the parameters\n\ the operation may be extremely slow.\n\ "); static PyObject * BZ2File_seek(BZ2FileObject *self, PyObject *args) { int where = 0; PyObject *offobj; Py_off_t offset; char small_buffer[SMALLCHUNK]; char *buffer = small_buffer; size_t buffersize = SMALLCHUNK; Py_off_t bytesread = 0; size_t readsize;