Remove ToPythonPointer from public API
This commit is contained in:
parent
e6b5312c81
commit
280cdab7a2
|
@ -47,8 +47,8 @@ pub trait ToPyObject<'p> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn with_borrowed_ptr<F, R>(&self, py: Python<'p>, f: F) -> R
|
fn with_borrowed_ptr<F, R>(&self, py: Python<'p>, f: F) -> R
|
||||||
where F: FnOnce(*mut ffi::PyObject) -> R {
|
where F: FnOnce(*mut ffi::PyObject) -> R {
|
||||||
let obj = self.to_py_object(py);
|
let obj = self.to_py_object(py).into_object();
|
||||||
f(ToPythonPointer::as_ptr(&obj))
|
f(obj.as_ptr())
|
||||||
}
|
}
|
||||||
|
|
||||||
// FFI functions that accept a borrowed reference will use:
|
// FFI functions that accept a borrowed reference will use:
|
||||||
|
|
|
@ -48,7 +48,7 @@ macro_rules! py_fn {
|
||||||
match $f(py, &args) {
|
match $f(py, &args) {
|
||||||
Ok(val) => {
|
Ok(val) => {
|
||||||
let obj = $crate::ToPyObject::into_py_object(val, py);
|
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) => {
|
Err(e) => {
|
||||||
e.restore();
|
e.restore();
|
||||||
|
|
|
@ -86,7 +86,7 @@ extern crate python3_sys as ffi;
|
||||||
pub use ffi::Py_ssize_t;
|
pub use ffi::Py_ssize_t;
|
||||||
pub use err::{PyErr, PyResult};
|
pub use err::{PyErr, PyResult};
|
||||||
pub use objects::*;
|
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 pythonrun::{GILGuard, GILProtected, prepare_freethreaded_python};
|
||||||
pub use conversion::{FromPyObject, ToPyObject};
|
pub use conversion::{FromPyObject, ToPyObject};
|
||||||
pub use objectprotocol::{ObjectProtocol};
|
pub use objectprotocol::{ObjectProtocol};
|
||||||
|
@ -277,7 +277,7 @@ pub unsafe fn py_module_initializer_impl(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match init(py, &module) {
|
match init(py, &module) {
|
||||||
Ok(()) => module.steal_ptr(),
|
Ok(()) => module.into_object().steal_ptr(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
e.restore();
|
e.restore();
|
||||||
return ptr::null_mut();
|
return ptr::null_mut();
|
||||||
|
|
|
@ -44,18 +44,6 @@ macro_rules! pyobject_newtype(
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct $name<'p>(::objects::object::PyObject<'p>);
|
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> {
|
impl <'p> ::python::PythonObject<'p> for $name<'p> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_object(&self) -> &::objects::object::PyObject<'p> {
|
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> {
|
impl <'p> PyObject<'p> {
|
||||||
/// Creates a PyObject instance for the given FFI pointer.
|
/// Creates a PyObject instance for the given FFI pointer.
|
||||||
/// This moves ownership over the pointer into the PyObject.
|
/// This moves ownership over the pointer into the PyObject.
|
||||||
|
@ -155,6 +140,23 @@ impl <'p> PyObject<'p> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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`.
|
/// Transmutes an owned FFI pointer to `&PyObject`.
|
||||||
/// Undefined behavior if the pointer is NULL or invalid.
|
/// Undefined behavior if the pointer is NULL or invalid.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -238,6 +240,19 @@ impl <'p> PartialEq for PyObject<'p> {
|
||||||
/// `obj1 == obj2` in rust is equivalent to `obj1 is obj2` in python.
|
/// `obj1 == obj2` in rust is equivalent to `obj1 is obj2` in python.
|
||||||
impl <'p> Eq for PyObject<'p> { }
|
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]
|
#[test]
|
||||||
fn test_sizeof() {
|
fn test_sizeof() {
|
||||||
|
|
|
@ -30,7 +30,7 @@ impl <'p> PyType<'p> {
|
||||||
/// Retrieves the underlying FFI pointer associated with this python object.
|
/// Retrieves the underlying FFI pointer associated with this python object.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_type_ptr(&self) -> *mut ffi::PyTypeObject {
|
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.
|
/// Retrieves the PyType instance for the given FFI pointer.
|
||||||
|
@ -59,7 +59,7 @@ impl <'p> PyType<'p> {
|
||||||
where A: ToPyObject<'p, ObjectType=PyTuple<'p>> {
|
where A: ToPyObject<'p, ObjectType=PyTuple<'p>> {
|
||||||
let py = self.python();
|
let py = self.python();
|
||||||
args.with_borrowed_ptr(py, |args| unsafe {
|
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,7 +50,7 @@ pub trait ToPythonPointer {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait implemented by all python object types.
|
/// 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.
|
/// Casts the python object to PyObject.
|
||||||
fn as_object(&self) -> &PyObject<'p>;
|
fn as_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> {
|
impl <'a, 'p, T> ToPythonPointer for &'a T where T: PythonObject<'p> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_ptr(&self) -> *mut ffi::PyObject {
|
fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||||
(**self).as_ptr()
|
self.as_object().as_ptr()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn steal_ptr(self) -> *mut ffi::PyObject {
|
fn steal_ptr(self) -> *mut ffi::PyObject {
|
||||||
(*self).clone().steal_ptr()
|
self.as_object().clone().steal_ptr()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Convert None into a null pointer.
|
/// Convert None into a null pointer.
|
||||||
impl <T> ToPythonPointer for Option<T> where T: ToPythonPointer {
|
impl <T> ToPythonPointer for Option<T> where T: ToPythonPointer {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -78,7 +78,7 @@ macro_rules! py_method {
|
||||||
match $f(&slf, &args) {
|
match $f(&slf, &args) {
|
||||||
Ok(val) => {
|
Ok(val) => {
|
||||||
let obj = $crate::ToPyObject::into_py_object(val, py);
|
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) => {
|
Err(e) => {
|
||||||
e.restore();
|
e.restore();
|
||||||
|
@ -167,7 +167,7 @@ macro_rules! py_class_method {
|
||||||
match $f(&slf, &args) {
|
match $f(&slf, &args) {
|
||||||
Ok(val) => {
|
Ok(val) => {
|
||||||
let obj = $crate::ToPyObject::into_py_object(val, py);
|
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) => {
|
Err(e) => {
|
||||||
e.restore();
|
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> {
|
unsafe fn alloc(ty: &PyType<'p>, (val, base_val): Self::InitType) -> PyResult<'p, Self> {
|
||||||
let obj = try!(B::alloc(ty, base_val));
|
let obj = try!(B::alloc(ty, base_val));
|
||||||
let offset = PyRustObject::<T, B>::offset() as isize;
|
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()))
|
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> {
|
impl <'p, 's, T, B> ToPyObject<'p> for PyRustObject<'s, T, B> where T: 'static + Send, B: PythonBaseObject<'s> {
|
||||||
type ObjectType = PyObject<'p>;
|
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 {
|
impl <'p, T> Clone for PyRustType<'p, T> where T: 'p + Send {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
|
|
|
@ -92,7 +92,7 @@ impl <'p, T> PyRustTypeBuilder<'p, T> where T: 'static + Send {
|
||||||
unsafe {
|
unsafe {
|
||||||
ffi::Py_XDECREF((*self.ht).ht_type.tp_base as *mut ffi::PyObject);
|
ffi::Py_XDECREF((*self.ht).ht_type.tp_base as *mut ffi::PyObject);
|
||||||
(*self.ht).ht_type.tp_base = base_type.as_type_ptr();
|
(*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 {
|
PyRustTypeBuilder {
|
||||||
type_obj: self.type_obj,
|
type_obj: self.type_obj,
|
||||||
|
|
Loading…
Reference in New Issue