In this post, we will take a dissecion of source code of Python.
To benefit the simplicity and meanwhile follow the most recent functionnalities, I choose
Python 3.6.9 to do the analysis.
The first step, is to build!
As other projects, Python uses
autoconf toolset to configure and then
make to build itself. If it doesn’t make sense to you, just ignore it. What you really need is just a set of single commands to make it:
./configure, it will detect your environment along with the architecture, the dependencies, the features supported by your compiler
- After that, if there is no error, a
Makefilefile should be generated and placed in the same directory
make, and you will get your own Python build!
Now we use default configurations to build, because we don’t aim at building a Python binary. If you’d like to play with Python builds, you can find more information at https://docs.python.org/3.6/using/unix.html#building-python.
Before we configure the project, the structure of project is really clear and simple:
aclocal.m4 configure Lib Misc Programs python-config.py
pyconfig.h.in are files which concerns the configuration and the compilation.
LICENSE is the license file.
This folder contains the documentation of Python.
In Grammar folder, there is only one file, which described the abstract grammar representation of Python.
All Python headers used during Python compilation. Some of those will be intalled in your system for a further development.
All libraries written in Python.
Build tools for macOS build.
Other things, not so important.
Modules written in C.
Declarations and implementations of various Python Objects.
Python code parser.
The code for Windows PC.
The Windows PC build files, such as Visual Studio Prject files.
Main functions of Python.
Python main implementation codes.
Auxiliary tools and demo.
In the analysis, we start by the main entry of program.
At the beginning,
<locale.h> are imported.
Then, the main function has two branches:
On Windows, the function
wmain instead of
main is used as the main entry.
This function is for the unicode environment, you can go to the doc page of Microsoft for further information:
In the main function, it calls the real Main Function of Python,
Py_Main with command-line arguments.
It’s not so simple as the one for Windows.
In this code, some necessary variables are declared, regarding C89 or above. Then, to copy arguments, Python requests to use
malloc to allocate memories by invoking
Then two memory spaces are allocated:
argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
Then, Python did one thing like this:
oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
This line will return
C locale and store it in
Then Python tries to set locale with user-prefered one by
setlocale(LC_ALL, ""), and to decode all command line arguments with new locale.
After decoding command line arguments with user-prefered locale, Python would like to use the old default
C locale and do some clean things.
After everything about decoding and copying arguments is ready, run Python main process:
res = Py_Main(argc, argv_copy);
After finishing Python main process, all memory spaces which were allocated should be released properly with following code:
/* Force again malloc() allocator to release memory blocks allocated
And then, return the result through
In this post, we tried to compile Python with all default configurations, and we explicited the main entry function.
In the next post, we’ll get involve with
Py_Main function, as well as some Python Objects or types if possible.
See you then!