Skip to content
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

fix: unintended unclosed status when exception #622

Merged
merged 4 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 1 addition & 4 deletions py7zr/py7zr.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ def __init__(
raise TypeError(f"invalid file: {type(file)}")
self.encoded_header_mode = True
self.header_encryption = header_encryption
self._fileRefCnt = 1
try:
if mode == "r":
self._real_get_contents(password)
Expand Down Expand Up @@ -428,9 +427,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

def _fpclose(self) -> None:
assert self._fileRefCnt > 0
self._fileRefCnt -= 1
if not self._fileRefCnt and not self._filePassed:
if not self._filePassed:
self.fp.close()

def _real_get_contents(self, password) -> None:
Expand Down
Binary file added tests/data/data_corrupted.7z
Binary file not shown.
13 changes: 12 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
from io import BytesIO

import _lzma
import pytest

import py7zr
Expand Down Expand Up @@ -140,12 +141,22 @@ def test_digests():


@pytest.mark.basic
def test_digests_corrupted():
def test_digests_crc_corrupted():
arcfile = os.path.join(testdata_path, "crc_corrupted.7z")
with py7zr.SevenZipFile(arcfile) as archive:
assert archive.test() is None
archive.reset()
assert archive.testzip() == "src/scripts/py7zr"
assert archive.fp.closed


@pytest.mark.basic
def test_digests_data_corrupted():
arcfile = os.path.join(testdata_path, "data_corrupted.7z")
with pytest.raises(_lzma.LZMAError):
with py7zr.SevenZipFile(arcfile) as archive:
archive.testzip()
assert archive.fp.closed


@pytest.mark.unit
Expand Down