I added a function to CPython (build & structure grasp)

1.First of all

This section describes the CPython build and CPython file structure required to add functionality to CPython. This article uses Python 3.10.

I added a function to CPython (ternary operator) Add two types of switch statements to python Pre-increment ()

2. What is CPython?

CPython is Python written in C language, and when it is generally called Python, it often refers to this Python. The code is managed on GitHub and you can also publish a PR.

3. Build CPython

The Official Article is very helpful for building CPython. First, clone the code from the CPython 3.10 repository.


$ git clone https://github.com/python/cpython

Then change directories and run the configure script. Here, -g includes the "debug symbol" in the executable and -O0 is the lowest level of optimization. Also, the folder to install is specified by adding --prefix.


$ cd cpython
$ CFLAGS="-O0 -g" ./configure  --with-pydebug   --prefix=(Installation directory)

Next, compile and install.

$ make -s -j2
$ make install

Now you can install CPython safely. To run the installed Python:

$cd (installed directory)/bin
$ ./python3

4. Added features to CPython

The Checklist for adding features to CPython is officially summarized. In the following, we will explain with reference to this.

4.1 CPython file structure

These are the four most important files to make changes when adding functionality to CPython. I will look at them in order. スクリーンショット 2020-10-20 18.40.25.png

4.1.1 python.gram First, let's talk about python.gram. スクリーンショット 2020-10-25 12.00.23.png It is a file in Grammer / python.gram of CPython. python.gram defines the Python grammar. Write the syntax to add in this file.


$ make regen-pegen

It will automatically generate a Parser.

4.1.2 python.asdl Next, I will explain about python.asdl. スクリーンショット 2020-10-25 12.00.27.png A file for creating an abstract syntax tree (AST). If you have defined a new function in python.gram, you also need to write the function definition in this file.


$ make regen-ast

AST is automatically generated.

4.1.3 compile.c Next, I will explain about compile.c. スクリーンショット 2020-10-25 12.00.29.png A file that converts AST to bytecode. Describe the processing for each syntax in bytecode. When adding a grammar, describe the processing of that grammar in bytecode here.

4.1.4 ceval.c Next, I will explain about ceval.c. スクリーンショット 2020-10-25 12.00.32.png Execute bytecode. If the existing bytecode cannot be implemented, add a new bytecode here.

5. Summary

So far, we have explained how to build and understand the structure of CPython. CPython has a huge amount of code, but I felt that the amount of code that needed attention was small because the procedure for changing the grammar was organized in the official checklist.

Reference material

-Exploring large-scale software
This is an experimental site.

-CPython
CPython repository

-24. Changing CPython ’s Grammar
A checklist for adding features to CPython was included to help you understand the structure of CPython.

-1. Getting Started
It was helpful for building CPython.

Recommended Posts

I added a function to CPython (build & structure grasp)
I added a function to CPython (ternary operator)
I want to build a Python environment
I tried to build a super-resolution method / ESPCN
I tried to build a super-resolution method / SRCNN ①
I tried to build a super-resolution method / SRCNN ③
I tried to build a super-resolution method / SRCNN ②
I want to easily build a model-based development environment
[Go + Gin] I tried to build a Docker environment
How to call a function
I made a function to check the model of DCGAN
Added achievement function to Sublime Text
I just wrote a script to build Android on another machine
How to make a recursive function
I wrote a function to load a Git extension script in Python
Added a function to register desired shifts in the Django shift table
I tried to build a Mac Python development environment with pythonz + direnv
Python: I want to measure the processing time of a function neatly
I made a function to see the movement of a two-dimensional array (Python)
How to build a sphinx translation environment
I want to print in a comprehension
I tried to create a linebot (preparation)
I had turtle draw a trigonometric function
A simple IDAPython script to name a function
I tried adding post-increment to CPython Implementation
I made a script to display emoji
I tried to make a Web API
I tried to make a dictionary function that does not distinguish between cases
[Python] I tried to get the type name as a string from the type function
I want to make matplotlib a dark theme
I want to easily create a Noise Model
To execute a Python enumerate function in JavaScript
I want to INSERT a DataFrame into MSSQL
How to create a function object from a string
I want to create a window in Python
How to make a dictionary with a hierarchical structure.
I want to make a game with Python
I don't want to take a coding test
I made a tool to compile Hy natively
I want to create a plug-in type implementation
I read "How to make a hacking lab"
I wrote a script to upload a WordPress plugin
I tried to generate a random character string
I made a tool to get new articles
I tried adding post-increment to CPython Extra edition
I want to easily find a delicious restaurant
[Go] How to write or call a function
I want to write to a file with Python
[PyTorch] Sample ⑧ ~ How to build a complex model ~
How to Mock a Public function in Pytest
I want to use the activation function Mish
I tried to make a ○ ✕ game using TensorFlow
I want to upload a Django app to heroku
I can't sleep until I build a server !! (Introduction to Python server made in one day)