reenabled RefFromPyObject trait; fixed ref cnt in PyErr::fetch

This commit is contained in:
Nikolay Kim 2017-05-26 18:25:34 -07:00
parent 52bcc11cd8
commit 467cce6c6d
4 changed files with 22 additions and 19 deletions

View file

@ -80,24 +80,24 @@ pub trait FromPyObject<'source> : Sized {
where S: PyTypeInfo;
}
/*pub trait RefFromPyObject<'p> : Sized {
fn with_extracted<F, R>(py: Python<'p>, obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
where F: FnOnce(Self) -> R;
pub trait RefFromPyObject<'p> {
fn with_extracted<F, R>(obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
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<F, R>(_py: Python<'p>, obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
where F: FnOnce(Self) -> R
fn with_extracted<F, R>(obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
where F: FnOnce(&Self) -> R
{
match FromPyObject::extract(obj) {
Ok(val) => Ok(f(val)),
Err(e) => Err(e)
}
}
}*/
}
// Default IntoPyObject implementation
impl <T> IntoPyObject for T where T: ToPyObject

View file

@ -177,10 +177,10 @@ impl PyErr {
ptype: if ptype.is_null() {
py.get_ptype::<exc::SystemError>()
} else {
Py::<PyType>::from_owned_ptr(py, ptype).into_pptr()
Py::<PyType>::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)
}
}

View file

@ -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::<PyString>()).to_string()
//});
pyobject_extract!(obj to Cow<'source, str> => {
try!(obj.cast_as::<PyString>()).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<F, R>(py: Python<'p>, obj: &Py<'p, PyObject>, f: F) -> PyResult<R>
impl<'p> RefFromPyObject<'p> for str {
fn with_extracted<F, R>(obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
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::<Cow<str>>());
Ok(f(&s))
}
}*/
}
#[cfg(test)]
mod test {
use python::{Python, PythonObject};
use python::Python;
use conversion::{ToPyObject, RefFromPyObject};
#[test]

View file

@ -25,7 +25,9 @@ impl<T> PyPtr<T> {
}
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<T> IntoPythonPointer for PyPtr<T> {
#[inline]
#[must_use]
fn into_ptr(self) -> *mut ffi::PyObject {
println!("INTO PTR: {:?}", self.inner);
let ptr = self.inner;
std::mem::forget(self);
ptr