cleanup cb_unary and gc protcol

This commit is contained in:
Nikolay Kim 2017-07-10 00:40:30 +06:00
parent ce05cb91c0
commit 49de1f1d40
4 changed files with 16 additions and 33 deletions

View File

@ -7,12 +7,11 @@ use std::{any, mem, ptr, isize, io, panic};
use libc;
use pythonrun;
use python::{Python, IntoPyPointer};
use python::{Python, PyDowncastFrom, IntoPyPointer};
use objects::exc;
use conversion::IntoPyObject;
use ffi::{self, Py_hash_t};
use err::{PyErr, PyResult};
use instance::{Py, AsPyRef};
use typeob::PyTypeInfo;
@ -180,16 +179,16 @@ pub unsafe fn cb_unary<Slf, F, T, C>(location: &str,
slf: *mut ffi::PyObject, _c: C, f: F) -> C::R
where F: for<'p> FnOnce(Python<'p>, &'p mut Slf) -> PyResult<T>,
F: panic::UnwindSafe,
Slf: PyTypeInfo,
Slf: PyTypeInfo + PyDowncastFrom,
C: CallbackConverter<T>
{
let guard = AbortOnDrop(location);
let pool = pythonrun::Pool::new();
let ret = panic::catch_unwind(|| {
let py = Python::assume_gil_acquired();
let slf = Py::<Slf>::from_borrowed_ptr(slf);
let mut slf = py.mut_cast_from_borrowed_ptr::<Slf>(slf);
let result = match f(py, slf.as_mut(py)) {
match f(py, slf) {
Ok(val) => {
C::convert(val, py)
}
@ -197,9 +196,7 @@ pub unsafe fn cb_unary<Slf, F, T, C>(location: &str,
e.restore(py);
C::error_value()
}
};
py.release(slf);
result
}
});
let ret = match ret {
Ok(r) => r,
@ -217,17 +214,15 @@ pub unsafe fn cb_unary<Slf, F, T, C>(location: &str,
pub unsafe fn cb_unary_unit<Slf, F>(location: &str, slf: *mut ffi::PyObject, f: F) -> c_int
where F: for<'p> FnOnce(Python<'p>, &'p mut Slf) -> c_int,
F: panic::UnwindSafe,
Slf: PyTypeInfo,
Slf: PyTypeInfo + PyDowncastFrom,
{
let guard = AbortOnDrop(location);
let pool = pythonrun::Pool::new();
let ret = panic::catch_unwind(|| {
let py = Python::assume_gil_acquired();
let slf = Py::<Slf>::from_borrowed_ptr(slf);
let slf = py.mut_cast_from_borrowed_ptr::<Slf>(slf);
let result = f(py, slf.as_mut(py));
py.release(slf);
result
f(py, slf)
});
let ret = match ret {
Ok(r) => r,

View File

@ -10,12 +10,13 @@ use std::os::raw::c_int;
use ffi;
use err::PyResult;
use typeob::PyTypeInfo;
use python::PyDowncastFrom;
use callback::UnitCallbackConverter;
/// Buffer protocol interface
#[allow(unused_variables)]
pub trait PyBufferProtocol<'p> : PyTypeInfo + Sized + 'static
pub trait PyBufferProtocol<'p> : PyTypeInfo + PyDowncastFrom
{
fn bf_getbuffer(&'p self,
view: *mut ffi::Py_buffer, flags: c_int) -> Self::Result

View File

@ -7,14 +7,14 @@ use std::os::raw::{c_int, c_void};
use ffi;
use callback;
use python::{Python, ToPyPointer};
use python::{Python, ToPyPointer, PyDowncastFrom};
use typeob::PyTypeInfo;
pub struct PyTraverseError(c_int);
/// GC support
#[allow(unused_variables)]
pub trait PyGCProtocol<'p> : PyTypeInfo {
pub trait PyGCProtocol<'p> : PyTypeInfo + PyDowncastFrom {
fn __traverse__(&'p self, visit: PyVisit)
-> Result<(), PyTraverseError> { unimplemented!() }
@ -26,19 +26,15 @@ pub trait PyGCProtocol<'p> : PyTypeInfo {
pub trait PyGCTraverseProtocol<'p>: PyGCProtocol<'p> {}
pub trait PyGCClearProtocol<'p>: PyGCProtocol<'p> {}
impl<'p, T> PyGCProtocol<'p> for T where T: PyTypeInfo {
default fn __traverse__(&'p self, _: PyVisit) -> Result<(), PyTraverseError> {
Ok(())
}
default fn __clear__(&'p mut self) {}
}
#[doc(hidden)]
pub trait PyGCProtocolImpl {
fn update_type_object(type_object: &mut ffi::PyTypeObject);
}
impl<'p, T> PyGCProtocolImpl for T {
default fn update_type_object(_type_object: &mut ffi::PyTypeObject) {}
}
impl<'p, T> PyGCProtocolImpl for T where T: PyGCProtocol<'p>
{
fn update_type_object(type_object: &mut ffi::PyTypeObject) {

View File

@ -401,15 +401,6 @@ impl<'p> Python<'p> {
}
}
}
/// Release PyResult<PyObject> object
#[inline]
pub fn release_res<T>(self, res: PyResult<T>) where T: IntoPyPointer {
match res {
Ok(ob) => unsafe {ffi::Py_DECREF(ob.into_ptr())},
Err(e) => e.release(self)
}
}
}
#[cfg(test)]