Skip to content

gh-129640: Add tests for reference cycle in gzip.GzipFile #130916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
19 changes: 19 additions & 0 deletions Lib/test/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,25 @@ def sizes():
self.assertEqual(f.read(100), b'')
self.assertEqual(nread, len(uncompressed))

def test_no_refloop_with_writestream(self):
"""GH-129640 and GH-129726: Test that the _WriteBufferStream reference cycle method prevents reference loops."""
import gc

gc.collect()

before_count = len(gc.get_objects())

with io.BytesIO() as bio:
with gzip.GzipFile(fileobj=bio, mode="wb") as f:
f.write(b"test data")

gc.collect(0)

# Check that all objects were properly cleaned up
after_count = len(gc.get_objects())
self.assertLess(after_count - before_count, 10,
"Too many objects remain after GzipFile cleanup")

def test_textio_readlines(self):
# Issue #10791: TextIOWrapper.readlines() fails when wrapping GzipFile.
lines = (data1 * 50).decode("ascii").splitlines(keepends=True)
Expand Down
Loading