Repository navigation
gh-81954: Eagerly import warnings module to failure of ZipFile.__del__ during finalization #159419
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
base: main
Are you sure you want to change the base?
Changes from all commits
e48aea1
29ef754
9c41b95
48693fb
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 |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ | |
| import sys | ||
| import threading | ||
| import time | ||
| lazy import warnings | ||
| import warnings # must stay eager to support finalization | ||
|
Member
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. You reverted I suggest using the same code pattern than def __del__(self, _warn=warnings.warn):
"""Call the "close()" method in case the user forgot."""
# gh-81954: Warn if writable ZipFile is implicitly closed.
# GC cleanup order is non-deterministic and can result in data loss.
try:
if self.fp is not None and self.mode in ('w', 'x', 'a'):
_warn(f"unclosed ZipFile {self!r}",
ResourceWarning, source=self, stacklevel=2)
finally:
self.close() |
||
|
|
||
| try: | ||
| import zlib # We may need its compression method | ||
|
|
||
| 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`. | ||
|
Member
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. You should revert your |
||
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.
Oh. You simplified the test too much, it doesn't test that the zipfile was written properly. For example, it doesn't catch a now obvious error: close() is not called if ResourceWarning is treated as an error (raise an exception). I suggest to rewrite the test as: