Skip to content

Commit

Permalink
Merge pull request #996 from davidhewitt/pyproto-simplifications
Browse files Browse the repository at this point in the history
Refactor `#[pyproto]` Result types
  • Loading branch information
davidhewitt committed Jun 23, 2020
2 parents f757c99 + c7a4b47 commit 0c59b05
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 528 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Update `num-bigint` optional dependendency from `0.2` to `0.3`. [#978](https://github.com/PyO3/pyo3/pull/978)
- `#[pyproto]` is re-implemented without specialization. [#961](https://github.com/PyO3/pyo3/pull/961)
- `PyClassAlloc::alloc` is renamed to `PyClassAlloc::new`. [#990](https://github.com/PyO3/pyo3/pull/990)
- `#[pyproto]` methods can now have return value `T` or `PyResult<T>` (previously only `PyResult<T>` was supported). [#996](https://github.com/PyO3/pyo3/pull/996)

### Removed
- Remove `ManagedPyRef` (unused, and needs specialization) [#930](https://github.com/PyO3/pyo3/pull/930)
Expand All @@ -34,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed
- Fix passing explicit `None` to `Option<T>` argument `#[pyfunction]` with a default value. [#936](https://github.com/PyO3/pyo3/pull/936)
- Fix `PyClass.__new__`'s not respecting subclasses when inherited by a Python class. [#990](https://github.com/PyO3/pyo3/pull/990)
- Fix returning `Option<T>` from `#[pyproto]` methods. [#996](https://github.com/PyO3/pyo3/pull/996)

## [0.10.1] - 2020-05-14
### Fixed
Expand Down
21 changes: 12 additions & 9 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ mapping or number protocols. PyO3 defines separate traits for each of them. To p
Python object behavior, you need to implement the specific trait for your struct. Important note,
each protocol implementation block has to be annotated with the `#[pyproto]` attribute.

All `#[pyproto]` methods which can be defined below can return `T` instead of `PyResult<T>` if the
method implementation is infallible.

### Basic object customization

The [`PyObjectProtocol`] trait provides several basic customizations.
Expand Down Expand Up @@ -823,11 +826,11 @@ struct MyIterator {

#[pyproto]
impl PyIterProtocol for MyIterator {
fn __iter__(slf: PyRef<Self>) -> PyResult<Py<MyIterator>> {
Ok(slf.into())
fn __iter__(slf: PyRef<Self>) -> Py<MyIterator> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyObject>> {
Ok(slf.iter.next())
fn __next__(mut slf: PyRefMut<Self>) -> Option<PyObject> {
slf.iter.next()
}
}
```
Expand All @@ -848,12 +851,12 @@ struct Iter {

#[pyproto]
impl PyIterProtocol for Iter {
fn __iter__(slf: PyRefMut<Self>) -> PyResult<Py<Iter>> {
Ok(slf.into())
fn __iter__(slf: PyRefMut<Self>) -> Py<Iter> {
slf.into()
}

fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<usize>> {
Ok(slf.inner.next())
fn __next__(mut slf: PyRefMut<Self>) -> Option<usize> {
slf.inner.next()
}
}

Expand All @@ -868,7 +871,7 @@ impl PyIterProtocol for Container {
let iter = Iter {
inner: slf.iter.clone().into_iter(),
};
PyCell::new(slf.py(), iter).map(Into::into)
Py::new(slf.py(), iter)
}
}

Expand Down
Loading

0 comments on commit 0c59b05

Please sign in to comment.