You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
29
29
- Deprecate `PyModule` methods `call`, `call0`, `call1` and `get`. [#1492](https://github.com/PyO3/pyo3/pull/1492)
30
30
- Add length information to `PyBufferError`s raised from `PyBuffer::copy_to_slice` and `PyBuffer::copy_from_slice`. [#1534](https://github.com/PyO3/pyo3/pull/1534)
31
31
- Automatically provide `-undefined` and `dynamic_lookup` linker arguments on macOS with `extension-module` feature. [#1539](https://github.com/PyO3/pyo3/pull/1539)
32
+
- Deprecate `#[pyproto]` methods which are easier to implement as `#[pymethods]`: [#1560](https://github.com/PyO3/pyo3/pull/1560)
33
+
-`PyBasicProtocol::__bytes__` and `PyBasicProtocol::__format__`
34
+
-`PyContextProtocol::__enter__` and `PyContextProtocol::__exit__`
35
+
-`PyDescrProtocol::__delete__` and `PyDescrProtocol::__set_name__`
36
+
-`PyMappingProtocol::__reversed__`
37
+
-`PyNumberProtocol::__complex__` and `PyNumberProtocol::__round__`
38
+
-`PyAsyncProtocol::__aenter__` and `PyAsyncProtocol::__aexit__`
32
39
33
40
### Removed
34
41
- Remove deprecated exception names `BaseException` etc. [#1426](https://github.com/PyO3/pyo3/pull/1426)
Copy file name to clipboardExpand all lines: guide/src/class/protocols.md
+8-23Lines changed: 8 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,14 @@
1
1
## Class customizations
2
2
3
-
Python's object model defines several protocols for different object behavior, like sequence,
4
-
mapping or number protocols. PyO3 defines separate traits for each of them. To provide specific
5
-
Python object behavior, you need to implement the specific trait for your struct. Important note,
6
-
each protocol implementation block has to be annotated with the `#[pyproto]` attribute.
3
+
PyO3 uses the `#[pyproto]` attribute in combination with special traits to implement certain protocol (aka `__dunder__`) methods of Python classes. The special traits are listed in this chapter of the guide. See also the [documentation for the `pyo3::class` module]({{#PYO3_DOCS_URL}}/pyo3/class/index.html).
7
4
8
-
All `#[pyproto]` methods which can be defined below can return `T` instead of `PyResult<T>` if the
9
-
method implementation is infallible. In addition, if the return type is `()`, it can be omitted altogether.
5
+
Python's object model defines several protocols for different object behavior, such as the sequence, mapping, and number protocols. You may be familiar with implementing these protocols in Python classes by "dunder" methods, such as `__str__` or `__repr__`.
6
+
7
+
In the Python C-API which PyO3 is dependent upon, many of these protocol methods have to be provided into special "slots" on the class type object. To fill these slots PyO3 uses the `#[pyproto]` attribute in combination with special traits.
8
+
9
+
All `#[pyproto]` methods can return `T` instead of `PyResult<T>` if the method implementation is infallible. In addition, if the return type is `()`, it can be omitted altogether.
10
+
11
+
There are many "dunder" methods which are not included in any of PyO3's protocol traits, such as `__dir__`. These methods can be implemented in `#[pymethods]` as already covered in the previous section.
10
12
11
13
### Basic object customization
12
14
@@ -29,15 +31,6 @@ Each method corresponds to Python's `self.attr`, `self.attr = value` and `del se
29
31
30
32
Possible return types for `__str__` and `__repr__` are `PyResult<String>` or `PyResult<PyString>`.
Copy file name to clipboardExpand all lines: guide/src/migration.md
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,47 @@ For projects embedding Python in Rust, PyO3 no longer automatically initalizes a
15
15
16
16
The limitation of the new default implementation is that it cannot support multiple `#[pymethods]` blocks for the same `#[pyclass]`. If you need this functionality, you must enable the `multiple-pymethods` feature which will switch `#[pymethods]` to the inventory-based implementation.
17
17
18
+
### Deprecated `#[pyproto]` methods
19
+
20
+
Some protocol (aka `__dunder__`) methods such as `__bytes__` and `__format__` have been possible to implement two ways in PyO3 for some time: via a `#[pyproto]` (e.g. `PyBasicProtocol` for the methods listed here), or by writing them directly in `#[pymethods]`. This is only true for a handful of the `#[pyproto]` methods (for technical reasons to do with the way PyO3 currently interacts with the Python C-API).
21
+
22
+
In the interest of having onle one way to do things, the `#[pyproto]` forms of these methods have been deprecated.
23
+
24
+
To migrate just move the affected methods from a `#[pyproto]` to a `#[pymethods]` block.
0 commit comments