Skip to content

Commit 77a9c36

Browse files
committed
Chain UnicodeDecodeError as __cause__ and verify in test
1 parent 4382c89 commit 77a9c36

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

Lib/test/test_zipimport.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,9 @@ def testInvalidUTF8FileName(self):
10881088
os_helper.unlink(TESTMOD)
10891089
with open(TESTMOD, 'wb') as fp:
10901090
fp.write(cdh + eocd)
1091-
self.assertZipFailure(TESTMOD)
1091+
with self.assertRaises(zipimport.ZipImportError) as cm:
1092+
zipimport.zipimporter(TESTMOD)
1093+
self.assertIsInstance(cm.exception.__cause__, UnicodeDecodeError)
10921094

10931095
# XXX: disabled until this works on Big-endian machines
10941096
def _testBogusZipFile(self):

Lib/zipimport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,10 @@ def _read_directory(archive):
429429
# UTF-8 file names extension
430430
try:
431431
name = name.decode()
432-
except UnicodeDecodeError:
432+
except UnicodeDecodeError as exc:
433433
raise ZipImportError(
434434
f"can't decode file name in Zip file: {archive!r}",
435-
path=archive)
435+
path=archive) from exc
436436
else:
437437
# Historical ZIP filename encoding
438438
try:

0 commit comments

Comments
 (0)