From fd12a769df9951717d7b3a3a7e8f568cd8fc58ec Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Tue, 28 May 2019 19:19:00 +0300 Subject: [PATCH 1/2] bpo-36991: Fix TypeError in ZipFile.extract() --- Lib/test/test_zipfile.py | 13 +++++++++++-- Lib/zipfile.py | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index bf5bb4d0f13e3d..9910e3ce03e57c 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -4,11 +4,10 @@ import os import pathlib import posixpath -import shutil import struct -import tempfile import time import unittest +import unittest.mock as mock import zipfile @@ -1739,6 +1738,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) diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 8f8cb863b00343..90fd95c87b16d6 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -702,6 +702,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: From 27f2fac32ff3cf00d2432ec8f80efcf51159ea9c Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Thu, 12 Sep 2019 14:52:41 +0100 Subject: [PATCH 2/2] NEWS! --- .../next/Library/2019-09-12-14-52-38.bpo-36991.1OcSm8.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-09-12-14-52-38.bpo-36991.1OcSm8.rst diff --git a/Misc/NEWS.d/next/Library/2019-09-12-14-52-38.bpo-36991.1OcSm8.rst b/Misc/NEWS.d/next/Library/2019-09-12-14-52-38.bpo-36991.1OcSm8.rst new file mode 100644 index 00000000000000..c6fa852e94ad9b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-09-12-14-52-38.bpo-36991.1OcSm8.rst @@ -0,0 +1,2 @@ +Fixes a potential incorrect AttributeError exception escaping +ZipFile.extract() in some unsupported input error situations.