Reading CPython Source (5): Coroutines

Reading CPython Source (5): Coroutines Originally published in Chinese on 2021-08-04; this English edition preserves the original scope and technical context. Coroutines are lightweight threads of user space, they can pause or resume at specific positions within a function, and the caller can retrieve the state from or pass the state to a coroutine. A typical application of coroutines in Python is the generator, and this article analyzes the implementation of generators in Python. ...

August 4, 2021 · 8 min · Zhengyu Chen

Reading CPython Source (4): The Compiler and Virtual Machine

Reading CPython Source (4): The Compiler and Virtual Machine Originally published in Chinese on 2021-05-26; this English edition preserves the original scope and technical context. Python is a language that is generally interpreted before use. We usually download the Python interpreter, CPython, from the official Python website. The source code used in this article are from CPython. The Python interpreter is composed of a Python Compiler and a Python Virtual Machine. When we execute Python code via the Python command, the Python Compiler compiles the Python code into Python bytecode; subsequently, the Python Virtual Machine reads and executes these bytecode sequentially. ...

May 26, 2021 · 22 min · Zhengyu Chen

Reading CPython Source (3): The list Type

Reading CPython Source (3): The list Type Originally published in Chinese on 2021-05-06; this English edition preserves the original scope and technical context. In Python, the list type is defined as a struct named PyListObject in the listobject.h file. // Include/cpython/listobject.h typedef struct { PyObject_VAR_HEAD /* Vector of pointers to list elements. list[0] is ob_item[0], etc. */ PyObject **ob_item; /* ob_item contains space for 'allocated' elements. The number * currently in use is ob_size. * Invariants: * 0 <= ob_size <= allocated * len(list) == ob_size * ob_item == NULL implies ob_size == allocated == 0 * list.sort() temporarily sets allocated to -1 to detect mutations. * * Items must normally not be NULL, except during construction when * the list is not yet visible outside the function that builds it. */ Py_ssize_t allocated; } PyListObject; Its implementation is similar to std::vector in C++, both maintaining a dynamically allocated array and expanding the array’s capacity dynamically when adding data. The PyListObject structure contains a variable-length object header PyObject_VAR_HEAD, ob_size represents the current length of the dynamic array, **ob_item points to the dynamic array, and allocated is the capacity of the dynamic array. We can find the methods related to the list object from its type pointer PyTypeObject PyList_Type. ...

May 6, 2021 · 10 min · Zhengyu Chen

Reading CPython Source (2): The int Type

Reading CPython Source (2): The int Type Originally published in Chinese on 2021-03-31; this English edition preserves the original scope and technical context. In Python, there are six standard data types, which are number, string, list, tuple, set, and dictionary. As already explained, the objects of these types are instances of the PyBaseObject_Type class, which itself is an instance of the PyType_Type class. This article, however, will delve into the implementation of the int type in Python. ...

March 31, 2021 · 13 min · Zhengyu Chen

Reading CPython Source (1): Types and Objects

Reading CPython Source (1): Types and Objects Originally published in Chinese on 2021-03-14; this English edition preserves the original scope and technical context. Python is an interpreted, dynamically typed, multi-paradigm programming language. When we download and install a version of Python from python.org, we are actually running the C language-compiled CPython. In addition to CPython’s runtime, there are also Jython, PyPy, Cython, and others; within the source code of CPython, there are a series of libraries, components, and tools. ...

March 14, 2021 · 9 min · Zhengyu Chen