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

Skip to content

Commit 2f1b857

Browse files
berkerpeksaggpshead
authored andcommitted
bpo-36991: Fix incorrect exception escaping ZipFile.extract() (GH-13632)
1 parent 99b54d6 commit 2f1b857

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

@@ -1766,6 +1767,16 @@ def test_seek_tell(self):
17661767
fp.seek(0, os.SEEK_SET)
17671768
self.assertEqual(fp.tell(), 0)
17681769

1770+
@requires_bz2
1771+
def test_decompress_without_3rd_party_library(self):
1772+
data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
1773+
zip_file = io.BytesIO(data)
1774+
with zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_BZIP2) as zf:
1775+
zf.writestr('a.txt', b'a')
1776+
with mock.patch('zipfile.bz2', None):
1777+
with zipfile.ZipFile(zip_file) as zf:
1778+
self.assertRaises(RuntimeError, zf.extract, 'a.txt')
1779+
17691780
def tearDown(self):
17701781
unlink(TESTFN)
17711782
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