Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def seekable(self):
def writable(self):
return True

def __del__(self):
del self.gzip_file


class GzipFile(_compression.BaseStream):
"""The GzipFile class simulates most of the methods of a file object with
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,25 @@ def sizes():
self.assertEqual(f.read(100), b'')
self.assertEqual(nread, len(uncompressed))

def test_no_refloop_with_writestream(self):
"""Test that the _WriteBufferStream __del__ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a reference cycle in ``gzip.GzipFile`` when writing to a file.
Loading