8000 gh-129726: Break `gzip.GzipFile` reference loop by cmaloney · Pull Request #130055 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-129726: Break gzip.GzipFile reference loop #130055

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

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
PEP-8 import order, move to weakref.ref
  • Loading branch information
cmaloney committed Feb 28, 2025
commit 194cf3f1f7efb7b6e62c692a8cb5b20f40a277e1
18 changes: 12 additions & 6 deletions Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

# based on Andrew Kuchling's minigzip.py distributed with the zlib module

import struct, sys, time, os
import zlib
import _compression
import builtins
import io
import _compression
import os
import struct
import sys
import time
import weakref
Copy link
Contributor Author

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.

Copy link
Member

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.

import zlib

__all__ = ["BadGzipFile", "GzipFile", "open", "compress", "decompress"]

Expand Down Expand Up @@ -126,10 +129,13 @@ class BadGzipFile(OSError):
class _WriteBufferStream(io.RawIOBase):
"""Minimal object to pass WriteBuffer flushes into GzipFile"""
def __init__(self, gzip_file):
self.gzip_file = gzip_file
self.gzip_file = weakref.ref(gzip_file)

def write(self, data):
return self.gzip_file._write_raw(data)
gzip_file = self.gzip_file()
if gzip_file is None:
raise RuntimeError("lost gzip_file")
return gzip_file._write_raw(data)

def seekable(self):
return False
Expand Down Expand Up @@ -227,7 +233,7 @@ def __init__(self, filename=None, mode=None,
0)
self._write_mtime = mtime
self._buffer_size = _WRITE_BUFFER_SIZE
write_wrap = _WriteBufferStream(weakref.proxy(self))
write_wrap = _WriteBufferStream(self)
self._buffer = io.BufferedWriter(write_wrap,
buffer_size=self._buffer_size)
else:
Expand Down
Loading
0