reenabled RefFromPyObject trait; fixed ref cnt in PyErr::fetch
This commit is contained in:
parent
52bcc11cd8
commit
467cce6c6d
|
@ -80,24 +80,24 @@ pub trait FromPyObject<'source> : Sized {
|
||||||
where S: PyTypeInfo;
|
where S: PyTypeInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*pub trait RefFromPyObject<'p> : Sized {
|
pub trait RefFromPyObject<'p> {
|
||||||
fn with_extracted<F, R>(py: Python<'p>, obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
|
fn with_extracted<F, R>(obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
|
||||||
where F: FnOnce(Self) -> R;
|
where F: FnOnce(&Self) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'p, T: ?Sized> RefFromPyObject<'p> for T
|
impl <'p, T: ?Sized> RefFromPyObject<'p> for T
|
||||||
where for<'a> &'a T: FromPyObject<'p> + Sized
|
where for<'a> &'a T: FromPyObject<'p> + Sized
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn with_extracted<F, R>(_py: Python<'p>, obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
|
fn with_extracted<F, R>(obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
|
||||||
where F: FnOnce(Self) -> R
|
where F: FnOnce(&Self) -> R
|
||||||
{
|
{
|
||||||
match FromPyObject::extract(obj) {
|
match FromPyObject::extract(obj) {
|
||||||
Ok(val) => Ok(f(val)),
|
Ok(val) => Ok(f(val)),
|
||||||
Err(e) => Err(e)
|
Err(e) => Err(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
|
|
||||||
// Default IntoPyObject implementation
|
// Default IntoPyObject implementation
|
||||||
impl <T> IntoPyObject for T where T: ToPyObject
|
impl <T> IntoPyObject for T where T: ToPyObject
|
||||||
|
|
|
@ -177,10 +177,10 @@ impl PyErr {
|
||||||
ptype: if ptype.is_null() {
|
ptype: if ptype.is_null() {
|
||||||
py.get_ptype::<exc::SystemError>()
|
py.get_ptype::<exc::SystemError>()
|
||||||
} else {
|
} 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),
|
pvalue: PyObject::from_borrowed_pptr_opt(py, pvalue),
|
||||||
ptraceback: PyObject::from_owned_pptr_opt(py, ptraceback)
|
ptraceback: PyObject::from_borrowed_pptr_opt(py, ptraceback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ use ffi;
|
||||||
use python::{AsPy, Python, ToPythonPointer};
|
use python::{AsPy, Python, ToPythonPointer};
|
||||||
use super::{exc, PyObject};
|
use super::{exc, PyObject};
|
||||||
use err::{PyResult, PyErr};
|
use err::{PyResult, PyErr};
|
||||||
use conversion::{ToPyObject}; //RefFromPyObject,
|
use conversion::{ToPyObject, RefFromPyObject};
|
||||||
|
|
||||||
/// Represents a Python string.
|
/// Represents a Python string.
|
||||||
pub struct PyString;
|
pub struct PyString;
|
||||||
|
@ -245,9 +245,9 @@ impl ToPyObject for String {
|
||||||
|
|
||||||
// /// Allows extracting strings from Python objects.
|
// /// Allows extracting strings from Python objects.
|
||||||
// /// Accepts Python `str` and `unicode` objects.
|
// /// Accepts Python `str` and `unicode` objects.
|
||||||
//pyobject_extract!(obj to Cow<'source, str> => {
|
pyobject_extract!(obj to Cow<'source, str> => {
|
||||||
// try!(obj.cast_as::<PyString>()).to_string()
|
try!(obj.cast_as::<PyString>()).to_string()
|
||||||
//});
|
});
|
||||||
|
|
||||||
|
|
||||||
/// Allows extracting strings from Python objects.
|
/// Allows extracting strings from Python objects.
|
||||||
|
@ -258,19 +258,19 @@ pyobject_extract!(obj to String => {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/*impl<'p> RefFromPyObject<'p> for str {
|
impl<'p> RefFromPyObject<'p> for str {
|
||||||
fn with_extracted<F, R>(py: Python<'p>, obj: &Py<'p, PyObject>, f: F) -> PyResult<R>
|
fn with_extracted<F, R>(obj: &'p Py<'p, PyObject>, f: F) -> PyResult<R>
|
||||||
where F: FnOnce(&str) -> 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>>());
|
let s = try!(p.extract::<Cow<str>>());
|
||||||
Ok(f(&s))
|
Ok(f(&s))
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use python::{Python, PythonObject};
|
use python::Python;
|
||||||
use conversion::{ToPyObject, RefFromPyObject};
|
use conversion::{ToPyObject, RefFromPyObject};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -25,7 +25,9 @@ impl<T> PyPtr<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_ref<'p>(self, _py: Python<'p>) -> Py<'p, 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.
|
/// Gets the reference count of this PyPtr object.
|
||||||
|
@ -54,6 +56,7 @@ impl<T> IntoPythonPointer for PyPtr<T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn into_ptr(self) -> *mut ffi::PyObject {
|
fn into_ptr(self) -> *mut ffi::PyObject {
|
||||||
|
println!("INTO PTR: {:?}", self.inner);
|
||||||
let ptr = self.inner;
|
let ptr = self.inner;
|
||||||
std::mem::forget(self);
|
std::mem::forget(self);
|
||||||
ptr
|
ptr
|
||||||
|
|
Loading…
Reference in a new issue