Python
As a widely used programming language, its concise syntax and powerful features are loved by developers.
However, for manyPython
For users,CPython
(Python
The official implementation of the internal working mechanism is still a mysterious black box.
Today, we will continue to exploreCPython
Source code, try to understandPython
The mystery from program startup to word running.
1. CPython code summary
CPython
The code base is huge, containing about 350,000 linesC codeand nearly 600,000 rowsPython code。
These codes are distributed in multiple directories, and these directory structures areCPython
The development and maintenance provide a clear way to organize.
The main directories include:
-
Grammar/
: Python syntax file -
Include/
:Head file -
Lib/
: Standard library module (Python implementation) -
Modules/
: Standard library module (C implementation) -
Objects/
: Built-in type implementation. -
Parser/
: parser-related code -
Programs/
: The source code of the executable file -
Python/
: Interpreter core code
CPython
It is open source, and the code is hosted ingihtub
If you are interested, you can download it and view it.
After downloading, you can switch to the branch version you are interested in.
git clone /python/cpython/ && cd cpython
git checkout 3.12
The process of compiling the source code is relatively simple, just run the following command:
./configure
make
make test
sudo make install
After compilation is completed, run./
You can start your own compiledCPython
Version.
2. Start Python
CPython
The entry point ismain()
Function, located inPrograms/
in the file.
/* Minimal main program -- everything is loaded from the library */
#include ""
#ifdef MS_WINDOWS
int
wmain(int argc, wchar_t **argv)
{
return Py_Main(argc, argv);
}
#else
int
main(int argc, char **argv)
{
return Py_BytesMain(argc, argv);
}
#endif
This function is the starting point of program startup and is responsible for initializationCPython
And start executing user code.
existWindows
On the platform,CPython
usewmain()
As an entry point to supportUTF-16
Encoded command line parameters. This design makesCPython
Better handle character encoding issues on different platforms.
main()
The main responsibility of a function is to callPy_Main()
orPy_BytesMain()
, These two functions handle command line parameters of wide characters and byte strings respectively.
Py_Main
andPy_BytesMain
lie inModules/
in the file.
These two functions are further calledpymain_main()
,startCPython
initialization process.
Py_Main
,Py_BytesMain
as well aspymain_main
These functions areModules/
in the file.
3. Initialize Python
CPython
The initialization process is divided intoThree stages:Pre-initialization、Core InitializationandMain Initialization, each stage has its own specific tasks and goals.
- Pre-initialization:
The pre-initialization phase is mainly responsible for setting the runtime status, default memory allocator and basic configuration.
This stage is called_PyRuntime_Initialize()
andPyPreConfig_InitPythonConfig()
Wait for the function to complete.
These functions are initializedCPython
global runtime state and prepare for subsequent initialization phases.
in,_PyRuntime_Initialize
The implementation of the function is located at:Python/
PyPreConfig_InitPythonConfig
The implementation of the function is located at:Python/
- Core Initialization:
The core initialization phase isCPython
The key part of initialization.
This stage initializes the main interpreter state, thread state, built-in type, builtins module, sys module, and import system.
These components make upPython
The core foundation of operation makesCPython
Able to start executionPython
Code.
Core initialization is calledPy_InitializeFromConfig()
The function is completed, and the function is further calledpyinit_core()
Then, the functions are gradually constructedPython
The core environment at runtime.
in,Py_InitializeFromConfig
The implementation of the function is located at:Python/
- Main Initialization:
The main initialization phase isCPython
The final step in initialization.
This stage has been completedCPython
Fully initialized, including setting up , importing site modules, etc.
These tasks makeCPython
Able to support completePython
Functions, including module import and script execution.
The main initialization is calledpyinit_main()
The function is completed, and the function is further calledinit_interp_main()
Wait for the function, completedCPython
final configuration.
in,pyinit_main
andinit_interp_main
The implementation of the function is located at:Python/
4. Run Python
After the initialization is completed,CPython
Enter the program running stage.Py_RunMain()
function(Modules/
The file is the core of this stage, which is responsible for runningPython
and clean up.
Depending on the different running modes (such as scripts, modules, command lines, etc.),Py_RunMain()
Functions call different functions to execute code.
For example,pymain_run_python()
Function processing settings and module import ensurePython
The program can run in the correct environment.
5. Compile and execute
Python
The compilation and execution of the code isCPython
The core part of the operation.
PyRun_FileExFlags()
function(Python/
The file is the entry point of this process, which is responsible for thePython
The code is compiled into bytecode and loaded into the runtime environment.
The compilation process is called_PyAST_Compile()
function(Python/
Completed in the file, the function will abstract the syntax tree (AST
) compiled into a code object.
final,PyEval_EvalCode()
function(Python/
Execute the code object in the file and enterBytecode execution loop。
Bytecode execution loopyesCPython
The final stage of operation.
This stage is called_PyEval_EvalFrame()
function(Include/internal/pycore_ceval.h
to execute bytecode instructions,_PyEval_EvalFrame()
A function is a complex function that handles various kinds ofPython
Operations, including function calls, variable access and exception handling.
If you want to understand betterPython
The operating mechanism of this function can be studied in depth.
6. Summary
This article mainly comes fromCPython
From the source code perspectivePython
The entire process from startup to execution of the program.
The source code is based onCPyhton 3.12
branched.
Through reading and analysisCPython
We can not only better understand the source code ofPython
The internal mechanism can also discover opportunities for optimization and improvement.
If it is correctCPython
Internal working mechanisms are of interest, and it is best to be able to explore its source code in person. By reading and understanding the source code, you will gain deeper knowledge and be able to better utilize the power of Python.