Anonymous avatar Anonymous committed 6a45567

Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
attribute when called without a max_length argument.

Comments (0)

Files changed (3)

Lib/test/test_zlib.py

         self.assertRaises(ValueError, dco.decompress, b"", -1)
         self.assertEqual(b'', dco.unconsumed_tail)
 
+    def test_clear_unconsumed_tail(self):
+        # Issue #12050: calling decompress() without providing max_length
+        # should clear the unconsumed_tail attribute.
+        cdata = b"x\x9cKLJ\x06\x00\x02M\x01"    # "abc"
+        dco = zlib.decompressobj()
+        ddata = dco.decompress(cdata, 1)
+        ddata += dco.decompress(dco.unconsumed_tail)
+        self.assertEqual(dco.unconsumed_tail, b"")
+
     def test_flushes(self):
         # Test flush() with the various options, using all the
         # different levels in order to provide more variations.
 Library
 -------
 
+- Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
+  attribute when called without a max_length argument.
+
 - Issue #12062: Fix a flushing bug when doing a certain type of I/O sequence
   on a file opened in read+write mode (namely: reading, seeking a bit forward,
   writing, then seeking before the previous write but still within buffered

Modules/zlibmodule.c

         Py_END_ALLOW_THREADS
     }
 
-    /* Not all of the compressed data could be accommodated in the output buffer
-       of specified size. Return the unconsumed tail in an attribute.*/
     if(max_length) {
+        /* Not all of the compressed data could be accommodated in a buffer of
+           the specified size. Return the unconsumed tail in an attribute. */
         Py_DECREF(self->unconsumed_tail);
         self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
                                                            self->zst.avail_in);
-        if(!self->unconsumed_tail) {
-            Py_DECREF(RetVal);
-            RetVal = NULL;
-            goto error;
-        }
+    }
+    else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
+        /* All of the compressed data was consumed. Clear unconsumed_tail. */
+        Py_DECREF(self->unconsumed_tail);
+        self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
+    }
+    if (self->unconsumed_tail == NULL) {
+        Py_DECREF(RetVal);
+        RetVal = NULL;
+        goto error;
     }
 
     /* The end of the compressed data has been reached, so set the
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.