Remove ToPythonPointer from public API
This commit is contained in:
parent
e6b5312c81
commit
280cdab7a2
|
@ -47,8 +47,8 @@ pub trait ToPyObject<'p> {
|
|||
#[inline]
|
||||
fn with_borrowed_ptr<F, R>(&self, py: Python<'p>, f: F) -> R
|
||||
where F: FnOnce(*mut ffi::PyObject) -> R {
|
||||
let obj = self.to_py_object(py);
|
||||
f(ToPythonPointer::as_ptr(&obj))
|
||||
let obj = self.to_py_object(py).into_object();
|
||||
f(obj.as_ptr())
|
||||
}
|
||||
|
||||
// FFI functions that accept a borrowed reference will use:
|
||||
|
|
|
@ -48,7 +48,7 @@ macro_rules! py_fn {
|
|||
match $f(py, &args) {
|
||||
Ok(val) => {
|
||||
let obj = $crate::ToPyObject::into_py_object(val, py);
|
||||
return $crate::ToPythonPointer::steal_ptr(obj);
|
||||
return $crate::PythonObject::into_object(obj).steal_ptr();
|
||||
}
|
||||
Err(e) => {
|
||||
e.restore();
|
||||
|
|
|
@ -86,7 +86,7 @@ extern crate python3_sys as ffi;
|
|||
pub use ffi::Py_ssize_t;
|
||||
pub use err::{PyErr, PyResult};
|
||||
pub use objects::*;
|
||||
pub use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PythonObjectWithTypeObject, ToPythonPointer};
|
||||
pub use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PythonObjectWithTypeObject};
|
||||
pub use pythonrun::{GILGuard, GILProtected, prepare_freethreaded_python};
|
||||
pub use conversion::{FromPyObject, ToPyObject};
|
||||
pub use objectprotocol::{ObjectProtocol};
|
||||
|
@ -277,7 +277,7 @@ pub unsafe fn py_module_initializer_impl(
|
|||
}
|
||||
};
|
||||
match init(py, &module) {
|
||||
Ok(()) => module.steal_ptr(),
|
||||
Ok(()) => module.into_object().steal_ptr(),
|
||||
Err(e) => {
|
||||
e.restore();
|
||||
return ptr::null_mut();
|
||||
|
|
|
@ -44,18 +44,6 @@ macro_rules! pyobject_newtype(
|
|||
#[derive(Clone)]
|
||||
pub struct $name<'p>(::objects::object::PyObject<'p>);
|
||||
|
||||
impl <'p> ::python::ToPythonPointer for $name<'p> {
|
||||
#[inline]
|
||||
fn as_ptr(&self) -> *mut ::ffi::PyObject {
|
||||
::python::ToPythonPointer::as_ptr(&self.0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn steal_ptr(self) -> *mut ::ffi::PyObject {
|
||||
::python::ToPythonPointer::steal_ptr(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl <'p> ::python::PythonObject<'p> for $name<'p> {
|
||||
#[inline]
|
||||
fn as_object(&self) -> &::objects::object::PyObject<'p> {
|
||||
|
|
|
@ -98,21 +98,6 @@ impl <'p> PythonObjectWithTypeObject<'p> for PyObject<'p> {
|
|||
}
|
||||
}
|
||||
|
||||
impl <'p> ToPythonPointer for PyObject<'p> {
|
||||
#[inline]
|
||||
fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||
self.ptr
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn steal_ptr(self) -> *mut ffi::PyObject {
|
||||
let ptr = self.ptr;
|
||||
mem::forget(self);
|
||||
ptr
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl <'p> PyObject<'p> {
|
||||
/// Creates a PyObject instance for the given FFI pointer.
|
||||
/// This moves ownership over the pointer into the PyObject.
|
||||
|
@ -154,7 +139,24 @@ impl <'p> PyObject<'p> {
|
|||
Some(PyObject::from_borrowed_ptr(py, ptr))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Gets the underlying FFI pointer.
|
||||
/// Returns a borrowed pointer.
|
||||
#[inline]
|
||||
pub fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||
self.ptr
|
||||
}
|
||||
|
||||
/// Gets the underlying FFI pointer.
|
||||
/// Consumes `self` without calling `Py_DECREF()`, thus returning an owned pointer.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn steal_ptr(self) -> *mut ffi::PyObject {
|
||||
let ptr = self.ptr;
|
||||
mem::forget(self);
|
||||
ptr
|
||||
}
|
||||
|
||||
/// Transmutes an owned FFI pointer to `&PyObject`.
|
||||
/// Undefined behavior if the pointer is NULL or invalid.
|
||||
#[inline]
|
||||
|
@ -238,6 +240,19 @@ impl <'p> PartialEq for PyObject<'p> {
|
|||
/// `obj1 == obj2` in rust is equivalent to `obj1 is obj2` in python.
|
||||
impl <'p> Eq for PyObject<'p> { }
|
||||
|
||||
impl <'p> ToPythonPointer for PyObject<'p> {
|
||||
// forward to inherit methods
|
||||
#[inline]
|
||||
fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||
PyObject::as_ptr(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn steal_ptr(self) -> *mut ffi::PyObject {
|
||||
PyObject::steal_ptr(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_sizeof() {
|
||||
|
|
|
@ -30,7 +30,7 @@ impl <'p> PyType<'p> {
|
|||
/// Retrieves the underlying FFI pointer associated with this python object.
|
||||
#[inline]
|
||||
pub fn as_type_ptr(&self) -> *mut ffi::PyTypeObject {
|
||||
self.as_ptr() as *mut ffi::PyTypeObject
|
||||
self.0.as_ptr() as *mut ffi::PyTypeObject
|
||||
}
|
||||
|
||||
/// Retrieves the PyType instance for the given FFI pointer.
|
||||
|
@ -59,7 +59,7 @@ impl <'p> PyType<'p> {
|
|||
where A: ToPyObject<'p, ObjectType=PyTuple<'p>> {
|
||||
let py = self.python();
|
||||
args.with_borrowed_ptr(py, |args| unsafe {
|
||||
result_from_owned_ptr(py, ffi::PyObject_Call(self.as_ptr(), args, kwargs.as_ptr()))
|
||||
result_from_owned_ptr(py, ffi::PyObject_Call(self.0.as_ptr(), args, kwargs.as_ptr()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,10 +50,10 @@ pub trait ToPythonPointer {
|
|||
}
|
||||
|
||||
/// Trait implemented by all python object types.
|
||||
pub trait PythonObject<'p> : 'p + Clone + ToPythonPointer {
|
||||
pub trait PythonObject<'p> : 'p + Clone {
|
||||
/// Casts the python object to PyObject.
|
||||
fn as_object(&self) -> &PyObject<'p>;
|
||||
|
||||
|
||||
/// Casts the python object to PyObject.
|
||||
fn into_object(self) -> PyObject<'p>;
|
||||
|
||||
|
@ -94,16 +94,15 @@ pub trait PythonObjectWithTypeObject<'p> : PythonObjectWithCheckedDowncast<'p> {
|
|||
impl <'a, 'p, T> ToPythonPointer for &'a T where T: PythonObject<'p> {
|
||||
#[inline]
|
||||
fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||
(**self).as_ptr()
|
||||
self.as_object().as_ptr()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn steal_ptr(self) -> *mut ffi::PyObject {
|
||||
(*self).clone().steal_ptr()
|
||||
self.as_object().clone().steal_ptr()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Convert None into a null pointer.
|
||||
impl <T> ToPythonPointer for Option<T> where T: ToPythonPointer {
|
||||
#[inline]
|
||||
|
|
|
@ -78,7 +78,7 @@ macro_rules! py_method {
|
|||
match $f(&slf, &args) {
|
||||
Ok(val) => {
|
||||
let obj = $crate::ToPyObject::into_py_object(val, py);
|
||||
return $crate::ToPythonPointer::steal_ptr(obj);
|
||||
return $crate::PythonObject::into_object(obj).steal_ptr();
|
||||
}
|
||||
Err(e) => {
|
||||
e.restore();
|
||||
|
@ -167,7 +167,7 @@ macro_rules! py_class_method {
|
|||
match $f(&slf, &args) {
|
||||
Ok(val) => {
|
||||
let obj = $crate::ToPyObject::into_py_object(val, py);
|
||||
return $crate::ToPythonPointer::steal_ptr(obj);
|
||||
return $crate::PythonObject::into_object(obj).steal_ptr();
|
||||
}
|
||||
Err(e) => {
|
||||
e.restore();
|
||||
|
|
|
@ -121,7 +121,7 @@ impl <'p, T, B> PythonBaseObject<'p> for PyRustObject<'p, T, B> where T: 'static
|
|||
unsafe fn alloc(ty: &PyType<'p>, (val, base_val): Self::InitType) -> PyResult<'p, Self> {
|
||||
let obj = try!(B::alloc(ty, base_val));
|
||||
let offset = PyRustObject::<T, B>::offset() as isize;
|
||||
ptr::write((obj.as_ptr() as *mut u8).offset(offset) as *mut T, val);
|
||||
ptr::write((obj.as_object().as_ptr() as *mut u8).offset(offset) as *mut T, val);
|
||||
Ok(Self::unchecked_downcast_from(obj.into_object()))
|
||||
}
|
||||
|
||||
|
@ -142,18 +142,6 @@ impl <'p, T, B> Clone for PyRustObject<'p, T, B> where T: 'static + Send, B: Pyt
|
|||
}
|
||||
}
|
||||
|
||||
impl <'p, T, B> ToPythonPointer for PyRustObject<'p, T, B> where T: 'static + Send, B: PythonBaseObject<'p> {
|
||||
#[inline]
|
||||
fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||
self.obj.as_ptr()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn steal_ptr(self) -> *mut ffi::PyObject {
|
||||
self.obj.steal_ptr()
|
||||
}
|
||||
}
|
||||
|
||||
impl <'p, 's, T, B> ToPyObject<'p> for PyRustObject<'s, T, B> where T: 'static + Send, B: PythonBaseObject<'s> {
|
||||
type ObjectType = PyObject<'p>;
|
||||
|
||||
|
@ -231,18 +219,6 @@ impl <'p, T, B> ops::Deref for PyRustType<'p, T, B> where T: 'p + Send, B: Pytho
|
|||
}
|
||||
}
|
||||
|
||||
impl <'p, T> ToPythonPointer for PyRustType<'p, T> where T: 'p + Send {
|
||||
#[inline]
|
||||
fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||
self.type_obj.as_ptr()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn steal_ptr(self) -> *mut ffi::PyObject {
|
||||
self.type_obj.steal_ptr()
|
||||
}
|
||||
}
|
||||
|
||||
impl <'p, T> Clone for PyRustType<'p, T> where T: 'p + Send {
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
|
|
|
@ -92,7 +92,7 @@ impl <'p, T> PyRustTypeBuilder<'p, T> where T: 'static + Send {
|
|||
unsafe {
|
||||
ffi::Py_XDECREF((*self.ht).ht_type.tp_base as *mut ffi::PyObject);
|
||||
(*self.ht).ht_type.tp_base = base_type.as_type_ptr();
|
||||
ffi::Py_INCREF(base_type.as_ptr());
|
||||
ffi::Py_INCREF(base_type.as_object().as_ptr());
|
||||
}
|
||||
PyRustTypeBuilder {
|
||||
type_obj: self.type_obj,
|
||||
|
|
Loading…
Reference in a new issue