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
10 changes: 9 additions & 1 deletion Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from test.support import (
findfile, requires_zlib, requires_bz2, requires_lzma,
requires_zstd, captured_stdout, captured_stderr, requires_subprocess,
cpython_only, gc_collect
cpython_only, gc_collect, catch_unraisable_exception
)
from test.support.os_helper import (
TESTFN, unlink, rmtree, temp_dir, temp_cwd, fd_count, FakePath,
Expand Down Expand Up @@ -4095,6 +4095,14 @@ def test_garbage_collection(self):
zf.close()
del zf

# ResourceWarning escalated to an error is reported as unraisable.
zf = zipfile.ZipFile(io.BytesIO(), "w")
with (warnings.catch_warnings(action="error", category=ResourceWarning),
catch_unraisable_exception() as cm):
del zf
gc_collect()
self.assertIs(cm.unraisable.exc_type, ResourceWarning)

def test_unsupported_version(self):
# File has an extract_version of 120
data = (b'PK\x03\x04x\x00\x00\x00\x00\x00!p\xa1@\x00\x00\x00\x00\x00\x00'
Expand Down
2 changes: 1 addition & 1 deletion Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
import threading
import time
lazy import warnings
import warnings # must stay eager to support finalization

try:
import zlib # We may need its compression method
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`zipfile` now imports :mod:`warnings` eagerly so that a writable
:class:`~zipfile.ZipFile` left unclosed at interpreter shutdown can emit its
:exc:`ResourceWarning` and be closed instead of failing with an
:exc:`ImportError`.
Loading