Skip to content

gh-131492: gh-131461: handle exceptions in GzipFile constructor while owning resources #131462

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

Merged
merged 20 commits into from
Mar 20, 2025
Merged
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
14 changes: 12 additions & 2 deletions Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,18 @@ def __init__(self, filename=None, mode=None,

self.fileobj = fileobj

if self.mode == WRITE:
self._write_gzip_header(compresslevel)
try:
if self.mode == WRITE:
self._write_gzip_header(compresslevel)
except BaseException:
# Avoid a ResourceWarning if the write fails, eg read-only file or KI
try:
if self.myfileobj is not None:
self.myfileobj.close()
finally:
self.fileobj = None
self.fileobj = None
raise

@property
def mtime(self):
Expand Down
Loading