PyO3 0.9.0 released

Hi. I'm happy to announce the release of PyO3 0.9.0.
PyO3 is an FFI library that makes it easy to interact with Python interpreter, thanks to the flexibility of proc-macro and useful toolchains like maturin.

This version has some breaking changes, mainly for preventing undefined behaviors. Probably, the most notable one is PyCell, which a RefCell-like object wrapper for #[pyclass].
#[pyclass] attribute is one of our primary APIs for making Python extension, which generates all boilerplates you need to convert a struct to a Python object. You can use it like:

#[pyclass]
struct MyClass { ... }

#[pymethods]
impl MyClass {
    #[new]
    fn new() -> Self { ... }
    fn merge(&mut self, other: &mut Names) { ... }
}

And once a struct is declared with #[pyclass], you can use it from Python scripts:

from my_module import MyClass
m = MyClass()

Isn't it an easy API?

However, here comes a problem: basically we use Python objects as references, but they are not as smart as Rust's reference. They don't have mutability flags and are thus always mutable.
So not to violate the the rules of references, we wrap Rust's struct by PyCell and dynamically tracks references counts.

As a result, when trying to get an invalid reference, we can throw exceptions like this:


For other breaking changes, please see the migration guide.

PyCell is inspired by WasmRefCell. In addition to wasm-bindgen, I really appreciate all the community's efforts for making safe FFI-bindings. Actually, I learned a lot from reading stdweb and rlua.

And I also appreciate all contributors, issue reporters, and discussions on gitter, which helps us
to step forward to this safer direction.

Finally, I want to say thanks to the users. We know Rust is good, but also know it can be too difficult for non-programmers like Physics or Biology researchers. So it's my pleasure to see PyO3 is used for providing made in Rust software to broader areas, like Astronomy or Natural Language Processing.
Thank you.

21 Likes

Thanks for all your work on PyO3 ! It might be good to announce it for Python users as well. Maybe at https://discuss.python.org/ (possibly under "Packaging")? The message might need to be adjusted a bit for Python audience with potentially little knowledge of Rust.

3 Likes

Awesome work! I've really loved my experience with PyO3. It's great to see it continuing to improve.

1 Like

So basically this would allow me to write some code in Rust, and use it in Python as if it would be a Python module?

Which will result in better performance of course because it's actually not a Python module but Rust?

Exactly. I used it to create a Blender plugin, a component of it being the fastest gltf exporter for Blender that I know of. ( only with very basic features, so far, though. :wink: )

2 Likes

Damn that's sick! Will test it out soon.

Thank you for the proposal.
I'm not sure how I should explain the merits of Rust and PyO3 to Python users, but I'll try it later :wink:

2 Likes

Does it still require nightly? If yes, why exactly?

Cheers

Yes. This is why exactly.