Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 1 addition & 2 deletions Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import struct
import sys
import time
import warnings # must stay eager to support finalization
import weakref
import zlib
from compression._common import _streams
Expand Down Expand Up @@ -229,7 +230,6 @@ def __init__(self, filename=None, mode=None,

elif mode.startswith(('w', 'a', 'x')):
if origmode is None:
import warnings
warnings.warn(
"GzipFile was opened for writing, but this will "
"change in future Python releases. "
Expand Down Expand Up @@ -468,7 +468,6 @@ def readline(self, size=-1):

def __del__(self):
if self.mode == WRITE and not self.closed:
import warnings
warnings.warn("unclosed GzipFile",
ResourceWarning, source=self, stacklevel=2)

Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,17 @@ def test_refloop_unraisable(self):
gc.collect()
self.assertIsNone(cm.unraisable)

@requires_subprocess()
def test_unclosed_at_shutdown(self):
rc, out, err = assert_python_ok("-c", f"""if 1:
import gzip
f = gzip.GzipFile({self.filename!r}, "wb")
f.write({data1!r})
""")
self.assertNotIn(b"Exception ignored", err)
with gzip.GzipFile(self.filename) as f:
self.assertEqual(f.read(), data1)


class TestOpen(BaseTest):
def test_binary_modes(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`gzip` now imports :mod:`warnings` eagerly so that a writable
:class:`~gzip.GzipFile` left unclosed at interpreter shutdown can emit its
:exc:`ResourceWarning` and be closed instead of failing with an
:exc:`ImportError`.
Loading