mirror of
https://github.com/python/cpython.git
synced 2025-11-11 14:44:57 +00:00
Add zlib_error() helper.
It sets a ZlibError exception, using the msg from the z_stream pointer if one is available.
This commit is contained in:
parent
2c40adb1e4
commit
0965e084cd
1 changed files with 25 additions and 92 deletions
|
|
@ -82,6 +82,15 @@ typedef struct
|
||||||
int is_initialised;
|
int is_initialised;
|
||||||
} compobject;
|
} compobject;
|
||||||
|
|
||||||
|
static void
|
||||||
|
zlib_error(z_stream zst, int err, char *msg)
|
||||||
|
{
|
||||||
|
if (zst.msg == Z_NULL)
|
||||||
|
PyErr_Format(ZlibError, "Error %d %s", err, msg);
|
||||||
|
else
|
||||||
|
PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
|
||||||
|
}
|
||||||
|
|
||||||
static char compressobj__doc__[] =
|
static char compressobj__doc__[] =
|
||||||
"compressobj() -- Return a compressor object.\n"
|
"compressobj() -- Return a compressor object.\n"
|
||||||
"compressobj(level) -- Return a compressor object, using the given compression level.\n"
|
"compressobj(level) -- Return a compressor object, using the given compression level.\n"
|
||||||
|
|
@ -175,40 +184,21 @@ PyZlib_compress(PyObject *self, PyObject *args)
|
||||||
"Bad compression level");
|
"Bad compression level");
|
||||||
return_error = 1;
|
return_error = 1;
|
||||||
break;
|
break;
|
||||||
default: {
|
default:
|
||||||
if (zst.msg == Z_NULL)
|
|
||||||
PyErr_Format(ZlibError, "Error %i while compressing data",
|
|
||||||
err);
|
|
||||||
else
|
|
||||||
PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
|
|
||||||
err, zst.msg);
|
|
||||||
deflateEnd(&zst);
|
deflateEnd(&zst);
|
||||||
|
zlib_error(zst, err, "while compressing data");
|
||||||
return_error = 1;
|
return_error = 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!return_error) {
|
if (!return_error) {
|
||||||
Py_BEGIN_ALLOW_THREADS;
|
Py_BEGIN_ALLOW_THREADS;
|
||||||
err=deflate(&zst, Z_FINISH);
|
err = deflate(&zst, Z_FINISH);
|
||||||
Py_END_ALLOW_THREADS;
|
Py_END_ALLOW_THREADS;
|
||||||
|
|
||||||
switch(err)
|
if (err != Z_STREAM_END) {
|
||||||
{
|
zlib_error(zst, err, "while compressing data");
|
||||||
case(Z_STREAM_END):
|
|
||||||
break;
|
|
||||||
/* Are there other errors to be trapped here? */
|
|
||||||
default: {
|
|
||||||
if (zst.msg == Z_NULL)
|
|
||||||
PyErr_Format(ZlibError, "Error %i while compressing data",
|
|
||||||
err);
|
|
||||||
else
|
|
||||||
PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
|
|
||||||
err, zst.msg);
|
|
||||||
|
|
||||||
deflateEnd(&zst);
|
deflateEnd(&zst);
|
||||||
|
|
||||||
return_error = 1;
|
return_error = 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!return_error) {
|
if (!return_error) {
|
||||||
|
|
@ -216,16 +206,8 @@ PyZlib_compress(PyObject *self, PyObject *args)
|
||||||
if (err == Z_OK)
|
if (err == Z_OK)
|
||||||
ReturnVal = PyString_FromStringAndSize((char *)output,
|
ReturnVal = PyString_FromStringAndSize((char *)output,
|
||||||
zst.total_out);
|
zst.total_out);
|
||||||
else {
|
else
|
||||||
if (zst.msg == Z_NULL)
|
zlib_error(zst, err, "while finishing compression");
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while finishing compression",
|
|
||||||
err);
|
|
||||||
else
|
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while finishing compression: %.200s",
|
|
||||||
err, zst.msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -286,18 +268,10 @@ PyZlib_decompress(PyObject *self, PyObject *args)
|
||||||
PyErr_SetString(PyExc_MemoryError,
|
PyErr_SetString(PyExc_MemoryError,
|
||||||
"Out of memory while decompressing data");
|
"Out of memory while decompressing data");
|
||||||
return_error = 1;
|
return_error = 1;
|
||||||
default: {
|
default:
|
||||||
if (zst.msg == Z_NULL)
|
|
||||||
PyErr_Format(ZlibError, "Error %i preparing to decompress data",
|
|
||||||
err);
|
|
||||||
else
|
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while preparing to decompress data: %.200s",
|
|
||||||
err, zst.msg);
|
|
||||||
inflateEnd(&zst);
|
inflateEnd(&zst);
|
||||||
|
zlib_error(zst, err, "while preparing to decompress data");
|
||||||
return_error = 1;
|
return_error = 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
@ -317,8 +291,7 @@ PyZlib_decompress(PyObject *self, PyObject *args)
|
||||||
* and we get this error, assume that it means zlib cannot
|
* and we get this error, assume that it means zlib cannot
|
||||||
* process the inflate call() due to an error in the data.
|
* process the inflate call() due to an error in the data.
|
||||||
*/
|
*/
|
||||||
if (zst.avail_out > 0)
|
if (zst.avail_out > 0) {
|
||||||
{
|
|
||||||
PyErr_Format(ZlibError, "Error %i while decompressing data",
|
PyErr_Format(ZlibError, "Error %i while decompressing data",
|
||||||
err);
|
err);
|
||||||
inflateEnd(&zst);
|
inflateEnd(&zst);
|
||||||
|
|
@ -338,33 +311,17 @@ PyZlib_decompress(PyObject *self, PyObject *args)
|
||||||
zst.avail_out = r_strlen;
|
zst.avail_out = r_strlen;
|
||||||
r_strlen = r_strlen << 1;
|
r_strlen = r_strlen << 1;
|
||||||
break;
|
break;
|
||||||
default: {
|
default:
|
||||||
if (zst.msg == Z_NULL)
|
|
||||||
PyErr_Format(ZlibError, "Error %i while decompressing data",
|
|
||||||
err);
|
|
||||||
else
|
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while decompressing data: %.200s",
|
|
||||||
err, zst.msg);
|
|
||||||
inflateEnd(&zst);
|
inflateEnd(&zst);
|
||||||
|
zlib_error(zst, err, "while decompressing data");
|
||||||
return_error = 1;
|
return_error = 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} while (err != Z_STREAM_END);
|
} while (err != Z_STREAM_END);
|
||||||
|
|
||||||
if (!return_error) {
|
if (!return_error) {
|
||||||
err = inflateEnd(&zst);
|
err = inflateEnd(&zst);
|
||||||
if (err != Z_OK) {
|
if (err != Z_OK) {
|
||||||
if (zst.msg == Z_NULL)
|
zlib_error(zst, err, "while finishing data decompression");
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while finishing data decompression",
|
|
||||||
err);
|
|
||||||
else
|
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while finishing data decompression: %.200s",
|
|
||||||
err, zst.msg);
|
|
||||||
|
|
||||||
return_error = 1;
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -410,18 +367,9 @@ PyZlib_compressobj(PyObject *selfptr, PyObject *args)
|
||||||
"Invalid initialization option");
|
"Invalid initialization option");
|
||||||
return NULL;
|
return NULL;
|
||||||
default:
|
default:
|
||||||
{
|
zlib_error(self->zst, err, "while creating compression object");
|
||||||
if (self->zst.msg == Z_NULL)
|
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while creating compression object",
|
|
||||||
err);
|
|
||||||
else
|
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while creating compression object: %.200s",
|
|
||||||
err, self->zst.msg);
|
|
||||||
Py_DECREF(self);
|
Py_DECREF(self);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -455,18 +403,9 @@ PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
|
||||||
"Can't allocate memory for decompression object");
|
"Can't allocate memory for decompression object");
|
||||||
return NULL;
|
return NULL;
|
||||||
default:
|
default:
|
||||||
{
|
zlib_error(self->zst, err, "while creating decompression object");
|
||||||
if (self->zst.msg == Z_NULL)
|
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while creating decompression object",
|
|
||||||
err);
|
|
||||||
else
|
|
||||||
PyErr_Format(ZlibError,
|
|
||||||
"Error %i while creating decompression object: %.200s",
|
|
||||||
err, self->zst.msg);
|
|
||||||
Py_DECREF(self);
|
Py_DECREF(self);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -851,13 +790,7 @@ PyZlib_unflush(compobject *self, PyObject *args)
|
||||||
|
|
||||||
err = inflateEnd(&(self->zst));
|
err = inflateEnd(&(self->zst));
|
||||||
if (err != Z_OK) {
|
if (err != Z_OK) {
|
||||||
if (self->zst.msg == Z_NULL)
|
zlib_error(self->zst, err, "from inflateEnd()");
|
||||||
PyErr_Format(ZlibError, "Error %i from inflateEnd()",
|
|
||||||
err);
|
|
||||||
else
|
|
||||||
PyErr_Format(ZlibError, "Error %i from inflateEnd(): %.200s",
|
|
||||||
err, self->zst.msg);
|
|
||||||
|
|
||||||
retval = NULL;
|
retval = NULL;
|
||||||
} else {
|
} else {
|
||||||
self->is_initialised = 0;
|
self->is_initialised = 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue