8000 bpo-36991: Fix incorrect exception escaping ZipFile.extract() (GH-13632) · python/cpython@717cc61 · GitHub
[go: up one dir, main page]

Skip to content

Commit 717cc61

Browse files
bpo-36991: Fix incorrect exception escaping ZipFile.extract() (GH-13632)
(cherry picked from commit 2f1b857) Co-authored-by: Berker Peksag <berker.peksag@gmail.com>
1 parent 0d7cb5b commit 717cc61

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/test/test_zipfile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import time
1111
import unittest
12+
import unittest.mock as mock
1213
import zipfile
1314

1415

@@ -1739,6 +1740,16 @@ def test_seek_tell(self):
17391740
fp.seek(0, os.SEEK_SET)
17401741
self.assertEqual(fp.tell(), 0)
17411742

1743+
@requires_bz2
1744+
def test_decompress_without_3rd_party_library(self):
1745+
data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
1746+
zip_file = io.BytesIO(data)
1747+
with zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_BZIP2) as zf:
1748+
zf.writestr('a.txt', b'a')
1749+
with mock.patch('zipfile.bz2', None):
1750+
with zipfile.ZipFile(zip_file) as zf:
1751+
self.assertRaises(RuntimeError, zf.extract, 'a.txt')
1752+
17421753
def tearDown(self):
17431754
unlink(TESTFN)
17441755
unlink(TESTFN2)

Lib/zipfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ def _get_compressor(compress_type, compresslevel=None):
703703

704704

705705
def _get_decompressor(compress_type):
706+
_check_compression(compress_type)
706707
if compress_type == ZIP_STORED:
707708
return None
708709
elif compress_type == ZIP_DEFLATED:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes a potential incorrect AttributeError exception escaping
2+
ZipFile.extract() in some unsupported input error situations.

0 commit comments

Comments
 (0)
0