6 min read
|
Saved February 14, 2026
|
Copied!
Do you care about this?
This article proposes integrating Rust into CPython to enhance memory safety and thread safety. Initially, Rust will be used for optional extension modules, with plans for it to become a required dependency in future versions. The authors highlight Rust's advantages, including its strong safety guarantees and efficient data structures.
If you do, here's more
The proposal suggests integrating Rust into CPython, initially for optional extension modules, with plans for broader use in the codebase. Rust's memory safety features can eliminate many common bugs and security vulnerabilities that arise from invalid memory accesses. Its ownership model not only prevents data races but also aligns well with Python's move towards free-threaded implementations. Adoption of Rust in CPython could spur contributions to third-party extensions, especially since a significant portion of these already use Rust.
Implementing Rust would start with adding Rust-based modules to the existing C extensions directory. This process involves creating foreign function interface (FFI) definitions in Rust to interact with CPython's C API, using a new crate called cpython-sys. Tools like bindgen will help automate the binding process, although some manual work will be necessary due to C macros. While introducing some unsafe Rust code is unavoidable, the goal is to minimize unsafe interactions and eventually adopt safe abstractions to streamline module and function definitions.
Rust also brings a robust build system through its Cargo package manager, which simplifies dependency management and supports cross-compilation. The proposal outlines support for all platforms that CPython targets, with most Rust tiers matching those of Python. Lastly, it mentions the rejection of using PyO3 for safe abstractions due to potential inflexibility and overhead in managing updates to the API.
Questions about this article
No questions yet.