From c1db75d8e24b8f94630313184ecf6f87c92a42ed Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 3 Sep 2024 09:50:42 -0600 Subject: [PATCH] fixup migration guide examples --- guide/src/migration.md | 43 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/guide/src/migration.md b/guide/src/migration.md index dfc9bd4e406..b9de0da78a3 100644 --- a/guide/src/migration.md +++ b/guide/src/migration.md @@ -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}; @@ -232,20 +234,20 @@ use std::cell::RefCell; static OBJECTS: GILProtected>>> = 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}; @@ -260,17 +262,16 @@ static OBJECTS: GILProtected>>> = #[cfg(Py_GIL_DISABLED)] static OBJECTS: Mutex>> = 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()); +}); +# } ```