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: guide/src/migration.md
+37-7Lines changed: 37 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,23 +7,34 @@ For a detailed list of all changes, see the [CHANGELOG](changelog.md).
7
7
8
8
### Default to supporting free-threaded Python
9
9
10
+
<detailsopen>
11
+
<summary><small>Click to expand</small></summary>
12
+
10
13
When PyO3 0.23 added support for free-threaded Python, this was as an opt-in feature for modules by annotating with `#[pymodule(gil_used = false)]`.
11
14
12
15
As the support has matured and PyO3's own API has evolved to remove reliance on the GIL, the time is right to switch the default.
13
16
Modules now automatically allow use on free-threaded Python, unless they directly state they require the GIL with `#[pymodule(gil_used = true)]`.
17
+
</details>
14
18
15
19
### Deprecation of automatic `FromPyObject` for `#[pyclass]` types which implement `Clone`
16
20
21
+
<detailsopen>
22
+
<summary><small>Click to expand</small></summary>
23
+
17
24
`#[pyclass]` types which implement `Clone` used to also implement `FromPyObject` automatically.
18
25
This behaviour is phased out and replaced by an explicit opt-in.
19
26
Affected types will by marked by a deprecation message.
20
27
To migrate use either
21
28
22
29
-`from_py_object` to keep the automatic derive, or
23
30
-`skip_from_py_object` to accept the new behaviour
31
+
</details>
24
32
25
33
### Deprecation of `Py<T>` constructors from raw pointer
26
34
35
+
<detailsopen>
36
+
<summary><small>Click to expand</small></summary>
37
+
27
38
The constructors `Py::from_owned_ptr`, `Py::from_owned_ptr_or_opt`, and `Py::from_owned_ptr_or_err` (and similar "borrowed" variants) perform an unchecked cast to the `Py<T>` target type `T`.
28
39
This unchecked cast is a footgun on APIs where the primary concern is about constructing PyO3's safe smart pointer types correctly from the raw pointer value.
### Removal of `From<Bound<'_, T>` and `From<Py<T>> for PyClassInitializer<T>`
63
76
77
+
<detailsopen>
78
+
<summary><small>Click to expand</small></summary>
79
+
64
80
As part of refactoring the initialization code these impls were removed and its functionality was moved into the generated code for `#[new]`.
65
81
As a small side side effect the following pattern will not be accepted anymore:
66
82
@@ -88,8 +104,13 @@ let obj_2 = existing_bound.clone();
88
104
# })
89
105
```
90
106
107
+
</details>
108
+
91
109
### Internal change to use multi-phase initialization
92
110
111
+
<detailsopen>
112
+
<summary><small>Click to expand</small></summary>
113
+
93
114
[PEP 489](https://peps.python.org/pep-0489/) introduced "multi-phase initialization" for extension modules which provides ways to allocate and clean up per-module state.
94
115
This is a necessary step towards supporting Python "subinterpreters" which run on their own copy of state.
95
116
@@ -98,12 +119,13 @@ The possibility of creating and consuming per-module state (and supporting subin
98
119
This should not require migration, nor is there expected to be breakage caused by the change.
99
120
100
121
Nevertheless, this affects the order of initialization so seemed worth noting in this guide.
122
+
</details>
101
123
102
124
## from 0.26.* to 0.27
103
125
104
126
### `FromPyObject` reworked for flexibility and efficiency
105
127
106
-
<detailsopen>
128
+
<details>
107
129
<summary><small>Click to expand</small></summary>
108
130
109
131
With the removal of the `gil-ref` API in PyO3 0.23 it is now possible to fully split the Python lifetime `'py` and the input lifetime `'a`.
@@ -236,7 +258,10 @@ This is very similar to `serde`s [`Deserialize`] and [`DeserializeOwned`] traits
## `.downcast()` and `DowncastError` replaced with `.cast()` and `CastError`
261
+
### `.downcast()` and `DowncastError` replaced with `.cast()` and `CastError`
262
+
263
+
<details>
264
+
<summary><small>Click to expand</small></summary>
240
265
241
266
The `.downcast()` family of functions were only available on `Bound<PyAny>`.
242
267
In corner cases (particularly related to `.downcast_into()`) this would require use of `.as_any().downcast()` or `.into_any().downcast_into()` chains.
@@ -248,18 +273,23 @@ This produces a nicer experience for both PyO3 module authors and consumers.
248
273
To migrate, replace `.downcast()` with `.cast()` and `DowncastError` with `CastError` (and similar with `.downcast_into()` / `DowncastIntoError` etc).
249
274
250
275
`CastError` requires a Python `type` object (or other "classinfo" object compatible with `isinstance()`) as the second object, so in the rare case where `DowncastError` was manually constructed, small adjustments to code may apply.
276
+
</details>
251
277
252
-
## `PyTypeCheck` is now an `unsafe trait`
278
+
### `PyTypeCheck` is now an `unsafe trait`
279
+
280
+
<details>
281
+
<summary><small>Click to expand</small></summary>
253
282
254
283
Because `PyTypeCheck` is the trait used to guard the `.cast()` functions to treat Python objects as specific concrete types, the trait is `unsafe` to implement.
255
284
256
285
This should always have been the case, it was an unfortunate omission from its original implementation which is being corrected in this release.
286
+
</details>
257
287
258
288
## from 0.25.* to 0.26
259
289
260
290
### Rename of `Python::with_gil`, `Python::allow_threads`, and `pyo3::prepare_freethreaded_python`
261
291
262
-
<detailsopen>
292
+
<details>
263
293
<summary><small>Click to expand</small></summary>
264
294
265
295
The names for these APIs were created when the global interpreter lock (GIL) was mandatory.
@@ -274,7 +304,7 @@ For this reason, we chose to rename these to more modern terminology introduced
274
304
275
305
### Deprecation of `PyObject` type alias
276
306
277
-
<detailsopen>
307
+
<details>
278
308
<summary><small>Click to expand</small></summary>
279
309
280
310
The type alias `PyObject` (aka `Py<PyAny>`) is often confused with the identically named FFI definition `pyo3::ffi::PyObject`.
@@ -284,7 +314,7 @@ To migrate simply replace its usage by the target type `Py<PyAny>`.
284
314
285
315
### Replacement of `GILOnceCell` with `PyOnceLock`
286
316
287
-
<detailsopen>
317
+
<details>
288
318
<summary><small>Click to expand</small></summary>
289
319
290
320
Similar to the above renaming of `Python::with_gil` and related APIs, the `GILOnceCell` type was designed for a Python interpreter which was limited by the GIL.
0 commit comments