Skip to content

Commit

Permalink
Merge pull request #622 from miurahr/topic/miurahr/fix-exception-hand…
Browse files Browse the repository at this point in the history
…ling-fp-close

fix: unintended unclosed status when exception
  • Loading branch information
miurahr authored Oct 13, 2024
2 parents 92f879a + ede54ce commit 4f0fec8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
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

0 comments on commit 4f0fec8

Please sign in to comment.