[Python-checkins] r81100 - in python/branches/py3k: Lib/test/test_zlib.py Misc/NEWS Modules/zlibmodule.c

antoine.pitrou python-checkins at python.org
Wed May 12 01:46:03 CEST 2010


Author: antoine.pitrou
Date: Wed May 12 01:46:02 2010
New Revision: 81100

Log:
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.
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Lib/test/test_zlib.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/zlibmodule.c

Modified: python/branches/py3k/Lib/test/test_zlib.py
==============================================================================
--- python/branches/py3k/Lib/test/test_zlib.py	(original)
+++ python/branches/py3k/Lib/test/test_zlib.py	Wed May 12 01:46:02 2010
@@ -140,6 +140,13 @@
         for ob in x, bytearray(x):
             self.assertEqual(zlib.decompress(ob), 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)

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed May 12 01:46:02 2010
@@ -351,6 +351,9 @@
 Library
 -------
 
+- Issue #8681: Make the zlib module's error messages more informative when
+  the zlib itself doesn't give any detailed explanation.
+
 - The audioop module now supports sound fragments of length greater
   than 2**31 bytes on 64-bit machines, and is PY_SSIZE_T_CLEAN.
 

Modified: python/branches/py3k/Modules/zlibmodule.c
==============================================================================
--- python/branches/py3k/Modules/zlibmodule.c	(original)
+++ python/branches/py3k/Modules/zlibmodule.c	Wed May 12 01:46:02 2010
@@ -52,10 +52,24 @@
 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__,
@@ -241,8 +255,7 @@
              * 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;
             }


More information about the Python-checkins mailing list