https://github.com/python/cpython/commit/8122ff4818f418e8ae984cf6964215689b00808d
commit: 8122ff4818f418e8ae984cf6964215689b00808d
branch: main
author: GruffElixir <[email protected]>
committer: StanFromIreland <[email protected]>
date: 2026-09-26T11:48:13+01:00
summary:

gh-158169: Clear `zlib.Decompress.unconsumed_tail` at end of stream (#158177)

Co-authored-by: Stan Ulbrych <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-09-25-22-15-54.gh-issue-158169.ZlibTail.rst
M Lib/test/test_zlib.py
M Modules/zlibmodule.c

diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index 205916579b5f49..dcb4eac1b77952 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -822,6 +822,18 @@ def test_decompress_unused_data(self):
                 self.assertEqual(dco.unconsumed_tail, b'')
                 self.assertEqual(dco.unused_data, remainder)
 
+    def test_decompress_unconsumed_tail_after_eof(self):
+        source = b'abcdefghijklmnopqrstuvwxyz'
+        remainder = b'0123456789'
+        dco = zlib.decompressobj()
+        data = dco.decompress(zlib.compress(source) + remainder, 1)
+        data += dco.decompress(dco.unconsumed_tail)
+
+        self.assertTrue(dco.eof)
+        self.assertEqual(data, source)
+        self.assertEqual(dco.unconsumed_tail, b'')
+        self.assertEqual(dco.unused_data, remainder)
+
     # issue27164
     def test_decompress_raw_with_dictionary(self):
         zdict = b'abcdefghijklmnopqrstuvwxyz'
diff --git 
a/Misc/NEWS.d/next/Library/2026-09-25-22-15-54.gh-issue-158169.ZlibTail.rst 
b/Misc/NEWS.d/next/Library/2026-09-25-22-15-54.gh-issue-158169.ZlibTail.rst
new file mode 100644
index 00000000000000..7c965451c056a4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-09-25-22-15-54.gh-issue-158169.ZlibTail.rst
@@ -0,0 +1,2 @@
+Fix :attr:`zlib.Decompress.unconsumed_tail` duplicating
+:attr:`~zlib.Decompress.unused_data` after end of stream when *max_length* was 
used.
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 56c119db7d10c0..31533fa047af0b 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -838,6 +838,7 @@ save_unconsumed_input(compobject *self, Py_buffer *data, 
int err)
                 return -1;
             }
             Py_SETREF(self->unused_data, new_unused_data);
+            self->zst.next_in += left_size;
             self->zst.avail_in = 0;
         }
     }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to