Sometimes we need to extend existing Python projects with other languages to solve performance issues or to include some functionality already written in another language. In this chapter, we will create a full-featured Python integration for a Rust crate we built in the previous article. As the primary goal, I want to focus on a pleasant end-user experience and share tips on improving the usability and debuggability of the resulting library.

Overview:

Target audience: People who are looking for ways to connect existing Rust code with Python. Some general knowledge of Rust syntax would be useful.

It is the last part of the 3 chapter series about Rust for Python users. Other chapters:


Python C API

NOTE. In this article, I will imply using CPython, which is the de-facto reference Python implementation.

As the first step, let’s check how Python can use code written in other languages. The most popular way to do so is to use Python’s C API. It allows programmers to access Python interpreter internals and defines an API that the interpreter will expect from an extension.

Alternatively, it is possible to use ctypes, but it may perform significantly worse. See this example of integration

The cornerstone of Python’s C API is PyObject. It is a struct containing information about the object’s type and the number of references to this object (which is needed for garbage collection). Python objects are mostly moved around via PyObject * pointers, and you could see them in many C API function signatures. Accessing the concrete type is done via casting such pointer to a pointer to a specific type.

#python #bindings #c api

1.45 GEEK