How hard is it to get CPython build with Rust?

With multiple projects, traditionally written in C/C++ has added support for newer code to be written in Rust, notably Linux Kernel and Chromium. I am wondering if this can be done with CPython? The benefits is that since Rust is a memory-safe language, we can reduce the number of CVE related to the CPython projects.

If you mean writing Python extensions in Rust, that’s possible today (although you’d have to go looking for details, I don’t know much myself beyond that it’s possible). If you mean implementing the Python language in Rust, that would be a huge undertaking, and isn’t something the CPython project would do, as there is way too much investment in C at this point for it to be a good use of the core devs’ time. There’s nothing stopping an interested party from writing a new Python implementation in Rust, though - just expect it to be a major piece of work.

1 Like

RustPython exists: GitHub - RustPython/RustPython: A Python Interpreter written in Rust

However, the GitHub homepage says:

RustPython is in development, and while the interpreter certainly can be used in interesting use cases like running Python in WASM and embedding into a Rust project, do note that RustPython is not totally production-ready.

It’s a completely different project to CPython because, well, it’s written in a different language.

3 Likes

It’s a completely different project to CPython because, well, it’s written in a different language.

I glanced at it several weeks ago. Not only is it written in Rust, it seems they’ve reimplemented the virtual machine from scratch.

Skip

I think what’s being asked here is a bit different from what has been addressed so far. It’s whether CPython would be able to move some components into Rust. For example, what if we wanted to rewrite a single file in Modules/ in Rust?

I am not a build expert so I can’t give an authoritative answer, but my understanding is that CPython supports more platforms than Rust itself and the build chain would need a lot of work to support a Rust build, so probably the answer is “no”.

5 Likes

I wouldn’t write that project off completely as it has some interesting use cases. For example, that is what Ruff uses.

edit: oops I interpreted your signature as a recommendation to not use it :man_facepalming:

3 Likes

yeah exactly as I understand that’s what happening in Chromium and Linux. So wondering if we can do the same for CPython.

The answer is currently no. The team just doesn’t have the capacity for adopting a new set of build tools in addition to the existing tooling (which is already separate for Windows and UNIX-style systems).

I also suspect there’s less need because Python is one or more orders of magnitude smaller than Chromium and Linux.

1 Like