GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-81954: Eagerly import warnings module to failure of ZipFile.__del__ during finalization by brittanyrey · Pull Request #159419 · python/cpython

Conversation

@brittanyrey

@brittanyrey brittanyrey commented Oct 14, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

BUG: Now that ZipFile delivers ResourceWarnings during __del__ calls, if there was an issue lazily loading warnings during finalization, the close() call was missed entirely. This left the file in an unexpected state and introduced a possibility of data loss that didn't exist previously.

FIX: Eager import warnings

  • Issue: ValueError: I/O operation on closed file. in ZipFile destructor #81954

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@brittanyrey
Always close ZipFile in __del__ even if the warning fails
e48aea1
ZipFile.__del__ emitted its ResourceWarning before calling close().
When warnings.warn() raised, close() was skipped and the central
directory of a writable archive was never written, leaving a corrupt
file. This happens at interpreter shutdown, where resolving the lazy
"warnings" import raises ImportError, and with -W
error::ResourceWarning at any time.

Call close() in a finally block.
@bedevere-app bedevere-app Bot mentioned this pull request Oct 14, 2026
ValueError: I/O operation on closed file. in ZipFile destructor #81954
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Oct 14, 2026
@brittanyrey
brittanyrey requested review from cmaloney and vstinner October 14, 2026 14:47
@brittanyrey
Add NEWS entry
29ef754
@brittanyrey brittanyrey mentioned this pull request Oct 14, 2026
Fix possible dataloss during finalization with GzipFile #159422
Open
msullivan
msullivan reviewed Oct 14, 2026
View reviewed changes

@msullivan msullivan left a comment

Copy link
Copy Markdown
Contributor

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.

Seems good to me

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/zipfile/__init__.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@brittanyrey
Explain why ZipFile.__del__ wraps the warning in try/finally
9c41b95
@brittanyrey

brittanyrey commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review Sully @msullivan !

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cmaloney
cmaloney reviewed Oct 15, 2026
View reviewed changes
Comment thread Lib/test/test_zipfile/test_core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_zipfile/test_core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

cmaloney
cmaloney reviewed Oct 15, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2026-10-14-14-47-05.gh-issue-81954.KvM0gY.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

cmaloney
cmaloney reviewed Oct 15, 2026
View reviewed changes
Comment thread Lib/test/test_zipfile/test_core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner reviewed Oct 15, 2026
View reviewed changes
Comment thread Lib/test/test_zipfile/test_core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_zipfile/test_core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@brittanyrey
Import warnings eagerly in zipfile and simplify the tests
48693fb
@brittanyrey brittanyrey changed the title gh-81954: Always close ZipFile in __del__ even if the warning fails gh-81954: Eagerly import warnings module to failure of ZipFile.__del__ during finalization Oct 15, 2026
@brittanyrey

brittanyrey commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Updated!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cmaloney
cmaloney previously approved these changes Oct 15, 2026
View reviewed changes

@cmaloney cmaloney left a comment

Copy link
Copy Markdown
Contributor

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.

The NEWS is a little bit long but overall looking good. I like the comment about why the import warnings is not lazy.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Oct 15, 2026
vstinner
vstinner reviewed Oct 16, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

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.

You reintroduced a bug when you simplified your change.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_zipfile/test_core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/zipfile/__init__.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2026-10-14-14-47-05.gh-issue-81954.KvM0gY.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@cmaloney
cmaloney dismissed their stale review October 16, 2026 17:23

further review

@bedevere-app bedevere-app Bot added awaiting review and removed awaiting core review labels Oct 16, 2026
brittanyrey added 4 commits October 16, 2026 13:55
@brittanyrey
Restore try/finally in ZipFile.__del__ and check the archive in the test
251fa94
Keep a reference to warnings.warn as a default argument, like
subprocess.Popen.__del__, and always call close() even if emitting the
ResourceWarning raises. Verify the archive contents after collection
so the test catches a skipped close(). Shorten the NEWS entry.
@brittanyrey
Explain why ZipFile.__del__ wraps the warning in try/finally
64af802
@brittanyrey
Reword NEWS entry to describe the shutdown ImportError
728e7fe
@brittanyrey
Simplify comment in ZipFile.__del__
5e6d164
@ambv
ambv merged commit f9f33a6 into python:main Oct 23, 2026
56 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting review label Oct 23, 2026
@brittanyrey
brittanyrey deleted the b-zipfile-del-close branch October 23, 2026 21:06
@vstinner

vstinner commented Oct 24, 2026

Copy link
Copy Markdown
Member

Thanks for the fix @brittanyrey!

brittanyrey reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner left review comments
@msullivan msullivan msullivan left review comments
@cmaloney cmaloney cmaloney left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@brittanyrey @vstinner @msullivan @cmaloney @ambv
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.