Skip to content

Commit

Permalink
Further cleanup to imports
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Mar 11, 2020
1 parent 914a03d commit f1938d4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
13 changes: 8 additions & 5 deletions src/class/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -487,16 +489,17 @@ 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::<crate::PyCell<T>>(slf);
let arg = py.from_borrowed_ptr::<PyAny>(arg);

let borrowed_slf = slf.try_borrow()?;
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::<T>)
Expand Down
13 changes: 5 additions & 8 deletions src/class/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::<crate::PyCell<T>>(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)
})
}
Expand Down
24 changes: 18 additions & 6 deletions src/class/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>>(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())
})
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -311,7 +321,9 @@ macro_rules! py_func_del {

if value.is_null() {
let slf = py.from_borrowed_ptr::<$crate::PyCell<U>>(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::<exceptions::NotImplementedError, _>(
Expand Down
36 changes: 19 additions & 17 deletions src/class/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::<crate::PyCell<T>>(slf);
let py = Python::assume_gil_acquired();
catch_unwind!(py, {
let _pool = GILPool::new(py);
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);

if value.is_null() {
return Err(PyErr::new::<exceptions::NotImplementedError, _>(format!(
Expand All @@ -271,7 +273,7 @@ mod sq_ass_item_impl {
let value = py.from_borrowed_ptr::<PyAny>(value);
let value = value.extract()?;
let result = slf.__setitem__(key.into(), value).into();
crate::callback::convert(py, result)
callback::convert(py, result)
})
}
Some(wrap::<T>)
Expand Down Expand Up @@ -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::<crate::PyCell<T>>(slf);
let py = Python::assume_gil_acquired();
catch_unwind!(py, {
let _pool = GILPool::new(py);
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);

let result = if value.is_null() {
slf.borrow_mut().__delitem__(key.into()).into()
Expand All @@ -318,7 +320,7 @@ mod sq_ass_item_impl {
)))
};

crate::callback::convert(py, result)
callback::convert(py, result)
})
}
Some(wrap::<T>)
Expand Down Expand Up @@ -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::<crate::PyCell<T>>(slf);
let py = Python::assume_gil_acquired();
catch_unwind!(py, {
let _pool = GILPool::new(py);
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);

let result = if value.is_null() {
call_mut!(slf, __delitem__; key.into())
Expand All @@ -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::<T>)
Expand Down Expand Up @@ -459,7 +461,7 @@ where
py_binary_func!(
PySequenceInplaceConcatProtocol,
T::__inplace_concat__,
*mut crate::ffi::PyObject,
*mut ffi::PyObject,
call_mut
)
}
Expand Down

0 comments on commit f1938d4

Please sign in to comment.