Skip to content

Commit

Permalink
bpo-36991: Fix incorrect exception escaping ZipFile.extract() (GH-13632)
Browse files Browse the repository at this point in the history
(cherry picked from commit 2f1b857)

Co-authored-by: Berker Peksag <berker.peksag@gmail.com>
  • Loading branch information
miss-islington and berkerpeksag committed Sep 12, 2019
1 parent 0d7cb5b commit 717cc61
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
import time
import unittest
import unittest.mock as mock
import zipfile


Expand Down Expand Up @@ -1739,6 +1740,16 @@ def test_seek_tell(self):
fp.seek(0, os.SEEK_SET)
self.assertEqual(fp.tell(), 0)

@requires_bz2
def test_decompress_without_3rd_party_library(self):
data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
zip_file = io.BytesIO(data)
with zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_BZIP2) as zf:
zf.writestr('a.txt', b'a')
with mock.patch('zipfile.bz2', None):
with zipfile.ZipFile(zip_file) as zf:
self.assertRaises(RuntimeError, zf.extract, 'a.txt')

def tearDown(self):
unlink(TESTFN)
unlink(TESTFN2)
Expand Down
1 change: 1 addition & 0 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ def _get_compressor(compress_type, compresslevel=None):


def _get_decompressor(compress_type):
_check_compression(compress_type)
if compress_type == ZIP_STORED:
return None
elif compress_type == ZIP_DEFLATED:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes a potential incorrect AttributeError exception escaping
ZipFile.extract() in some unsupported input error situations.

0 comments on commit 717cc61

Please sign in to comment.