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
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
44
44
-`PyErr::new_type` now takes an optional docstring and now returns `PyResult<Py<PyType>>` rather than a `ffi::PyTypeObject` pointer.
45
45
- The `create_exception!` macro can now take an optional docstring. This docstring, if supplied, is visible to users (with `.__doc__` and `help()`) and
46
46
accompanies your error type in your crate's documentation.
47
+
-`__getitem__`, `__setitem__` and `__delitem__` in `#[pymethods]` now implement both a Python mapping and sequence by default. [#2065](https://github.com/PyO3/pyo3/pull/2065)
47
48
- Improve performance and error messages for `#[derive(FromPyObject)]` for enums. [#2068](https://github.com/PyO3/pyo3/pull/2068)
Copy file name to clipboardExpand all lines: guide/src/class/protocols.md
+1-6Lines changed: 1 addition & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,6 @@ In PyO3 0.15, if a function name in `#[pymethods]` is a recognised magic method,
18
18
The magic methods handled by PyO3 are very similar to the standard Python ones on [this page](https://docs.python.org/3/reference/datamodel.html#special-method-names) - in particular they are the the subset which have slots as [defined here](https://docs.python.org/3/c-api/typeobj.html). Some of the slots do not have a magic method in Python, which leads to a few additional magic methods defined only in PyO3:
19
19
- Magic methods for garbage collection
20
20
- Magic methods for the buffer protocol
21
-
- Magic methods for the sequence protocol
22
21
23
22
When PyO3 handles a magic method, a couple of changes apply compared to other `#[pymethods]`:
24
23
- The `#[pyo3(text_signature = "...")]` attribute is not allowed
@@ -86,11 +85,7 @@ given signatures should be interpreted as follows:
Copy file name to clipboardExpand all lines: guide/src/migration.md
+31-1Lines changed: 31 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,40 @@ For a detailed list of all changes, see the [CHANGELOG](changelog.md).
5
5
6
6
## from 0.15.* to 0.16
7
7
8
-
### Drop support for older technogies
8
+
### Drop support for older technologies
9
9
10
10
PyO3 0.16 has increased minimum Rust version to 1.48 and minimum Python version to 3.7. This enables ore use of newer language features (enabling some of the other additions in 0.16) and simplifies maintenance of the project.
11
11
12
+
### Container magic methods now match Python behavior
13
+
14
+
In PyO3 0.15, `__getitem__`, `__setitem__` and `__delitem__` in `#[pymethods]` would generate only the _mapping_ implementation for a `#[pyclass]`. To match the Python behavior, these methods now generate both the _mapping_**and**_sequence_ implementations.
15
+
16
+
This means that classes implementing these `#[pymethods]` will now also be treated as sequences, same as a Python `class` would be. Small differences in behavior may result:
17
+
- PyO3 will allow instances of these classes to be cast to `PySequence` as well as `PyMapping`.
18
+
- Python will provide a default implementation of `__iter__` (if the class did not have one) which repeatedly calls `__getitem__` with integers (starting at 0) until an `IndexError` is raised.
19
+
20
+
To explain this in detail, consider the following Python class:
21
+
22
+
```python
23
+
classExampleContainer:
24
+
25
+
def__len__(self):
26
+
return5
27
+
28
+
def__getitem__(self, idx: int) -> int:
29
+
if idx <0or idx >5:
30
+
raiseIndexError()
31
+
return idx
32
+
```
33
+
34
+
This class implements a Python [sequence](https://docs.python.org/3/glossary.html#term-sequence).
35
+
36
+
The `__len__` and `__getitem__` methods are also used to implement a Python [mapping](https://docs.python.org/3/glossary.html#term-mapping). In the Python C-API, these methods are not shared: the sequence `__len__` and `__getitem__` are defined by the `sq_len` and `sq_item` slots, and the mapping equivalents are `mp_len` and `mp_subscript`. There are similar distinctions for `__setitem__` and `__delitem__`.
37
+
38
+
Because there is no such distinction from Python, implementing these methods will fill the mapping and sequence slots simultaneously. A Python class with `__len__` implemented, for example, will have both the `sq_len` and `mp_len` slots filled.
39
+
40
+
The PyO3 behavior in 0.16 has been changed to be closer to this Python behavior by default.
0 commit comments