-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
gh-129726: Break gzip.GzipFile
reference loop
#130055
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import builtins | ||
import io | ||
import _compression | ||
import weakref | ||
|
||
__all__ = ["BadGzipFile", "GzipFile", "open", "compress", "decompress"] | ||
|
||
|
@@ -226,7 +227,8 @@ def __init__(self, filename=None, mode=None, | |
0) | ||
self._write_mtime = mtime | ||
self._buffer_size = _WRITE_BUFFER_SIZE | ||
self._buffer = io.BufferedWriter(_WriteBufferStream(self), | ||
write_wrap = _WriteBufferStream(weakref.proxy(self)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to modify _WriteBufferStream to use a simpler weakref.ref() to store gzip_file instead? This object is private so we can change its API. Something like: class _WriteBufferStream(io.RawIOBase):
"""Minimal object to pass WriteBuffer flushes into GzipFile"""
def __init__(self, gzip_file):
self.gzip_file = weakref.ref(gzip_file)
def write(self, data):
gzip_file = self.gzip_file()
if gzip_file is None:
raise RuntimeError("lost gzip_file")
return gzip_file._write_raw(data) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to your suggestion here. I Spent a bit of time trying to remove |
||
self._buffer = io.BufferedWriter(write_wrap, | ||
buffer_size=self._buffer_size) | ||
else: | ||
raise ValueError("Invalid mode: {!r}".format(mode)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fix :class:`gzip.GzipFile` raising an unraisable exception during garbage | ||
collection when referring to a temporary object by breaking the reference | ||
loop with :mod:`weakref`. |
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.
the ordering of imports here is painful to me, but resisted bringing to PEP8 because this fix needs backporting.
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.
Please reformat these imports for PEP 8 :-) I will handle merge conflicts on backports if needed.