diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index 708db4d4387df5e..037433df2cc4534 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -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, @@ -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' diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py index d817bdd8769e7d7..ff2e3ed438fc5b0 100644 --- a/Lib/zipfile/__init__.py +++ b/Lib/zipfile/__init__.py @@ -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 diff --git a/Misc/NEWS.d/next/Library/2026-10-14-14-47-05.gh-issue-81954.KvM0gY.rst b/Misc/NEWS.d/next/Library/2026-10-14-14-47-05.gh-issue-81954.KvM0gY.rst new file mode 100644 index 000000000000000..b8f4693029b488d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-14-14-47-05.gh-issue-81954.KvM0gY.rst @@ -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`.