Location>code7788 >text

"Python underlying principle"--How to run Python code in CPython

Popularity:334 ℃/2025-02-09 11:38:54

PythonAs a widely used programming language, its concise syntax and powerful features are loved by developers.

However, for manyPythonFor users,CPythonPythonThe official implementation of the internal working mechanism is still a mysterious black box.

Today, we will continue to exploreCPythonSource code, try to understandPythonThe mystery from program startup to word running.

1. CPython code summary

CPythonThe 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 areCPythonThe 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

CPythonIt is open source, and the code is hosted ingihtubIf 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 compiledCPythonVersion.

2. Start Python

CPythonThe 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 initializationCPythonAnd start executing user code.

existWindowsOn the platform,CPythonusewmain()As an entry point to supportUTF-16Encoded command line parameters. This design makesCPythonBetter 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_MainandPy_BytesMainlie inModules/in the file.

These two functions are further calledpymain_main(),startCPythoninitialization process.

Py_MainPy_BytesMainas well aspymain_mainThese functions areModules/in the file.

3. Initialize Python

CPythonThe initialization process is divided intoThree stagesPre-initializationCore InitializationandMain Initialization, each stage has its own specific tasks and goals.

  1. 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 initializedCPythonglobal runtime state and prepare for subsequent initialization phases.

in,_PyRuntime_InitializeThe implementation of the function is located at:Python/

PyPreConfig_InitPythonConfigThe implementation of the function is located at:Python/

  1. Core Initialization:

The core initialization phase isCPythonThe 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 upPythonThe core foundation of operation makesCPythonAble to start executionPythonCode.

Core initialization is calledPy_InitializeFromConfig() The function is completed, and the function is further calledpyinit_core()Then, the functions are gradually constructedPythonThe core environment at runtime.

in,Py_InitializeFromConfigThe implementation of the function is located at:Python/

  1. Main Initialization:

The main initialization phase isCPythonThe final step in initialization.

This stage has been completedCPythonFully initialized, including setting up , importing site modules, etc.

These tasks makeCPythonAble to support completePythonFunctions, 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, completedCPythonfinal configuration.

in,pyinit_mainandinit_interp_mainThe implementation of the function is located at:Python/

4. Run Python

After the initialization is completed,CPythonEnter the program running stage.Py_RunMain()function(Modules/The file is the core of this stage, which is responsible for runningPythonand 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 ensurePythonThe program can run in the correct environment.

5. Compile and execute

PythonThe compilation and execution of the code isCPythonThe core part of the operation.

PyRun_FileExFlags()function(Python/The file is the entry point of this process, which is responsible for thePythonThe 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 loopyesCPythonThe final stage of operation.

This stage is called_PyEval_EvalFrame()function(Include/internal/pycore_ceval.hto execute bytecode instructions,_PyEval_EvalFrame()A function is a complex function that handles various kinds ofPythonOperations, including function calls, variable access and exception handling.

If you want to understand betterPythonThe operating mechanism of this function can be studied in depth.

6. Summary

This article mainly comes fromCPythonFrom the source code perspectivePythonThe entire process from startup to execution of the program.

The source code is based onCPyhton 3.12branched.

Through reading and analysisCPythonWe can not only better understand the source code ofPythonThe internal mechanism can also discover opportunities for optimization and improvement.

If it is correctCPythonInternal 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.