Merge pull request #1752 from mejrs/with_gil

remove some of python::acquire_gil usage
This commit is contained in:
David Hewitt 2021-08-02 19:56:21 +01:00 committed by GitHub
commit 61aaed711d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 9 deletions

View File

@ -828,9 +828,7 @@ where
T::AsRefTarget: std::fmt::Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let gil = Python::acquire_gil();
let py = gil.python();
std::fmt::Display::fmt(self.as_ref(py), f)
Python::with_gil(|py| std::fmt::Display::fmt(self.as_ref(py), f))
}
}

View File

@ -15,12 +15,13 @@ use crate::{PyDowncastError, PyTryFrom};
/// use pyo3::types::PyIterator;
///
/// # fn main() -> PyResult<()> {
/// let gil = Python::acquire_gil();
/// let py = gil.python();
/// let list = py.eval("iter([1, 2, 3, 4])", None, None)?;
/// let numbers: PyResult<Vec<usize>> = list.iter()?.map(|i| i.and_then(PyAny::extract::<usize>)).collect();
/// let sum: usize = numbers?.iter().sum();
/// assert_eq!(sum, 10);
/// Python::with_gil(|py| -> PyResult<()> {
/// let list = py.eval("iter([1, 2, 3, 4])", None, None)?;
/// let numbers: PyResult<Vec<usize>> = list.iter()?.map(|i| i.and_then(PyAny::extract::<usize>)).collect();
/// let sum: usize = numbers?.iter().sum();
/// assert_eq!(sum, 10);
/// Ok(())
/// });
/// # Ok(())
/// # }
/// ```