E&K Labs
Published in

E&K Labs

Analysis of CPython

Sometimes, beginners in programming often ask questions about things that have been taken for granted by developers.

How arithmetic operations work, the difference between using operators to manipulate objects and using methods…

Most of the developers say, “It’s just like that”, “Just accept it.”

However, it is necessary to analyze the source code of the implementation of the language in order to deal with the essential content rather than the function provided as a library, and in the case of Python, CPython is an example.

If you’re studying Python but don’t know which interpreter you’re using, you’re most likely using CPython.

Various interpreters exist, but in this article, we will only focus on CPython.

Before reading

It is probably difficult to cover everything in this article, and there are many shortcomings.

As the author of this article, I am a student in the IT department, and I may not have enough professional skills to read.

Basically, in this article, I will explain the process I went through while exploring and analyzing.

Download the source code

Python is an open-source language.

You can easily download it from Python.org(http://Python.org) or Github (https://github.com/python/cpython).

Open the file and prepare it for analysis

Open the project using an IDE you are familiar with.
In general, I would recommend Visual Studio or Clion.

Begin analysis

From here on, I will explain using an example.
The scenario I prepared is as follows.

In order to explain the difference between list extend and addition operator, we are going to analyze the internal structure.

I want to know what extend() returns, and if it returns anything other than None.

First, I explored how the list is being defined inside.
Then I found the most plausible struct among the code that the List goes into.

In JetBrains IDE, you can use find in files to search for a specific structure throughout the project. (Ctrl + Shift + f / Cmd + Shift + f)

You can find the PyListObject in a specific location.

Also, I found _PyList_Extend corresponding to the extend function here.

One more search, in the same way, will find the implementation.

And I have list_extend in the same file.

You can see that there is code that sends a goto statement as an error label, but it only returns NULL as a result.


Analyzing the implementation so diligently does not significantly improve coding skills, and in practice, perhaps this analysis does not occur much.

At best, I just analyze the library to the extent of tearing it apart.

Most of the time, you can get what you want by just analyzing builtins.py or site-package.

However, I think analyzing the implementation of this language is the most useless challenge to enjoy IT as a student.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store