-
Notifications
You must be signed in to change notification settings - Fork 792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write migration guide #795
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ded8302
Write migration guide for 0.9
kngwyu 8eb0065
Apply suggestions from code review
kngwyu 3a0cd8e
Apply suggestions from georg's review to migration.md
kngwyu 084b362
Relax version requirements for dependencies
kngwyu a6765e3
Mention `let obj: T = obj.extract()?;` in migration.md
kngwyu 107c0cf
Unify AsPyRef for Py<T> to make rust-numpy work
kngwyu 433b812
Remove ^ from Cargo.toml + small improvements for migration.md
kngwyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,187 @@ | ||||||
# Appendix B: Migrating from older PyO3 versions | ||||||
This guide can help you upgrade code through breaking changes from one PyO3 version to the next. For a detailed list of all changes, see [CHANGELOG.md](https://github.com/PyO3/pyo3/blob/master/CHANGELOG.md) | ||||||
## from 0.8.* to 0.9 | ||||||
|
||||||
### `#[new]` interface | ||||||
[`PyRawObject`](https://docs.rs/pyo3/0.8.5/pyo3/type_object/struct.PyRawObject.html) | ||||||
is now removed and our syntax for constructors has changed. | ||||||
|
||||||
Before: | ||||||
```compile_fail | ||||||
#[pyclass] | ||||||
struct MyClass {} | ||||||
|
||||||
#[pymethods] | ||||||
impl MyClass { | ||||||
#[new] | ||||||
fn new(obj: &PyRawObject) { | ||||||
obj.init(MyClass { }) | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
After: | ||||||
``` | ||||||
# use pyo3::prelude::*; | ||||||
#[pyclass] | ||||||
struct MyClass {} | ||||||
|
||||||
#[pymethods] | ||||||
impl MyClass { | ||||||
#[new] | ||||||
kngwyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
fn new() -> Self { | ||||||
MyClass {} | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
Basically you can return `Self` or `Result<Self>` directly. | ||||||
For more, see [the constructor section](https://pyo3.rs/master/class.html#constructor) of this guide. | ||||||
|
||||||
### PyCell | ||||||
PyO3 0.9 introduces [`PyCell`](https://pyo3.rs/master/doc/pyo3/pycell/struct.PyCell.html), which is | ||||||
a [`RefCell`](https://doc.rust-lang.org/std/cell/struct.RefCell.html) like object wrapper | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for dynamically ensuring | ||||||
[The Rules of References](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#the-rules-of-references). | ||||||
|
||||||
For `#[pymethods]` or `#[pyfunction]`s, your existing code should continue to work without any change. | ||||||
Python exceptions will automatically be raised when your functions are used in a way which breaks Rust's | ||||||
rules of references. | ||||||
|
||||||
Here is an example. | ||||||
``` | ||||||
# use pyo3::prelude::*; | ||||||
#[pyclass] | ||||||
struct Names { | ||||||
names: Vec<String> | ||||||
} | ||||||
|
||||||
#[pymethods] | ||||||
impl Names { | ||||||
#[new] | ||||||
fn new() -> Self { | ||||||
Names { names: vec![] } | ||||||
} | ||||||
fn merge(&mut self, other: &mut Names) { | ||||||
self.names.append(&mut other.names) | ||||||
} | ||||||
} | ||||||
# let gil = Python::acquire_gil(); | ||||||
# let py = gil.python(); | ||||||
# let names = PyCell::new(py, Names::new()).unwrap(); | ||||||
# let borrow_mut_err = py.get_type::<pyo3::pycell::PyBorrowMutError>(); | ||||||
# pyo3::py_run!(py, names borrow_mut_err, r" | ||||||
# try: | ||||||
# names.merge(names) | ||||||
# assert False, 'Unreachable' | ||||||
# except Exception as e: | ||||||
# isinstance(e, borrow_mut_err) | ||||||
# "); | ||||||
``` | ||||||
`Names` has a `merge` method, which takes `&mut self` and another argument of type `&mut Self`. | ||||||
Given this `#[pyclass]`, calling `names.merge(names)` in Python raises a `PyBorrowMutError` exception, | ||||||
since it requires two mutable borrows of `names`. | ||||||
|
||||||
However, for `#[pyproto]` and some functions, you need to manually fix the code. | ||||||
|
||||||
#### Object creation | ||||||
In 0.8 object creation was done with `PyRef::new` and `PyRefMut::new`. | ||||||
In 0.9 these have both been removed. | ||||||
To upgrade code, please use `PyCell::new` instead. | ||||||
If a `PyRef` or `PyRefMut` is needed, just call `.borrow()` or `.borrow_mut()` | ||||||
on the newly-created `PyCell`. | ||||||
|
||||||
Before: | ||||||
```compile_fail | ||||||
# use pyo3::prelude::*; | ||||||
# #[pyclass] | ||||||
# struct MyClass {} | ||||||
let gil = Python::acquire_gil(); | ||||||
let py = gil.python(); | ||||||
let obj_ref = PyRef::new(py, MyClass {}).unwrap(); | ||||||
``` | ||||||
|
||||||
After: | ||||||
``` | ||||||
# use pyo3::prelude::*; | ||||||
# #[pyclass] | ||||||
# struct MyClass {} | ||||||
let gil = Python::acquire_gil(); | ||||||
let py = gil.python(); | ||||||
let obj = PyCell::new(py, MyClass {}).unwrap(); | ||||||
let obj_ref = obj.borrow(); | ||||||
``` | ||||||
|
||||||
#### Object extraction | ||||||
For `PyClass` types `T`, `&T` and `&mut T` no longer have `FromPyObject` implementations. | ||||||
Instead you should extract `PyRef<T>` or `PyRefMut<T>`, respectively. You can also extract `&PyCell<T>`. | ||||||
|
||||||
Before: | ||||||
```ignore | ||||||
let obj: &PyAny = create_obj(); | ||||||
let obj_ref: &MyClass = obj.extract().unwrap(); | ||||||
let obj_ref_mut: &mut MyClass = obj.extract().unwrap(); | ||||||
``` | ||||||
|
||||||
After: | ||||||
``` | ||||||
# use pyo3::prelude::*; | ||||||
# use pyo3::types::{PyAny, IntoPyDict}; | ||||||
# #[pyclass] #[derive(Clone)] struct MyClass {} | ||||||
# #[pymethods] impl MyClass { #[new]fn new() -> Self { MyClass {} }} | ||||||
# let gil = Python::acquire_gil(); | ||||||
# let py = gil.python(); | ||||||
# let typeobj = py.get_type::<MyClass>(); | ||||||
# let d = [("c", typeobj)].into_py_dict(py); | ||||||
# let create_obj = || py.eval("c()", None, Some(d)).unwrap(); | ||||||
let obj: &PyAny = create_obj(); | ||||||
let obj_cell: &PyCell<MyClass> = obj.extract().unwrap(); | ||||||
let obj_cloned: MyClass = obj.extract().unwrap(); // extracted via Clone | ||||||
{ | ||||||
let obj_ref: PyRef<MyClass> = obj.extract().unwrap(); | ||||||
// we need to drop obj_ref before we can extract a PyRefMut due to Rust's rules of references | ||||||
} | ||||||
let obj_ref_mut: PyRefMut<MyClass> = obj.extract().unwrap(); | ||||||
``` | ||||||
|
||||||
|
||||||
#### `#[pyproto]` | ||||||
Most of the arguments to methods in `#[pyproto]` impls require a [`FromPyObject`] implementation. | ||||||
So if your protocol methods take `&T` or `&mut T` (where `T: PyClass`), | ||||||
please use `PyRef` or `PyRefMut` instead. | ||||||
|
||||||
Before: | ||||||
```compile_fail | ||||||
# use pyo3::prelude::*; | ||||||
# use pyo3::class::PySequenceProtocol; | ||||||
#[pyclass] | ||||||
struct ByteSequence { | ||||||
elements: Vec<u8>, | ||||||
} | ||||||
#[pyproto] | ||||||
impl PySequenceProtocol for ByteSequence { | ||||||
fn __concat__(&self, other: &Self) -> PyResult<Self> { | ||||||
let mut elements = self.elements.clone(); | ||||||
elements.extend_from_slice(&other.elements); | ||||||
Ok(Self { elements }) | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
After: | ||||||
``` | ||||||
# use pyo3::prelude::*; | ||||||
# use pyo3::class::PySequenceProtocol; | ||||||
#[pyclass] | ||||||
struct ByteSequence { | ||||||
elements: Vec<u8>, | ||||||
} | ||||||
#[pyproto] | ||||||
impl PySequenceProtocol for ByteSequence { | ||||||
fn __concat__(&self, other: PyRef<'p, Self>) -> PyResult<Self> { | ||||||
let mut elements = self.elements.clone(); | ||||||
elements.extend_from_slice(&other.elements); | ||||||
Ok(Self { elements }) | ||||||
} | ||||||
} | ||||||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
is the default operator, this doesn't change anything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I misunderstood that it is equal to "=="