Files
micropython/tests/import/import_override2.py
Chris Liechti 48d96b400e py/builtinimport: Support relative import in custom __import__ callback.
The globals need to be forwarded from the caller's context.

Signed-off-by: Damien George <damien@micropython.org>
2025-11-21 00:01:01 +11:00

19 lines
488 B
Python

# test overriding __import__ combined with importing from the filesystem
def custom_import(name, globals, locals, fromlist, level):
if level > 0:
print("import", name, fromlist, level)
return orig_import(name, globals, locals, fromlist, level)
orig_import = __import__
try:
__import__("builtins").__import__ = custom_import
except AttributeError:
print("SKIP")
raise SystemExit
# import calls __import__ behind the scenes
import pkg7.subpkg1.subpkg2.mod3