Skip to content

inspect.getframeinfo forces all lazy modules to be loaded #139669

@SpecLad

Description

@SpecLad

Bug report

Bug description:

Create two files as follows:

main.py:

import importlib.util
import inspect
import sys

# https://docs.python.org/3/library/importlib.html#implementing-lazy-imports
def lazy_import(name):
    spec = importlib.util.find_spec(name)
    loader = importlib.util.LazyLoader(spec.loader)
    spec.loader = loader
    module = importlib.util.module_from_spec(spec)
    sys.modules[name] = module
    loader.exec_module(module)
    return module
    
lazy_import('m')

f = inspect.currentframe()
inspect.getframeinfo(f)

m.py:

print("I'm loaded!")

Now run:

$ python main.py 
I'm loaded!

This behavior doesn't make much sense, since f isn't even inside m.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions