I am creating a runtime module in my python code similar to
string_code = """
import existing_module
a = "my_string"
existing_module.register(a)
"""
mod = ModuleType("mymodule")
sys.modules["mymodule"] = mod
exec(string_code, mod.__dict__)
Inside the existing_module I have code which checks if the caller is a module
def register(a: str):
caller = inspect.stack()[1]
caller_module = inspect.getmodule(caller[0])
assert caller_module is not None
Since I am calling the existing_module from a runtime module, I expected the caller_module to be not None. However, I am hitting that assert. Not sure why.
Can anyone help?