Skip to content

gh-81954: Warn when ZipFile is closed with unwritten data#152354

Open
cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:zipfile_resourcewarning
Open

gh-81954: Warn when ZipFile is closed with unwritten data#152354
cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:zipfile_resourcewarning

Conversation

@cmaloney

@cmaloney cmaloney commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Bring zipfile.ZipFile into alignment with other buffered file writers like GzipFile and TextIOWrapper by emitting a ResourceWarning if there is unwritten data and it is closed implicitly.

ZipFile should be closed either by using it as a context manager or explicitly calling .close().

cc: @jaraco the _path/test_path fixtures have quite a few ZipFiles they don't close. For now I just blanket silenced, not sure your preferred upstream fix.

Bring `zipfile.ZipFile` into alignment with other buffered file writers
like `GzipFile` and `TextIOWrapper` by emitting a `ResourceWarning` if
there is unwritten data and it is closed implicitly.

ZipFile should be closed either by using it as a context manager or
explicitly calling `.close()`.
@read-the-docs-community

read-the-docs-community Bot commented Jun 27, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33451527 | 📁 Comparing 03007df against main (1812162)

  🔍 Preview build  

54 files changed · ± 53 modified · - 1 deleted

± Modified

- Deleted

self.fixtures.enter_context(warnings.catch_warnings())
warnings.filterwarnings(
'ignore', category=ResourceWarning, message='unclosed ZipFile'
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be better to have the fixtures yield the zipfile and close it after so they don't leak the resource?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried yielding but it added a lot of code. Moved so that the fixture binding code will auto-enter context manager objects + using unittest.enterContext

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants