Meta hook configuration during multi-phase initialization

The documentation on multi-phase initialization contains this tantalizing hint:

Python Initialization Configuration ā€” Python 3.11.3 documentation
It may allow to customize Python in Python to override or tune the Path Configuration, maybe install a custom sys.meta_path importer or an import hook, etc.

This implies that at least in principle it might be possible to configure sys.meta_path prior to any imports.

Along similar lines, the documentation on meta hooks suggests that it is possible to override frozen and/or builtin modules:

5. The import system ā€” Python 3.11.3 documentation
Meta hooks are called at the start of import processing, before any other import processing has occurred, other than sys.modules cache look up. This allows meta hooks to override sys.path processing, frozen modules, or even built-in modules.

However, if I understand correctly, nearly all frozen modules are going to be imported before it is ever possible to modify sys.meta_path during normal Python execution.

So what Iā€™m wondering is whether there is an existing mechanism to override sys.meta_path (likely using the C API) such that it could affect the import of any/all frozen and/or built-in modules. If so, Iā€™m wondering whether multi-phase initialization provides such a mechanism. If not, is there any appetite for supporting such a feature?

In either case, it seems like any such hooks would need to be implemented purely in terms of the C API in order to avoid imports during multi-phase initialization. When combined with custom AST processing, this could be particularly useful for implementing various kinds of instrumentation and other features, although it would require the ability to manipulate AST via the C API as well.

1 Like