From 467cce6c6d8f9ac46ae90731d5657fe8165cad55 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 26 May 2017 18:25:34 -0700 Subject: [PATCH] reenabled RefFromPyObject trait; fixed ref cnt in PyErr::fetch --- src/conversion.rs | 12 ++++++------ src/err.rs | 6 +++--- src/objects/string.rs | 18 +++++++++--------- src/pyptr.rs | 5 ++++- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/conversion.rs b/src/conversion.rs index 27105dca..3e56b521 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -80,24 +80,24 @@ pub trait FromPyObject<'source> : Sized { where S: PyTypeInfo; } -/*pub trait RefFromPyObject<'p> : Sized { - fn with_extracted(py: Python<'p>, obj: &'p Py<'p, PyObject>, f: F) -> PyResult - where F: FnOnce(Self) -> R; +pub trait RefFromPyObject<'p> { + fn with_extracted(obj: &'p Py<'p, PyObject>, f: F) -> PyResult + where F: FnOnce(&Self) -> R; } impl <'p, T: ?Sized> RefFromPyObject<'p> for T where for<'a> &'a T: FromPyObject<'p> + Sized { #[inline] - fn with_extracted(_py: Python<'p>, obj: &'p Py<'p, PyObject>, f: F) -> PyResult - where F: FnOnce(Self) -> R + fn with_extracted(obj: &'p Py<'p, PyObject>, f: F) -> PyResult + where F: FnOnce(&Self) -> R { match FromPyObject::extract(obj) { Ok(val) => Ok(f(val)), Err(e) => Err(e) } } -}*/ +} // Default IntoPyObject implementation impl IntoPyObject for T where T: ToPyObject diff --git a/src/err.rs b/src/err.rs index 7a777d8d..f5d69354 100644 --- a/src/err.rs +++ b/src/err.rs @@ -177,10 +177,10 @@ impl PyErr { ptype: if ptype.is_null() { py.get_ptype::() } else { - Py::::from_owned_ptr(py, ptype).into_pptr() + Py::::from_borrowed_ptr(py, ptype).into_pptr() }, - pvalue: PyObject::from_owned_pptr_opt(py, pvalue), - ptraceback: PyObject::from_owned_pptr_opt(py, ptraceback) + pvalue: PyObject::from_borrowed_pptr_opt(py, pvalue), + ptraceback: PyObject::from_borrowed_pptr_opt(py, ptraceback) } } diff --git a/src/objects/string.rs b/src/objects/string.rs index 041e5aeb..79167c72 100644 --- a/src/objects/string.rs +++ b/src/objects/string.rs @@ -13,7 +13,7 @@ use ffi; use python::{AsPy, Python, ToPythonPointer}; use super::{exc, PyObject}; use err::{PyResult, PyErr}; -use conversion::{ToPyObject}; //RefFromPyObject, +use conversion::{ToPyObject, RefFromPyObject}; /// Represents a Python string. pub struct PyString; @@ -245,9 +245,9 @@ impl ToPyObject for String { // /// Allows extracting strings from Python objects. // /// Accepts Python `str` and `unicode` objects. -//pyobject_extract!(obj to Cow<'source, str> => { -// try!(obj.cast_as::()).to_string() -//}); +pyobject_extract!(obj to Cow<'source, str> => { + try!(obj.cast_as::()).to_string() +}); /// Allows extracting strings from Python objects. @@ -258,19 +258,19 @@ pyobject_extract!(obj to String => { }); -/*impl<'p> RefFromPyObject<'p> for str { - fn with_extracted(py: Python<'p>, obj: &Py<'p, PyObject>, f: F) -> PyResult +impl<'p> RefFromPyObject<'p> for str { + fn with_extracted(obj: &'p Py<'p, PyObject>, f: F) -> PyResult where F: FnOnce(&str) -> R { - let p = PyObject::from_borrowed_ptr(py, obj.as_ptr()); + let p = PyObject::from_borrowed_ptr(obj.py(), obj.as_ptr()); let s = try!(p.extract::>()); Ok(f(&s)) } -}*/ +} #[cfg(test)] mod test { - use python::{Python, PythonObject}; + use python::Python; use conversion::{ToPyObject, RefFromPyObject}; #[test] diff --git a/src/pyptr.rs b/src/pyptr.rs index b1a17f1e..c09c8c84 100644 --- a/src/pyptr.rs +++ b/src/pyptr.rs @@ -25,7 +25,9 @@ impl PyPtr { } pub fn into_ref<'p>(self, _py: Python<'p>) -> Py<'p, T> { - Py{inner: self.inner, _t: PhantomData, _py: PhantomData} + let p = Py{inner: self.inner, _t: PhantomData, _py: PhantomData}; + std::mem::forget(self); + p } /// Gets the reference count of this PyPtr object. @@ -54,6 +56,7 @@ impl IntoPythonPointer for PyPtr { #[inline] #[must_use] fn into_ptr(self) -> *mut ffi::PyObject { + println!("INTO PTR: {:?}", self.inner); let ptr = self.inner; std::mem::forget(self); ptr