summaryrefslogtreecommitdiff
path: root/Lib/test/test_zipimport.py
diff options
context:
space:
mode:
authorOren Milman <orenmn@gmail.com>2017-09-29 21:34:31 +0300
committerBrett Cannon <brettcannon@users.noreply.github.com>2017-09-29 11:34:31 -0700
commit01c6a8859ef2ff5545a87cf537573bd342c848bf (patch)
tree67a72ecdad511b4890802ea8c12494f2e3888a58 /Lib/test/test_zipimport.py
parent66033733aa032707b57cf1b0e8d5a1c5e4afceb9 (diff)
downloadcpython-git-01c6a8859ef2ff5545a87cf537573bd342c848bf.tar.gz
bpo-31602: Fix an assertion failure in zipimporter.get_source() in case of a bad zlib.decompress() (GH-3784)
While a rare potential failure (it requires swapping out zlib.decompress() itself and forcing it to return a non-bytes object), this change prevents a potential C-level assertion failure and instead substitutes it with an exception. Thanks to Oren Milman for the patch.
Diffstat (limited to 'Lib/test/test_zipimport.py')
-rw-r--r--Lib/test/test_zipimport.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index daa5138751..4a934ff6cb 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -17,6 +17,10 @@ import doctest
import inspect
import io
from traceback import extract_tb, extract_stack, print_tb
+try:
+ import zlib
+except ImportError:
+ zlib = None
test_src = """\
def get_name():
@@ -669,6 +673,19 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
class CompressedZipImportTestCase(UncompressedZipImportTestCase):
compression = ZIP_DEFLATED
+ @support.cpython_only
+ def test_issue31602(self):
+ # There shouldn't be an assertion failure in zipimporter.get_source()
+ # in case of a bad zlib.decompress().
+ def bad_decompress(*args):
+ return None
+ with ZipFile(TEMP_ZIP, 'w') as zip_file:
+ self.addCleanup(support.unlink, TEMP_ZIP)
+ zip_file.writestr('bar.py', b'print("hello world")', ZIP_DEFLATED)
+ zi = zipimport.zipimporter(TEMP_ZIP)
+ with support.swap_attr(zlib, 'decompress', bad_decompress):
+ self.assertRaises(TypeError, zi.get_source, 'bar')
+
class BadFileZipImportTestCase(unittest.TestCase):
def assertZipFailure(self, filename):