Skip to content

Commit 79b7f6c

Browse files
committed
test: Add test for reference cycle in gzip.GzipFile
1 parent 06999db commit 79b7f6c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Lib/test/test_gzip.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,25 @@ def sizes():
467467
self.assertEqual(f.read(100), b'')
468468
self.assertEqual(nread, len(uncompressed))
469469

470+
def test_no_refloop_with_writestream(self):
471+
"""Test that the _WriteBufferStream __del__ method prevents reference loops."""
472+
import gc
473+
474+
gc.collect()
475+
476+
before_count = len(gc.get_objects())
477+
478+
with io.BytesIO() as bio:
479+
with gzip.GzipFile(fileobj=bio, mode="wb") as f:
480+
f.write(b"test data")
481+
482+
gc.collect(0)
483+
484+
# Check that all objects were properly cleaned up
485+
after_count = len(gc.get_objects())
486+
self.assertLess(after_count - before_count, 10,
487+
"Too many objects remain after GzipFile cleanup")
488+
470489
def test_textio_readlines(self):
471490
# Issue #10791: TextIOWrapper.readlines() fails when wrapping GzipFile.
472491
lines = (data1 * 50).decode("ascii").splitlines(keepends=True)

0 commit comments

Comments
 (0)