Skip to content

Commit

Permalink
fixup migration guide examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Sep 3, 2024
1 parent e33212c commit c1db75d
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions guide/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ was not needed in the first place.

Before:

```rust,ignore
```rust
# fn main() {
# #[cfg(not(Py_GIL_DISABLED))] {
# use pyo3::prelude::*;
use pyo3::sync::GILProtected;
use pyo3::types::{PyDict, PyNone};
Expand All @@ -232,20 +234,20 @@ use std::cell::RefCell;
static OBJECTS: GILProtected<RefCell<Vec<Py<PyDict>>>> =
GILProtected::new(RefCell::new(Vec::new()));

fn main() {
Python::with_gil(|py: Python| {
let d = PyDict::new(py);
// stand-in for something that executes arbitrary python code
d.set_item(PyNone::get(py), PyNone::get(py)).unwrap();
OBJECTS.get(py).borrow_mut().push(d.unbind());
});
}
Python::with_gil(|py| {
// stand-in for something that executes arbitrary python code
let d = PyDict::new(py);
d.set_item(PyNone::get(py), PyNone::get(py)).unwrap();
OBJECTS.get(py).borrow_mut().push(d.unbind());
});
# }}
```

After:

```rust
use pyo3::prelude::*;
# use pyo3::prelude::*;
# fn main() {
#[cfg(not(Py_GIL_DISABLED))]
use pyo3::sync::GILProtected;
use pyo3::types::{PyDict, PyNone};
Expand All @@ -260,17 +262,16 @@ static OBJECTS: GILProtected<RefCell<Vec<Py<PyDict>>>> =
#[cfg(Py_GIL_DISABLED)]
static OBJECTS: Mutex<Vec<Py<PyDict>>> = Mutex::new(Vec::new());

fn main() {
Python::with_gil(|py| {
let d = PyDict::new(py);
// stand-in for something that executes arbitrary python code
d.set_item(PyNone::get(py), PyNone::get(py)).unwrap();
#[cfg(not(Py_GIL_DISABLED))]
OBJECTS.get(py).borrow_mut().push(d.unbind());
#[cfg(Py_GIL_DISABLED)]
OBJECTS.lock().unwrap().push(d.unbind());
});
}
Python::with_gil(|py| {
// stand-in for something that executes arbitrary python code
let d = PyDict::new(py);
d.set_item(PyNone::get(py), PyNone::get(py)).unwrap();
#[cfg(not(Py_GIL_DISABLED))]
OBJECTS.get(py).borrow_mut().push(d.unbind());
#[cfg(Py_GIL_DISABLED)]
OBJECTS.lock().unwrap().push(d.unbind());
});
# }
```

</details>
Expand Down

0 comments on commit c1db75d

Please sign in to comment.