refactor ToPyObject
This commit is contained in:
parent
b8913a3705
commit
3040ac12b9
|
@ -1,16 +1,16 @@
|
||||||
use ffi;
|
use ffi;
|
||||||
use err::PyResult;
|
use err::PyResult;
|
||||||
use pointers::{pptr, PyPtr};
|
use pointers::pptr;
|
||||||
use python::{Python, ToPythonPointer};
|
use python::{Python, ToPythonPointer};
|
||||||
use objects::{PyObject, PyTuple};
|
use objects::{PyObject, PyTuple};
|
||||||
use token::PyObjectMarker;
|
use native::PyNativeObject;
|
||||||
|
|
||||||
|
|
||||||
/// Conversion trait that allows various objects to be converted into PyObject
|
/// Conversion trait that allows various objects to be converted into PyObject
|
||||||
pub trait ToPyObject {
|
pub trait ToPyObject {
|
||||||
|
|
||||||
/// Converts self into a Python object.
|
/// Converts self into a Python object.
|
||||||
fn to_object<'p>(&self, py: Python<'p>) -> PyPtr<PyObjectMarker>;
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p>;
|
||||||
|
|
||||||
/// Converts self into a Python object and calls the specified closure
|
/// Converts self into a Python object and calls the specified closure
|
||||||
/// on the native FFI pointer underlying the Python object.
|
/// on the native FFI pointer underlying the Python object.
|
||||||
|
@ -113,7 +113,7 @@ impl<T> IntoPyObject for T where T: ToPyObject
|
||||||
impl <'a, T: ?Sized> ToPyObject for &'a T where T: ToPyObject {
|
impl <'a, T: ?Sized> ToPyObject for &'a T where T: ToPyObject {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
default fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
default fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
<T as ToPyObject>::to_object(*self, py)
|
<T as ToPyObject>::to_object(*self, py)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,10 +129,10 @@ impl <'a, T: ?Sized> ToPyObject for &'a T where T: ToPyObject {
|
||||||
/// `Option::None` is converted to Python `None`.
|
/// `Option::None` is converted to Python `None`.
|
||||||
impl <T> ToPyObject for Option<T> where T: ToPyObject {
|
impl <T> ToPyObject for Option<T> where T: ToPyObject {
|
||||||
|
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
match *self {
|
match *self {
|
||||||
Some(ref val) => val.to_object(py),
|
Some(ref val) => val.to_object(py),
|
||||||
None => py.None().into_pobject(),
|
None => py.None().into_object(py),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -150,8 +150,8 @@ impl<T> IntoPyObject for Option<T> where T: IntoPyObject {
|
||||||
|
|
||||||
/// `()` is converted to Python `None`.
|
/// `()` is converted to Python `None`.
|
||||||
impl ToPyObject for () {
|
impl ToPyObject for () {
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
py.None().into_pobject()
|
py.None().into_object(py)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
src/err.rs
21
src/err.rs
|
@ -4,11 +4,10 @@ use std::os::raw::c_char;
|
||||||
use libc;
|
use libc;
|
||||||
|
|
||||||
use ffi;
|
use ffi;
|
||||||
use pointers::{pptr, PyPtr};
|
use pointers::pptr;
|
||||||
use python::{ToPythonPointer, IntoPythonPointer, Python};
|
use python::{ToPythonPointer, IntoPythonPointer, Python};
|
||||||
use objects::{PyObject, PyType, exc};
|
use objects::{PyObject, PyType, exc};
|
||||||
use native::PyNativeObject;
|
use native::PyNativeObject;
|
||||||
use token::PyObjectMarker;
|
|
||||||
use typeob::{PyTypeObject};
|
use typeob::{PyTypeObject};
|
||||||
use conversion::{ToPyObject, ToPyTuple, IntoPyObject};
|
use conversion::{ToPyObject, ToPyTuple, IntoPyObject};
|
||||||
|
|
||||||
|
@ -117,9 +116,9 @@ impl PyErr {
|
||||||
/// Example:
|
/// Example:
|
||||||
/// `return Err(PyErr::new::<exc::TypeError, _>(py, "Error message"));`
|
/// `return Err(PyErr::new::<exc::TypeError, _>(py, "Error message"));`
|
||||||
pub fn new<T, V>(py: Python, value: V) -> PyErr
|
pub fn new<T, V>(py: Python, value: V) -> PyErr
|
||||||
where T: PyTypeObject, V: ToPyObject
|
where T: PyTypeObject, V: IntoPyObject
|
||||||
{
|
{
|
||||||
PyErr::new_helper(py, py.get_type::<T>().park(), value.to_object(py))
|
PyErr::new_helper(py, py.get_type::<T>().park(), value.into_object(py))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets whether an error is present in the Python interpreter's global state.
|
/// Gets whether an error is present in the Python interpreter's global state.
|
||||||
|
@ -184,11 +183,11 @@ impl PyErr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_helper(_py: Python, ty: pptr, value: PyPtr<PyObjectMarker>) -> PyErr {
|
fn new_helper(_py: Python, ty: pptr, value: pptr) -> PyErr {
|
||||||
assert!(unsafe { ffi::PyExceptionClass_Check(ty.as_ptr()) } != 0);
|
assert!(unsafe { ffi::PyExceptionClass_Check(ty.as_ptr()) } != 0);
|
||||||
PyErr {
|
PyErr {
|
||||||
ptype: ty,
|
ptype: ty,
|
||||||
pvalue: Some(value.park()),
|
pvalue: Some(value),
|
||||||
ptraceback: None
|
ptraceback: None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,21 +197,21 @@ impl PyErr {
|
||||||
/// `obj` must be an Python exception instance, the PyErr will use that instance.
|
/// `obj` must be an Python exception instance, the PyErr will use that instance.
|
||||||
/// If `obj` is a Python exception type object, the PyErr will (lazily) create a new instance of that type.
|
/// If `obj` is a Python exception type object, the PyErr will (lazily) create a new instance of that type.
|
||||||
/// Otherwise, a `TypeError` is created instead.
|
/// Otherwise, a `TypeError` is created instead.
|
||||||
pub fn from_instance<O>(py: Python, obj: O) -> PyErr where O: ToPyObject {
|
pub fn from_instance<O>(py: Python, obj: O) -> PyErr where O: IntoPyObject {
|
||||||
PyErr::from_instance_helper(py, obj.to_object(py))
|
PyErr::from_instance_helper(py, obj.into_object(py))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_instance_helper<'p>(py: Python, obj: PyPtr<PyObjectMarker>) -> PyErr {
|
fn from_instance_helper<'p>(py: Python, obj: pptr) -> PyErr {
|
||||||
if unsafe { ffi::PyExceptionInstance_Check(obj.as_ptr()) } != 0 {
|
if unsafe { ffi::PyExceptionInstance_Check(obj.as_ptr()) } != 0 {
|
||||||
PyErr {
|
PyErr {
|
||||||
ptype: unsafe { pptr::from_borrowed_ptr(
|
ptype: unsafe { pptr::from_borrowed_ptr(
|
||||||
ffi::PyExceptionInstance_Class(obj.as_ptr())) },
|
ffi::PyExceptionInstance_Class(obj.as_ptr())) },
|
||||||
pvalue: Some(obj.park()),
|
pvalue: Some(obj),
|
||||||
ptraceback: None
|
ptraceback: None
|
||||||
}
|
}
|
||||||
} else if unsafe { ffi::PyExceptionClass_Check(obj.as_ptr()) } != 0 {
|
} else if unsafe { ffi::PyExceptionClass_Check(obj.as_ptr()) } != 0 {
|
||||||
PyErr {
|
PyErr {
|
||||||
ptype: obj.park(),
|
ptype: obj,
|
||||||
pvalue: None,
|
pvalue: None,
|
||||||
ptraceback: None
|
ptraceback: None
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,7 @@ mod ppptr;
|
||||||
pub use ppptr::{pyptr};
|
pub use ppptr::{pyptr};
|
||||||
|
|
||||||
mod token;
|
mod token;
|
||||||
pub use token::{PyObjectMarker, PythonToken, PythonObjectWithToken, PythonObjectWithGilToken};
|
pub use token::{PythonToken, PythonObjectWithToken, PythonObjectWithGilToken};
|
||||||
|
|
||||||
pub use err::{PyErr, PyResult, PyDowncastError};
|
pub use err::{PyErr, PyResult, PyDowncastError};
|
||||||
pub use objects::*;
|
pub use objects::*;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
// Copyright (c) 2017-present PyO3 Project and Contributors
|
// Copyright (c) 2017-present PyO3 Project and Contributors
|
||||||
|
|
||||||
use pointers::{pptr, PyPtr};
|
use pointers::pptr;
|
||||||
use token::PyObjectMarker;
|
|
||||||
use objects::PyObject;
|
use objects::PyObject;
|
||||||
|
|
||||||
pub trait PyBaseObject : Sized {}
|
pub trait PyBaseObject : Sized {}
|
||||||
|
@ -12,8 +11,6 @@ pub trait PyNativeObject<'p> : PyBaseObject {
|
||||||
|
|
||||||
fn as_object(self) -> PyObject<'p>;
|
fn as_object(self) -> PyObject<'p>;
|
||||||
|
|
||||||
fn into_object(self) -> PyPtr<PyObjectMarker>;
|
|
||||||
|
|
||||||
fn clone_object(&self) -> Self;
|
fn clone_object(&self) -> Self;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -479,7 +479,7 @@ mod test {
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let one = 1i32.to_object(py).into_object(py);
|
let one = 1i32.to_object(py);
|
||||||
assert_eq!(one.compare(1).unwrap(), Ordering::Equal);
|
assert_eq!(one.compare(1).unwrap(), Ordering::Equal);
|
||||||
assert_eq!(one.compare(2).unwrap(), Ordering::Less);
|
assert_eq!(one.compare(2).unwrap(), Ordering::Less);
|
||||||
assert_eq!(one.compare(0).unwrap(), Ordering::Greater);
|
assert_eq!(one.compare(0).unwrap(), Ordering::Greater);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use ::{pyptr, PyPtr};
|
use pyptr;
|
||||||
use ffi;
|
use ffi;
|
||||||
use token::PyObjectMarker;
|
|
||||||
use python::{ToPythonPointer, Python};
|
use python::{ToPythonPointer, Python};
|
||||||
|
use objects::PyObject;
|
||||||
use native::PyNativeObject;
|
use native::PyNativeObject;
|
||||||
use conversion::ToPyObject;
|
use conversion::ToPyObject;
|
||||||
|
|
||||||
|
@ -29,8 +29,8 @@ impl<'p> PyBool<'p> {
|
||||||
/// Converts a rust `bool` to a Python `bool`.
|
/// Converts a rust `bool` to a Python `bool`.
|
||||||
impl ToPyObject for bool {
|
impl ToPyObject for bool {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
PyBool::new(py, *self).into_object()
|
PyBool::new(py, *self).as_object()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -62,7 +62,7 @@ mod test {
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
assert!(py.True().is_true());
|
assert!(py.True().is_true());
|
||||||
assert_eq!(true, py.True().as_object().extract().unwrap());
|
assert_eq!(true, py.True().as_object().extract().unwrap());
|
||||||
assert!(true.to_object(py).into_object(py) == py.True().as_object());
|
assert!(true.to_object(py) == py.True().as_object());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -71,6 +71,6 @@ mod test {
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
assert!(!py.False().is_true());
|
assert!(!py.False().is_true());
|
||||||
assert_eq!(false, py.False().as_object().extract().unwrap());
|
assert_eq!(false, py.False().as_object().extract().unwrap());
|
||||||
assert!(false.to_object(py).into_object(py) == py.False().as_object());
|
assert!(false.to_object(py) == py.False().as_object());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,7 +93,7 @@ mod test {
|
||||||
assert_eq!(src, bytearray.data());
|
assert_eq!(src, bytearray.data());
|
||||||
|
|
||||||
let ba = bytearray.to_object(py);
|
let ba = bytearray.to_object(py);
|
||||||
let bytearray = PyByteArray::from(ba.as_object(py)).unwrap();
|
let bytearray = PyByteArray::from(&ba).unwrap();
|
||||||
assert_eq!(src.len(), bytearray.len());
|
assert_eq!(src.len(), bytearray.len());
|
||||||
assert_eq!(src, bytearray.data());
|
assert_eq!(src, bytearray.data());
|
||||||
|
|
||||||
|
|
|
@ -2,15 +2,16 @@
|
||||||
//
|
//
|
||||||
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
||||||
|
|
||||||
use ::pyptr;
|
use std::{mem, collections, hash, cmp};
|
||||||
|
|
||||||
use ffi;
|
use ffi;
|
||||||
use pointers::PyPtr;
|
use pyptr;
|
||||||
use python::{Python, ToPythonPointer, PyDowncastInto};
|
use python::{Python, ToPythonPointer, PyDowncastInto};
|
||||||
use conversion::{ToPyObject};
|
use conversion::{ToPyObject};
|
||||||
use objects::{PyObject, PyList};
|
use objects::{PyObject, PyList};
|
||||||
use token::{PyObjectMarker, PythonObjectWithGilToken};
|
use token::PythonObjectWithGilToken;
|
||||||
use err::{self, PyResult, PyErr};
|
use err::{self, PyResult, PyErr};
|
||||||
use std::{mem, collections, hash, cmp};
|
use native::PyNativeObject;
|
||||||
|
|
||||||
/// Represents a Python `dict`.
|
/// Represents a Python `dict`.
|
||||||
pub struct PyDict<'p>(pyptr<'p>);
|
pub struct PyDict<'p>(pyptr<'p>);
|
||||||
|
@ -129,12 +130,12 @@ impl <K, V> ToPyObject for collections::HashMap<K, V>
|
||||||
where K: hash::Hash+cmp::Eq+ToPyObject,
|
where K: hash::Hash+cmp::Eq+ToPyObject,
|
||||||
V: ToPyObject
|
V: ToPyObject
|
||||||
{
|
{
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
let dict = PyDict::new(py);
|
let dict = PyDict::new(py);
|
||||||
for (key, value) in self {
|
for (key, value) in self {
|
||||||
dict.set_item(key, value).unwrap();
|
dict.set_item(key, value).unwrap();
|
||||||
};
|
};
|
||||||
dict.to_object(py)
|
dict.as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,19 +143,19 @@ impl <K, V> ToPyObject for collections::BTreeMap<K, V>
|
||||||
where K: cmp::Eq+ToPyObject,
|
where K: cmp::Eq+ToPyObject,
|
||||||
V: ToPyObject
|
V: ToPyObject
|
||||||
{
|
{
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
let dict = PyDict::new(py);
|
let dict = PyDict::new(py);
|
||||||
for (key, value) in self {
|
for (key, value) in self {
|
||||||
dict.set_item(key, value).unwrap();
|
dict.set_item(key, value).unwrap();
|
||||||
};
|
};
|
||||||
dict.to_object(py)
|
dict.as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
use python::{Python, PyDowncastInto};
|
use python::Python;
|
||||||
use conversion::ToPyObject;
|
use conversion::ToPyObject;
|
||||||
use objects::{PyDict, PyTuple};
|
use objects::{PyDict, PyTuple};
|
||||||
use ::PyDowncastFrom;
|
use ::PyDowncastFrom;
|
||||||
|
@ -166,11 +167,11 @@ mod test {
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let mut v = HashMap::new();
|
let mut v = HashMap::new();
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let dict = PyDict::downcast_from(ob.as_object(py)).unwrap();
|
let dict = PyDict::downcast_from(&ob).unwrap();
|
||||||
assert_eq!(0, dict.len());
|
assert_eq!(0, dict.len());
|
||||||
v.insert(7, 32);
|
v.insert(7, 32);
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let dict2 = PyDict::downcast_from(ob.as_object(py)).unwrap();
|
let dict2 = PyDict::downcast_from(&ob).unwrap();
|
||||||
assert_eq!(1, dict2.len());
|
assert_eq!(1, dict2.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,7 +182,7 @@ mod test {
|
||||||
let mut v = HashMap::new();
|
let mut v = HashMap::new();
|
||||||
v.insert(7, 32);
|
v.insert(7, 32);
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let dict = PyDict::downcast_from(ob.as_object(py)).unwrap();
|
let dict = PyDict::downcast_from(&ob).unwrap();
|
||||||
assert_eq!(true, dict.contains(7i32).unwrap());
|
assert_eq!(true, dict.contains(7i32).unwrap());
|
||||||
assert_eq!(false, dict.contains(8i32).unwrap());
|
assert_eq!(false, dict.contains(8i32).unwrap());
|
||||||
}
|
}
|
||||||
|
@ -193,7 +194,7 @@ mod test {
|
||||||
let mut v = HashMap::new();
|
let mut v = HashMap::new();
|
||||||
v.insert(7, 32);
|
v.insert(7, 32);
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let dict = PyDict::downcast_from(ob.as_object(py)).unwrap();
|
let dict = PyDict::downcast_from(&ob).unwrap();
|
||||||
assert_eq!(32, dict.get_item(7i32).unwrap().extract::<i32>().unwrap());
|
assert_eq!(32, dict.get_item(7i32).unwrap().extract::<i32>().unwrap());
|
||||||
assert_eq!(None, dict.get_item(8i32));
|
assert_eq!(None, dict.get_item(8i32));
|
||||||
}
|
}
|
||||||
|
@ -205,7 +206,7 @@ mod test {
|
||||||
let mut v = HashMap::new();
|
let mut v = HashMap::new();
|
||||||
v.insert(7, 32);
|
v.insert(7, 32);
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let dict = PyDict::downcast_from(ob.as_object(py)).unwrap();
|
let dict = PyDict::downcast_from(&ob).unwrap();
|
||||||
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
|
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
|
||||||
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
|
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
|
||||||
assert_eq!(42i32, dict.get_item(7i32).unwrap().extract::<i32>().unwrap());
|
assert_eq!(42i32, dict.get_item(7i32).unwrap().extract::<i32>().unwrap());
|
||||||
|
@ -219,7 +220,7 @@ mod test {
|
||||||
let mut v = HashMap::new();
|
let mut v = HashMap::new();
|
||||||
v.insert(7, 32);
|
v.insert(7, 32);
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let dict = PyDict::downcast_from(ob.as_object(py)).unwrap();
|
let dict = PyDict::downcast_from(&ob).unwrap();
|
||||||
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
|
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
|
||||||
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
|
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
|
||||||
assert_eq!(32i32, *v.get(&7i32).unwrap()); // not updated!
|
assert_eq!(32i32, *v.get(&7i32).unwrap()); // not updated!
|
||||||
|
@ -234,7 +235,7 @@ mod test {
|
||||||
let mut v = HashMap::new();
|
let mut v = HashMap::new();
|
||||||
v.insert(7, 32);
|
v.insert(7, 32);
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let dict = PyDict::downcast_from(ob.as_object(py)).unwrap();
|
let dict = PyDict::downcast_from(&ob).unwrap();
|
||||||
assert!(dict.del_item(7i32).is_ok());
|
assert!(dict.del_item(7i32).is_ok());
|
||||||
assert_eq!(0, dict.len());
|
assert_eq!(0, dict.len());
|
||||||
assert_eq!(None, dict.get_item(7i32));
|
assert_eq!(None, dict.get_item(7i32));
|
||||||
|
@ -247,7 +248,7 @@ mod test {
|
||||||
let mut v = HashMap::new();
|
let mut v = HashMap::new();
|
||||||
v.insert(7, 32);
|
v.insert(7, 32);
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let dict = PyDict::downcast_from(ob.as_object(py)).unwrap();
|
let dict = PyDict::downcast_from(&ob).unwrap();
|
||||||
assert!(dict.del_item(7i32).is_ok()); // change
|
assert!(dict.del_item(7i32).is_ok()); // change
|
||||||
assert_eq!(32i32, *v.get(&7i32).unwrap()); // not updated!
|
assert_eq!(32i32, *v.get(&7i32).unwrap()); // not updated!
|
||||||
}
|
}
|
||||||
|
@ -260,7 +261,8 @@ mod test {
|
||||||
v.insert(7, 32);
|
v.insert(7, 32);
|
||||||
v.insert(8, 42);
|
v.insert(8, 42);
|
||||||
v.insert(9, 123);
|
v.insert(9, 123);
|
||||||
let dict = PyDict::downcast_into(py, v.to_object(py)).unwrap();
|
let ob = v.to_object(py);
|
||||||
|
let dict = PyDict::downcast_from(&ob).unwrap();
|
||||||
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
|
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
|
||||||
let mut key_sum = 0;
|
let mut key_sum = 0;
|
||||||
let mut value_sum = 0;
|
let mut value_sum = 0;
|
||||||
|
@ -282,7 +284,7 @@ mod test {
|
||||||
v.insert(8, 42);
|
v.insert(8, 42);
|
||||||
v.insert(9, 123);
|
v.insert(9, 123);
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let dict = PyDict::downcast_from(ob.as_object(py)).unwrap();
|
let dict = PyDict::downcast_from(&ob).unwrap();
|
||||||
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
|
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
|
||||||
let mut key_sum = 0;
|
let mut key_sum = 0;
|
||||||
let mut value_sum = 0;
|
let mut value_sum = 0;
|
||||||
|
@ -303,7 +305,7 @@ mod test {
|
||||||
map.insert(1, 1);
|
map.insert(1, 1);
|
||||||
|
|
||||||
let m = map.to_object(py);
|
let m = map.to_object(py);
|
||||||
let py_map = PyDict::downcast_from(m.as_object(py)).unwrap();
|
let py_map = PyDict::downcast_from(&m).unwrap();
|
||||||
|
|
||||||
assert!(py_map.len() == 1);
|
assert!(py_map.len() == 1);
|
||||||
assert!( py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
|
assert!( py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
|
||||||
|
@ -318,7 +320,7 @@ mod test {
|
||||||
map.insert(1, 1);
|
map.insert(1, 1);
|
||||||
|
|
||||||
let m = map.to_object(py);
|
let m = map.to_object(py);
|
||||||
let py_map = PyDict::downcast_from(m.as_object(py)).unwrap();
|
let py_map = PyDict::downcast_from(&m).unwrap();
|
||||||
|
|
||||||
assert!(py_map.len() == 1);
|
assert!(py_map.len() == 1);
|
||||||
assert!( py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
|
assert!( py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
|
||||||
|
|
|
@ -66,7 +66,7 @@ mod tests {
|
||||||
fn vec_iter() {
|
fn vec_iter() {
|
||||||
let gil_guard = Python::acquire_gil();
|
let gil_guard = Python::acquire_gil();
|
||||||
let py = gil_guard.python();
|
let py = gil_guard.python();
|
||||||
let obj = vec![10, 20].to_object(py).into_object(py);
|
let obj = vec![10, 20].to_object(py);
|
||||||
let mut it = obj.iter().unwrap();
|
let mut it = obj.iter().unwrap();
|
||||||
assert_eq!(10, it.next().unwrap().unwrap().extract().unwrap());
|
assert_eq!(10, it.next().unwrap().unwrap().extract().unwrap());
|
||||||
assert_eq!(20, it.next().unwrap().unwrap().extract().unwrap());
|
assert_eq!(20, it.next().unwrap().unwrap().extract().unwrap());
|
||||||
|
|
|
@ -5,10 +5,9 @@
|
||||||
use ::pyptr;
|
use ::pyptr;
|
||||||
use err::{self, PyResult};
|
use err::{self, PyResult};
|
||||||
use ffi::{self, Py_ssize_t};
|
use ffi::{self, Py_ssize_t};
|
||||||
use pointers::PyPtr;
|
|
||||||
use python::{Python, ToPythonPointer, IntoPythonPointer};
|
use python::{Python, ToPythonPointer, IntoPythonPointer};
|
||||||
use objects::PyObject;
|
use objects::PyObject;
|
||||||
use token::{PyObjectMarker, PythonObjectWithGilToken};
|
use token::PythonObjectWithGilToken;
|
||||||
use conversion::{ToPyObject, IntoPyObject};
|
use conversion::{ToPyObject, IntoPyObject};
|
||||||
|
|
||||||
/// Represents a Python `list`.
|
/// Represents a Python `list`.
|
||||||
|
@ -103,21 +102,21 @@ impl <'p> Iterator for PyListIterator<'p> {
|
||||||
|
|
||||||
impl <T> ToPyObject for [T] where T: ToPyObject {
|
impl <T> ToPyObject for [T] where T: ToPyObject {
|
||||||
|
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr = ffi::PyList_New(self.len() as Py_ssize_t);
|
let ptr = ffi::PyList_New(self.len() as Py_ssize_t);
|
||||||
for (i, e) in self.iter().enumerate() {
|
for (i, e) in self.iter().enumerate() {
|
||||||
let obj = e.to_object(py).into_ptr();
|
let obj = e.to_object(py).into_ptr();
|
||||||
ffi::PyList_SetItem(ptr, i as Py_ssize_t, obj);
|
ffi::PyList_SetItem(ptr, i as Py_ssize_t, obj);
|
||||||
}
|
}
|
||||||
PyPtr::from_owned_ptr_or_panic(ptr)
|
PyObject::from_owned_ptr_or_panic(py, ptr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> ToPyObject for Vec<T> where T: ToPyObject {
|
impl <T> ToPyObject for Vec<T> where T: ToPyObject {
|
||||||
|
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
self.as_slice().to_object(py)
|
self.as_slice().to_object(py)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,7 +169,7 @@ mod test {
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = vec![2, 3, 5, 7];
|
let v = vec![2, 3, 5, 7];
|
||||||
let list = PyList::downcast_into(py, v.to_object(py)).unwrap();
|
let list = PyList::downcast_into(py, v.to_object(py)).unwrap();
|
||||||
let val = 42i32.to_object(py).into_object(py);
|
let val = 42i32.to_object(py);
|
||||||
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
|
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
|
||||||
list.set_item(0, val).unwrap();
|
list.set_item(0, val).unwrap();
|
||||||
assert_eq!(42, list.get_item(0).extract::<i32>().unwrap());
|
assert_eq!(42, list.get_item(0).extract::<i32>().unwrap());
|
||||||
|
@ -182,7 +181,7 @@ mod test {
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = vec![2, 3, 5, 7];
|
let v = vec![2, 3, 5, 7];
|
||||||
let list = PyList::downcast_into(py, v.to_object(py)).unwrap();
|
let list = PyList::downcast_into(py, v.to_object(py)).unwrap();
|
||||||
let val = 42i32.to_object(py).into_object(py);
|
let val = 42i32.to_object(py);
|
||||||
assert_eq!(4, list.len());
|
assert_eq!(4, list.len());
|
||||||
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
|
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
|
||||||
list.insert_item(0, val).unwrap();
|
list.insert_item(0, val).unwrap();
|
||||||
|
|
|
@ -57,9 +57,6 @@ macro_rules! pyobject_nativetype(
|
||||||
fn as_object(self) -> $crate::PyObject<'p> {
|
fn as_object(self) -> $crate::PyObject<'p> {
|
||||||
unsafe { $crate::std::mem::transmute(self) }
|
unsafe { $crate::std::mem::transmute(self) }
|
||||||
}
|
}
|
||||||
fn into_object(self) -> $crate::PyPtr<$crate::PyObjectMarker> {
|
|
||||||
unsafe { $crate::std::mem::transmute(self) }
|
|
||||||
}
|
|
||||||
fn clone_object(&self) -> $name<'p> {
|
fn clone_object(&self) -> $name<'p> {
|
||||||
use $crate::{ToPythonPointer, PythonObjectWithGilToken};
|
use $crate::{ToPythonPointer, PythonObjectWithGilToken};
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -177,9 +174,8 @@ macro_rules! pyobject_nativetype(
|
||||||
impl<'a> $crate::ToPyObject for $name<'a>
|
impl<'a> $crate::ToPyObject for $name<'a>
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_object<'p>(&self, _py: $crate::Python<'p>)
|
fn to_object<'p>(&self, py: $crate::Python<'p>) -> $crate::PyObject<'p> {
|
||||||
-> $crate::PyPtr<$crate::PyObjectMarker> {
|
$crate::PyObject::from_borrowed_ptr(py, self.0.as_ptr())
|
||||||
unsafe { $crate::PyPtr::from_borrowed_ptr(self.0.as_ptr()) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -7,13 +7,14 @@ extern crate num_traits;
|
||||||
use self::num_traits::cast::cast;
|
use self::num_traits::cast::cast;
|
||||||
use std::os::raw::{c_long, c_double};
|
use std::os::raw::{c_long, c_double};
|
||||||
|
|
||||||
use ::{PyPtr, pyptr};
|
|
||||||
use ffi;
|
use ffi;
|
||||||
use super::exc;
|
use pyptr;
|
||||||
|
use objects::exc;
|
||||||
use objects::PyObject;
|
use objects::PyObject;
|
||||||
use token::{PyObjectMarker, PythonObjectWithGilToken};
|
use token::PythonObjectWithGilToken;
|
||||||
use python::{ToPythonPointer, Python};
|
use python::{ToPythonPointer, Python};
|
||||||
use err::{PyResult, PyErr};
|
use err::{PyResult, PyErr};
|
||||||
|
use native::PyNativeObject;
|
||||||
use conversion::{ToPyObject, FromPyObject};
|
use conversion::{ToPyObject, FromPyObject};
|
||||||
|
|
||||||
/// Represents a Python `int` object.
|
/// Represents a Python `int` object.
|
||||||
|
@ -53,9 +54,9 @@ impl<'p> PyFloat<'p> {
|
||||||
macro_rules! int_fits_c_long(
|
macro_rules! int_fits_c_long(
|
||||||
($rust_type:ty) => (
|
($rust_type:ty) => (
|
||||||
impl ToPyObject for $rust_type {
|
impl ToPyObject for $rust_type {
|
||||||
fn to_object(&self, _py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
unsafe {
|
unsafe {
|
||||||
PyPtr::from_owned_ptr_or_panic(ffi::PyLong_FromLong(*self as c_long))
|
PyObject::from_owned_ptr_or_panic(py, ffi::PyLong_FromLong(*self as c_long))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,7 +79,7 @@ macro_rules! int_fits_larger_int(
|
||||||
($rust_type:ty, $larger_type:ty) => (
|
($rust_type:ty, $larger_type:ty) => (
|
||||||
impl ToPyObject for $rust_type {
|
impl ToPyObject for $rust_type {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
(*self as $larger_type).to_object(py)
|
(*self as $larger_type).to_object(py)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,10 +109,10 @@ fn err_if_invalid_value<'p, T: PartialEq>
|
||||||
macro_rules! int_convert_u64_or_i64 (
|
macro_rules! int_convert_u64_or_i64 (
|
||||||
($rust_type:ty, $pylong_from_ll_or_ull:expr, $pylong_as_ull_or_ull:expr) => (
|
($rust_type:ty, $pylong_from_ll_or_ull:expr, $pylong_as_ull_or_ull:expr) => (
|
||||||
impl ToPyObject for $rust_type {
|
impl ToPyObject for $rust_type {
|
||||||
|
#[inline]
|
||||||
fn to_object(&self, _py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
unsafe {
|
unsafe {
|
||||||
PyPtr::from_owned_ptr_or_panic($pylong_from_ll_or_ull(*self))
|
PyObject::from_owned_ptr_or_panic(py, $pylong_from_ll_or_ull(*self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,8 +170,8 @@ int_fits_larger_int!(usize, u64);
|
||||||
int_convert_u64_or_i64!(u64, ffi::PyLong_FromUnsignedLongLong, ffi::PyLong_AsUnsignedLongLong);
|
int_convert_u64_or_i64!(u64, ffi::PyLong_FromUnsignedLongLong, ffi::PyLong_AsUnsignedLongLong);
|
||||||
|
|
||||||
impl ToPyObject for f64 {
|
impl ToPyObject for f64 {
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
PyFloat::new(py, *self).to_object(py)
|
PyFloat::new(py, *self).as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,8 +189,8 @@ fn overflow_error(py: Python) -> PyErr {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToPyObject for f32 {
|
impl ToPyObject for f32 {
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
PyFloat::new(py, *self as f64).to_object(py)
|
PyFloat::new(py, *self as f64).as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,7 +211,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let val = 123 as $t1;
|
let val = 123 as $t1;
|
||||||
let obj = val.to_object(py).into_object(py);
|
let obj = val.to_object(py);
|
||||||
assert_eq!(obj.extract::<$t2>().unwrap(), val as $t2);
|
assert_eq!(obj.extract::<$t2>().unwrap(), val as $t2);
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -239,7 +240,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = std::u32::MAX;
|
let v = std::u32::MAX;
|
||||||
let obj = v.to_object(py).into_object(py);
|
let obj = v.to_object(py);
|
||||||
assert_eq!(v, obj.extract::<u32>().unwrap());
|
assert_eq!(v, obj.extract::<u32>().unwrap());
|
||||||
assert_eq!(v as u64, obj.extract::<u64>().unwrap());
|
assert_eq!(v as u64, obj.extract::<u64>().unwrap());
|
||||||
assert!(obj.extract::<i32>().is_err());
|
assert!(obj.extract::<i32>().is_err());
|
||||||
|
@ -250,7 +251,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = std::i64::MAX;
|
let v = std::i64::MAX;
|
||||||
let obj = v.to_object(py).into_object(py);
|
let obj = v.to_object(py);
|
||||||
assert_eq!(v, obj.extract::<i64>().unwrap());
|
assert_eq!(v, obj.extract::<i64>().unwrap());
|
||||||
assert_eq!(v as u64, obj.extract::<u64>().unwrap());
|
assert_eq!(v as u64, obj.extract::<u64>().unwrap());
|
||||||
assert!(obj.extract::<u32>().is_err());
|
assert!(obj.extract::<u32>().is_err());
|
||||||
|
@ -261,7 +262,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = std::i64::MIN;
|
let v = std::i64::MIN;
|
||||||
let obj = v.to_object(py).into_object(py);
|
let obj = v.to_object(py);
|
||||||
assert_eq!(v, obj.extract::<i64>().unwrap());
|
assert_eq!(v, obj.extract::<i64>().unwrap());
|
||||||
assert!(obj.extract::<i32>().is_err());
|
assert!(obj.extract::<i32>().is_err());
|
||||||
assert!(obj.extract::<u64>().is_err());
|
assert!(obj.extract::<u64>().is_err());
|
||||||
|
@ -272,7 +273,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = std::u64::MAX;
|
let v = std::u64::MAX;
|
||||||
let obj = v.to_object(py).into_object(py);
|
let obj = v.to_object(py);
|
||||||
assert_eq!(v, obj.extract::<u64>().unwrap());
|
assert_eq!(v, obj.extract::<u64>().unwrap());
|
||||||
assert!(obj.extract::<i64>().is_err());
|
assert!(obj.extract::<i64>().is_err());
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,12 @@ impl<'p> PyObject<'p> {
|
||||||
unsafe { Ok(PyObject(pyptr::from_owned_ptr_or_err(py, ptr)?)) }
|
unsafe { Ok(PyObject(pyptr::from_owned_ptr_or_err(py, ptr)?)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn from_owned_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject)
|
||||||
|
-> PyObject<'p> {
|
||||||
|
unsafe { PyObject(pyptr::from_owned_ptr_or_panic(py, ptr)) }
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject)
|
pub fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject)
|
||||||
-> Option<PyObject<'p>> {
|
-> Option<PyObject<'p>> {
|
||||||
|
|
|
@ -259,7 +259,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = 42i32;
|
let v = 42i32;
|
||||||
assert!(v.to_object(py).into_object(py).cast_into::<PySequence>(py).is_err());
|
assert!(v.to_object(py).cast_into::<PySequence>(py).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -267,17 +267,17 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = "London Calling";
|
let v = "London Calling";
|
||||||
assert!(v.to_object(py).into_object(py).cast_into::<PySequence>(py).is_ok());
|
assert!(v.to_object(py).cast_into::<PySequence>(py).is_ok());
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_seq_empty() {
|
fn test_seq_empty() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v : Vec<i32> = vec![];
|
let v : Vec<i32> = vec![];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
assert_eq!(0, seq.len().unwrap());
|
assert_eq!(0, seq.len().unwrap());
|
||||||
|
|
||||||
let needle = 7i32.to_object(py).into_object(py);
|
let needle = 7i32.to_object(py);
|
||||||
assert_eq!(false, seq.contains(&needle).unwrap());
|
assert_eq!(false, seq.contains(&needle).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,16 +286,16 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
assert_eq!(6, seq.len().unwrap());
|
assert_eq!(6, seq.len().unwrap());
|
||||||
|
|
||||||
let bad_needle = 7i32.to_object(py).into_object(py);
|
let bad_needle = 7i32.to_object(py);
|
||||||
assert_eq!(false, seq.contains(&bad_needle).unwrap());
|
assert_eq!(false, seq.contains(&bad_needle).unwrap());
|
||||||
|
|
||||||
let good_needle = 8i32.to_object(py).into_object(py);
|
let good_needle = 8i32.to_object(py);
|
||||||
assert_eq!(true, seq.contains(&good_needle).unwrap());
|
assert_eq!(true, seq.contains(&good_needle).unwrap());
|
||||||
|
|
||||||
let type_coerced_needle = 8f32.to_object(py).into_object(py);
|
let type_coerced_needle = 8f32.to_object(py);
|
||||||
assert_eq!(true, seq.contains(&type_coerced_needle).unwrap());
|
assert_eq!(true, seq.contains(&type_coerced_needle).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,7 +304,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
assert_eq!(1, seq.get_item(0).unwrap().extract::<i32>().unwrap());
|
assert_eq!(1, seq.get_item(0).unwrap().extract::<i32>().unwrap());
|
||||||
assert_eq!(1, seq.get_item(1).unwrap().extract::<i32>().unwrap());
|
assert_eq!(1, seq.get_item(1).unwrap().extract::<i32>().unwrap());
|
||||||
assert_eq!(2, seq.get_item(2).unwrap().extract::<i32>().unwrap());
|
assert_eq!(2, seq.get_item(2).unwrap().extract::<i32>().unwrap());
|
||||||
|
@ -328,7 +328,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
assert!(seq.del_item(10).is_err());
|
assert!(seq.del_item(10).is_err());
|
||||||
assert_eq!(1, seq.get_item(0).unwrap().extract::<i32>().unwrap());
|
assert_eq!(1, seq.get_item(0).unwrap().extract::<i32>().unwrap());
|
||||||
assert!(seq.del_item(0).is_ok());
|
assert!(seq.del_item(0).is_ok());
|
||||||
|
@ -351,7 +351,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
assert_eq!(0, seq.index(1i32).unwrap());
|
assert_eq!(0, seq.index(1i32).unwrap());
|
||||||
assert_eq!(2, seq.index(2i32).unwrap());
|
assert_eq!(2, seq.index(2i32).unwrap());
|
||||||
assert_eq!(3, seq.index(3i32).unwrap());
|
assert_eq!(3, seq.index(3i32).unwrap());
|
||||||
|
@ -365,7 +365,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
assert_eq!(2, seq.count(1i32).unwrap());
|
assert_eq!(2, seq.count(1i32).unwrap());
|
||||||
assert_eq!(1, seq.count(2i32).unwrap());
|
assert_eq!(1, seq.count(2i32).unwrap());
|
||||||
assert_eq!(1, seq.count(3i32).unwrap());
|
assert_eq!(1, seq.count(3i32).unwrap());
|
||||||
|
@ -379,7 +379,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
let v : Vec<i32> = vec![1, 1, 2, 3, 5, 8];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
for el in seq.iter().unwrap() {
|
for el in seq.iter().unwrap() {
|
||||||
assert_eq!(v[idx], el.unwrap().extract::<i32>().unwrap());
|
assert_eq!(v[idx], el.unwrap().extract::<i32>().unwrap());
|
||||||
|
@ -393,7 +393,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = vec!["It", "was", "the", "worst", "of", "times"];
|
let v = vec!["It", "was", "the", "worst", "of", "times"];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
|
|
||||||
let bad_needle = "blurst".to_object(py);
|
let bad_needle = "blurst".to_object(py);
|
||||||
assert_eq!(false, seq.contains(bad_needle).unwrap());
|
assert_eq!(false, seq.contains(bad_needle).unwrap());
|
||||||
|
@ -407,7 +407,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v : Vec<i32> = vec![1, 2, 3];
|
let v : Vec<i32> = vec![1, 2, 3];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
let concat_seq = seq.concat(&seq).unwrap();
|
let concat_seq = seq.concat(&seq).unwrap();
|
||||||
assert_eq!(6, concat_seq.len().unwrap());
|
assert_eq!(6, concat_seq.len().unwrap());
|
||||||
let concat_v : Vec<i32> = vec![1, 2, 3, 1, 2, 3];
|
let concat_v : Vec<i32> = vec![1, 2, 3, 1, 2, 3];
|
||||||
|
@ -421,7 +421,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = "string";
|
let v = "string";
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
let concat_seq = seq.concat(&seq).unwrap();
|
let concat_seq = seq.concat(&seq).unwrap();
|
||||||
assert_eq!(12, concat_seq.len().unwrap());
|
assert_eq!(12, concat_seq.len().unwrap());
|
||||||
/*let concat_v = "stringstring".to_owned();
|
/*let concat_v = "stringstring".to_owned();
|
||||||
|
@ -435,7 +435,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = vec!["foo", "bar"];
|
let v = vec!["foo", "bar"];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
let repeat_seq = seq.repeat(3).unwrap();
|
let repeat_seq = seq.repeat(3).unwrap();
|
||||||
assert_eq!(6, repeat_seq.len().unwrap());
|
assert_eq!(6, repeat_seq.len().unwrap());
|
||||||
let repeated = vec!["foo", "bar", "foo", "bar", "foo", "bar"];
|
let repeated = vec!["foo", "bar", "foo", "bar", "foo", "bar"];
|
||||||
|
@ -449,7 +449,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = vec!["foo", "bar"];
|
let v = vec!["foo", "bar"];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
assert!(seq.list().is_ok());
|
assert!(seq.list().is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -467,7 +467,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = ("foo", "bar");
|
let v = ("foo", "bar");
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
assert!(seq.tuple().is_ok());
|
assert!(seq.tuple().is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -476,7 +476,7 @@ mod test {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let v = vec!["foo", "bar"];
|
let v = vec!["foo", "bar"];
|
||||||
let seq = v.to_object(py).into_object(py).cast_into::<PySequence>(py).unwrap();
|
let seq = v.to_object(py).cast_into::<PySequence>(py).unwrap();
|
||||||
assert!(seq.tuple().is_ok());
|
assert!(seq.tuple().is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,14 +3,13 @@
|
||||||
|
|
||||||
use std::{hash, collections};
|
use std::{hash, collections};
|
||||||
use ffi;
|
use ffi;
|
||||||
use pointers::PyPtr;
|
use pyptr;
|
||||||
use python::{Python, ToPythonPointer};
|
use python::{Python, ToPythonPointer};
|
||||||
use conversion::ToPyObject;
|
use conversion::ToPyObject;
|
||||||
use objects::{PyObject, PyIterator};
|
use objects::PyObject;
|
||||||
use err::{self, PyResult, PyErr};
|
use err::{self, PyResult, PyErr};
|
||||||
use pyptr;
|
use native::PyNativeObject;
|
||||||
use token::{PyObjectMarker, PythonObjectWithGilToken};
|
use token::{PythonObjectWithGilToken};
|
||||||
use objectprotocol::ObjectProtocol;
|
|
||||||
|
|
||||||
|
|
||||||
/// Represents a Python `set`
|
/// Represents a Python `set`
|
||||||
|
@ -80,34 +79,29 @@ impl<'p> PySet<'p> {
|
||||||
ffi::PySet_Pop(self.as_ptr()))
|
ffi::PySet_Pop(self.as_ptr()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn iter(&self) -> PyResult<PyIterator<'p>> {
|
|
||||||
Ok(self.to_object(self.gil()).into_object(self.gil()).iter()?)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ToPyObject for collections::HashSet<T>
|
impl<T> ToPyObject for collections::HashSet<T>
|
||||||
where T: hash::Hash + Eq + ToPyObject
|
where T: hash::Hash + Eq + ToPyObject
|
||||||
{
|
{
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
let set = PySet::new::<T>(py, &[]);
|
let set = PySet::new::<T>(py, &[]);
|
||||||
for val in self {
|
for val in self {
|
||||||
set.add(val).unwrap();
|
set.add(val).unwrap();
|
||||||
}
|
}
|
||||||
set.to_object(py)
|
set.as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ToPyObject for collections::BTreeSet<T>
|
impl<T> ToPyObject for collections::BTreeSet<T>
|
||||||
where T: hash::Hash + Eq + ToPyObject
|
where T: hash::Hash + Eq + ToPyObject
|
||||||
{
|
{
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
let set = PySet::new::<T>(py, &[]);
|
let set = PySet::new::<T>(py, &[]);
|
||||||
for val in self {
|
for val in self {
|
||||||
set.add(val).unwrap();
|
set.add(val).unwrap();
|
||||||
}
|
}
|
||||||
set.to_object(py)
|
set.as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,18 +135,15 @@ impl<'p> PyFrozenSet<'p> {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn iter(&self) -> PyResult<PyIterator<'p>> {
|
|
||||||
Ok(self.to_object(self.gil()).into_object(self.gil()).iter()?)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::collections::{HashSet};
|
use std::collections::{HashSet};
|
||||||
use python::{Python, PyDowncastFrom};
|
use python::{Python, PyDowncastInto};
|
||||||
|
use native::PyNativeObject;
|
||||||
use conversion::ToPyObject;
|
use conversion::ToPyObject;
|
||||||
|
use objectprotocol::ObjectProtocol;
|
||||||
use super::{PySet, PyFrozenSet};
|
use super::{PySet, PyFrozenSet};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -171,11 +162,11 @@ mod test {
|
||||||
|
|
||||||
let mut v = HashSet::new();
|
let mut v = HashSet::new();
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let set = PySet::downcast_from(ob.as_object(py)).unwrap();
|
let set = PySet::downcast_into(py, ob.as_object()).unwrap();
|
||||||
assert_eq!(0, set.len());
|
assert_eq!(0, set.len());
|
||||||
v.insert(7);
|
v.insert(7);
|
||||||
let ob = v.to_object(py);
|
let ob = v.to_object(py);
|
||||||
let set2 = PySet::downcast_from(ob.as_object(py)).unwrap();
|
let set2 = PySet::downcast_into(py, ob.as_object()).unwrap();
|
||||||
assert_eq!(1, set2.len());
|
assert_eq!(1, set2.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,13 @@
|
||||||
use std::os::raw::c_long;
|
use std::os::raw::c_long;
|
||||||
|
|
||||||
use ::pyptr;
|
use ::pyptr;
|
||||||
use pointers::PyPtr;
|
|
||||||
use python::{ToPythonPointer, Python};
|
use python::{ToPythonPointer, Python};
|
||||||
use err::{PyErr, PyResult};
|
use err::{PyErr, PyResult};
|
||||||
use ffi::{self, Py_ssize_t};
|
use ffi::{self, Py_ssize_t};
|
||||||
|
use native::PyNativeObject;
|
||||||
|
use objects::PyObject;
|
||||||
use conversion::ToPyObject;
|
use conversion::ToPyObject;
|
||||||
use token::{PyObjectMarker, PythonObjectWithGilToken};
|
use token::PythonObjectWithGilToken;
|
||||||
|
|
||||||
/// Represents a Python `slice` indices
|
/// Represents a Python `slice` indices
|
||||||
pub struct PySliceIndices {
|
pub struct PySliceIndices {
|
||||||
|
@ -78,7 +79,7 @@ impl<'p> PySlice<'p> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToPyObject for PySliceIndices {
|
impl ToPyObject for PySliceIndices {
|
||||||
fn to_object<'p>(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
PySlice::new(py, self.start, self.stop, self.step).to_object(py)
|
PySlice::new(py, self.start, self.stop, self.step).as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,12 +8,13 @@ use std::ascii::AsciiExt;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
|
|
||||||
use ::{PyPtr, pyptr};
|
|
||||||
use ffi;
|
use ffi;
|
||||||
|
use pyptr;
|
||||||
use python::{ToPythonPointer, Python};
|
use python::{ToPythonPointer, Python};
|
||||||
use super::{exc, PyObject};
|
use super::{exc, PyObject};
|
||||||
use token::{PyObjectMarker, PythonObjectWithGilToken};
|
use token::PythonObjectWithGilToken;
|
||||||
use err::{PyResult, PyErr};
|
use err::{PyResult, PyErr};
|
||||||
|
use native::PyNativeObject;
|
||||||
use conversion::{ToPyObject, RefFromPyObject};
|
use conversion::{ToPyObject, RefFromPyObject};
|
||||||
|
|
||||||
/// Represents a Python string.
|
/// Represents a Python string.
|
||||||
|
@ -220,8 +221,8 @@ impl<'p> PyBytes<'p> {
|
||||||
/// See `PyString::new` for details on the conversion.
|
/// See `PyString::new` for details on the conversion.
|
||||||
impl ToPyObject for str {
|
impl ToPyObject for str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
PyString::new(py, self).to_object(py)
|
PyString::new(py, self).as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,8 +230,8 @@ impl ToPyObject for str {
|
||||||
/// See `PyString::new` for details on the conversion.
|
/// See `PyString::new` for details on the conversion.
|
||||||
impl <'a> ToPyObject for Cow<'a, str> {
|
impl <'a> ToPyObject for Cow<'a, str> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
PyString::new(py, self).to_object(py)
|
PyString::new(py, self).as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,8 +239,8 @@ impl <'a> ToPyObject for Cow<'a, str> {
|
||||||
/// See `PyString::new` for details on the conversion.
|
/// See `PyString::new` for details on the conversion.
|
||||||
impl ToPyObject for String {
|
impl ToPyObject for String {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
PyString::new(py, self).to_object(py)
|
PyString::new(py, self).as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,6 +272,7 @@ impl<'p> RefFromPyObject<'p> for str {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use python::Python;
|
use python::Python;
|
||||||
|
use native::PyNativeObject;
|
||||||
use conversion::{ToPyObject, RefFromPyObject};
|
use conversion::{ToPyObject, RefFromPyObject};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -279,7 +281,7 @@ mod test {
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let s = "\u{1F30F}";
|
let s = "\u{1F30F}";
|
||||||
let py_string = s.to_object(py);
|
let py_string = s.to_object(py);
|
||||||
assert_eq!(s, py_string.as_object(py).extract::<String>().unwrap());
|
assert_eq!(s, py_string.as_object().extract::<String>().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -289,7 +291,7 @@ mod test {
|
||||||
let s = "Hello Python";
|
let s = "Hello Python";
|
||||||
let py_string = s.to_object(py);
|
let py_string = s.to_object(py);
|
||||||
let mut called = false;
|
let mut called = false;
|
||||||
RefFromPyObject::with_extracted(&py_string.as_object(py),
|
RefFromPyObject::with_extracted(&py_string.as_object(),
|
||||||
|s2: &str| {
|
|s2: &str| {
|
||||||
assert_eq!(s, s2);
|
assert_eq!(s, s2);
|
||||||
called = true;
|
called = true;
|
||||||
|
|
|
@ -4,13 +4,14 @@
|
||||||
|
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
|
||||||
use ::{PyPtr, pyptr};
|
use pyptr;
|
||||||
use ffi::{self, Py_ssize_t};
|
use ffi::{self, Py_ssize_t};
|
||||||
use err::{PyErr, PyResult};
|
use err::{PyErr, PyResult};
|
||||||
use python::{Python, ToPythonPointer, IntoPythonPointer};
|
use python::{Python, ToPythonPointer, IntoPythonPointer};
|
||||||
use conversion::{FromPyObject, ToPyObject, ToPyTuple, IntoPyObject};
|
use conversion::{FromPyObject, ToPyObject, ToPyTuple, IntoPyObject};
|
||||||
use objects::PyObject;
|
use objects::PyObject;
|
||||||
use token::{PyObjectMarker, PythonObjectWithGilToken};
|
use native::PyNativeObject;
|
||||||
|
use token::PythonObjectWithGilToken;
|
||||||
use super::exc;
|
use super::exc;
|
||||||
|
|
||||||
/// Represents a Python tuple object.
|
/// Represents a Python tuple object.
|
||||||
|
@ -108,10 +109,10 @@ fn wrong_tuple_length(py: Python, t: &PyTuple, expected_length: usize) -> PyErr
|
||||||
|
|
||||||
macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+} => {
|
macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+} => {
|
||||||
impl <$($T: ToPyObject),+> ToPyObject for ($($T,)+) {
|
impl <$($T: ToPyObject),+> ToPyObject for ($($T,)+) {
|
||||||
fn to_object<'p>(&self, py: Python<'p>) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
PyTuple::new(py, &[
|
PyTuple::new(py, &[
|
||||||
$(py_coerce_expr!(self.$n.to_object(py)),)+
|
$(py_coerce_expr!(self.$n.to_object(py)),)+
|
||||||
]).to_object(py)
|
]).as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +126,6 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
|
||||||
|
|
||||||
impl<'s, $($T: FromPyObject<'s>),+> FromPyObject<'s> for ($($T,)+) {
|
impl<'s, $($T: FromPyObject<'s>),+> FromPyObject<'s> for ($($T,)+) {
|
||||||
fn extract(obj: &'s PyObject<'s>) -> PyResult<Self>
|
fn extract(obj: &'s PyObject<'s>) -> PyResult<Self>
|
||||||
//where S: ::typeob::PyTypeInfo
|
|
||||||
{
|
{
|
||||||
let t = try!(obj.cast_as::<PyTuple>());
|
let t = try!(obj.cast_as::<PyTuple>());
|
||||||
let slice = t.as_slice();
|
let slice = t.as_slice();
|
||||||
|
@ -174,8 +174,8 @@ pub struct NoArgs;
|
||||||
/// Converts `NoArgs` to an empty Python tuple.
|
/// Converts `NoArgs` to an empty Python tuple.
|
||||||
impl ToPyObject for NoArgs {
|
impl ToPyObject for NoArgs {
|
||||||
|
|
||||||
fn to_object(&self, py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
PyTuple::empty(py).to_object(py)
|
PyTuple::empty(py).as_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ use err::{PyErr, PyResult, PyDowncastError};
|
||||||
use conversion::{ToPyObject, IntoPyObject};
|
use conversion::{ToPyObject, IntoPyObject};
|
||||||
use objects::PyObject;
|
use objects::PyObject;
|
||||||
use python::{Python, ToPythonPointer, IntoPythonPointer};
|
use python::{Python, ToPythonPointer, IntoPythonPointer};
|
||||||
use token::{PyObjectMarker, PythonObjectWithGilToken};
|
use token::PythonObjectWithGilToken;
|
||||||
use typeob::{PyTypeInfo, PyObjectAlloc};
|
use typeob::{PyTypeInfo, PyObjectAlloc};
|
||||||
|
|
||||||
|
|
||||||
|
@ -123,13 +123,6 @@ impl pptr {
|
||||||
unsafe { std::mem::transmute(self) }
|
unsafe { std::mem::transmute(self) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts `pptr` instance -> PyObject<'p>
|
|
||||||
/// Consumes `self` without calling `Py_DECREF()`
|
|
||||||
#[inline]
|
|
||||||
pub fn into_pobject(self) -> PyPtr<PyObjectMarker> {
|
|
||||||
unsafe { std::mem::transmute(self) }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clone self, Calls Py_INCREF() on the ptr.
|
/// Clone self, Calls Py_INCREF() on the ptr.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn clone_ref(&self, _py: Python) -> pptr {
|
pub fn clone_ref(&self, _py: Python) -> pptr {
|
||||||
|
@ -503,15 +496,6 @@ impl<'p, T> Py<'p, T>
|
||||||
ptr
|
ptr
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts Py<'p, T> -> Py<'p, PyObject>
|
|
||||||
/// Consumes `self` without calling `Py_DECREF()`
|
|
||||||
#[inline]
|
|
||||||
pub fn into_object(self) -> Py<'p, PyObjectMarker> {
|
|
||||||
let p = Py {inner: self.inner, _t: PhantomData, py: self.py};
|
|
||||||
std::mem::forget(self);
|
|
||||||
p
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Converts Py<'p, T> -> PyObject<'p>. Calls Py_INCREF() on the ptr.
|
/// Converts Py<'p, T> -> PyObject<'p>. Calls Py_INCREF() on the ptr.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_pyobject(&self) -> &PyObject<'p> {
|
pub fn as_pyobject(&self) -> &PyObject<'p> {
|
||||||
|
@ -801,8 +785,8 @@ impl<'source, T> ::FromPyObject<'source> for Py<'source, T> where T: PyTypeInfo
|
||||||
impl <'a, T> ToPyObject for Py<'a, T> {
|
impl <'a, T> ToPyObject for Py<'a, T> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
default fn to_object<'p>(&self, _py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
unsafe { PyPtr::from_borrowed_ptr(self.inner) }
|
PyObject::from_borrowed_ptr(py, self.inner)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -816,8 +800,8 @@ impl <'a, T> ToPyObject for Py<'a, T> {
|
||||||
impl<T> ToPyObject for PyPtr<T> {
|
impl<T> ToPyObject for PyPtr<T> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
default fn to_object(&self, _py: Python) -> PyPtr<PyObjectMarker> {
|
fn to_object<'p>(&self, py: Python<'p>) -> PyObject<'p> {
|
||||||
unsafe { PyPtr::from_borrowed_ptr(self.inner) }
|
PyObject::from_borrowed_ptr(py, self.inner)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
63
src/token.rs
63
src/token.rs
|
@ -1,13 +1,9 @@
|
||||||
// Copyright (c) 2017-present PyO3 Project and Contributors
|
// Copyright (c) 2017-present PyO3 Project and Contributors
|
||||||
|
|
||||||
use std::fmt;
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use ffi;
|
use pointers::Py;
|
||||||
use pointers::{Py, PyPtr};
|
use python::Python;
|
||||||
use err::{PyResult};
|
|
||||||
use python::{Python, ToPythonPointer, PyDowncastInto};
|
|
||||||
use objects::PyString;
|
|
||||||
use typeob::{PyTypeInfo, PyObjectAlloc};
|
use typeob::{PyTypeInfo, PyObjectAlloc};
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,58 +36,3 @@ pub trait PythonObjectWithGilToken<'p> : Sized {
|
||||||
pub trait PythonObjectWithToken : Sized {
|
pub trait PythonObjectWithToken : Sized {
|
||||||
fn token<'p>(&'p self) -> Python<'p>;
|
fn token<'p>(&'p self) -> Python<'p>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PyObjectMarker;
|
|
||||||
|
|
||||||
|
|
||||||
impl PyObjectMarker {
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn from_owned_ptr(_py: Python, ptr: *mut ffi::PyObject) -> PyPtr<PyObjectMarker> {
|
|
||||||
unsafe { PyPtr::from_owned_ptr(ptr) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject)
|
|
||||||
-> PyResult<PyPtr<PyObjectMarker>> {
|
|
||||||
unsafe { PyPtr::from_owned_ptr_or_err(py, ptr) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn from_owned_ptr_or_opt(py: Python, ptr: *mut ffi::PyObject)
|
|
||||||
-> Option<PyPtr<PyObjectMarker>> {
|
|
||||||
unsafe { PyPtr::from_owned_ptr_or_opt(py, ptr) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn from_borrowed_ptr(_py: Python, ptr: *mut ffi::PyObject) -> PyPtr<PyObjectMarker> {
|
|
||||||
unsafe { PyPtr::from_borrowed_ptr(ptr) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl<'p> fmt::Debug for PyPtr<PyObjectMarker> {
|
|
||||||
default fn fmt(&self, f : &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
||||||
let gil = Python::acquire_gil();
|
|
||||||
let py = gil.python();
|
|
||||||
|
|
||||||
let repr_obj = unsafe {
|
|
||||||
PyString::downcast_from_owned_ptr(py, ffi::PyObject_Repr(self.as_ptr()))
|
|
||||||
.map_err(|_| fmt::Error)?
|
|
||||||
};
|
|
||||||
f.write_str(&repr_obj.to_string_lossy())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'p> fmt::Display for PyPtr<PyObjectMarker> {
|
|
||||||
default fn fmt(&self, f : &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
||||||
let gil = Python::acquire_gil();
|
|
||||||
let py = gil.python();
|
|
||||||
|
|
||||||
let str_obj = unsafe {
|
|
||||||
PyString::downcast_from_owned_ptr(py, ffi::PyObject_Str(self.as_ptr()))
|
|
||||||
.map_err(|_| fmt::Error)?
|
|
||||||
};
|
|
||||||
f.write_str(&str_obj.to_string_lossy())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -26,16 +26,16 @@ struct Test {}
|
||||||
#[py::proto]
|
#[py::proto]
|
||||||
impl<'p> PyMappingProtocol<'p> for Test
|
impl<'p> PyMappingProtocol<'p> for Test
|
||||||
{
|
{
|
||||||
fn __getitem__(&self, py: Python, idx: PyObject<'p>) -> PyResult<PyPtr<PyObjectMarker>> {
|
fn __getitem__(&self, py: Python, idx: PyObject<'p>) -> PyResult<pptr> {
|
||||||
if let Ok(slice) = idx.cast_as::<PySlice>() {
|
if let Ok(slice) = idx.cast_as::<PySlice>() {
|
||||||
let indices = slice.indices(1000)?;
|
let indices = slice.indices(1000)?;
|
||||||
if indices.start == 100 && indices.stop == 200 && indices.step == 1 {
|
if indices.start == 100 && indices.stop == 200 && indices.step == 1 {
|
||||||
return Ok("slice".to_object(py))
|
return Ok("slice".into_object(py))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if let Ok(idx) = idx.extract::<isize>() {
|
else if let Ok(idx) = idx.extract::<isize>() {
|
||||||
if idx == 1 {
|
if idx == 1 {
|
||||||
return Ok("int".to_object(py))
|
return Ok("int".into_object(py))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(PyErr::new::<exc::ValueError, _>(py, "error"))
|
Err(PyErr::new::<exc::ValueError, _>(py, "error"))
|
||||||
|
|
Loading…
Reference in a new issue