rename PyInstance to PyObjectRef
This commit is contained in:
parent
4e836cb229
commit
3a2004eab2
|
@ -13,7 +13,7 @@ pub fn build_py_class(ast: &mut syn::DeriveInput, attr: String) -> Tokens {
|
|||
let params = parse_attribute(attr);
|
||||
let doc = utils::get_doc(&ast.attrs, true);
|
||||
|
||||
let base = syn::Ident::from("_pyo3::PyInstance");
|
||||
let base = syn::Ident::from("_pyo3::PyObjectRef");
|
||||
let mut token: Option<syn::Ident> = None;
|
||||
|
||||
match ast.body {
|
||||
|
@ -98,8 +98,8 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
|
|||
unsafe { _pyo3::PyObject::from_borrowed_ptr(py, self.as_ptr()) }
|
||||
}
|
||||
}
|
||||
impl std::convert::AsRef<PyInstance> for #cls {
|
||||
fn as_ref(&self) -> &_pyo3::PyInstance {
|
||||
impl std::convert::AsRef<PyObjectRef> for #cls {
|
||||
fn as_ref(&self) -> &_pyo3::PyObjectRef {
|
||||
unsafe{std::mem::transmute(self.as_ptr())}
|
||||
}
|
||||
}
|
||||
|
@ -223,7 +223,7 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
|
|||
|
||||
impl _pyo3::PyDowncastFrom for #cls
|
||||
{
|
||||
fn downcast_from(ob: &_pyo3::PyInstance) -> Result<&#cls, _pyo3::PyDowncastError>
|
||||
fn downcast_from(ob: &_pyo3::PyObjectRef) -> Result<&#cls, _pyo3::PyDowncastError>
|
||||
{
|
||||
unsafe {
|
||||
let checked = ffi::PyObject_TypeCheck(
|
||||
|
@ -239,13 +239,13 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
|
|||
}
|
||||
}
|
||||
|
||||
unsafe fn unchecked_downcast_from(ob: &_pyo3::PyInstance) -> &Self
|
||||
unsafe fn unchecked_downcast_from(ob: &_pyo3::PyObjectRef) -> &Self
|
||||
{
|
||||
let offset = <#cls as _pyo3::typeob::PyTypeInfo>::offset();
|
||||
let ptr = (ob.as_ptr() as *mut u8).offset(offset) as *mut #cls;
|
||||
std::mem::transmute(ptr)
|
||||
}
|
||||
unsafe fn unchecked_mut_downcast_from(ob: &_pyo3::PyInstance) -> &mut Self
|
||||
unsafe fn unchecked_mut_downcast_from(ob: &_pyo3::PyObjectRef) -> &mut Self
|
||||
{
|
||||
let offset = <#cls as _pyo3::typeob::PyTypeInfo>::offset();
|
||||
let ptr = (ob.as_ptr() as *mut u8).offset(offset) as *mut #cls;
|
||||
|
@ -254,7 +254,7 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
|
|||
}
|
||||
impl _pyo3::PyMutDowncastFrom for #cls
|
||||
{
|
||||
fn downcast_mut_from(ob: &mut _pyo3::PyInstance)
|
||||
fn downcast_mut_from(ob: &mut _pyo3::PyObjectRef)
|
||||
-> Result<&mut #cls, _pyo3::PyDowncastError>
|
||||
{
|
||||
unsafe {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
use ffi;
|
||||
use python::Python;
|
||||
use objects::{PyInstance, PyTuple, PyDict, PyString, exc};
|
||||
use objects::{PyObjectRef, PyTuple, PyDict, PyString, exc};
|
||||
//use conversion::RefFromPyObject;
|
||||
use err::{self, PyResult};
|
||||
|
||||
|
@ -32,7 +32,7 @@ pub fn parse_args<'p>(py: Python<'p>,
|
|||
fname: Option<&str>, params: &[ParamDescription],
|
||||
args: &'p PyTuple, kwargs: Option<&'p PyDict>,
|
||||
accept_args: bool, accept_kwargs: bool,
|
||||
output: &mut[Option<&'p PyInstance>]) -> PyResult<()>
|
||||
output: &mut[Option<&'p PyObjectRef>]) -> PyResult<()>
|
||||
{
|
||||
|
||||
|
||||
|
@ -107,7 +107,7 @@ pub unsafe fn get_kwargs<'p>(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<
|
|||
|
||||
/*#[doc(hidden)] // used in py_argparse_extract!() macro
|
||||
pub fn with_extracted_or_default<P: ?Sized, R, F>(
|
||||
py: Python, obj: Option<&PyInstance>, f: F, default: &'static P) -> PyResult<R>
|
||||
py: Python, obj: Option<&PyObjectRef>, f: F, default: &'static P) -> PyResult<R>
|
||||
where F: FnOnce(&P) -> PyResult<R>,
|
||||
P: RefFromPyObject
|
||||
{
|
||||
|
|
|
@ -26,7 +26,7 @@ use ffi;
|
|||
use exc;
|
||||
use err::{self, PyResult};
|
||||
use python::{Python, ToPyPointer};
|
||||
use objects::PyInstance;
|
||||
use objects::PyObjectRef;
|
||||
|
||||
/// Allows access to the underlying buffer used by a python object such as `bytes`, `bytearray` or `array.array`.
|
||||
pub struct PyBuffer(Box<ffi::Py_buffer>); // use Box<> because Python expects that the Py_buffer struct has a stable memory address
|
||||
|
@ -139,7 +139,7 @@ fn validate(b: &ffi::Py_buffer) {
|
|||
|
||||
impl PyBuffer {
|
||||
/// Get the underlying buffer from the specified python object.
|
||||
pub fn get(py: Python, obj: &PyInstance) -> PyResult<PyBuffer> {
|
||||
pub fn get(py: Python, obj: &PyObjectRef) -> PyResult<PyBuffer> {
|
||||
unsafe {
|
||||
let mut buf = Box::new(mem::zeroed::<ffi::Py_buffer>());
|
||||
err::error_on_minusone(
|
||||
|
|
|
@ -14,7 +14,7 @@ use ffi;
|
|||
use callback;
|
||||
use err::{PyErr, PyResult};
|
||||
use python::{Python, IntoPyPointer, PyDowncastFrom};
|
||||
use objects::{exc, PyInstance};
|
||||
use objects::{exc, PyObjectRef};
|
||||
use typeob::PyTypeInfo;
|
||||
use conversion::{FromPyObject, IntoPyObject};
|
||||
use objectprotocol::ObjectProtocol;
|
||||
|
@ -368,7 +368,7 @@ impl<T> PyObjectRichcmpProtocolImpl for T
|
|||
|
||||
callback::cb_meth(LOCATION, |py| {
|
||||
let slf = py.cast_from_borrowed_ptr::<T>(slf);
|
||||
let arg = py.cast_from_borrowed_ptr::<PyInstance>(arg);
|
||||
let arg = py.cast_from_borrowed_ptr::<PyObjectRef>(arg);
|
||||
|
||||
let res = match extract_op(py, op) {
|
||||
Ok(op) => match arg.extract() {
|
||||
|
|
|
@ -10,7 +10,7 @@ use std::os::raw::c_int;
|
|||
use ffi;
|
||||
use err::PyResult;
|
||||
use python::PyDowncastFrom;
|
||||
use objects::{PyType, PyInstance};
|
||||
use objects::{PyType, PyObjectRef};
|
||||
use callback::{PyObjectCallbackConverter, UnitCallbackConverter};
|
||||
use typeob::PyTypeInfo;
|
||||
use class::methods::PyMethodDef;
|
||||
|
@ -21,16 +21,16 @@ use conversion::{IntoPyObject, FromPyObject};
|
|||
#[allow(unused_variables)]
|
||||
pub trait PyDescrProtocol<'p>: PyTypeInfo + PyDowncastFrom {
|
||||
|
||||
fn __get__(&'p self, instance: &'p PyInstance, owner: Option<&'p PyType>)
|
||||
fn __get__(&'p self, instance: &'p PyObjectRef, owner: Option<&'p PyType>)
|
||||
-> Self::Result where Self: PyDescrGetProtocol<'p> { unimplemented!() }
|
||||
|
||||
fn __set__(&'p self, instance: &'p PyInstance, value: &'p PyInstance)
|
||||
fn __set__(&'p self, instance: &'p PyObjectRef, value: &'p PyObjectRef)
|
||||
-> Self::Result where Self: PyDescrSetProtocol<'p> { unimplemented!() }
|
||||
|
||||
fn __delete__(&'p self, instance: &'p PyInstance)
|
||||
fn __delete__(&'p self, instance: &'p PyObjectRef)
|
||||
-> Self::Result where Self: PyDescrDeleteProtocol<'p> { unimplemented!() }
|
||||
|
||||
fn __set_name__(&'p self, instance: &'p PyInstance)
|
||||
fn __set_name__(&'p self, instance: &'p PyObjectRef)
|
||||
-> Self::Result where Self: PyDescrSetNameProtocol<'p> { unimplemented!() }
|
||||
}
|
||||
|
||||
|
|
|
@ -78,7 +78,7 @@ macro_rules! py_binary_func{
|
|||
|
||||
$crate::callback::cb_pyfunc::<_, _, $res_type>(LOCATION, $conv, |py| {
|
||||
let slf = py.mut_cast_from_borrowed_ptr::<T>(slf);
|
||||
let arg = py.cast_from_borrowed_ptr::<$crate::PyInstance>(arg);
|
||||
let arg = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(arg);
|
||||
|
||||
let result = match arg.extract() {
|
||||
Ok(arg) => slf.$f(arg).into(),
|
||||
|
@ -105,7 +105,7 @@ macro_rules! py_binary_self_func{
|
|||
|
||||
$crate::callback::cb_meth(LOCATION, |py| {
|
||||
let slf1 = py.mut_cast_from_borrowed_ptr::<T>(slf);
|
||||
let arg = py.cast_from_borrowed_ptr::<$crate::PyInstance>(arg);
|
||||
let arg = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(arg);
|
||||
|
||||
let result = match arg.extract() {
|
||||
Ok(arg) => slf1.$f(arg).into(),
|
||||
|
@ -166,8 +166,8 @@ macro_rules! py_ternary_func{
|
|||
|
||||
$crate::callback::cb_pyfunc::<_, _, $res_type>(LOCATION, $conv, |py| {
|
||||
let slf = py.mut_cast_from_borrowed_ptr::<T>(slf);
|
||||
let arg1 = py.cast_from_borrowed_ptr::<$crate::PyInstance>(arg1);
|
||||
let arg2 = py.cast_from_borrowed_ptr::<$crate::PyInstance>(arg2);
|
||||
let arg1 = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(arg1);
|
||||
let arg2 = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(arg2);
|
||||
|
||||
let result = match arg1.extract() {
|
||||
Ok(arg1) => match arg2.extract() {
|
||||
|
@ -199,8 +199,8 @@ macro_rules! py_ternary_self_func{
|
|||
|
||||
$crate::callback::cb_meth(LOCATION, |py| {
|
||||
let slf1 = py.mut_cast_from_borrowed_ptr::<T>(slf);
|
||||
let arg1 = py.cast_from_borrowed_ptr::<$crate::PyInstance>(arg1);
|
||||
let arg2 = py.cast_from_borrowed_ptr::<$crate::PyInstance>(arg2);
|
||||
let arg1 = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(arg1);
|
||||
let arg2 = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(arg2);
|
||||
|
||||
let result = match arg1.extract() {
|
||||
Ok(arg1) => match arg2.extract() {
|
||||
|
@ -242,8 +242,8 @@ macro_rules! py_func_set{
|
|||
e.restore(py);
|
||||
-1
|
||||
} else {
|
||||
let name = py.mut_cast_from_borrowed_ptr::<$crate::PyInstance>(name);
|
||||
let value = py.cast_from_borrowed_ptr::<$crate::PyInstance>(value);
|
||||
let name = py.mut_cast_from_borrowed_ptr::<$crate::PyObjectRef>(name);
|
||||
let value = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(value);
|
||||
let result = match name.extract() {
|
||||
Ok(name) => match value.extract() {
|
||||
Ok(value) =>
|
||||
|
@ -287,7 +287,7 @@ macro_rules! py_func_del{
|
|||
{
|
||||
if value.is_null() {
|
||||
let slf = py.mut_cast_from_borrowed_ptr::<T>(slf);
|
||||
let name = py.cast_from_borrowed_ptr::<$crate::PyInstance>(name);
|
||||
let name = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(name);
|
||||
|
||||
let result = match name.extract() {
|
||||
Ok(name) => slf.$f(name).into(),
|
||||
|
@ -331,7 +331,7 @@ macro_rules! py_func_set_del{
|
|||
LOCATION, $crate::callback::UnitCallbackConverter, |py|
|
||||
{
|
||||
let slf = py.mut_cast_from_borrowed_ptr::<T>(slf);
|
||||
let name = py.cast_from_borrowed_ptr::<$crate::PyInstance>(name);
|
||||
let name = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(name);
|
||||
|
||||
if value.is_null() {
|
||||
let result = match name.extract() {
|
||||
|
@ -346,7 +346,7 @@ macro_rules! py_func_set_del{
|
|||
}
|
||||
}
|
||||
} else {
|
||||
let value = py.cast_from_borrowed_ptr::<$crate::PyInstance>(value);
|
||||
let value = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(value);
|
||||
let result = match name.extract() {
|
||||
Ok(name) => match value.extract() {
|
||||
Ok(value) => {
|
||||
|
|
|
@ -8,7 +8,7 @@ use std::os::raw::c_int;
|
|||
use ffi;
|
||||
use python::PyDowncastFrom;
|
||||
use err::{PyErr, PyResult};
|
||||
use objects::{exc, PyInstance};
|
||||
use objects::{exc, PyObjectRef};
|
||||
use objectprotocol::ObjectProtocol;
|
||||
use callback::{PyObjectCallbackConverter, LenResultConverter, BoolCallbackConverter};
|
||||
use typeob::PyTypeInfo;
|
||||
|
@ -225,7 +225,7 @@ impl<T> PySequenceSetItemProtocolImpl for T
|
|||
e.restore(py);
|
||||
-1
|
||||
} else {
|
||||
let value = py.cast_from_borrowed_ptr::<PyInstance>(value);
|
||||
let value = py.cast_from_borrowed_ptr::<PyObjectRef>(value);
|
||||
let result = match value.extract() {
|
||||
Ok(value) => {
|
||||
slf.__setitem__(key as isize, value).into()
|
||||
|
@ -314,7 +314,7 @@ impl<T> PySequenceDelItemProtocolImpl for T
|
|||
}
|
||||
}
|
||||
} else {
|
||||
let value = py.cast_from_borrowed_ptr::<PyInstance>(value);
|
||||
let value = py.cast_from_borrowed_ptr::<PyObjectRef>(value);
|
||||
let result = match value.extract() {
|
||||
Ok(value) => {
|
||||
slf.__setitem__(key as isize, value).into()
|
||||
|
|
|
@ -5,7 +5,7 @@ use ffi;
|
|||
use err::PyResult;
|
||||
use python::{Python, ToPyPointer, PyDowncastFrom};
|
||||
use pointer::PyObject;
|
||||
use objects::{PyInstance, PyTuple};
|
||||
use objects::{PyObjectRef, PyTuple};
|
||||
use objectprotocol::ObjectProtocol;
|
||||
use typeob::PyTypeInfo;
|
||||
use instance::Py;
|
||||
|
@ -73,11 +73,11 @@ pub trait IntoPyTuple {
|
|||
/// the inherent method `PyObject::extract()` can be used.
|
||||
pub trait FromPyObject<'source> : Sized {
|
||||
/// Extracts `Self` from the source `PyObject`.
|
||||
fn extract(ob: &'source PyInstance) -> PyResult<Self>;
|
||||
fn extract(ob: &'source PyObjectRef) -> PyResult<Self>;
|
||||
}
|
||||
|
||||
pub trait RefFromPyObject {
|
||||
fn with_extracted<F, R>(ob: &PyInstance, f: F) -> PyResult<R>
|
||||
fn with_extracted<F, R>(ob: &PyObjectRef, f: F) -> PyResult<R>
|
||||
where F: FnOnce(&Self) -> R;
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ impl <T: ?Sized> RefFromPyObject for T
|
|||
where for<'a> &'a T: FromPyObject<'a> + Sized
|
||||
{
|
||||
#[inline]
|
||||
fn with_extracted<F, R>(obj: &PyInstance, f: F) -> PyResult<R>
|
||||
fn with_extracted<F, R>(obj: &PyObjectRef, f: F) -> PyResult<R>
|
||||
where F: FnOnce(&Self) -> R
|
||||
{
|
||||
match FromPyObject::extract(obj) {
|
||||
|
@ -150,7 +150,7 @@ impl<'a, T> FromPyObject<'a> for &'a T
|
|||
where T: PyTypeInfo + PyDowncastFrom
|
||||
{
|
||||
#[inline]
|
||||
default fn extract(ob: &'a PyInstance) -> PyResult<&'a T>
|
||||
default fn extract(ob: &'a PyObjectRef) -> PyResult<&'a T>
|
||||
{
|
||||
Ok(ob.cast_as()?)
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ impl<'a, T> FromPyObject<'a> for &'a T
|
|||
|
||||
impl<'source, T> FromPyObject<'source> for Option<T> where T: FromPyObject<'source>
|
||||
{
|
||||
fn extract(obj: &'source PyInstance) -> PyResult<Self>
|
||||
fn extract(obj: &'source PyObjectRef) -> PyResult<Self>
|
||||
{
|
||||
if obj.as_ptr() == unsafe { ffi::Py_None() } {
|
||||
Ok(None)
|
||||
|
|
|
@ -9,7 +9,7 @@ use libc;
|
|||
use ffi;
|
||||
use python::{ToPyPointer, IntoPyPointer, Python, PyClone};
|
||||
use PyObject;
|
||||
use objects::{PyInstance, PyType, exc};
|
||||
use objects::{PyObjectRef, PyType, exc};
|
||||
use instance::Py;
|
||||
use typeob::PyTypeObject;
|
||||
use conversion::{ToPyObject, IntoPyTuple, IntoPyObject};
|
||||
|
@ -338,7 +338,7 @@ impl PyErr {
|
|||
|
||||
/// Issue a warning message.
|
||||
/// May return a PyErr if warnings-as-errors is enabled.
|
||||
pub fn warn(py: Python, category: &PyInstance, message: &str, stacklevel: i32) -> PyResult<()> {
|
||||
pub fn warn(py: Python, category: &PyObjectRef, message: &str, stacklevel: i32) -> PyResult<()> {
|
||||
let message = CString::new(message).map_err(|e| e.to_pyerr(py))?;
|
||||
unsafe {
|
||||
error_on_minusone(py, ffi::PyErr_WarnEx(
|
||||
|
|
|
@ -7,7 +7,7 @@ use std::marker::PhantomData;
|
|||
use ffi;
|
||||
use err::{PyResult, PyErr, PyDowncastError};
|
||||
use pointer::PyObject;
|
||||
use objects::PyInstance;
|
||||
use objects::PyObjectRef;
|
||||
use objectprotocol::ObjectProtocol;
|
||||
use conversion::{ToPyObject, IntoPyObject, FromPyObject};
|
||||
use python::{Python, IntoPyPointer, ToPyPointer, PyDowncastInto, PyDowncastFrom};
|
||||
|
@ -375,7 +375,7 @@ impl<T> PyDowncastInto for Py<T> where T: PyTypeInfo
|
|||
impl<'a, T> FromPyObject<'a> for Py<T> where T: ToPyPointer + FromPyObject<'a>
|
||||
{
|
||||
/// Extracts `Self` from the source `PyObject`.
|
||||
fn extract(ob: &'a PyInstance) -> PyResult<Self>
|
||||
fn extract(ob: &'a PyObjectRef) -> PyResult<Self>
|
||||
{
|
||||
unsafe {
|
||||
ob.extract::<T>().map(|val| Py::from_borrowed_ptr(val.as_ptr()))
|
||||
|
|
|
@ -8,7 +8,7 @@ use ffi;
|
|||
use err::{PyErr, PyResult, PyDowncastError, self};
|
||||
use python::{Python, ToPyPointer, PyDowncastFrom, PyClone};
|
||||
use pointer::PyObject;
|
||||
use objects::{PyInstance, PyDict, PyString, PyIterator, PyType};
|
||||
use objects::{PyObjectRef, PyDict, PyString, PyIterator, PyType};
|
||||
use conversion::{ToPyObject, IntoPyTuple, FromPyObject};
|
||||
use instance::PyObjectWithToken;
|
||||
|
||||
|
@ -21,7 +21,7 @@ pub trait ObjectProtocol {
|
|||
|
||||
/// Retrieves an attribute value.
|
||||
/// This is equivalent to the Python expression 'self.attr_name'.
|
||||
fn getattr<N>(&self, attr_name: N) -> PyResult<&PyInstance> where N: ToPyObject;
|
||||
fn getattr<N>(&self, attr_name: N) -> PyResult<&PyObjectRef> where N: ToPyObject;
|
||||
|
||||
/// Sets an attribute value.
|
||||
/// This is equivalent to the Python expression 'self.attr_name = value'.
|
||||
|
@ -74,13 +74,13 @@ pub trait ObjectProtocol {
|
|||
|
||||
/// Calls the object.
|
||||
/// This is equivalent to the Python expression: 'self(*args, **kwargs)'
|
||||
fn call<A>(&self, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyInstance>
|
||||
fn call<A>(&self, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyObjectRef>
|
||||
where A: IntoPyTuple;
|
||||
|
||||
/// Calls a method on the object.
|
||||
/// This is equivalent to the Python expression: 'self.name(*args, **kwargs)'
|
||||
fn call_method<A>(&self, name: &str, args: A, kwargs: Option<&PyDict>)
|
||||
-> PyResult<&PyInstance>
|
||||
-> PyResult<&PyObjectRef>
|
||||
where A: IntoPyTuple;
|
||||
|
||||
/// Retrieves the hash code of the object.
|
||||
|
@ -101,7 +101,7 @@ pub trait ObjectProtocol {
|
|||
fn len(&self) -> PyResult<usize>;
|
||||
|
||||
/// This is equivalent to the Python expression: 'self[key]'
|
||||
fn get_item<K>(&self, key: K) -> PyResult<&PyInstance> where K: ToPyObject;
|
||||
fn get_item<K>(&self, key: K) -> PyResult<&PyObjectRef> where K: ToPyObject;
|
||||
|
||||
/// Sets an item value.
|
||||
/// This is equivalent to the Python expression 'self[key] = value'.
|
||||
|
@ -124,14 +124,14 @@ pub trait ObjectProtocol {
|
|||
/// Fails with `PyDowncastError` if the object is not of the expected type.
|
||||
fn cast_as<'a, D>(&'a self) -> Result<&'a D, PyDowncastError<'a>>
|
||||
where D: PyDowncastFrom,
|
||||
&'a PyInstance: std::convert::From<&'a Self>;
|
||||
&'a PyObjectRef: std::convert::From<&'a Self>;
|
||||
|
||||
/// Extracts some type from the Python object.
|
||||
/// This is a wrapper function around `FromPyObject::extract()`.
|
||||
#[inline]
|
||||
fn extract<'a, D>(&'a self) -> PyResult<D>
|
||||
where D: FromPyObject<'a>,
|
||||
&'a PyInstance: std::convert::From<&'a Self>;
|
||||
&'a PyObjectRef: std::convert::From<&'a Self>;
|
||||
|
||||
/// Returns reference count for python object.
|
||||
fn get_refcnt(&self) -> isize;
|
||||
|
@ -156,7 +156,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn getattr<N>(&self, attr_name: N) -> PyResult<&PyInstance> where N: ToPyObject
|
||||
fn getattr<N>(&self, attr_name: N) -> PyResult<&PyObjectRef> where N: ToPyObject
|
||||
{
|
||||
attr_name.with_borrowed_ptr(self.token(), |attr_name| unsafe {
|
||||
self.token().cast_from_ptr_or_err(
|
||||
|
@ -247,7 +247,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn call<A>(&self, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyInstance>
|
||||
fn call<A>(&self, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyObjectRef>
|
||||
where A: IntoPyTuple
|
||||
{
|
||||
let t = args.into_tuple(self.token());
|
||||
|
@ -261,7 +261,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
|
|||
|
||||
#[inline]
|
||||
fn call_method<A>(&self, name: &str, args: A, kwargs: Option<&PyDict>)
|
||||
-> PyResult<&PyInstance>
|
||||
-> PyResult<&PyObjectRef>
|
||||
where A: IntoPyTuple
|
||||
{
|
||||
name.with_borrowed_ptr(self.token(), |name| unsafe {
|
||||
|
@ -310,7 +310,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn get_item<K>(&self, key: K) -> PyResult<&PyInstance> where K: ToPyObject {
|
||||
fn get_item<K>(&self, key: K) -> PyResult<&PyObjectRef> where K: ToPyObject {
|
||||
key.with_borrowed_ptr(self.token(), |key| unsafe {
|
||||
self.token().cast_from_ptr_or_err(
|
||||
ffi::PyObject_GetItem(self.as_ptr(), key))
|
||||
|
@ -356,7 +356,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
|
|||
#[inline]
|
||||
fn cast_as<'a, D>(&'a self) -> Result<&'a D, PyDowncastError<'a>>
|
||||
where D: PyDowncastFrom,
|
||||
&'a PyInstance: std::convert::From<&'a Self>
|
||||
&'a PyObjectRef: std::convert::From<&'a Self>
|
||||
{
|
||||
<D as PyDowncastFrom>::downcast_from(self.into())
|
||||
}
|
||||
|
@ -364,7 +364,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
|
|||
#[inline]
|
||||
fn extract<'a, D>(&'a self) -> PyResult<D>
|
||||
where D: FromPyObject<'a>,
|
||||
&'a PyInstance: std::convert::From<&'a T>
|
||||
&'a PyObjectRef: std::convert::From<&'a T>
|
||||
{
|
||||
FromPyObject::extract(self.into())
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ pyobject_extract!(py, obj to bool => {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use python::Python;
|
||||
use objects::{PyBool, PyInstance};
|
||||
use objects::{PyBool, PyObjectRef};
|
||||
use conversion::ToPyObject;
|
||||
use objectprotocol::ObjectProtocol;
|
||||
|
||||
|
@ -73,7 +73,7 @@ mod test {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
assert!(PyBool::new(py, true).is_true());
|
||||
let t: &PyInstance = PyBool::new(py, true).into();
|
||||
let t: &PyObjectRef = PyBool::new(py, true).into();
|
||||
assert_eq!(true, t.extract().unwrap());
|
||||
assert!(true.to_object(py) == PyBool::new(py, true).into());
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ mod test {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
assert!(!PyBool::new(py, false).is_true());
|
||||
let t: &PyInstance = PyBool::new(py, false).into();
|
||||
let t: &PyObjectRef = PyBool::new(py, false).into();
|
||||
assert_eq!(false, t.extract().unwrap());
|
||||
assert!(false.to_object(py) == PyBool::new(py, false).into());
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ use pointer::PyObject;
|
|||
use instance::PyObjectWithToken;
|
||||
use python::{Python, ToPyPointer};
|
||||
use conversion::ToPyObject;
|
||||
use objects::{PyInstance, PyList};
|
||||
use objects::{PyObjectRef, PyList};
|
||||
use err::{self, PyResult, PyErr};
|
||||
|
||||
/// Represents a Python `dict`.
|
||||
|
@ -64,7 +64,7 @@ impl PyDict {
|
|||
|
||||
/// Gets an item from the dictionary.
|
||||
/// Returns None if the item is not present, or if an error occurs.
|
||||
pub fn get_item<K>(&self, key: K) -> Option<&PyInstance> where K: ToPyObject {
|
||||
pub fn get_item<K>(&self, key: K) -> Option<&PyObjectRef> where K: ToPyObject {
|
||||
key.with_borrowed_ptr(self.token(), |key| unsafe {
|
||||
self.token().cast_from_borrowed_ptr_or_opt(
|
||||
ffi::PyDict_GetItem(self.as_ptr(), key))
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
||||
|
||||
use ffi;
|
||||
use objects::PyInstance;
|
||||
use objects::PyObjectRef;
|
||||
use python::{Python, ToPyPointer, IntoPyPointer};
|
||||
use instance::PyObjectWithToken;
|
||||
use err::{PyErr, PyResult, PyDowncastError};
|
||||
|
@ -12,7 +12,7 @@ use err::{PyErr, PyResult, PyDowncastError};
|
|||
///
|
||||
/// Unlike other python objects, this class includes a `Python<'p>` token
|
||||
/// so that `PyIterator` can implement the rust `Iterator` trait.
|
||||
pub struct PyIterator<'p>(&'p PyInstance);
|
||||
pub struct PyIterator<'p>(&'p PyObjectRef);
|
||||
|
||||
|
||||
impl <'p> PyIterator<'p> {
|
||||
|
@ -34,7 +34,7 @@ impl <'p> PyIterator<'p> {
|
|||
}
|
||||
|
||||
impl <'p> Iterator for PyIterator<'p> {
|
||||
type Item = PyResult<&'p PyInstance>;
|
||||
type Item = PyResult<&'p PyObjectRef>;
|
||||
|
||||
/// Retrieves the next item from an iterator.
|
||||
/// Returns `None` when the iterator is exhausted.
|
||||
|
@ -64,7 +64,7 @@ mod tests {
|
|||
use instance::AsPyRef;
|
||||
use python::{Python, PyDowncastFrom};
|
||||
use conversion::ToPyObject;
|
||||
use objects::PyInstance;
|
||||
use objects::PyObjectRef;
|
||||
use objectprotocol::ObjectProtocol;
|
||||
|
||||
#[test]
|
||||
|
@ -72,7 +72,7 @@ mod tests {
|
|||
let gil_guard = Python::acquire_gil();
|
||||
let py = gil_guard.python();
|
||||
let obj = vec![10, 20].to_object(py);
|
||||
let inst = PyInstance::downcast_from(obj.as_ref(py)).unwrap();
|
||||
let inst = PyObjectRef::downcast_from(obj.as_ref(py)).unwrap();
|
||||
let mut it = inst.iter().unwrap();
|
||||
assert_eq!(10, it.next().unwrap().unwrap().extract().unwrap());
|
||||
assert_eq!(20, it.next().unwrap().unwrap().extract().unwrap());
|
||||
|
|
|
@ -6,7 +6,7 @@ use err::{self, PyResult};
|
|||
use ffi::{self, Py_ssize_t};
|
||||
use instance::PyObjectWithToken;
|
||||
use pointer::PyObject;
|
||||
use objects::PyInstance;
|
||||
use objects::PyObjectRef;
|
||||
use python::{Python, ToPyPointer, IntoPyPointer};
|
||||
use conversion::{ToPyObject, IntoPyObject};
|
||||
|
||||
|
@ -48,7 +48,7 @@ impl PyList {
|
|||
/// Gets the item at the specified index.
|
||||
///
|
||||
/// Panics if the index is out of range.
|
||||
pub fn get_item(&self, index: isize) -> &PyInstance {
|
||||
pub fn get_item(&self, index: isize) -> &PyObjectRef {
|
||||
unsafe {
|
||||
let ptr = ffi::PyList_GetItem(self.as_ptr(), index as Py_ssize_t);
|
||||
let ob = PyObject::from_borrowed_ptr(self.token(), ptr);
|
||||
|
@ -103,10 +103,10 @@ pub struct PyListIterator<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Iterator for PyListIterator<'a> {
|
||||
type Item = &'a PyInstance;
|
||||
type Item = &'a PyObjectRef;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a PyInstance> {
|
||||
fn next(&mut self) -> Option<&'a PyObjectRef> {
|
||||
if self.index < self.list.len() as isize {
|
||||
let item = self.list.get_item(self.index);
|
||||
self.index += 1;
|
||||
|
|
|
@ -33,7 +33,7 @@ macro_rules! pyobject_downcast(
|
|||
($name: ident, $checkfunction: ident) => (
|
||||
impl $crate::python::PyDowncastFrom for $name
|
||||
{
|
||||
fn downcast_from(ob: &$crate::PyInstance)
|
||||
fn downcast_from(ob: &$crate::PyObjectRef)
|
||||
-> Result<&$name, $crate::PyDowncastError>
|
||||
{
|
||||
use $crate::{ToPyPointer, PyObjectWithToken};
|
||||
|
@ -47,11 +47,11 @@ macro_rules! pyobject_downcast(
|
|||
}
|
||||
}
|
||||
|
||||
unsafe fn unchecked_downcast_from(ob: &$crate::PyInstance) -> &Self
|
||||
unsafe fn unchecked_downcast_from(ob: &$crate::PyObjectRef) -> &Self
|
||||
{
|
||||
$crate::std::mem::transmute(ob)
|
||||
}
|
||||
unsafe fn unchecked_mut_downcast_from(ob: &$crate::PyInstance) -> &mut Self
|
||||
unsafe fn unchecked_mut_downcast_from(ob: &$crate::PyObjectRef) -> &mut Self
|
||||
{
|
||||
#[allow(mutable_transmutes)]
|
||||
$crate::std::mem::transmute(ob)
|
||||
|
@ -61,7 +61,7 @@ macro_rules! pyobject_downcast(
|
|||
impl<'a> $crate::FromPyObject<'a> for &'a $name
|
||||
{
|
||||
/// Extracts `Self` from the source `PyObject`.
|
||||
fn extract(ob: &'a $crate::PyInstance) -> $crate::PyResult<Self>
|
||||
fn extract(ob: &'a $crate::PyObjectRef) -> $crate::PyResult<Self>
|
||||
{
|
||||
use instance::PyObjectWithToken;
|
||||
unsafe {
|
||||
|
@ -78,7 +78,7 @@ macro_rules! pyobject_downcast(
|
|||
|
||||
macro_rules! pyobject_convert(
|
||||
($name: ident) => (
|
||||
impl<'a> $crate::std::convert::From<&'a $name> for &'a $crate::PyInstance {
|
||||
impl<'a> $crate::std::convert::From<&'a $name> for &'a $crate::PyObjectRef {
|
||||
fn from(ob: &'a $name) -> Self {
|
||||
unsafe{$crate::std::mem::transmute(ob)}
|
||||
}
|
||||
|
@ -90,8 +90,8 @@ macro_rules! pyobject_nativetype(
|
|||
($name: ident) => {
|
||||
impl $crate::PyNativeType for $name {}
|
||||
|
||||
impl $crate::std::convert::AsRef<$crate::PyInstance> for $name {
|
||||
fn as_ref(&self) -> &$crate::PyInstance {
|
||||
impl $crate::std::convert::AsRef<$crate::PyObjectRef> for $name {
|
||||
fn as_ref(&self) -> &$crate::PyObjectRef {
|
||||
unsafe{$crate::std::mem::transmute(self)}
|
||||
}
|
||||
}
|
||||
|
@ -224,7 +224,7 @@ macro_rules! pyobject_extract(
|
|||
($py:ident, $obj:ident to $t:ty => $body: block) => {
|
||||
impl<'source> $crate::FromPyObject<'source> for $t
|
||||
{
|
||||
fn extract($obj: &'source $crate::PyInstance) -> $crate::PyResult<Self>
|
||||
fn extract($obj: &'source $crate::PyObjectRef) -> $crate::PyResult<Self>
|
||||
{
|
||||
#[allow(unused_imports)]
|
||||
use objectprotocol::ObjectProtocol;
|
||||
|
@ -239,8 +239,8 @@ macro_rules! pyobject_extract(
|
|||
use python::ToPyPointer;
|
||||
|
||||
/// Represents general python instance.
|
||||
pub struct PyInstance(::PyObject);
|
||||
pyobject_nativetype!(PyInstance, PyBaseObject_Type, PyObject_Check);
|
||||
pub struct PyObjectRef(::PyObject);
|
||||
pyobject_nativetype!(PyObjectRef, PyBaseObject_Type, PyObject_Check);
|
||||
|
||||
mod typeobject;
|
||||
mod module;
|
||||
|
|
|
@ -10,7 +10,7 @@ use std::ffi::{CStr, CString};
|
|||
use conversion::{ToPyObject, IntoPyTuple};
|
||||
use pointer::PyObject;
|
||||
use python::{Python, ToPyPointer};
|
||||
use objects::{PyInstance, PyDict, PyType, exc};
|
||||
use objects::{PyObjectRef, PyDict, PyType, exc};
|
||||
use objectprotocol::ObjectProtocol;
|
||||
use instance::PyObjectWithToken;
|
||||
use err::{PyResult, PyErr, ToPyErr};
|
||||
|
@ -81,7 +81,7 @@ impl PyModule {
|
|||
|
||||
/// Calls a function in the module.
|
||||
/// This is equivalent to the Python expression: `getattr(module, name)(*args, **kwargs)`
|
||||
pub fn call<A>(&self, name: &str, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyInstance>
|
||||
pub fn call<A>(&self, name: &str, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyObjectRef>
|
||||
where A: IntoPyTuple
|
||||
{
|
||||
self.getattr(name)?.call(args, kwargs)
|
||||
|
@ -89,7 +89,7 @@ impl PyModule {
|
|||
|
||||
/// Gets a member from the module.
|
||||
/// This is equivalent to the Python expression: `getattr(module, name)`
|
||||
pub fn get(&self, name: &str) -> PyResult<&PyInstance>
|
||||
pub fn get(&self, name: &str) -> PyResult<&PyObjectRef>
|
||||
{
|
||||
self.getattr(name)
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ use pointer::PyObject;
|
|||
use python::{ToPyPointer, IntoPyPointer, Python};
|
||||
use err::{PyResult, PyErr};
|
||||
use instance::{Py, PyObjectWithToken};
|
||||
use objects::{exc, PyInstance};
|
||||
use objects::{exc, PyObjectRef};
|
||||
use conversion::{ToPyObject, IntoPyObject, FromPyObject};
|
||||
|
||||
/// Represents a Python `int` object.
|
||||
|
@ -153,7 +153,7 @@ macro_rules! int_convert_u64_or_i64 (
|
|||
}
|
||||
|
||||
impl <'source> FromPyObject<'source> for $rust_type {
|
||||
fn extract(obj: &'source PyInstance) -> PyResult<$rust_type>
|
||||
fn extract(obj: &'source PyObjectRef) -> PyResult<$rust_type>
|
||||
{
|
||||
let ptr = obj.as_ptr();
|
||||
unsafe {
|
||||
|
|
|
@ -11,7 +11,7 @@ use ffi;
|
|||
use pointer::PyObject;
|
||||
use python::{ToPyPointer, Python};
|
||||
use err::{PyResult, PyErr};
|
||||
use objects::{exc, PyInstance};
|
||||
use objects::{exc, PyObjectRef};
|
||||
use instance::PyObjectWithToken;
|
||||
use conversion::{ToPyObject, IntoPyObject, FromPyObject};
|
||||
|
||||
|
@ -110,7 +110,7 @@ macro_rules! int_convert_u64_or_i64 (
|
|||
}
|
||||
}
|
||||
impl<'source> FromPyObject<'source> for $rust_type {
|
||||
fn extract(ob: &'source PyInstance) -> PyResult<$rust_type>
|
||||
fn extract(ob: &'source PyObjectRef) -> PyResult<$rust_type>
|
||||
{
|
||||
let ptr = ob.as_ptr();
|
||||
unsafe {
|
||||
|
|
|
@ -7,7 +7,7 @@ use pointer::PyObject;
|
|||
use instance::PyObjectWithToken;
|
||||
use python::{Python, ToPyPointer, PyDowncastFrom};
|
||||
use conversion::{FromPyObject, ToPyObject};
|
||||
use objects::{PyInstance, PyList, PyTuple};
|
||||
use objects::{PyObjectRef, PyList, PyTuple};
|
||||
use ffi::Py_ssize_t;
|
||||
use err;
|
||||
use err::{PyErr, PyResult};
|
||||
|
@ -84,7 +84,7 @@ impl PySequence {
|
|||
|
||||
/// Return the ith element of the Sequence. Equivalent to python `o[index]`
|
||||
#[inline]
|
||||
pub fn get_item(&self, index: isize) -> PyResult<&PyInstance> {
|
||||
pub fn get_item(&self, index: isize) -> PyResult<&PyObjectRef> {
|
||||
unsafe {
|
||||
self.token().cast_from_ptr_or_err(
|
||||
ffi::PySequence_GetItem(self.as_ptr(), index as Py_ssize_t))
|
||||
|
@ -94,7 +94,7 @@ impl PySequence {
|
|||
/// Return the slice of sequence object o between begin and end.
|
||||
/// This is the equivalent of the Python expression `o[begin:end]`
|
||||
#[inline]
|
||||
pub fn get_slice(&self, begin: isize, end: isize) -> PyResult<&PyInstance> {
|
||||
pub fn get_slice(&self, begin: isize, end: isize) -> PyResult<&PyObjectRef> {
|
||||
unsafe {
|
||||
self.token().cast_from_ptr_or_err(
|
||||
ffi::PySequence_GetSlice(
|
||||
|
@ -105,7 +105,7 @@ impl PySequence {
|
|||
/// Assign object v to the ith element of o.
|
||||
/// Equivalent to Python statement `o[i] = v`
|
||||
#[inline]
|
||||
pub fn set_item(&self, i: isize, v: &PyInstance) -> PyResult<()> {
|
||||
pub fn set_item(&self, i: isize, v: &PyObjectRef) -> PyResult<()> {
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
self.token(),
|
||||
|
@ -127,7 +127,7 @@ impl PySequence {
|
|||
/// Assign the sequence object v to the slice in sequence object o from i1 to i2.
|
||||
/// This is the equivalent of the Python statement `o[i1:i2] = v`
|
||||
#[inline]
|
||||
pub fn set_slice(&self, i1: isize, i2: isize, v: &PyInstance) -> PyResult<()> {
|
||||
pub fn set_slice(&self, i1: isize, i2: isize, v: &PyObjectRef) -> PyResult<()> {
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
self.token(), ffi::PySequence_SetSlice(
|
||||
|
@ -210,7 +210,7 @@ impl PySequence {
|
|||
|
||||
impl<'a, T> FromPyObject<'a> for Vec<T> where T: FromPyObject<'a>
|
||||
{
|
||||
default fn extract(obj: &'a PyInstance) -> PyResult<Self> {
|
||||
default fn extract(obj: &'a PyObjectRef) -> PyResult<Self> {
|
||||
extract_sequence(obj)
|
||||
}
|
||||
}
|
||||
|
@ -219,7 +219,7 @@ impl<'a, T> FromPyObject<'a> for Vec<T> where T: FromPyObject<'a>
|
|||
impl <'source, T> FromPyObject<'source> for Vec<T>
|
||||
where for<'a> T: FromPyObject<'a> + buffer::Element + Copy
|
||||
{
|
||||
fn extract(py: Python, obj: &'source PyInstance) -> PyResult<Self> {
|
||||
fn extract(py: Python, obj: &'source PyObjectRef) -> PyResult<Self> {
|
||||
// first try buffer protocol
|
||||
if let Ok(buf) = buffer::PyBuffer::get(py, obj) {
|
||||
if buf.dimensions() == 1 {
|
||||
|
@ -235,7 +235,7 @@ impl <'source, T> FromPyObject<'source> for Vec<T>
|
|||
}
|
||||
}*/
|
||||
|
||||
fn extract_sequence<'s, T>(obj: &'s PyInstance) -> PyResult<Vec<T>> where T: FromPyObject<'s>
|
||||
fn extract_sequence<'s, T>(obj: &'s PyObjectRef) -> PyResult<Vec<T>> where T: FromPyObject<'s>
|
||||
{
|
||||
let seq = PySequence::downcast_from(obj)?;
|
||||
let mut v = Vec::new();
|
||||
|
|
|
@ -10,7 +10,7 @@ use std::os::raw::c_char;
|
|||
use ffi;
|
||||
use instance::{Py, PyObjectWithToken};
|
||||
use pointer::PyObject;
|
||||
use objects::PyInstance;
|
||||
use objects::PyObjectRef;
|
||||
use python::{ToPyPointer, Python};
|
||||
use err::{PyResult, PyErr};
|
||||
use super::PyStringData;
|
||||
|
@ -45,7 +45,7 @@ impl PyString {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_object(py: Python, src: &PyInstance, encoding: &str, errors: &str)
|
||||
pub fn from_object(py: Python, src: &PyObjectRef, encoding: &str, errors: &str)
|
||||
-> PyResult<Py<PyString>> {
|
||||
unsafe {
|
||||
Ok(Py::from_owned_ptr_or_err(
|
||||
|
|
|
@ -14,7 +14,7 @@ use pointer::PyObject;
|
|||
use instance::{Py, PyObjectWithToken};
|
||||
use python::{Python, ToPyPointer};
|
||||
use objectprotocol::ObjectProtocol;
|
||||
use super::{PyInstance, PyStringData};
|
||||
use super::{PyObjectRef, PyStringData};
|
||||
|
||||
/// Represents a Python string.
|
||||
pub struct PyString(PyObject);
|
||||
|
@ -51,7 +51,7 @@ impl PyString {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_object(src: &PyInstance, encoding: &str, errors: &str) -> PyResult<Py<PyString>> {
|
||||
pub fn from_object(src: &PyObjectRef, encoding: &str, errors: &str) -> PyResult<Py<PyString>> {
|
||||
unsafe {
|
||||
Ok(Py::from_owned_ptr_or_err(
|
||||
src.token(), ffi::PyUnicode_FromEncodedObject(
|
||||
|
@ -135,7 +135,7 @@ impl PyUnicode {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_object(src: &PyInstance, encoding: &str, errors: &str) -> PyResult<Py<PyUnicode>>
|
||||
pub fn from_object(src: &PyObjectRef, encoding: &str, errors: &str) -> PyResult<Py<PyUnicode>>
|
||||
{
|
||||
unsafe {
|
||||
Ok(Py::from_owned_ptr_or_err(
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::borrow::Cow;
|
|||
|
||||
use err::PyResult;
|
||||
use pointer::PyObject;
|
||||
use objects::{PyInstance, PyString};
|
||||
use objects::{PyObjectRef, PyString};
|
||||
use objectprotocol::ObjectProtocol;
|
||||
use python::Python;
|
||||
use conversion::{ToPyObject, IntoPyObject, RefFromPyObject};
|
||||
|
@ -57,7 +57,7 @@ impl<'a> IntoPyObject for &'a String {
|
|||
/// Accepts Python `str` and `unicode` objects.
|
||||
impl<'source> ::FromPyObject<'source> for Cow<'source, str>
|
||||
{
|
||||
fn extract(ob: &'source PyInstance) -> PyResult<Self>
|
||||
fn extract(ob: &'source PyObjectRef) -> PyResult<Self>
|
||||
{
|
||||
try!(ob.cast_as::<PyString>()).to_string()
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ pyobject_extract!(py, obj to String => {
|
|||
});
|
||||
|
||||
impl RefFromPyObject for str {
|
||||
fn with_extracted<F, R>(obj: &PyInstance, f: F) -> PyResult<R>
|
||||
fn with_extracted<F, R>(obj: &PyObjectRef, f: F) -> PyResult<R>
|
||||
where F: FnOnce(&str) -> R
|
||||
{
|
||||
let s = try!(obj.extract::<Cow<str>>());
|
||||
|
|
|
@ -8,7 +8,7 @@ use ffi::{self, Py_ssize_t};
|
|||
use err::{PyErr, PyResult};
|
||||
use instance::{Py, PyObjectWithToken};
|
||||
use pointer::PyObject;
|
||||
use objects::PyInstance;
|
||||
use objects::PyObjectRef;
|
||||
use objectprotocol::ObjectProtocol;
|
||||
use python::{Python, ToPyPointer, IntoPyPointer};
|
||||
use conversion::{FromPyObject, ToPyObject, IntoPyTuple, IntoPyObject};
|
||||
|
@ -54,7 +54,7 @@ impl PyTuple {
|
|||
/// Gets the item at the specified index.
|
||||
///
|
||||
/// Panics if the index is out of range.
|
||||
pub fn get_item(&self, index: usize) -> &PyInstance {
|
||||
pub fn get_item(&self, index: usize) -> &PyObjectRef {
|
||||
// TODO: reconsider whether we should panic
|
||||
// It's quite inconsistent that this method takes `Python` when `len()` does not.
|
||||
assert!(index < self.len());
|
||||
|
@ -144,7 +144,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
|
|||
}
|
||||
|
||||
impl<'s, $($T: FromPyObject<'s>),+> FromPyObject<'s> for ($($T,)+) {
|
||||
fn extract(obj: &'s PyInstance) -> PyResult<Self>
|
||||
fn extract(obj: &'s PyObjectRef) -> PyResult<Self>
|
||||
{
|
||||
let t = try!(obj.cast_as::<PyTuple>());
|
||||
let slice = t.as_slice();
|
||||
|
@ -239,7 +239,7 @@ mod test {
|
|||
use instance::AsPyRef;
|
||||
use python::{Python, PyDowncastFrom};
|
||||
use conversion::ToPyObject;
|
||||
use objects::PyInstance;
|
||||
use objects::PyObjectRef;
|
||||
use objectprotocol::ObjectProtocol;
|
||||
|
||||
#[test]
|
||||
|
@ -249,7 +249,7 @@ mod test {
|
|||
let pyob = PyTuple::new(py, &[1, 2, 3]);
|
||||
let ob = pyob.as_ref(py);
|
||||
assert_eq!(3, ob.len());
|
||||
let ob: &PyInstance = ob.into();
|
||||
let ob: &PyObjectRef = ob.into();
|
||||
assert_eq!((1, 2, 3), ob.extract().unwrap());
|
||||
}
|
||||
|
||||
|
@ -260,7 +260,7 @@ mod test {
|
|||
let ob = (1, 2, 3).to_object(py);
|
||||
let tuple = PyTuple::downcast_from(ob.as_ref(py)).unwrap();
|
||||
assert_eq!(3, tuple.len());
|
||||
let ob: &PyInstance = tuple.into();
|
||||
let ob: &PyObjectRef = tuple.into();
|
||||
assert_eq!((1, 2, 3), ob.extract().unwrap());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use std;
|
|||
use ffi;
|
||||
use err::{PyErr, PyResult, PyDowncastError};
|
||||
use instance::{AsPyRef, PyObjectWithToken};
|
||||
use objects::{PyInstance, PyDict};
|
||||
use objects::{PyObjectRef, PyDict};
|
||||
use conversion::{ToPyObject, IntoPyObject, IntoPyTuple, FromPyObject};
|
||||
use python::{Python, PyClone, ToPyPointer, IntoPyPointer};
|
||||
|
||||
|
@ -199,15 +199,15 @@ impl PyObject {
|
|||
}
|
||||
}
|
||||
|
||||
impl AsPyRef<PyInstance> for PyObject {
|
||||
impl AsPyRef<PyObjectRef> for PyObject {
|
||||
|
||||
#[inline]
|
||||
fn as_ref(&self, _py: Python) -> &PyInstance {
|
||||
fn as_ref(&self, _py: Python) -> &PyObjectRef {
|
||||
unsafe {std::mem::transmute(self)}
|
||||
}
|
||||
#[inline]
|
||||
fn as_mut(&self, _py: Python) -> &mut PyInstance {
|
||||
unsafe {std::mem::transmute(self as *const _ as *mut PyInstance)}
|
||||
fn as_mut(&self, _py: Python) -> &mut PyObjectRef {
|
||||
unsafe {std::mem::transmute(self as *const _ as *mut PyObjectRef)}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -280,7 +280,7 @@ impl<'a> FromPyObject<'a> for PyObject
|
|||
{
|
||||
#[inline]
|
||||
/// Extracts `Self` from the source `PyObject`.
|
||||
fn extract(ob: &'a PyInstance) -> PyResult<Self>
|
||||
fn extract(ob: &'a PyObjectRef) -> PyResult<Self>
|
||||
{
|
||||
unsafe {
|
||||
Ok(PyObject::from_borrowed_ptr(ob.token(), ob.as_ptr()))
|
||||
|
|
|
@ -11,7 +11,7 @@ use ffi;
|
|||
use typeob::{PyTypeInfo, PyTypeObject, PyObjectAlloc};
|
||||
use instance::{Py, PyToken};
|
||||
use pointer::PyObject;
|
||||
use objects::{PyInstance, PyType, PyDict, PyModule};
|
||||
use objects::{PyObjectRef, PyType, PyDict, PyModule};
|
||||
use err::{PyErr, PyResult, PyDowncastError, ToPyErr};
|
||||
use pythonrun::{self, GILGuard};
|
||||
|
||||
|
@ -34,20 +34,20 @@ pub struct Python<'p>(PhantomData<&'p GILGuard>);
|
|||
pub trait PyDowncastFrom : Sized {
|
||||
|
||||
/// Cast from PyObject to a concrete Python object type.
|
||||
fn downcast_from(&PyInstance) -> Result<&Self, PyDowncastError>;
|
||||
fn downcast_from(&PyObjectRef) -> Result<&Self, PyDowncastError>;
|
||||
|
||||
/// Cast from PyObject to a concrete Python object type.
|
||||
unsafe fn unchecked_downcast_from(&PyInstance) -> &Self;
|
||||
unsafe fn unchecked_downcast_from(&PyObjectRef) -> &Self;
|
||||
|
||||
/// Cast from PyObject to a concrete Python object type.
|
||||
unsafe fn unchecked_mut_downcast_from(&PyInstance) -> &mut Self;
|
||||
unsafe fn unchecked_mut_downcast_from(&PyObjectRef) -> &mut Self;
|
||||
}
|
||||
|
||||
/// Trait implemented by Python object types that allow a checked downcast.
|
||||
pub trait PyMutDowncastFrom : Sized {
|
||||
|
||||
/// Cast from PyObject to a concrete Python object type.
|
||||
fn downcast_mut_from(&mut PyInstance) -> Result<&mut Self, PyDowncastError>;
|
||||
fn downcast_mut_from(&mut PyObjectRef) -> Result<&mut Self, PyDowncastError>;
|
||||
}
|
||||
|
||||
/// Trait implemented by Python object types that allow a checked downcast.
|
||||
|
@ -155,7 +155,7 @@ impl<'p> Python<'p> {
|
|||
/// If `globals` is `None`, it defaults to Python module `__main__`.
|
||||
/// If `locals` is `None`, it defaults to the value of `globals`.
|
||||
pub fn eval(self, code: &str, globals: Option<&PyDict>,
|
||||
locals: Option<&PyDict>) -> PyResult<&'p PyInstance> {
|
||||
locals: Option<&PyDict>) -> PyResult<&'p PyObjectRef> {
|
||||
self.run_code(code, ffi::Py_eval_input, globals, locals)
|
||||
}
|
||||
|
||||
|
@ -176,7 +176,7 @@ impl<'p> Python<'p> {
|
|||
/// If `globals` is `None`, it defaults to Python module `__main__`.
|
||||
/// If `locals` is `None`, it defaults to the value of `globals`.
|
||||
fn run_code(self, code: &str, start: c_int,
|
||||
globals: Option<&PyDict>, locals: Option<&PyDict>) -> PyResult<&'p PyInstance> {
|
||||
globals: Option<&PyDict>, locals: Option<&PyDict>) -> PyResult<&'p PyObjectRef> {
|
||||
let code = CString::new(code).map_err(|e| e.to_pyerr(self))?;
|
||||
|
||||
unsafe {
|
||||
|
@ -387,7 +387,7 @@ impl<'p> Python<'p> {
|
|||
<T as PyDowncastFrom>::unchecked_mut_downcast_from(p)
|
||||
}
|
||||
|
||||
pub fn track_object(self, obj: PyObject) -> &'p PyInstance
|
||||
pub fn track_object(self, obj: PyObject) -> &'p PyObjectRef
|
||||
{
|
||||
unsafe { pythonrun::register_owned(self, obj) }
|
||||
}
|
||||
|
@ -417,7 +417,7 @@ impl<'p> Python<'p> {
|
|||
mod test {
|
||||
use Python;
|
||||
use objectprotocol::ObjectProtocol;
|
||||
use objects::{PyInstance, PyBool, PyList, PyInt, PyDict};
|
||||
use objects::{PyObjectRef, PyBool, PyList, PyInt, PyDict};
|
||||
|
||||
#[test]
|
||||
fn test_eval() {
|
||||
|
@ -449,7 +449,7 @@ mod test {
|
|||
fn test_is_instance() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
assert!(py.is_instance::<PyBool, PyInstance>(PyBool::new(py, true).into()).unwrap());
|
||||
assert!(py.is_instance::<PyBool, PyObjectRef>(PyBool::new(py, true).into()).unwrap());
|
||||
let list = PyList::new(py, &[1, 2, 3, 4]);
|
||||
assert!(!py.is_instance::<PyBool, _>(list.as_ref()).unwrap());
|
||||
assert!(py.is_instance::<PyList, _>(list.as_ref()).unwrap());
|
||||
|
|
|
@ -6,7 +6,7 @@ use std::{sync, rc, marker, mem};
|
|||
use ffi;
|
||||
use python::{Python, ToPyPointer};
|
||||
use pointer::PyObject;
|
||||
use objects::PyInstance;
|
||||
use objects::PyObjectRef;
|
||||
|
||||
static START: sync::Once = sync::ONCE_INIT;
|
||||
static START_PYO3: sync::Once = sync::ONCE_INIT;
|
||||
|
@ -139,13 +139,13 @@ impl Drop for Pool {
|
|||
}
|
||||
}
|
||||
|
||||
pub unsafe fn register_owned<'p>(_py: Python<'p>, obj: PyObject) -> &'p PyInstance {
|
||||
pub unsafe fn register_owned<'p>(_py: Python<'p>, obj: PyObject) -> &'p PyObjectRef {
|
||||
let pool: &'static mut Vec<PyObject> = mem::transmute(OWNED);
|
||||
pool.push(obj);
|
||||
mem::transmute(&pool[pool.len()-1])
|
||||
}
|
||||
|
||||
pub unsafe fn register_borrowed<'p>(_py: Python<'p>, obj: PyObject) -> &'p PyInstance {
|
||||
pub unsafe fn register_borrowed<'p>(_py: Python<'p>, obj: PyObject) -> &'p PyObjectRef {
|
||||
let pool: &'static mut Vec<PyObject> = mem::transmute(BORROWED);
|
||||
pool.push(obj);
|
||||
mem::transmute(&pool[pool.len()-1])
|
||||
|
|
|
@ -797,35 +797,35 @@ impl PyObjectProtocol for BinaryArithmetic {
|
|||
|
||||
#[py::proto]
|
||||
impl PyNumberProtocol for BinaryArithmetic {
|
||||
fn __add__(&self, rhs: &PyInstance) -> PyResult<String> {
|
||||
fn __add__(&self, rhs: &PyObjectRef) -> PyResult<String> {
|
||||
Ok(format!("{:?} + {:?}", self, rhs))
|
||||
}
|
||||
|
||||
fn __sub__(&self, rhs: &PyInstance) -> PyResult<String> {
|
||||
fn __sub__(&self, rhs: &PyObjectRef) -> PyResult<String> {
|
||||
Ok(format!("{:?} - {:?}", self, rhs))
|
||||
}
|
||||
|
||||
fn __mul__(&self, rhs: &PyInstance) -> PyResult<String> {
|
||||
fn __mul__(&self, rhs: &PyObjectRef) -> PyResult<String> {
|
||||
Ok(format!("{:?} * {:?}", self, rhs))
|
||||
}
|
||||
|
||||
fn __lshift__(&self, rhs: &PyInstance) -> PyResult<String> {
|
||||
fn __lshift__(&self, rhs: &PyObjectRef) -> PyResult<String> {
|
||||
Ok(format!("{:?} << {:?}", self, rhs))
|
||||
}
|
||||
|
||||
fn __rshift__(&self, rhs: &PyInstance) -> PyResult<String> {
|
||||
fn __rshift__(&self, rhs: &PyObjectRef) -> PyResult<String> {
|
||||
Ok(format!("{:?} >> {:?}", self, rhs))
|
||||
}
|
||||
|
||||
fn __and__(&self, rhs: &PyInstance) -> PyResult<String> {
|
||||
fn __and__(&self, rhs: &PyObjectRef) -> PyResult<String> {
|
||||
Ok(format!("{:?} & {:?}", self, rhs))
|
||||
}
|
||||
|
||||
fn __xor__(&self, rhs: &PyInstance) -> PyResult<String> {
|
||||
fn __xor__(&self, rhs: &PyObjectRef) -> PyResult<String> {
|
||||
Ok(format!("{:?} ^ {:?}", self, rhs))
|
||||
}
|
||||
|
||||
fn __or__(&self, rhs: &PyInstance) -> PyResult<String> {
|
||||
fn __or__(&self, rhs: &PyObjectRef) -> PyResult<String> {
|
||||
Ok(format!("{:?} | {:?}", self, rhs))
|
||||
}
|
||||
}
|
||||
|
@ -868,7 +868,7 @@ impl PyObjectProtocol for RichComparisons {
|
|||
Ok("RC")
|
||||
}
|
||||
|
||||
fn __richcmp__(&self, other: &PyInstance, op: CompareOp) -> PyResult<String> {
|
||||
fn __richcmp__(&self, other: &PyObjectRef, op: CompareOp) -> PyResult<String> {
|
||||
match op {
|
||||
CompareOp::Lt => Ok(format!("{} < {:?}", self.__repr__().unwrap(), other)),
|
||||
CompareOp::Le => Ok(format!("{} <= {:?}", self.__repr__().unwrap(), other)),
|
||||
|
@ -891,7 +891,7 @@ impl PyObjectProtocol for RichComparisons2 {
|
|||
Ok("RC2")
|
||||
}
|
||||
|
||||
fn __richcmp__(&self, other: &'p PyInstance, op: CompareOp) -> PyResult<PyObject> {
|
||||
fn __richcmp__(&self, other: &'p PyObjectRef, op: CompareOp) -> PyResult<PyObject> {
|
||||
match op {
|
||||
CompareOp::Eq => Ok(true.to_object(self.token())),
|
||||
CompareOp::Ne => Ok(false.to_object(self.token())),
|
||||
|
@ -1054,8 +1054,8 @@ impl<'p> PyContextProtocol<'p> for ContextManager {
|
|||
|
||||
fn __exit__(&mut self,
|
||||
ty: Option<&'p PyType>,
|
||||
value: Option<&'p PyInstance>,
|
||||
traceback: Option<&'p PyInstance>) -> PyResult<bool> {
|
||||
value: Option<&'p PyObjectRef>,
|
||||
traceback: Option<&'p PyObjectRef>) -> PyResult<bool> {
|
||||
self.exit_called = true;
|
||||
if ty == Some(self.token().get_type::<exc::ValueError>()) {
|
||||
Ok(true)
|
||||
|
|
|
@ -28,7 +28,7 @@ struct Test {
|
|||
#[py::proto]
|
||||
impl<'p> PyMappingProtocol<'p> for Test
|
||||
{
|
||||
fn __getitem__(&self, idx: &PyInstance) -> PyResult<PyObject> {
|
||||
fn __getitem__(&self, idx: &PyObjectRef) -> PyResult<PyObject> {
|
||||
if let Ok(slice) = idx.cast_as::<PySlice>() {
|
||||
let indices = slice.indices(1000)?;
|
||||
if indices.start == 100 && indices.stop == 200 && indices.step == 1 {
|
||||
|
|
Loading…
Reference in a new issue