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.