]> granicus.if.org Git - python/commitdiff
Issue #8681: Make the zlib module's error messages more informative when
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 11 May 2010 23:42:28 +0000 (23:42 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 11 May 2010 23:42:28 +0000 (23:42 +0000)
the zlib itself doesn't give any detailed explanation.

Lib/test/test_zlib.py
Misc/NEWS
Modules/zlibmodule.c

index 2fc43139a2f6d0ee6f4f184ce9a207e5598b8e5c..cbc3ed498269d471edacf569db4bc56be785eb30 100644 (file)
@@ -135,6 +135,13 @@ 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)
+        self.assertRaisesRegexp(zlib.error,
+            "Error -5 while decompressing data: incomplete or truncated stream",
+            zlib.decompress, x[:-1])
+
     # Memory use of the following functions takes into account overallocation
 
     @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=3)
index e46162d01699866d3133f022fd28ea2f9a71dc3a..803e0603db597f0a0fb9ec34b3880fa1af56df15 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -62,6 +62,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 #8571: Fix an internal error when compressing or decompressing a
   chunk larger than 1GB with the zlib module's compressor and decompressor
   objects.
index bb52fdc5c5110217aebd9cf1e98ccffee091ce7e..183f01e91a15377272f8622ecca500feec5de9a4 100644 (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;
             }