From 49de1f1d409056c4796f80388d355fcc1eafb639 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 10 Jul 2017 00:40:30 +0600 Subject: [PATCH] cleanup cb_unary and gc protcol --- src/callback.rs | 21 ++++++++------------- src/class/buffer.rs | 3 ++- src/class/gc.rs | 16 ++++++---------- src/python.rs | 9 --------- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/callback.rs b/src/callback.rs index ff8436dd..cd76e736 100644 --- a/src/callback.rs +++ b/src/callback.rs @@ -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(location: &str, slf: *mut ffi::PyObject, _c: C, f: F) -> C::R where F: for<'p> FnOnce(Python<'p>, &'p mut Slf) -> PyResult, F: panic::UnwindSafe, - Slf: PyTypeInfo, + Slf: PyTypeInfo + PyDowncastFrom, C: CallbackConverter { let guard = AbortOnDrop(location); let pool = pythonrun::Pool::new(); let ret = panic::catch_unwind(|| { let py = Python::assume_gil_acquired(); - let slf = Py::::from_borrowed_ptr(slf); + let mut slf = py.mut_cast_from_borrowed_ptr::(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(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(location: &str, pub unsafe fn cb_unary_unit(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::::from_borrowed_ptr(slf); + let slf = py.mut_cast_from_borrowed_ptr::(slf); - let result = f(py, slf.as_mut(py)); - py.release(slf); - result + f(py, slf) }); let ret = match ret { Ok(r) => r, diff --git a/src/class/buffer.rs b/src/class/buffer.rs index 72a80162..4dc78967 100644 --- a/src/class/buffer.rs +++ b/src/class/buffer.rs @@ -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 diff --git a/src/class/gc.rs b/src/class/gc.rs index c46ddff5..7e15ae4f 100644 --- a/src/class/gc.rs +++ b/src/class/gc.rs @@ -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) { diff --git a/src/python.rs b/src/python.rs index 0fed0226..5d6d5a44 100644 --- a/src/python.rs +++ b/src/python.rs @@ -401,15 +401,6 @@ impl<'p> Python<'p> { } } } - - /// Release PyResult object - #[inline] - pub fn release_res(self, res: PyResult) where T: IntoPyPointer { - match res { - Ok(ob) => unsafe {ffi::Py_DECREF(ob.into_ptr())}, - Err(e) => e.release(self) - } - } } #[cfg(test)]