Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53b2166

Browse files
committed
Merged revisions 81098 via svnmerge from
svn+ssh://[email protected]/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. ........
1 parent c09c92f commit 53b2166

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

Lib/test/test_zlib.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def test_speech128(self):
140140
for ob in x, bytearray(x):
141141
self.assertEqual(zlib.decompress(ob), data)
142142

143+
def test_incomplete_stream(self):
144+
# An useful error message is given
145+
x = zlib.compress(HAMLET_SCENE)
146+
self.assertRaisesRegexp(zlib.error,
147+
"Error -5 while decompressing data: incomplete or truncated stream",
148+
zlib.decompress, x[:-1])
149+
143150
# Memory use of the following functions takes into account overallocation
144151

145152
@precisionbigmemtest(size=_1G + 1024 * 1024, memuse=3)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ C-API
351351
Library
352352
-------
353353

354+
- Issue #8681: Make the zlib module's error messages more informative when
355+
the zlib itself doesn't give any detailed explanation.
356+
354357
- The audioop module now supports sound fragments of length greater
355358
than 2**31 bytes on 64-bit machines, and is PY_SSIZE_T_CLEAN.
356359

Modules/zlibmodule.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,24 @@ typedef struct
5252
static void
5353
zlib_error(z_stream zst, int err, char *msg)
5454
{
55-
if (zst.msg == Z_NULL)
55+
const char *zmsg = zst.msg;
56+
if (zmsg == Z_NULL) {
57+
switch (err) {
58+
case Z_BUF_ERROR:
59+
zmsg = "incomplete or truncated stream";
60+
break;
61+
case Z_STREAM_ERROR:
62+
zmsg = "inconsistent stream state";
63+
break;
64+
case Z_DATA_ERROR:
65+
zmsg = "invalid input data";
66+
break;
67+
}
68+
}
69+
if (zmsg == Z_NULL)
5670
PyErr_Format(ZlibError, "Error %d %s", err, msg);
5771
else
58-
PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
72+
PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
5973
}
6074

6175
PyDoc_STRVAR(compressobj__doc__,
@@ -241,8 +255,7 @@ PyZlib_decompress(PyObject *self, PyObject *args)
241255
* process the inflate call() due to an error in the data.
242256
*/
243257
if (zst.avail_out > 0) {
244-
PyErr_Format(ZlibError, "Error %i while decompressing data",
245-
err);
258+
zlib_error(zst, err, "while decompressing data");
246259
inflateEnd(&zst);
247260
goto error;
248261
}

0 commit comments

Comments
 (0)