GeezerVison is showing github.com — gh-159999: Fix needs_input for bz2 and zlib decompressors by iamsaugatpandey · Pull Request #160696 · python/cpython
Conversation
iamsaugatpandey
commented
Oct 26, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
BZ2Decompressor and zlib._ZlibDecompressor had a bug where, if the input ran out right as the output buffer filled up, needs_input was set to true even though there was still decompressed data waiting. Callers would go looking for more input when they should have been draining the buffer.
While in there, I found a related problem in bz2’s decompress_buf(). It checked for exhausted input before checking for a full output buffer, so even with max_length=-1 it could hand back just the first 32 KiB of a block’s output. I reordered the checks.
Both fixes follow what was done for lzma in bpo-21872 (GH-14048).
One knock-on effect: gzip._GzipReader was relying on the old behavior. It raised EOFError whenever decompress(b"") returned nothing, but that can now legitimately happen on a valid stream. It now only raises when reading the file gives back no data, which is what DecompressReader already does.
Compatibility note: if anyone copied gzip’s old loop for use with BZ2Decompressor, they could see the same spurious EOFError. lzma has worked this way since 3.9, so this just makes bz2 and zlib consistent with it.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
BZ2Decompressor and zlib._ZlibDecompressor had a bug where, if the input
ran out right as the output buffer filled up, needs_input was set to
true even though there was still decompressed data waiting. Callers
would go looking for more input when they should have been draining the
buffer.
While in there, I found a related problem in bz2’s decompress_buf(). It
checked for exhausted input before checking for a full output buffer,
so even with max_length=-1 it could hand back just the first 32 KiB of
a block’s output. I reordered the checks.
Both fixes follow what was done for lzma in bpo-21872 (pythonGH-14048).
One knock-on effect: gzip._GzipReader was relying on the old behavior.
It raised EOFError whenever decompress(b"") returned nothing, but that
can now legitimately happen on a valid stream. It now only raises when
reading the file gives back no data, which is what DecompressReader
already does.
Compatibility note: if anyone copied gzip’s old loop for use with
BZ2Decompressor, they could see the same spurious EOFError. lzma has
worked this way since 3.9, so this just makes bz2 and zlib consistent
with it.
|
All commit authors signed the Contributor License Agreement.

|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Gentle ping: this is ready for review whenever someone has time. All checks pass and the CLA is signed.
A quick summary for reviewers:
Minimal reproducer (prints True before the fix, False after):
import bz2
d = bz2.BZ2Decompressor()
d.decompress(bz2.compress(b"\x00\x10 0@" * 2)[:40], max_length=0)
print(d.needs_input) # but d.decompress(b"") still returns data
I'm happy to make any changes.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
StanFromIreland
Awaiting requested review from StanFromIreland
StanFromIreland is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|