Merged revisions 81098 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r81098 | antoine.pitrou | 2010-05-12 01:42:28 +0200 (mer., 12 mai 2010) | 5 lines

  Issue #8681: Make the zlib module's error messages more informative when
  the zlib itself doesn't give any detailed explanation.
........
This commit is contained in:
Antoine Pitrou 2010-05-11 23:45:55 +00:00
parent 33c5813a59
commit ca561f5e72
3 changed files with 32 additions and 4 deletions

View File

@ -134,6 +134,18 @@ class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
x = zlib.compress(data)
self.assertEqual(zlib.decompress(x), data)
def test_incomplete_stream(self):
# An useful error message is given
x = zlib.compress(HAMLET_SCENE)
try:
zlib.decompress(x[:-1])
except zlib.error as e:
self.assertTrue(
"Error -5 while decompressing data: incomplete or truncated stream"
in str(e), str(e))
else:
self.fail("zlib.error not raised")
# Memory use of the following functions takes into account overallocation
@precisionbigmemtest(size=_1G + 1024 * 1024, memuse=3)

View File

@ -39,6 +39,9 @@ Core and Builtins
Library
-------
- Issue #8681: Make the zlib module's error messages more informative when
the zlib itself doesn't give any detailed explanation.
- Issue #8674: Fixed a number of incorrect or undefined-behaviour-inducing
overflow checks in the audioop module.

View File

@ -72,10 +72,24 @@ typedef struct
static void
zlib_error(z_stream zst, int err, char *msg)
{
if (zst.msg == Z_NULL)
const char *zmsg = zst.msg;
if (zmsg == Z_NULL) {
switch (err) {
case Z_BUF_ERROR:
zmsg = "incomplete or truncated stream";
break;
case Z_STREAM_ERROR:
zmsg = "inconsistent stream state";
break;
case Z_DATA_ERROR:
zmsg = "invalid input data";
break;
}
}
if (zmsg == Z_NULL)
PyErr_Format(ZlibError, "Error %d %s", err, msg);
else
PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
}
PyDoc_STRVAR(compressobj__doc__,
@ -248,8 +262,7 @@ PyZlib_decompress(PyObject *self, PyObject *args)
* process the inflate call() due to an error in the data.
*/
if (zst.avail_out > 0) {
PyErr_Format(ZlibError, "Error %i while decompressing data",
err);
zlib_error(zst, err, "while decompressing data");
inflateEnd(&zst);
goto error;
}