rename Ptr to Py
This commit is contained in:
parent
7b5521fdb3
commit
40efa5c955
|
@ -86,6 +86,19 @@ pub fn parse_arguments(items: &[syn::NestedMetaItem]) -> Vec<Argument> {
|
|||
arguments.push(Argument::Arg(name, Some(format!("{}", s))));
|
||||
}
|
||||
}
|
||||
&syn::Lit::Bool(ref b) => {
|
||||
if has_varargs {
|
||||
arguments.push(Argument::Kwarg(name, format!("{}", b)));
|
||||
} else {
|
||||
if has_kwargs {
|
||||
println!("syntax error, keyword arguments is defined: {:?}",
|
||||
args_str);
|
||||
return Vec::new()
|
||||
}
|
||||
has_kw = true;
|
||||
arguments.push(Argument::Arg(name, Some(format!("{}", b))));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
println!("Only string literal is supported, got: {:?}", lit);
|
||||
return Vec::new()
|
||||
|
|
|
@ -309,30 +309,6 @@ fn get_res_success(ty: &syn::Ty) -> (Tokens, syn::Ty) {
|
|||
_ => panic!("not supported: {:?}", ty),
|
||||
};
|
||||
|
||||
// add lifetime to Py<T>
|
||||
match succ {
|
||||
syn::Ty::Path(_, ref mut path) => {
|
||||
let last = path.segments.len()-1;
|
||||
let seg = path.segments[last].clone();
|
||||
|
||||
if seg.ident.as_ref() == "Py" {
|
||||
if let syn::PathParameters::AngleBracketed(ref data) = seg.parameters {
|
||||
path.segments[last] = syn::PathSegment{
|
||||
ident: seg.ident.clone(),
|
||||
parameters: syn::PathParameters::AngleBracketed(
|
||||
syn::AngleBracketedParameterData{
|
||||
lifetimes: vec![syn::Lifetime {
|
||||
ident: syn::Ident::from("'p")}],
|
||||
types: data.types.clone(),
|
||||
bindings: data.bindings.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// result
|
||||
let res = if result {
|
||||
quote! {PyResult<#succ>}
|
||||
|
|
|
@ -74,6 +74,13 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
|
|||
f(self.as_ptr())
|
||||
}
|
||||
}
|
||||
impl<'a> _pyo3::IntoPyObject for &'a #cls
|
||||
{
|
||||
#[inline]
|
||||
fn into_object<'p>(self, py: _pyo3::Python) -> _pyo3::PyObject {
|
||||
_pyo3::PyObject::from_borrowed_ptr(py, self.as_ptr())
|
||||
}
|
||||
}
|
||||
impl std::convert::AsRef<PyObject> for #cls {
|
||||
fn as_ref(&self) -> &_pyo3::PyObject {
|
||||
unsafe{std::mem::transmute(self.as_ptr())}
|
||||
|
|
|
@ -53,8 +53,7 @@ pub fn impl_wrap(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec, noargs: b
|
|||
const LOCATION: &'static str = concat!(
|
||||
stringify!(#cls), ".", stringify!(#name), "()");
|
||||
_pyo3::callback::cb_meth(LOCATION, |py| {
|
||||
//println!("METH {:?} =====: {:?} {:?} {:?}", LOCATION, slf, args, kwargs);
|
||||
let slf = _pyo3::Ptr::<#cls>::from_borrowed_ptr(slf);
|
||||
let slf = _pyo3::Py::<#cls>::from_borrowed_ptr(slf);
|
||||
|
||||
let result = {
|
||||
let result: #output = {
|
||||
|
@ -64,7 +63,6 @@ pub fn impl_wrap(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec, noargs: b
|
|||
_pyo3::callback::PyObjectCallbackConverter, py, result)
|
||||
};
|
||||
py.release(slf);
|
||||
//println!("METH {:?} =====", LOCATION);
|
||||
result
|
||||
})
|
||||
}
|
||||
|
@ -81,8 +79,7 @@ pub fn impl_wrap(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec, noargs: b
|
|||
const LOCATION: &'static str = concat!(
|
||||
stringify!(#cls), ".", stringify!(#name), "()");
|
||||
_pyo3::callback::cb_meth(LOCATION, |py| {
|
||||
//println!("METH {:?} =====: {:?} {:?} {:?}", LOCATION, slf, args, kwargs);
|
||||
let slf = _pyo3::Ptr::<#cls>::from_borrowed_ptr(slf);
|
||||
let slf = _pyo3::Py::<#cls>::from_borrowed_ptr(slf);
|
||||
let args = _pyo3::PyTuple::from_borrowed_ptr(py, args);
|
||||
let kwargs = _pyo3::argparse::get_kwargs(py, kwargs);
|
||||
|
||||
|
@ -96,7 +93,6 @@ pub fn impl_wrap(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec, noargs: b
|
|||
py.release(kwargs);
|
||||
py.release(args);
|
||||
py.release(slf);
|
||||
//println!("METH {:?} =====", LOCATION);
|
||||
result
|
||||
})
|
||||
}
|
||||
|
@ -117,7 +113,7 @@ pub fn impl_proto_wrap(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec) ->
|
|||
{
|
||||
const LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
|
||||
_pyo3::callback::cb_meth(LOCATION, |py| {
|
||||
let slf = _pyo3::Ptr::<#cls>::from_borrowed_ptr(slf);
|
||||
let slf = _pyo3::Py::<#cls>::from_borrowed_ptr(slf);
|
||||
let args = _pyo3::PyTuple::from_borrowed_ptr(py, args);
|
||||
let kwargs = _pyo3::argparse::get_kwargs(py, kwargs);
|
||||
|
||||
|
@ -262,7 +258,7 @@ fn impl_wrap_setter(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec) -> Tok
|
|||
const LOCATION: &'static str = concat!(
|
||||
stringify!(#cls), ".setter", stringify!(#name), "()");
|
||||
_pyo3::callback::cb_setter(LOCATION, |py| {
|
||||
let slf = _pyo3::Ptr::<#cls>::from_borrowed_ptr(slf);
|
||||
let slf = _pyo3::Py::<#cls>::from_borrowed_ptr(slf);
|
||||
let value = _pyo3::PyObject::from_borrowed_ptr(py, value);
|
||||
|
||||
let result = match <#val_ty as _pyo3::FromPyObject>::extract(py, &value) {
|
||||
|
|
|
@ -63,10 +63,9 @@ pub fn parse_args<'p>(py: Python<'p>,
|
|||
},
|
||||
None => {
|
||||
if p.kw_only {
|
||||
return Err(err::PyErr::new::<exc::TypeError, _>(
|
||||
py, format!("Required keywordargument ('{}') not found", p.name)));
|
||||
*out = None;
|
||||
}
|
||||
if i < nargs {
|
||||
else if i < nargs {
|
||||
*out = Some(args.get_item(py, i));
|
||||
} else {
|
||||
*out = None;
|
||||
|
|
|
@ -9,7 +9,7 @@ use objects::exc;
|
|||
use conversion::IntoPyObject;
|
||||
use ffi::{self, Py_hash_t};
|
||||
use err::{PyErr, PyResult};
|
||||
use token::{Ptr, InstancePtr};
|
||||
use token::{Py, InstancePtr};
|
||||
use typeob::PyTypeInfo;
|
||||
|
||||
|
||||
|
@ -204,7 +204,7 @@ pub unsafe fn cb_unary<Slf, F, T, C>(location: &str,
|
|||
let guard = AbortOnDrop(location);
|
||||
let ret = panic::catch_unwind(|| {
|
||||
let py = Python::assume_gil_acquired();
|
||||
let slf = Ptr::<Slf>::from_borrowed_ptr(slf);
|
||||
let slf = Py::<Slf>::from_borrowed_ptr(slf);
|
||||
|
||||
let result = match f(py, slf.as_mut(py)) {
|
||||
Ok(val) => {
|
||||
|
@ -238,7 +238,7 @@ pub unsafe fn cb_unary_unit<Slf, F>(location: &str, slf: *mut ffi::PyObject, f:
|
|||
let guard = AbortOnDrop(location);
|
||||
let ret = panic::catch_unwind(|| {
|
||||
let py = Python::assume_gil_acquired();
|
||||
let slf = Ptr::<Slf>::from_borrowed_ptr(slf);
|
||||
let slf = Py::<Slf>::from_borrowed_ptr(slf);
|
||||
|
||||
let result = f(py, slf.as_mut(py));
|
||||
py.release(slf);
|
||||
|
|
|
@ -15,7 +15,7 @@ use err::{PyErr, PyResult};
|
|||
use python::{Python, IntoPyPointer};
|
||||
use objects::PyObject;
|
||||
use objects::exc;
|
||||
use token::{Ptr, InstancePtr};
|
||||
use token::{Py, InstancePtr};
|
||||
use typeob::PyTypeInfo;
|
||||
use conversion::{FromPyObject, IntoPyObject};
|
||||
use callback::{PyObjectCallbackConverter, HashConverter, BoolCallbackConverter};
|
||||
|
@ -369,7 +369,7 @@ impl<T> PyObjectRichcmpProtocolImpl for T
|
|||
let guard = ::callback::AbortOnDrop(LOCATION);
|
||||
let ret = std::panic::catch_unwind(|| {
|
||||
let py = Python::assume_gil_acquired();
|
||||
let slf = Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = Py::<T>::from_borrowed_ptr(slf);
|
||||
let arg = PyObject::from_borrowed_ptr(py, arg);
|
||||
|
||||
let result = {
|
||||
|
|
|
@ -9,7 +9,7 @@ use std::os::raw::{c_int, c_void};
|
|||
use ffi;
|
||||
use python::{Python, ToPyPointer};
|
||||
use callback::AbortOnDrop;
|
||||
use token::{Ptr, InstancePtr};
|
||||
use token::{Py, InstancePtr};
|
||||
use typeob::PyTypeInfo;
|
||||
|
||||
pub struct PyTraverseError(c_int);
|
||||
|
@ -100,7 +100,7 @@ impl<T> PyGCTraverseProtocolImpl for T where T: for<'p> PyGCTraverseProtocol<'p>
|
|||
let guard = AbortOnDrop(LOCATION);
|
||||
let py = Python::assume_gil_acquired();
|
||||
let visit = PyVisit { visit: visit, arg: arg, _py: py };
|
||||
let slf = Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = Py::<T>::from_borrowed_ptr(slf);
|
||||
|
||||
let ret = match slf.as_ref(py).__traverse__(py, visit) {
|
||||
Ok(()) => 0,
|
||||
|
@ -138,7 +138,7 @@ impl<T> PyGCClearProtocolImpl for T where T: for<'p> PyGCClearProtocol<'p>
|
|||
|
||||
let guard = AbortOnDrop(LOCATION);
|
||||
let py = Python::assume_gil_acquired();
|
||||
let slf = Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = Py::<T>::from_borrowed_ptr(slf);
|
||||
T::__clear__(&mut slf.as_mut(py), py);
|
||||
mem::forget(guard);
|
||||
0
|
||||
|
|
|
@ -14,7 +14,7 @@ macro_rules! py_unary_func {
|
|||
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
|
||||
|
||||
$crate::callback::cb_pyfunc::<_, _, $res_type>(LOCATION, $conv, |py| {
|
||||
let slf = $crate::Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = $crate::Py::<T>::from_borrowed_ptr(slf);
|
||||
let result = {
|
||||
let res = slf.as_mut(py).$f(py).into();
|
||||
$crate::callback::cb_convert($conv, py, res)
|
||||
|
@ -39,7 +39,7 @@ macro_rules! py_unary_func {
|
|||
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
|
||||
|
||||
$crate::callback::cb_pyfunc::<_, _, $res_type>(LOCATION, $conv, |py| {
|
||||
let slf = $crate::Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = $crate::Py::<T>::from_borrowed_ptr(slf);
|
||||
let result = {
|
||||
let res = slf.as_mut(py).$f(py).into();
|
||||
$crate::callback::cb_convert($conv, py, res)
|
||||
|
@ -86,7 +86,7 @@ macro_rules! py_binary_func{
|
|||
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
|
||||
|
||||
$crate::callback::cb_pyfunc::<_, _, $res_type>(LOCATION, $conv, |py| {
|
||||
let slf = $crate::Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = $crate::Py::<T>::from_borrowed_ptr(slf);
|
||||
let arg = $crate::PyObject::from_borrowed_ptr(py, arg);
|
||||
|
||||
let result = {
|
||||
|
@ -120,7 +120,7 @@ macro_rules! py_binary_self_func{
|
|||
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
|
||||
|
||||
$crate::callback::cb_meth(LOCATION, |py| {
|
||||
let slf1 = $crate::Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf1 = $crate::Py::<T>::from_borrowed_ptr(slf);
|
||||
let arg = $crate::PyObject::from_borrowed_ptr(py, arg);
|
||||
|
||||
let result = {
|
||||
|
@ -165,7 +165,7 @@ macro_rules! py_ssizearg_func {
|
|||
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
|
||||
|
||||
$crate::callback::cb_meth(LOCATION, |py| {
|
||||
let slf = $crate::Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = $crate::Py::<T>::from_borrowed_ptr(slf);
|
||||
let result = {
|
||||
let result = slf.as_mut(py).$f(py, arg as isize).into();
|
||||
$crate::callback::cb_convert($conv, py, result)
|
||||
|
@ -194,7 +194,7 @@ macro_rules! py_ternary_func{
|
|||
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
|
||||
|
||||
$crate::callback::cb_pyfunc::<_, _, $res_type>(LOCATION, $conv, |py| {
|
||||
let slf = $crate::Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = $crate::Py::<T>::from_borrowed_ptr(slf);
|
||||
let arg1 = $crate::PyObject::from_borrowed_ptr(py, arg1);
|
||||
let arg2 = $crate::PyObject::from_borrowed_ptr(py, arg2);
|
||||
|
||||
|
@ -233,7 +233,7 @@ macro_rules! py_ternary_self_func{
|
|||
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
|
||||
|
||||
$crate::callback::cb_meth(LOCATION, |py| {
|
||||
let slf1 = $crate::Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf1 = $crate::Py::<T>::from_borrowed_ptr(slf);
|
||||
let arg1 = $crate::PyObject::from_borrowed_ptr(py, arg1);
|
||||
let arg2 = $crate::PyObject::from_borrowed_ptr(py, arg2);
|
||||
|
||||
|
@ -330,7 +330,7 @@ macro_rules! py_func_del{
|
|||
LOCATION, $crate::callback::UnitCallbackConverter, |py|
|
||||
{
|
||||
if value.is_null() {
|
||||
let slf = $crate::Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = $crate::Py::<T>::from_borrowed_ptr(slf);
|
||||
let name = PyObject::from_borrowed_ptr(py, name);
|
||||
|
||||
let result = {
|
||||
|
@ -381,7 +381,7 @@ macro_rules! py_func_set_del{
|
|||
$crate::callback::cb_pyfunc::<_, _, ()>(
|
||||
LOCATION, $crate::callback::UnitCallbackConverter, |py|
|
||||
{
|
||||
let slf = $crate::Ptr::<T>::from_borrowed_ptr(slf);
|
||||
let slf = $crate::Py::<T>::from_borrowed_ptr(slf);
|
||||
let name = PyObject::from_borrowed_ptr(py, name);
|
||||
|
||||
let result = {
|
||||
|
|
|
@ -107,7 +107,7 @@ pub struct PyErr {
|
|||
pub type PyResult<T> = Result<T, PyErr>;
|
||||
|
||||
|
||||
// Marker type that indicates an error while downcasting
|
||||
/// Marker type that indicates an error while downcasting
|
||||
pub struct PyDowncastError<'p>(pub Python<'p>, pub Option<&'p str>);
|
||||
|
||||
|
||||
|
|
|
@ -157,7 +157,7 @@ mod pointers;
|
|||
pub use pointers::PyPtr;
|
||||
|
||||
pub mod token;
|
||||
pub use token::{PyToken, PyObjectWithToken, InstancePtr, Ptr};
|
||||
pub use token::{PyToken, PyObjectWithToken, InstancePtr, Py};
|
||||
|
||||
pub use err::{PyErr, PyResult, PyDowncastError, ToPyErr};
|
||||
pub use objects::*;
|
||||
|
|
|
@ -9,7 +9,7 @@ use std::os::raw::c_int;
|
|||
|
||||
use ffi;
|
||||
use typeob::{PyTypeInfo, PyTypeObject, PyObjectAlloc};
|
||||
use token::{Ptr, PyToken};
|
||||
use token::{Py, PyToken};
|
||||
use objects::{PyObject, PyType, PyBool, PyDict, PyModule};
|
||||
use err::{PyErr, PyResult, PyDowncastError, ToPyErr};
|
||||
use pythonrun::GILGuard;
|
||||
|
@ -231,11 +231,11 @@ impl<'p> Python<'p> {
|
|||
|
||||
/// Create new python object and move T instance under python management
|
||||
#[inline]
|
||||
pub fn init<T, F>(self, f: F) -> PyResult<Ptr<T>>
|
||||
pub fn init<T, F>(self, f: F) -> PyResult<Py<T>>
|
||||
where F: FnOnce(PyToken) -> T,
|
||||
T: PyTypeInfo + PyObjectAlloc<T>
|
||||
{
|
||||
Ptr::new(self, f)
|
||||
Py::new(self, f)
|
||||
}
|
||||
|
||||
/// Release PyObject reference
|
||||
|
|
80
src/token.rs
80
src/token.rs
|
@ -70,57 +70,57 @@ pub trait InstancePtr<T> : Sized {
|
|||
}
|
||||
|
||||
/// Wrapper around unsafe `*mut ffi::PyObject` pointer. Decrement ref counter on `Drop`
|
||||
pub struct Ptr<T>(*mut ffi::PyObject, std::marker::PhantomData<T>);
|
||||
pub struct Py<T>(*mut ffi::PyObject, std::marker::PhantomData<T>);
|
||||
|
||||
// `PyPtr` is thread-safe, because any python related operations require a Python<'p> token.
|
||||
unsafe impl<T> Send for Ptr<T> {}
|
||||
unsafe impl<T> Sync for Ptr<T> {}
|
||||
unsafe impl<T> Send for Py<T> {}
|
||||
unsafe impl<T> Sync for Py<T> {}
|
||||
|
||||
|
||||
impl<T> Ptr<T> {
|
||||
/// Creates a `Ptr<T>` instance for the given FFI pointer.
|
||||
/// This moves ownership over the pointer into the `Ptr<T>`.
|
||||
impl<T> Py<T> {
|
||||
/// Creates a `Py<T>` instance for the given FFI pointer.
|
||||
/// This moves ownership over the pointer into the `Py<T>`.
|
||||
/// Undefined behavior if the pointer is NULL or invalid.
|
||||
#[inline]
|
||||
pub unsafe fn from_owned_ptr(ptr: *mut ffi::PyObject) -> Ptr<T> {
|
||||
pub unsafe fn from_owned_ptr(ptr: *mut ffi::PyObject) -> Py<T> {
|
||||
debug_assert!(!ptr.is_null() && ffi::Py_REFCNT(ptr) > 0,
|
||||
format!("REFCNT: {:?} - {:?}", ptr, ffi::Py_REFCNT(ptr)));
|
||||
Ptr(ptr, std::marker::PhantomData)
|
||||
Py(ptr, std::marker::PhantomData)
|
||||
}
|
||||
|
||||
/// Cast from ffi::PyObject ptr to a concrete object.
|
||||
#[inline]
|
||||
pub fn from_owned_ptr_or_panic(ptr: *mut ffi::PyObject) -> Ptr<T>
|
||||
pub fn from_owned_ptr_or_panic(ptr: *mut ffi::PyObject) -> Py<T>
|
||||
{
|
||||
if ptr.is_null() {
|
||||
::err::panic_after_error();
|
||||
} else {
|
||||
unsafe{ Ptr::from_owned_ptr(ptr) }
|
||||
unsafe{ Py::from_owned_ptr(ptr) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct `Ptr<T>` from the result of a Python FFI call that
|
||||
/// Construct `Py<T>` from the result of a Python FFI call that
|
||||
/// returns a new reference (owned pointer).
|
||||
/// Returns `Err(PyErr)` if the pointer is `null`.
|
||||
/// Unsafe because the pointer might be invalid.
|
||||
pub fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult<Ptr<T>>
|
||||
pub fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult<Py<T>>
|
||||
{
|
||||
if ptr.is_null() {
|
||||
Err(PyErr::fetch(py))
|
||||
} else {
|
||||
Ok(unsafe{ Ptr::from_owned_ptr(ptr) })
|
||||
Ok(unsafe{ Py::from_owned_ptr(ptr) })
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a `Ptr<T>` instance for the given Python FFI pointer.
|
||||
/// Creates a `Py<T>` instance for the given Python FFI pointer.
|
||||
/// Calls Py_INCREF() on the ptr.
|
||||
/// Undefined behavior if the pointer is NULL or invalid.
|
||||
#[inline]
|
||||
pub unsafe fn from_borrowed_ptr(ptr: *mut ffi::PyObject) -> Ptr<T> {
|
||||
pub unsafe fn from_borrowed_ptr(ptr: *mut ffi::PyObject) -> Py<T> {
|
||||
debug_assert!(!ptr.is_null() && ffi::Py_REFCNT(ptr) > 0,
|
||||
format!("REFCNT: {:?} - {:?}", ptr, ffi::Py_REFCNT(ptr)));
|
||||
ffi::Py_INCREF(ptr);
|
||||
Ptr::from_owned_ptr(ptr)
|
||||
Py::from_owned_ptr(ptr)
|
||||
}
|
||||
|
||||
/// Gets the reference count of the ffi::PyObject pointer.
|
||||
|
@ -137,11 +137,11 @@ impl<T> Ptr<T> {
|
|||
|
||||
/// Clone self, Calls Py_INCREF() on the ptr.
|
||||
#[inline]
|
||||
pub fn clone_ref(&self, _py: Python) -> Ptr<T> {
|
||||
unsafe { Ptr::from_borrowed_ptr(self.0) }
|
||||
pub fn clone_ref(&self, _py: Python) -> Py<T> {
|
||||
unsafe { Py::from_borrowed_ptr(self.0) }
|
||||
}
|
||||
|
||||
/// Casts the `Ptr<T>` imstance to a concrete Python object type.
|
||||
/// Casts the `Py<T>` imstance to a concrete Python object type.
|
||||
/// Fails with `PyDowncastError` if the object is not of the expected type.
|
||||
#[inline]
|
||||
pub fn cast_into<D>(self, py: Python) -> Result<D, PyDowncastError>
|
||||
|
@ -159,10 +159,10 @@ impl<T> Ptr<T> {
|
|||
}
|
||||
|
||||
|
||||
impl<T> Ptr<T> where T: PyTypeInfo,
|
||||
impl<T> Py<T> where T: PyTypeInfo,
|
||||
{
|
||||
/// Create new python object and move T instance under python management
|
||||
pub fn new<F>(py: Python, f: F) -> PyResult<Ptr<T>>
|
||||
pub fn new<F>(py: Python, f: F) -> PyResult<Py<T>>
|
||||
where F: FnOnce(::PyToken) -> T,
|
||||
T: PyObjectAlloc<T>
|
||||
{
|
||||
|
@ -170,13 +170,13 @@ impl<T> Ptr<T> where T: PyTypeInfo,
|
|||
|
||||
let ob = unsafe {
|
||||
let ob = try!(<T as PyObjectAlloc<T>>::alloc(py, ob));
|
||||
Ptr::from_owned_ptr(ob)
|
||||
Py::from_owned_ptr(ob)
|
||||
};
|
||||
Ok(ob)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> InstancePtr<T> for Ptr<T> where T: PyTypeInfo {
|
||||
impl<T> InstancePtr<T> for Py<T> where T: PyTypeInfo {
|
||||
|
||||
#[inline]
|
||||
fn as_ref(&self, _py: Python) -> &T {
|
||||
|
@ -196,15 +196,15 @@ impl<T> InstancePtr<T> for Ptr<T> where T: PyTypeInfo {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> ToPyObject for Ptr<T> {
|
||||
impl<T> ToPyObject for Py<T> {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
PyObject::from_borrowed_ptr(py, self.as_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T> IntoPyObject for Ptr<T> {
|
||||
/// Converts `Ptr` instance -> PyObject.
|
||||
impl<T> IntoPyObject for Py<T> {
|
||||
/// Converts `Py` instance -> PyObject.
|
||||
/// Consumes `self` without calling `Py_DECREF()`
|
||||
#[inline]
|
||||
fn into_object(self, _py: Python) -> PyObject {
|
||||
|
@ -212,7 +212,7 @@ impl<T> IntoPyObject for Ptr<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> ToPyPointer for Ptr<T> {
|
||||
impl<T> ToPyPointer for Py<T> {
|
||||
/// Gets the underlying FFI pointer, returns a borrowed pointer.
|
||||
#[inline]
|
||||
fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||
|
@ -220,7 +220,7 @@ impl<T> ToPyPointer for Ptr<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> IntoPyPointer for Ptr<T> {
|
||||
impl<T> IntoPyPointer for Py<T> {
|
||||
/// Gets the underlying FFI pointer, returns a owned pointer.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
|
@ -231,15 +231,15 @@ impl<T> IntoPyPointer for Ptr<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> PartialEq for Ptr<T> {
|
||||
impl<T> PartialEq for Py<T> {
|
||||
#[inline]
|
||||
fn eq(&self, o: &Ptr<T>) -> bool {
|
||||
fn eq(&self, o: &Py<T>) -> bool {
|
||||
self.0 == o.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Dropping a `PyPtr` instance decrements the reference count on the object by 1.
|
||||
impl<T> Drop for Ptr<T> {
|
||||
/// Dropping a `Py` instance decrements the reference count on the object by 1.
|
||||
impl<T> Drop for Py<T> {
|
||||
|
||||
fn drop(&mut self) {
|
||||
if !self.0.is_null() {
|
||||
|
@ -250,25 +250,25 @@ impl<T> Drop for Ptr<T> {
|
|||
}
|
||||
|
||||
|
||||
impl<T> std::convert::From<Ptr<T>> for PyObject {
|
||||
fn from(ob: Ptr<T>) -> Self {
|
||||
impl<T> std::convert::From<Py<T>> for PyObject {
|
||||
fn from(ob: Py<T>) -> Self {
|
||||
unsafe{std::mem::transmute(ob)}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> std::convert::From<&'a T> for Ptr<T>
|
||||
impl<'a, T> std::convert::From<&'a T> for Py<T>
|
||||
where T: ToPyPointer
|
||||
{
|
||||
fn from(ob: &'a T) -> Self {
|
||||
unsafe { Ptr::from_borrowed_ptr(ob.as_ptr()) }
|
||||
unsafe { Py::from_borrowed_ptr(ob.as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> std::convert::From<&'a mut T> for Ptr<T>
|
||||
impl<'a, T> std::convert::From<&'a mut T> for Py<T>
|
||||
where T: ToPyPointer
|
||||
{
|
||||
fn from(ob: &'a mut T) -> Self {
|
||||
unsafe { Ptr::from_borrowed_ptr(ob.as_ptr()) }
|
||||
unsafe { Py::from_borrowed_ptr(ob.as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -276,7 +276,7 @@ impl<'a, T> std::convert::From<&'a T> for PyObject
|
|||
where T: ToPyPointer,
|
||||
{
|
||||
fn from(ob: &'a T) -> Self {
|
||||
unsafe { Ptr::<T>::from_borrowed_ptr(ob.as_ptr()) }.into()
|
||||
unsafe { Py::<T>::from_borrowed_ptr(ob.as_ptr()) }.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -284,6 +284,6 @@ impl<'a, T> std::convert::From<&'a mut T> for PyObject
|
|||
where T: ToPyPointer,
|
||||
{
|
||||
fn from(ob: &'a mut T) -> Self {
|
||||
unsafe { Ptr::<T>::from_borrowed_ptr(ob.as_ptr()) }.into()
|
||||
unsafe { Py::<T>::from_borrowed_ptr(ob.as_ptr()) }.into()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -99,8 +99,8 @@ struct EmptyClassWithNew {
|
|||
#[py::methods]
|
||||
impl EmptyClassWithNew {
|
||||
#[__new__]
|
||||
fn __new__(cls: &PyType, py: Python) -> PyResult<Ptr<EmptyClassWithNew>> {
|
||||
Ptr::new(py, |t| EmptyClassWithNew{token: t})
|
||||
fn __new__(cls: &PyType, py: Python) -> PyResult<Py<EmptyClassWithNew>> {
|
||||
Py::new(py, |t| EmptyClassWithNew{token: t})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -121,8 +121,8 @@ struct NewWithOneArg {
|
|||
#[py::methods]
|
||||
impl NewWithOneArg {
|
||||
#[new]
|
||||
fn __new__(_cls: &PyType, py: Python, arg: i32) -> PyResult<Ptr<NewWithOneArg>> {
|
||||
Ptr::new(py, |t| NewWithOneArg{_data: arg, token: t})
|
||||
fn __new__(_cls: &PyType, py: Python, arg: i32) -> PyResult<Py<NewWithOneArg>> {
|
||||
Py::new(py, |t| NewWithOneArg{_data: arg, token: t})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,9 +147,9 @@ struct NewWithTwoArgs {
|
|||
#[py::methods]
|
||||
impl NewWithTwoArgs {
|
||||
#[new]
|
||||
fn __new__(_cls: &PyType, py: Python, arg1: i32, arg2: i32) -> PyResult<Ptr<NewWithTwoArgs>>
|
||||
fn __new__(_cls: &PyType, py: Python, arg1: i32, arg2: i32) -> PyResult<Py<NewWithTwoArgs>>
|
||||
{
|
||||
Ptr::new(py, |t| NewWithTwoArgs{_data1: arg1, _data2: arg2, token: t})
|
||||
Py::new(py, |t| NewWithTwoArgs{_data1: arg1, _data2: arg2, token: t})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,12 +172,12 @@ fn class_with_freelist() {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let inst = Ptr::new(py, |t| ClassWithFreelist{token: t}).unwrap();
|
||||
let inst2 = Ptr::new(py, |t| ClassWithFreelist{token: t}).unwrap();
|
||||
let inst = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap();
|
||||
let inst2 = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap();
|
||||
let ptr = inst.as_ptr();
|
||||
drop(inst);
|
||||
|
||||
let inst3 = Ptr::new(py, |t| ClassWithFreelist{token: t}).unwrap();
|
||||
let inst3 = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap();
|
||||
assert_eq!(ptr, inst3.as_ptr());
|
||||
}
|
||||
|
||||
|
@ -204,7 +204,7 @@ fn data_is_dropped() {
|
|||
|
||||
let drop_called1 = Arc::new(AtomicBool::new(false));
|
||||
let drop_called2 = Arc::new(AtomicBool::new(false));
|
||||
let inst = Ptr::new(py, |t| DataIsDropped{
|
||||
let inst = Py::new(py, |t| DataIsDropped{
|
||||
member1: TestDropCall { drop_called: drop_called1.clone() },
|
||||
member2: TestDropCall { drop_called: drop_called2.clone() },
|
||||
token: t
|
||||
|
@ -236,7 +236,7 @@ fn instance_method() {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let obj = Ptr::new(py, |t| InstanceMethod{member: 42, token: t}).unwrap();
|
||||
let obj = Py::new(py, |t| InstanceMethod{member: 42, token: t}).unwrap();
|
||||
assert!(obj.as_ref(py).method(py).unwrap() == 42);
|
||||
let d = PyDict::new(py);
|
||||
d.set_item(py, "obj", obj).unwrap();
|
||||
|
@ -262,7 +262,7 @@ fn instance_method_with_args() {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let obj = Ptr::new(py, |t| InstanceMethodWithArgs{member: 7, token: t}).unwrap();
|
||||
let obj = Py::new(py, |t| InstanceMethodWithArgs{member: 7, token: t}).unwrap();
|
||||
assert!(obj.as_ref(py).method(py, 6).unwrap() == 42);
|
||||
let d = PyDict::new(py);
|
||||
d.set_item(py, "obj", obj).unwrap();
|
||||
|
@ -277,8 +277,8 @@ struct ClassMethod {token: PyToken}
|
|||
#[py::methods]
|
||||
impl ClassMethod {
|
||||
#[new]
|
||||
fn __new__(cls: &PyType, py: Python) -> PyResult<Ptr<ClassMethod>> {
|
||||
Ptr::new(py, |t| ClassMethod{token: t})
|
||||
fn __new__(cls: &PyType, py: Python) -> PyResult<Py<ClassMethod>> {
|
||||
Py::new(py, |t| ClassMethod{token: t})
|
||||
}
|
||||
|
||||
#[classmethod]
|
||||
|
@ -328,8 +328,8 @@ struct StaticMethod {
|
|||
#[py::methods]
|
||||
impl StaticMethod {
|
||||
#[new]
|
||||
fn __new__(cls: &PyType, py: Python) -> PyResult<Ptr<StaticMethod>> {
|
||||
Ptr::new(py, |t| StaticMethod{token: t})
|
||||
fn __new__(cls: &PyType, py: Python) -> PyResult<Py<StaticMethod>> {
|
||||
Py::new(py, |t| StaticMethod{token: t})
|
||||
}
|
||||
|
||||
#[staticmethod]
|
||||
|
@ -398,7 +398,7 @@ fn gc_integration() {
|
|||
let py = gil.python();
|
||||
|
||||
let drop_called = Arc::new(AtomicBool::new(false));
|
||||
let inst = Ptr::new(py, |t| GCIntegration{
|
||||
let inst = Py::new(py, |t| GCIntegration{
|
||||
self_ref: RefCell::new(py.None()),
|
||||
dropped: TestDropCall { drop_called: drop_called.clone() },
|
||||
token: t}).unwrap();
|
||||
|
@ -428,14 +428,14 @@ fn len() {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let inst = Ptr::new(py, |t| Len{l: 10, token: t}).unwrap();
|
||||
let inst = Py::new(py, |t| Len{l: 10, token: t}).unwrap();
|
||||
py_assert!(py, inst, "len(inst) == 10");
|
||||
unsafe {
|
||||
assert_eq!(ffi::PyObject_Size(inst.as_ptr()), 10);
|
||||
assert_eq!(ffi::PyMapping_Size(inst.as_ptr()), 10);
|
||||
}
|
||||
|
||||
let inst = Ptr::new(py, |t| Len{l: (isize::MAX as usize) + 1, token: t}).unwrap();
|
||||
let inst = Py::new(py, |t| Len{l: (isize::MAX as usize) + 1, token: t}).unwrap();
|
||||
py_expect_exception!(py, inst, "len(inst)", OverflowError);
|
||||
}
|
||||
|
||||
|
@ -447,7 +447,7 @@ struct Iterator{
|
|||
|
||||
#[py::proto]
|
||||
impl PyIterProtocol for Iterator {
|
||||
fn __iter__(&mut self, py: Python) -> PyResult<Ptr<Iterator>> {
|
||||
fn __iter__(&mut self, py: Python) -> PyResult<Py<Iterator>> {
|
||||
Ok(self.into())
|
||||
}
|
||||
|
||||
|
@ -461,7 +461,7 @@ fn iterator() {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let inst = Ptr::new(py, |t| Iterator{iter: Box::new(5..8), token: t}).unwrap();
|
||||
let inst = Py::new(py, |t| Iterator{iter: Box::new(5..8), token: t}).unwrap();
|
||||
py_assert!(py, inst, "iter(inst) is inst");
|
||||
py_assert!(py, inst, "list(inst) == [5, 6, 7]");
|
||||
}
|
||||
|
@ -498,7 +498,7 @@ fn string_methods() {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let obj = Ptr::new(py, |t| StringMethods{token: t}).unwrap();
|
||||
let obj = Py::new(py, |t| StringMethods{token: t}).unwrap();
|
||||
py_assert!(py, obj, "str(obj) == 'str'");
|
||||
py_assert!(py, obj, "repr(obj) == 'repr'");
|
||||
py_assert!(py, obj, "'{0:x}'.format(obj) == 'format(x)'");
|
||||
|
@ -511,7 +511,7 @@ fn string_methods() {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let obj = Ptr::new(py, |t| StringMethods{token: t}).unwrap();
|
||||
let obj = Py::new(py, |t| StringMethods{token: t}).unwrap();
|
||||
py_assert!(py, obj, "str(obj) == 'str'");
|
||||
py_assert!(py, obj, "repr(obj) == 'repr'");
|
||||
py_assert!(py, obj, "unicode(obj) == 'unicode'");
|
||||
|
@ -541,10 +541,10 @@ fn comparisons() {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let zero = Ptr::new(py, |t| Comparisons{val: 0, token: t}).unwrap();
|
||||
let one = Ptr::new(py, |t| Comparisons{val: 1, token: t}).unwrap();
|
||||
let ten = Ptr::new(py, |t| Comparisons{val: 10, token: t}).unwrap();
|
||||
let minus_one = Ptr::new(py, |t| Comparisons{val: -1, token: t}).unwrap();
|
||||
let zero = Py::new(py, |t| Comparisons{val: 0, token: t}).unwrap();
|
||||
let one = Py::new(py, |t| Comparisons{val: 1, token: t}).unwrap();
|
||||
let ten = Py::new(py, |t| Comparisons{val: 10, token: t}).unwrap();
|
||||
let minus_one = Py::new(py, |t| Comparisons{val: -1, token: t}).unwrap();
|
||||
py_assert!(py, one, "hash(one) == 1");
|
||||
py_assert!(py, ten, "hash(ten) == 10");
|
||||
py_assert!(py, minus_one, "hash(minus_one) == -2");
|
||||
|
|
Loading…
Reference in New Issue