diff --git a/src/class/basic.rs b/src/class/basic.rs index 750ebfb0ab7..df969b13b92 100644 --- a/src/class/basic.rs +++ b/src/class/basic.rs @@ -10,12 +10,14 @@ use crate::callback::HashCallbackOutput; use crate::class::methods::PyMethodDef; -use crate::pycell::PyCell; use crate::err::{PyErr, PyResult}; use crate::gil::GILPool; use crate::objectprotocol::ObjectProtocol; +use crate::pycell::PyCell; use crate::types::PyAny; -use crate::{callback, catch_unwind, exceptions, ffi, FromPyObject, IntoPy, PyClass, PyObject, Python}; +use crate::{ + callback, catch_unwind, exceptions, ffi, FromPyObject, IntoPy, PyClass, PyObject, Python, +}; use std::os::raw::c_int; /// Operators for the __richcmp__ method @@ -487,8 +489,9 @@ where where T: for<'p> PyObjectRichcmpProtocol<'p>, { - let py = crate::Python::assume_gil_acquired(); - crate::catch_unwind!(py, { + let py = Python::assume_gil_acquired(); + catch_unwind!(py, { + let _pool = GILPool::new(_py); let slf = py.from_borrowed_ptr::>(slf); let arg = py.from_borrowed_ptr::(arg); @@ -496,7 +499,7 @@ where let op = extract_op(op)?; let arg = arg.extract()?; let result = borrowed_slf.__richcmp__(arg, op).into(); - crate::callback::convert(py, result) + callback::convert(py, result) }) } Some(wrap::) diff --git a/src/class/buffer.rs b/src/class/buffer.rs index 8da6357ab31..f3deaecf33a 100644 --- a/src/class/buffer.rs +++ b/src/class/buffer.rs @@ -6,7 +6,7 @@ //! c-api use crate::err::PyResult; use crate::gil::GILPool; -use crate::{callback, catch_unwind, ffi, PyClass, PyCell, PyRefMut, Python}; +use crate::{callback, catch_unwind, ffi, PyCell, PyClass, PyRefMut, Python}; use std::os::raw::c_int; /// Buffer protocol interface @@ -126,14 +126,11 @@ where where T: for<'p> PyBufferReleaseBufferProtocol<'p>, { - let py = crate::Python::assume_gil_acquired(); - crate::catch_unwind!(py, { - let _pool = crate::GILPool::new(py); + let py = Python::assume_gil_acquired(); + catch_unwind!(py, { + let _pool = GILPool::new(py); let slf = py.from_borrowed_ptr::>(slf); - let result = slf - .try_borrow_mut() - .map_err(crate::PyErr::from) - .and_then(|slf_mut| T::bf_releasebuffer(slf_mut, arg1).into()); + let result = T::bf_releasebuffer(slf.try_borrow_mut()?, arg1).into(); crate::callback::convert(py, result) }) } diff --git a/src/class/macros.rs b/src/class/macros.rs index 769a7ad607f..53e10cfe71b 100644 --- a/src/class/macros.rs +++ b/src/class/macros.rs @@ -185,8 +185,12 @@ macro_rules! py_ternary_func { $crate::catch_unwind!(py, { let _pool = $crate::GILPool::new(py); let slf = py.from_borrowed_ptr::<$crate::PyCell>(slf); - let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1).extract()?; - let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2).extract()?; + let arg1 = py + .from_borrowed_ptr::<$crate::types::PyAny>(arg1) + .extract()?; + let arg2 = py + .from_borrowed_ptr::<$crate::types::PyAny>(arg2) + .extract()?; $crate::callback::convert(py, slf.try_borrow()?.$f(arg1, arg2).into()) }) @@ -216,9 +220,15 @@ macro_rules! py_ternary_num_func { let py = $crate::Python::assume_gil_acquired(); $crate::catch_unwind!(py, { let _pool = $crate::GILPool::new(py); - let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1).extract()?; - let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2).extract()?; - let arg3 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg3).extract()?; + let arg1 = py + .from_borrowed_ptr::<$crate::types::PyAny>(arg1) + .extract()?; + let arg2 = py + .from_borrowed_ptr::<$crate::types::PyAny>(arg2) + .extract()?; + let arg3 = py + .from_borrowed_ptr::<$crate::types::PyAny>(arg3) + .extract()?; let result = $class::$f(arg1, arg2, arg3).into(); $crate::callback::convert(py, result) @@ -311,7 +321,9 @@ macro_rules! py_func_del { if value.is_null() { let slf = py.from_borrowed_ptr::<$crate::PyCell>(slf); - let name = py.from_borrowed_ptr::<$crate::types::PyAny>(name).extract()?; + let name = py + .from_borrowed_ptr::<$crate::types::PyAny>(name) + .extract()?; $crate::callback::convert(py, slf.try_borrow_mut()?.$fn_del(name).into()) } else { Err(PyErr::new::( diff --git a/src/class/sequence.rs b/src/class/sequence.rs index 6c3ab5a310f..baa0b2b0e0a 100644 --- a/src/class/sequence.rs +++ b/src/class/sequence.rs @@ -3,10 +3,12 @@ //! Python Sequence Interface //! Trait and support implementation for implementing sequence +use crate::conversion::{FromPyObject, IntoPy}; use crate::err::{PyErr, PyResult}; +use crate::gil::GILPool; use crate::objectprotocol::ObjectProtocol; use crate::types::PyAny; -use crate::{exceptions, ffi, FromPyObject, IntoPy, PyClass, PyObject}; +use crate::{callback, catch_unwind, exceptions, ffi, PyCell, PyClass, PyObject, Python}; use std::os::raw::c_int; /// Sequence interface @@ -255,10 +257,10 @@ mod sq_ass_item_impl { where T: for<'p> PySequenceSetItemProtocol<'p>, { - let py = crate::Python::assume_gil_acquired(); - crate::catch_unwind!(py, { - let _pool = crate::GILPool::new(py); - let slf = py.from_borrowed_ptr::>(slf); + let py = Python::assume_gil_acquired(); + catch_unwind!(py, { + let _pool = GILPool::new(py); + let slf = py.from_borrowed_ptr::>(slf); if value.is_null() { return Err(PyErr::new::(format!( @@ -271,7 +273,7 @@ mod sq_ass_item_impl { let value = py.from_borrowed_ptr::(value); let value = value.extract()?; let result = slf.__setitem__(key.into(), value).into(); - crate::callback::convert(py, result) + callback::convert(py, result) }) } Some(wrap::) @@ -304,10 +306,10 @@ mod sq_ass_item_impl { where T: for<'p> PySequenceDelItemProtocol<'p>, { - let py = crate::Python::assume_gil_acquired(); - crate::catch_unwind!(py, { - let _pool = crate::GILPool::new(py); - let slf = py.from_borrowed_ptr::>(slf); + let py = Python::assume_gil_acquired(); + catch_unwind!(py, { + let _pool = GILPool::new(py); + let slf = py.from_borrowed_ptr::>(slf); let result = if value.is_null() { slf.borrow_mut().__delitem__(key.into()).into() @@ -318,7 +320,7 @@ mod sq_ass_item_impl { ))) }; - crate::callback::convert(py, result) + callback::convert(py, result) }) } Some(wrap::) @@ -351,10 +353,10 @@ mod sq_ass_item_impl { where T: for<'p> PySequenceSetItemProtocol<'p> + for<'p> PySequenceDelItemProtocol<'p>, { - let py = crate::Python::assume_gil_acquired(); - crate::catch_unwind!(py, { - let _pool = crate::GILPool::new(py); - let slf = py.from_borrowed_ptr::>(slf); + let py = Python::assume_gil_acquired(); + catch_unwind!(py, { + let _pool = GILPool::new(py); + let slf = py.from_borrowed_ptr::>(slf); let result = if value.is_null() { call_mut!(slf, __delitem__; key.into()) @@ -364,7 +366,7 @@ mod sq_ass_item_impl { let value = value.extract()?; slf_.__setitem__(key.into(), value).into() }; - crate::callback::convert(py, result) + callback::convert(py, result) }) } Some(wrap::) @@ -459,7 +461,7 @@ where py_binary_func!( PySequenceInplaceConcatProtocol, T::__inplace_concat__, - *mut crate::ffi::PyObject, + *mut ffi::PyObject, call_mut ) }