GeezerVison is showing github.com — Syntax warnings are duplicated in zipimport · Issue #141276 · python/cpython
Syntax warnings are duplicated in zipimport #141276DescriptionWhen importing a not precompiled module from a ZIP archive, syntax warnings are emitted twice.
Discovered during adding tests for #139652.
Linked PRs
Activitytypenoob commented on Nov 11, 2025 The warnings are emitted both in _frozen_importlib._find_spec and _frozen_importlib._load_unlocked.
I think warnings are not supposed to be emitted in _frozen_importlib._find_spec.
|
if location is None: |
|
# The caller may simply want a partially populated location- |
|
# oriented spec. So we set the location to a bogus value and |
|
# fill in as much as we can. |
|
location = '<unknown>' |
|
if hasattr(loader, 'get_filename'): |
|
# ExecutionLoader |
|
try: |
|
location = loader.get_filename(name) |
|
except ImportError: |
|
pass |
_frozen_importlib._find_spec acquired the filename by get_filename
|
def get_filename(self, fullname): |
|
"""get_filename(fullname) -> filename string. |
|
|
|
Return the filename for the specified module or raise ZipImportError |
|
if it couldn't be imported. |
|
""" |
|
# Deciding the filename requires working out where the code |
|
# would come from if the module was actually loaded |
|
code, ispackage, modpath = _get_module_code(self, fullname) |
|
return modpath |
however it triggered the compile action which may be not intended?
|
modpath = toc_entry[0] |
|
data = _get_data(self.archive, toc_entry) |
|
code = None |
|
if isbytecode: |
|
try: |
|
code = _unmarshal_code(self, modpath, fullpath, fullname, data) |
|
except ImportError as exc: |
|
import_error = exc |
|
else: |
|
code = _compile_source(modpath, data) |
|
if code is None: |
|
# bad magic number or non-matching mtime |
|
# in byte code, try next |
|
continue |
|
modpath = toc_entry[0] |
|
return code, ispackage, modpath |
If all we want is just the filename, there's no need to compile the code right? typenoob commented on Nov 26, 2025 @serhiy-storchaka Sorry to bother you. I has found the root cause, but don't know whether to fix it or the best approach to fix it. Could you take a look at it? itamaro commented on Nov 27, 2025 I happened to stumble across this when noticing that modules loaded from pyc's with zipimport go through lookup and unmarshalling twice (once though get_filename and once through get_code).
There's a comment in get_filename about needing to get the code for the filename, so this seems intentional, but I'm not sure whether it is intentional to do the entire dance twice instead of some caching.
@serhiy-storchaka let me know if this should be a separate issue (or isn't an issue at all). harjothkhara commented on Aug 23, 2026 #153799 fixes this by not compiling the source in get_filename.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|