Skip to content

Syntax warnings are duplicated in zipimport #141276

Description

@serhiy-storchaka

When importing a not precompiled module from a ZIP archive, syntax warnings are emitted twice.

Discovered during adding tests for #139652.

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 9, 2025
  2. typenoob commented on Nov 11, 2025

    @typenoob

    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

    cpython/Lib/zipimport.py

    Lines 158 to 167 in 92741c5

    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?

    cpython/Lib/zipimport.py

    Lines 809 to 824 in 92741c5

    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?

  3. typenoob commented on Nov 26, 2025

    @typenoob

    @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?

  4. itamaro commented on Nov 27, 2025

    @itamaro
    Contributor

    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).

  5. added a commit that references this issue on Jul 16, 2026
  6. harjothkhara commented on Aug 23, 2026

    @harjothkhara
    Contributor

    #153799 fixes this by not compiling the source in get_filename.

  7. added 2 commits that reference this issue on Oct 8, 2026
  8. added 2 commits that reference this issue on Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-importlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions