Skip to content

Commit af80134

Browse files
committed
simplify migration guide example
1 parent c1db75d commit af80134

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

guide/src/migration.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -248,32 +248,28 @@ After:
248248
```rust
249249
# use pyo3::prelude::*;
250250
# fn main() {
251-
#[cfg(not(Py_GIL_DISABLED))]
252-
use pyo3::sync::GILProtected;
253251
use pyo3::types::{PyDict, PyNone};
254-
#[cfg(not(Py_GIL_DISABLED))]
255-
use std::cell::RefCell;
256-
#[cfg(Py_GIL_DISABLED)]
257252
use std::sync::Mutex;
258253

259-
#[cfg(not(Py_GIL_DISABLED))]
260-
static OBJECTS: GILProtected<RefCell<Vec<Py<PyDict>>>> =
261-
GILProtected::new(RefCell::new(Vec::new()));
262-
#[cfg(Py_GIL_DISABLED)]
263254
static OBJECTS: Mutex<Vec<Py<PyDict>>> = Mutex::new(Vec::new());
264255

265256
Python::with_gil(|py| {
266257
// stand-in for something that executes arbitrary python code
267258
let d = PyDict::new(py);
268259
d.set_item(PyNone::get(py), PyNone::get(py)).unwrap();
269-
#[cfg(not(Py_GIL_DISABLED))]
270-
OBJECTS.get(py).borrow_mut().push(d.unbind());
271-
#[cfg(Py_GIL_DISABLED)]
260+
// we're not executing python code while holding the lock, so GILProtected
261+
// was never needed
272262
OBJECTS.lock().unwrap().push(d.unbind());
273263
});
274264
# }
275265
```
276266

267+
If you are executing arbitrary Python code while holding the lock, then you will
268+
need to use conditional compilation to use `GILProtected` on GIL-enabled python
269+
builds and mutexes otherwise. Python 3.13 introduces `PyMutex`, which releases
270+
the GIL while the lock is held, so that is another option if you only need to
271+
support newer Python versions.
272+
277273
</details>
278274

279275
## from 0.21.* to 0.22

0 commit comments

Comments
 (0)