2021-03-06 14:59:46 +00:00
|
|
|
<!-- This file contains a rough overview of the PyO3 codebase. -->
|
2021-02-20 09:37:57 +00:00
|
|
|
<!-- Please do not make descriptions too specific, so that we can easily -->
|
|
|
|
<!-- keep this file in sync with the codebase. -->
|
|
|
|
|
2021-03-07 14:26:45 +00:00
|
|
|
# PyO3: Architecture
|
2021-02-20 09:37:57 +00:00
|
|
|
|
|
|
|
This document roughly describes the high-level architecture of PyO3.
|
2021-03-07 14:26:45 +00:00
|
|
|
If you want to become familiar with the codebase you are in the right place!
|
2021-02-20 09:37:57 +00:00
|
|
|
|
|
|
|
## Overview
|
|
|
|
|
2021-03-15 13:08:25 +00:00
|
|
|
PyO3 provides a bridge between Rust and Python, based on the [Python/C API].
|
2021-02-20 09:37:57 +00:00
|
|
|
Thus, PyO3 has low-level bindings of these API as its core.
|
|
|
|
On top of that, we have higher-level bindings to operate Python objects safely.
|
2021-03-14 14:48:49 +00:00
|
|
|
Also, to define Python classes and functions in Rust code, we have `trait PyClass` and a set of
|
2021-02-20 09:37:57 +00:00
|
|
|
protocol traits (e.g., `PyIterProtocol`) for supporting object protocols (i.e., `__dunder__` methods).
|
2021-03-07 14:26:45 +00:00
|
|
|
Since implementing `PyClass` requires lots of boilerplate, we have a proc-macro `#[pyclass]`.
|
2021-02-20 09:37:57 +00:00
|
|
|
|
2021-03-14 14:48:49 +00:00
|
|
|
To summarize, there are six main parts to the PyO3 codebase.
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-03-15 13:08:25 +00:00
|
|
|
1. [Low-level bindings of Python/C API.](#1-low-level-bindings-of-python-capi)
|
2022-01-25 13:46:27 +00:00
|
|
|
- [`pyo3-ffi`] and [`src/ffi`]
|
2021-03-13 07:31:27 +00:00
|
|
|
2. [Bindings to Python objects.](#2-bindings-to-python-objects)
|
2021-06-26 22:37:30 +00:00
|
|
|
- [`src/instance.rs`] and [`src/types`]
|
2021-03-14 14:48:49 +00:00
|
|
|
3. [`PyClass` and related functionalities.](#3-pyclass-and-related-functionalities)
|
2021-06-26 22:37:30 +00:00
|
|
|
- [`src/pycell.rs`], [`src/pyclass.rs`], and more
|
2021-03-13 07:31:27 +00:00
|
|
|
4. [Protocol methods like `__getitem__`.](#4-protocol-methods)
|
2021-06-26 22:37:30 +00:00
|
|
|
- [`src/class`]
|
2021-03-13 07:31:27 +00:00
|
|
|
5. [Procedural macros to simplify usage for users.](#5-procedural-macros-to-simplify-usage-for-users)
|
2021-06-26 22:37:30 +00:00
|
|
|
- [`src/derive_utils.rs`], [`pyo3-macros`] and [`pyo3-macros-backend`]
|
2021-11-10 08:34:54 +00:00
|
|
|
6. [`build.rs` and `pyo3-build-config`](#6-buildrs-and-pyo3-build-config)
|
2021-09-18 18:48:45 +00:00
|
|
|
- [`build.rs`](https://github.com/PyO3/pyo3/tree/main/build.rs)
|
2021-11-10 08:34:54 +00:00
|
|
|
- [`pyo3-build-config`]
|
2021-03-13 07:31:27 +00:00
|
|
|
|
2021-03-15 13:08:25 +00:00
|
|
|
## 1. Low-level bindings of Python/C API
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2022-01-25 13:46:27 +00:00
|
|
|
[`pyo3-ffi`] contains wrappers of [Python/C API].
|
2021-02-20 09:37:57 +00:00
|
|
|
|
2021-03-13 07:31:27 +00:00
|
|
|
We aim to provide straight-forward Rust wrappers resembling the file structure of
|
2021-02-20 09:37:57 +00:00
|
|
|
[`cpython/Include`](https://github.com/python/cpython/tree/v3.9.2/Include).
|
|
|
|
|
2021-05-17 02:49:23 +00:00
|
|
|
However, we still lack some APIs and are continuously updating the module to match
|
2021-03-07 14:26:45 +00:00
|
|
|
the file contents upstream in CPython.
|
2021-02-20 09:37:57 +00:00
|
|
|
The tracking issue is [#1289](https://github.com/PyO3/pyo3/issues/1289), and contribution is welcome.
|
|
|
|
|
2022-01-25 13:46:27 +00:00
|
|
|
In the [`pyo3-ffi`] crate, there is lots of conditional compilation such as `#[cfg(Py_LIMITED_API)]`,
|
2021-03-13 07:31:27 +00:00
|
|
|
`#[cfg(Py_37)]`, and `#[cfg(PyPy)]`.
|
2021-03-15 13:08:25 +00:00
|
|
|
`Py_LIMITED_API` corresponds to `#define Py_LIMITED_API` macro in Python/C API.
|
2021-03-14 14:48:49 +00:00
|
|
|
With `Py_LIMITED_API`, we can build a Python-version-agnostic binary called an
|
2021-07-24 07:47:02 +00:00
|
|
|
[abi3 wheel](https://pyo3.rs/latest/building_and_distribution.html#py_limited_apiabi3).
|
2021-03-13 07:31:27 +00:00
|
|
|
`Py_37` means that the API is available from Python >= 3.7.
|
|
|
|
There are also `Py_38`, `Py_39`, and so on.
|
|
|
|
`PyPy` means that the API definition is for PyPy.
|
2021-11-10 08:34:54 +00:00
|
|
|
Those flags are set in [`build.rs`](#6-buildrs-and-pyo3-build-config).
|
2021-03-13 07:31:27 +00:00
|
|
|
|
|
|
|
## 2. Bindings to Python objects
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
[`src/types`] contains bindings to [built-in types](https://docs.python.org/3/library/stdtypes.html)
|
|
|
|
of Python, such as `dict` and `list`.
|
2021-03-07 14:26:45 +00:00
|
|
|
For historical reasons, Python's `object` is called `PyAny` in PyO3 and located in [`src/types/any.rs`].
|
|
|
|
Currently, `PyAny` is a straightforward wrapper of `ffi::PyObject`, defined as:
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
```rust
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct PyAny(UnsafeCell<ffi::PyObject>);
|
|
|
|
```
|
|
|
|
|
|
|
|
All built-in types are defined as a C struct.
|
|
|
|
For example, `dict` is defined as:
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
```c
|
|
|
|
typedef struct {
|
|
|
|
/* Base object */
|
|
|
|
PyObject ob_base;
|
|
|
|
/* Number of items in the dictionary */
|
|
|
|
Py_ssize_t ma_used;
|
|
|
|
/* Dictionary version */
|
|
|
|
uint64_t ma_version_tag;
|
|
|
|
PyDictKeysObject *ma_keys;
|
|
|
|
PyObject **ma_values;
|
|
|
|
} PyDictObject;
|
|
|
|
```
|
|
|
|
|
|
|
|
However, we cannot access such a specific data structure with `#[cfg(Py_LIMITED_API)]` set.
|
2021-03-07 14:26:45 +00:00
|
|
|
Thus, all builtin objects are implemented as opaque types by wrapping `PyAny`, e.g.,:
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
```rust
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct PyDict(PyAny);
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that `PyAny` is not a pointer, and it is usually used as a pointer to the object in the
|
|
|
|
Python heap, as `&PyAny`.
|
|
|
|
This design choice can be changed
|
|
|
|
(see the discussion in [#1056](https://github.com/PyO3/pyo3/issues/1056)).
|
|
|
|
|
2021-03-06 14:59:46 +00:00
|
|
|
Since we need lots of boilerplate for implementing common traits for these types
|
2021-02-20 09:37:57 +00:00
|
|
|
(e.g., `AsPyPointer`, `AsRef<PyAny>`, and `Debug`), we have some macros in
|
|
|
|
[`src/types/mod.rs`].
|
|
|
|
|
2021-03-14 14:48:49 +00:00
|
|
|
## 3. `PyClass` and related functionalities
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-03-07 14:26:45 +00:00
|
|
|
[`src/pycell.rs`], [`src/pyclass.rs`], and [`src/type_object.rs`] contain types and
|
2021-02-20 09:37:57 +00:00
|
|
|
traits to make `#[pyclass]` work.
|
2021-12-30 12:29:42 +00:00
|
|
|
Also, [`src/pyclass_init.rs`] and [`src/impl_/pyclass.rs`] have related functionalities.
|
2021-02-20 09:37:57 +00:00
|
|
|
|
|
|
|
To realize object-oriented programming in C, all Python objects must have the following two fields
|
|
|
|
at the beginning.
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
```rust
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct PyObject {
|
|
|
|
pub ob_refcnt: usize,
|
|
|
|
pub ob_type: *mut PyTypeObject,
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-03-06 14:59:46 +00:00
|
|
|
Thanks to this guarantee, casting `*mut A` to `*mut PyObject` is valid if `A` is a Python object.
|
2021-02-20 09:37:57 +00:00
|
|
|
|
2021-03-06 14:59:46 +00:00
|
|
|
To ensure this guarantee, we have a wrapper struct `PyCell<T>` in [`src/pycell.rs`] which is roughly:
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
```rust
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct PyCell<T: PyClass> {
|
|
|
|
object: crate::ffi::PyObject,
|
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
```
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-03-06 14:59:46 +00:00
|
|
|
Thus, when copying a Rust struct to a Python object, we first allocate `PyCell` on the Python heap and then
|
2021-03-07 14:26:45 +00:00
|
|
|
move `T` into it.
|
2021-02-20 09:37:57 +00:00
|
|
|
Also, `PyCell` provides [RefCell](https://doc.rust-lang.org/std/cell/struct.RefCell.html)-like methods
|
|
|
|
to ensure Rust's borrow rules.
|
2021-03-07 14:26:45 +00:00
|
|
|
See [the documentation](https://docs.rs/pyo3/latest/pyo3/pycell/struct.PyCell.html) for more.
|
2021-02-20 09:37:57 +00:00
|
|
|
|
|
|
|
`PyCell<T>` requires that `T` implements `PyClass`.
|
2022-04-07 13:37:45 +00:00
|
|
|
This trait is somewhat complex and derives many traits, but the most important one is `PyTypeInfo`
|
2021-02-20 09:37:57 +00:00
|
|
|
in [`src/type_object.rs`].
|
2022-04-07 13:37:45 +00:00
|
|
|
`PyTypeInfo` is also implemented for built-in types.
|
2021-03-13 07:31:27 +00:00
|
|
|
In Python, all objects have their types, and types are also objects of `type`.
|
2021-02-20 09:37:57 +00:00
|
|
|
For example, you can see `type({})` shows `dict` and `type(type({}))` shows `type` in Python REPL.
|
2022-04-07 13:37:45 +00:00
|
|
|
`T: PyTypeInfo` implies that `T` has a corresponding type object.
|
2021-02-20 09:37:57 +00:00
|
|
|
|
2021-03-13 07:31:27 +00:00
|
|
|
## 4. Protocol methods
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2022-02-17 18:16:56 +00:00
|
|
|
Python has some built-in special methods called dunder methods, such as `__iter__`.
|
|
|
|
They are called "slots" in the [abstract objects layer](https://docs.python.org/3/c-api/abstract.html) in
|
2021-03-15 13:08:25 +00:00
|
|
|
Python/C API.
|
2022-02-17 18:16:56 +00:00
|
|
|
We provide a way to implement those protocols similarly, by recognizing special
|
|
|
|
names in `#[pymethods]`, with a few new ones for slots that can not be
|
|
|
|
implemented in Python, such as GC support.
|
2021-02-20 09:37:57 +00:00
|
|
|
|
2021-03-13 07:31:27 +00:00
|
|
|
## 5. Procedural macros to simplify usage for users.
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2022-04-21 19:19:14 +00:00
|
|
|
[`pyo3-macros`] provides five proc-macro APIs: `pymodule`, `pyfunction`, `pyclass`,
|
|
|
|
`pymethods`, and `#[derive(FromPyObject)]`. (And a deprecated `pyproto` macro.)
|
2021-03-06 14:59:46 +00:00
|
|
|
[`pyo3-macros-backend`] has the actual implementations of these APIs.
|
2021-03-07 14:26:45 +00:00
|
|
|
[`src/derive_utils.rs`] contains some utilities used in code generated by these proc-macros,
|
2021-02-20 09:37:57 +00:00
|
|
|
such as parsing function arguments.
|
|
|
|
|
2021-11-10 08:34:54 +00:00
|
|
|
## 6. `build.rs` and `pyo3-build-config`
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-11-10 08:34:54 +00:00
|
|
|
PyO3 supports a wide range of OSes, interpreters and use cases. The correct environment must be
|
|
|
|
detected at build time in order to set up relevant conditional compilation correctly. This logic
|
|
|
|
is captured in the [`pyo3-build-config`] crate, which is a `build-dependency` of `pyo3` and
|
|
|
|
`pyo3-macros`, and can also be used by downstream users in the same way.
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-11-10 08:34:54 +00:00
|
|
|
In [`pyo3-build-config`]'s `build.rs` the build environment is detected and inlined into the crate
|
|
|
|
as a "config file". This works in all cases except for cross-compiling, where it is necessary to
|
|
|
|
capture this from the `pyo3` `build.rs` to get some extra environment variables that Cargo doesn't
|
|
|
|
set for build dependencies.
|
|
|
|
|
|
|
|
The `pyo3` `build.rs` also runs some safety checks such as ensuring the Python version detected is
|
|
|
|
actually supported.
|
|
|
|
|
|
|
|
Some of the functionality of `pyo3-build-config`:
|
2021-03-15 13:08:25 +00:00
|
|
|
- Find the interpreter for build and detect the Python version.
|
2021-11-10 08:34:54 +00:00
|
|
|
- We have to set some version flags like `#[cfg(Py_3_7)]`.
|
|
|
|
- If the interpreter is PyPy, we set `#[cfg(PyPy)`.
|
|
|
|
- If the `PYO3_CONFIG_FILE` environment variable is set then that file's contents will be used
|
|
|
|
instead of any detected configuration.
|
|
|
|
- If the `PYO3_NO_PYTHON` environment variable is set then the interpreter detection is bypassed
|
2021-06-26 22:37:30 +00:00
|
|
|
entirely and only abi3 extensions can be built.
|
2021-03-14 14:48:49 +00:00
|
|
|
- Check if we are building a Python extension.
|
|
|
|
- If we are building an extension (e.g., Python library installable by `pip`),
|
2021-06-26 22:37:30 +00:00
|
|
|
we don't link `libpython`.
|
|
|
|
Currently we use the `extension-module` feature for this purpose. This may change in the future.
|
|
|
|
See [#1123](https://github.com/PyO3/pyo3/pull/1123).
|
2021-11-10 08:34:54 +00:00
|
|
|
- Cross-compiling configuration
|
2022-03-25 11:00:41 +00:00
|
|
|
- If `TARGET` architecture and `HOST` architecture differ, we can find cross compile information
|
2022-04-08 09:18:17 +00:00
|
|
|
from environment variables (`PYO3_CROSS_LIB_DIR`, `PYO3_CROSS_PYTHON_VERSION` and
|
|
|
|
`PYO3_CROSS_PYTHON_IMPLEMENTATION`) or system files.
|
2022-03-25 11:00:41 +00:00
|
|
|
When cross compiling extension modules it is often possible to make it work without any
|
|
|
|
additional user input.
|
2022-04-08 09:18:17 +00:00
|
|
|
- When an experimental feature `generate-abi3-import-lib` is enabled, the `pyo3-ffi` build script can
|
|
|
|
generate `python3.dll` import libraries for Windows targets automatically via an external
|
|
|
|
[`python3-dll-a`] crate. This enables the users to cross compile abi3 extensions for Windows without
|
|
|
|
having to install any Windows Python libraries.
|
2021-03-13 07:31:27 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
<!-- External Links -->
|
2021-06-26 22:37:30 +00:00
|
|
|
|
|
|
|
[python/c api]: https://docs.python.org/3/c-api/
|
2022-04-08 09:18:17 +00:00
|
|
|
[`python3-dll-a`]: https://docs.rs/python3-dll-a/latest/python3_dll_a/
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
<!-- Crates -->
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-09-18 18:48:45 +00:00
|
|
|
[`pyo3-macros`]: https://github.com/PyO3/pyo3/tree/main/pyo3-macros
|
|
|
|
[`pyo3-macros-backend`]: https://github.com/PyO3/pyo3/tree/main/pyo3-macros-backend
|
2021-11-10 08:34:54 +00:00
|
|
|
[`pyo3-build-config`]: https://github.com/PyO3/pyo3/tree/main/pyo3-build-config
|
2022-01-25 13:46:27 +00:00
|
|
|
[`pyo3-ffi`]: https://github.com/PyO3/pyo3/tree/main/pyo3-ffi
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
<!-- Directories -->
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-09-18 18:48:45 +00:00
|
|
|
[`src/class`]: https://github.com/PyO3/pyo3/tree/main/src/class
|
|
|
|
[`src/ffi`]: https://github.com/PyO3/pyo3/tree/main/src/ffi
|
|
|
|
[`src/types`]: https://github.com/PyO3/pyo3/tree/main/src/types
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-02-20 09:37:57 +00:00
|
|
|
<!-- Files -->
|
2021-06-26 22:37:30 +00:00
|
|
|
|
2021-09-18 18:48:45 +00:00
|
|
|
[`src/derive_utils.rs`]: https://github.com/PyO3/pyo3/blob/main/src/derive_utils.rs
|
|
|
|
[`src/instance.rs`]: https://github.com/PyO3/pyo3/tree/main/src/instance.rs
|
|
|
|
[`src/pycell.rs`]: https://github.com/PyO3/pyo3/tree/main/src/pycell.rs
|
|
|
|
[`src/pyclass.rs`]: https://github.com/PyO3/pyo3/tree/main/src/pyclass.rs
|
|
|
|
[`src/pyclass_init.rs`]: https://github.com/PyO3/pyo3/tree/main/src/pyclass_init.rs
|
|
|
|
[`src/pyclass_slot.rs`]: https://github.com/PyO3/pyo3/tree/main/src/pyclass_slot.rs
|
|
|
|
[`src/type_object.rs`]: https://github.com/PyO3/pyo3/tree/main/src/type_object.rs
|
|
|
|
[`src/class/methods.rs`]: https://github.com/PyO3/pyo3/tree/main/src/class/methods.rs
|
|
|
|
[`src/class/impl_.rs`]: https://github.com/PyO3/pyo3/tree/main/src/class/impl_.rs
|
|
|
|
[`src/types/any.rs`]: https://github.com/PyO3/pyo3/tree/main/src/types/any.rs
|
|
|
|
[`src/types/mod.rs`]: https://github.com/PyO3/pyo3/tree/main/src/types/mod.rs
|