Patch #1435422: zlib's compress and decompress objects now have a

copy() method.
This commit is contained in:
Georg Brandl 2006-05-16 07:38:27 +00:00
parent 5f5d99c215
commit 8d3342b489
4 changed files with 175 additions and 0 deletions

View file

@ -653,6 +653,104 @@ PyZlib_flush(compobject *self, PyObject *args)
return RetVal;
}
PyDoc_STRVAR(comp_copy__doc__,
"copy() -- Return a copy of the compression object.");
static PyObject *
PyZlib_copy(compobject *self)
{
compobject *retval = NULL;
int err;
retval = newcompobject(&Comptype);
if (!retval) return NULL;
/* Copy the zstream state
* We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
*/
ENTER_ZLIB
err = deflateCopy(&retval->zst, &self->zst);
switch(err) {
case(Z_OK):
break;
case(Z_STREAM_ERROR):
PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
goto error;
case(Z_MEM_ERROR):
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory for compression object");
goto error;
default:
zlib_error(self->zst, err, "while copying compression object");
goto error;
}
retval->unused_data = self->unused_data;
retval->unconsumed_tail = self->unconsumed_tail;
Py_INCREF(retval->unused_data);
Py_INCREF(retval->unconsumed_tail);
/* Mark it as being initialized */
retval->is_initialised = 1;
LEAVE_ZLIB
return (PyObject *)retval;
error:
LEAVE_ZLIB
Py_XDECREF(retval);
return NULL;
}
PyDoc_STRVAR(decomp_copy__doc__,
"copy() -- Return a copy of the decompression object.");
static PyObject *
PyZlib_uncopy(compobject *self)
{
compobject *retval = NULL;
int err;
retval = newcompobject(&Decomptype);
if (!retval) return NULL;
/* Copy the zstream state
* We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
*/
ENTER_ZLIB
err = inflateCopy(&retval->zst, &self->zst);
switch(err) {
case(Z_OK):
break;
case(Z_STREAM_ERROR):
PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
goto error;
case(Z_MEM_ERROR):
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory for decompression object");
goto error;
default:
zlib_error(self->zst, err, "while copying decompression object");
goto error;
}
retval->unused_data = self->unused_data;
retval->unconsumed_tail = self->unconsumed_tail;
Py_INCREF(retval->unused_data);
Py_INCREF(retval->unconsumed_tail);
/* Mark it as being initialized */
retval->is_initialised = 1;
LEAVE_ZLIB
return (PyObject *)retval;
error:
LEAVE_ZLIB
Py_XDECREF(retval);
return NULL;
}
PyDoc_STRVAR(decomp_flush__doc__,
"flush( [length] ) -- Return a string containing any remaining\n"
"decompressed data. length, if given, is the initial size of the\n"
@ -725,6 +823,8 @@ static PyMethodDef comp_methods[] =
comp_compress__doc__},
{"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
comp_flush__doc__},
{"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
comp_copy__doc__},
{NULL, NULL}
};
@ -734,6 +834,8 @@ static PyMethodDef Decomp_methods[] =
decomp_decompress__doc__},
{"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
decomp_flush__doc__},
{"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
decomp_copy__doc__},
{NULL, NULL}
};