Search code examples
importcpython

How does importlib.h generated from importlib._bootstrap in Cpython?


I am rewriting python import system in C. I encountered a problem: in Python/importlib.h, it says it is generated by Lib/importlib/_bootstrap.py, but even if I changed some code in Lib/importlib/_bootstrap.py, Python/importlib.h remains unchanged.

I have 3 questions:

1: How can I change the bytecode in Python/importlib.h?

2: What is the relationship between Lib/importlib/_bootstrap.py and Python/importlib.h?

3: How does the cpython import system work in total?


Solution

  • After several weeks of reading the source code of Python-3.6.10, I write an answer here to offer my understanding.

    1. Python/importlib.h is generated by Programs/_freeze_import.c. "make regen-all" is need to rerun the _freeze_import.c file. _freeze_import.c take the input of Lib/importlib/*.py to generate Python/importlib.h and Python/importlib_external.h.
    2. Lib/importlib/*.py is the source of Python/importlib.h and Python/importlib_external.h.
    3. You need to dive into the source code.