@@ -185,7 +185,7 @@ def get_code(self, fullname):
185
185
"""get_code(fullname) -> code object.
186
186
187
187
Return the code object for the specified module. Raise ZipImportError
188
- if the module couldn't be found .
188
+ if the module couldn't be imported .
189
189
"""
190
190
code , ispackage , modpath = _get_module_code (self , fullname )
191
191
return code
@@ -215,7 +215,8 @@ def get_data(self, pathname):
215
215
def get_filename (self , fullname ):
216
216
"""get_filename(fullname) -> filename string.
217
217
218
- Return the filename for the specified module.
218
+ Return the filename for the specified module or raise ZipImportError
219
+ if it couldn't be imported.
219
220
"""
220
221
# Deciding the filename requires working out where the code
221
222
# would come from if the module was actually loaded
@@ -267,7 +268,7 @@ def load_module(self, fullname):
267
268
268
269
Load the module specified by 'fullname'. 'fullname' must be the
269
270
fully qualified (dotted) module name. It returns the imported
270
- module, or raises ZipImportError if it wasn't found .
271
+ module, or raises ZipImportError if it could not be imported .
271
272
272
273
Deprecated since Python 3.10. Use exec_module() instead.
273
274
"""
@@ -613,20 +614,15 @@ def _eq_mtime(t1, t2):
613
614
614
615
615
616
# Given the contents of a .py[co] file, unmarshal the data
616
- # and return the code object. Return None if it the magic word doesn't
617
- # match, or if the recorded .py[co] metadata does not match the source,
618
- # (we do this instead of raising an exception as we fall back
619
- # to .py if available and we don't want to mask other errors).
617
+ # and return the code object. Raises ImportError it the magic word doesn't
618
+ # match, or if the recorded .py[co] metadata does not match the source.
620
619
def _unmarshal_code (self , pathname , fullpath , fullname , data ):
621
620
exc_details = {
622
621
'name' : fullname ,
623
622
'path' : fullpath ,
624
623
}
625
624
626
- try :
627
- flags = _bootstrap_external ._classify_pyc (data , fullname , exc_details )
628
- except ImportError :
629
- return None
625
+ flags = _bootstrap_external ._classify_pyc (data , fullname , exc_details )
630
626
631
627
hash_based = flags & 0b1 != 0
632
628
if hash_based :
@@ -640,11 +636,8 @@ def _unmarshal_code(self, pathname, fullpath, fullname, data):
640
636
source_bytes ,
641
637
)
642
638
643
- try :
644
- _bootstrap_external ._validate_hash_pyc (
645
- data , source_hash , fullname , exc_details )
646
- except ImportError :
647
- return None
639
+ _bootstrap_external ._validate_hash_pyc (
640
+ data , source_hash , fullname , exc_details )
648
641
else :
649
642
source_mtime , source_size = \
650
643
_get_mtime_and_size_of_source (self , fullpath )
@@ -730,6 +723,7 @@ def _get_pyc_source(self, path):
730
723
# 'fullname'.
731
724
def _get_module_code (self , fullname ):
732
725
path = _get_module_path (self , fullname )
726
+ import_error = None
733
727
for suffix , isbytecode , ispackage in _zip_searchorder :
734
728
fullpath = path + suffix
735
729
_bootstrap ._verbose_message ('trying {}{}{}' , self .archive , path_sep , fullpath , verbosity = 2 )
@@ -740,8 +734,12 @@ def _get_module_code(self, fullname):
740
734
else :
741
735
modpath = toc_entry [0 ]
742
736
data = _get_data (self .archive , toc_entry )
737
+ code = None
743
738
if isbytecode :
744
- code = _unmarshal_code (self , modpath , fullpath , fullname , data )
739
+ try :
740
+ code = _unmarshal_code (self , modpath , fullpath , fullname , data )
741
+ except ImportError as exc :
742
+ import_error = exc
745
743
else :
746
744
code = _compile_source (modpath , data )
747
745
if code is None :
@@ -751,4 +749,8 @@ def _get_module_code(self, fullname):
751
749
modpath = toc_entry [0 ]
752
750
return code , ispackage , modpath
753
751
else :
754
- raise ZipImportError (f"can't find module { fullname !r} " , name = fullname )
752
+ if import_error :
753
+ msg = f"module load failed: { import_error } "
754
+ raise ZipImportError (msg , name = fullname ) from import_error
755
+ else :
756
+ raise ZipImportError (f"can't find module { fullname !r} " , name = fullname )
0 commit comments