Skip to content

gh-150077:Fix fileobj leak in tarfile.TarFile.zstopen on BaseException#150080

Open
lpyu001 wants to merge 2 commits into
python:mainfrom
lpyu001:fix-tarfile-exception
Open

gh-150077:Fix fileobj leak in tarfile.TarFile.zstopen on BaseException#150080
lpyu001 wants to merge 2 commits into
python:mainfrom
lpyu001:fix-tarfile-exception

Conversation

@lpyu001
Copy link
Copy Markdown
Contributor

@lpyu001 lpyu001 commented May 19, 2026

@lpyu001 lpyu001 requested a review from ethanfurman as a code owner May 19, 2026 11:52
@lpyu001 lpyu001 changed the title gh-150077:fix tarfile- uses except Exception: and leaks fileobj on BaseException gh-150077:Fix fileobj leak in tarfile.TarFile.zstopen on BaseException May 19, 2026
Comment thread Lib/test/test_tarfile.py
"""
def test_zstopen_closes_fileobj_on_base_exception(self):
fileobj = unittest.mock.Mock()
with unittest.mock.patch("compression.zstd.ZstdFile",
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.

I would suggest not to use a mock and check if a real file is closed or not.

Comment thread Lib/test/test_tarfile.py
def test_zstopen_closes_fileobj_on_base_exception(self):
fileobj = unittest.mock.Mock()
with unittest.mock.patch("compression.zstd.ZstdFile",
return_value=fileobj), \
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.

Suggested change
return_value=fileobj), \
return_value=fileobj),

You can use with ( instead of \

Fix ``tarfile.TarFile.zstopen`` to close the underlying zstd file object
when opening the tar archive is interrupted by a :exc:`BaseException`
subclass such as :exc:`KeyboardInterrupt`.

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.

Suggested change

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