rename pptr to PyObjectPtr

This commit is contained in:
Nikolay Kim 2017-05-31 01:07:33 -07:00
parent a3e6117090
commit 92bf7b9c05
13 changed files with 85 additions and 85 deletions

View File

@ -24,8 +24,8 @@ use libc;
use ffi;
use exc;
use err::{self, PyResult};
use pointers::{Py};
use python::{Python, ToPythonPointer};
use token::PythonObjectWithGilToken;
use objects::PyObject;
/// Allows access to the underlying buffer used by a python object such as `bytes`, `bytearray` or `array.array`.
@ -143,7 +143,7 @@ impl PyBuffer {
unsafe {
let mut buf = Box::new(mem::zeroed::<ffi::Py_buffer>());
err::error_on_minusone(
obj.token(),
obj.gil(),
ffi::PyObject_GetBuffer(obj.as_ptr(), &mut *buf, ffi::PyBUF_FULL_RO))?;
validate(&buf);
Ok(PyBuffer(buf))

View File

@ -1,6 +1,6 @@
use ffi;
use err::PyResult;
use pointers::pptr;
use pointers::PyObjectPtr;
use python::{Python, ToPythonPointer, PyDowncastFrom};
use objects::{PyObject, PyTuple};
use native::PyNativeObject;
@ -31,7 +31,7 @@ pub trait IntoPyObject {
/// Converts self into a Python object. (Consumes self)
#[inline]
fn into_object(self, py: Python) -> pptr
fn into_object(self, py: Python) -> PyObjectPtr
where Self: Sized;
}
@ -102,7 +102,7 @@ impl <'p, T: ?Sized> RefFromPyObject<'p> for T
impl<T> IntoPyObject for T where T: ToPyObject
{
#[inline]
default fn into_object(self, py: Python) -> pptr
default fn into_object(self, py: Python) -> PyObjectPtr
{
self.to_object(py).park()
}
@ -140,7 +140,7 @@ impl <T> ToPyObject for Option<T> where T: ToPyObject {
impl<T> IntoPyObject for Option<T> where T: IntoPyObject {
fn into_object(self, py: Python) -> pptr {
fn into_object(self, py: Python) -> PyObjectPtr {
match self {
Some(val) => val.into_object(py),
None => py.None()

View File

@ -4,7 +4,7 @@ use std::os::raw::c_char;
use libc;
use ffi;
use pointers::pptr;
use pointers::PyObjectPtr;
use python::{ToPythonPointer, IntoPythonPointer, Python};
use objects::{PyObject, PyType, exc};
use native::PyNativeObject;
@ -82,16 +82,16 @@ macro_rules! py_exception {
#[derive(Debug)]
pub struct PyErr {
/// The type of the exception. This should be either a `PyClass` or a `PyType`.
pub ptype: pptr,
pub ptype: PyObjectPtr,
/// The value of the exception.
///
/// This can be either an instance of `ptype`,
/// a tuple of arguments to be passed to `ptype`'s constructor,
/// or a single argument to be passed to `ptype`'s constructor.
/// Call `PyErr::instance()` to get the exception instance in all cases.
pub pvalue: Option<pptr>,
pub pvalue: Option<PyObjectPtr>,
/// The `PyTraceBack` object associated with the error.
pub ptraceback: Option<pptr>,
pub ptraceback: Option<PyObjectPtr>,
}
@ -176,14 +176,14 @@ impl PyErr {
ptype: if ptype.is_null() {
py.get_type::<exc::SystemError>().park()
} else {
pptr::from_owned_ptr(ptype)
PyObjectPtr::from_owned_ptr(ptype)
},
pvalue: pptr::from_owned_ptr_or_opt(pvalue),
ptraceback: pptr::from_owned_ptr_or_opt(ptraceback)
pvalue: PyObjectPtr::from_owned_ptr_or_opt(pvalue),
ptraceback: PyObjectPtr::from_owned_ptr_or_opt(ptraceback)
}
}
fn new_helper(_py: Python, ty: pptr, value: pptr) -> PyErr {
fn new_helper(_py: Python, ty: PyObjectPtr, value: PyObjectPtr) -> PyErr {
assert!(unsafe { ffi::PyExceptionClass_Check(ty.as_ptr()) } != 0);
PyErr {
ptype: ty,
@ -201,10 +201,10 @@ impl PyErr {
PyErr::from_instance_helper(py, obj.into_object(py))
}
fn from_instance_helper<'p>(py: Python, obj: pptr) -> PyErr {
fn from_instance_helper<'p>(py: Python, obj: PyObjectPtr) -> PyErr {
if unsafe { ffi::PyExceptionInstance_Check(obj.as_ptr()) } != 0 {
PyErr {
ptype: unsafe { pptr::from_borrowed_ptr(
ptype: unsafe { PyObjectPtr::from_borrowed_ptr(
ffi::PyExceptionInstance_Class(obj.as_ptr())) },
pvalue: Some(obj),
ptraceback: None
@ -228,7 +228,7 @@ impl PyErr {
/// `exc` is the exception type; usually one of the standard exceptions like `py.get_type::<exc::RuntimeError>()`.
/// `value` is the exception instance, or a tuple of arguments to pass to the exception constructor.
#[inline]
pub fn new_lazy_init<'p>(exc: PyType<'p>, value: Option<pptr>) -> PyErr {
pub fn new_lazy_init<'p>(exc: PyType<'p>, value: Option<PyObjectPtr>) -> PyErr {
PyErr {
ptype: exc.park(),
pvalue: value,

View File

@ -65,7 +65,7 @@ pub mod ffi;
pub use ffi::{Py_ssize_t, Py_hash_t};
pub mod pointers;
pub use pointers::{Py, PyPtr, pptr};
pub use pointers::{Py, PyPtr, PyObjectPtr};
mod ppptr;
pub use ppptr::{pyptr};
@ -123,7 +123,7 @@ pub mod callback;
pub mod typeob;
pub mod argparse;
pub mod function;
// pub mod buffer;
pub mod buffer;
// re-export for simplicity
pub use std::os::raw::*;

View File

@ -1,13 +1,13 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use pointers::pptr;
use pointers::PyObjectPtr;
use objects::PyObject;
pub trait PyBaseObject : Sized {}
pub trait PyNativeObject<'p> : PyBaseObject {
fn park(self) -> pptr;
fn park(self) -> PyObjectPtr;
fn as_object(self) -> PyObject<'p>;

View File

@ -8,7 +8,7 @@ use std::cmp::Ordering;
use ffi;
use libc;
use pyptr;
use pointers::{pptr, Py, PyPtr};
use pointers::{PyObjectPtr, Py, PyPtr};
use python::{Python, PyDowncastInto, ToPythonPointer};
use objects::{PyObject, PyDict, PyString, PyIterator};
use token::PythonObjectWithGilToken;
@ -427,7 +427,7 @@ impl<'p, T> fmt::Display for PyPtr<T> where T: ObjectProtocol<'p> + PyTypeInfo {
}
impl fmt::Debug for pptr {
impl fmt::Debug for PyObjectPtr {
fn fmt(&self, f : &mut fmt::Formatter) -> Result<(), fmt::Error> {
let gil = Python::acquire_gil();
let py = gil.python();
@ -439,7 +439,7 @@ impl fmt::Debug for pptr {
}
}
impl fmt::Display for pptr {
impl fmt::Display for PyObjectPtr {
default fn fmt(&self, f : &mut fmt::Formatter) -> Result<(), fmt::Error> {
let gil = Python::acquire_gil();
let py = gil.python();

View File

@ -9,7 +9,7 @@ use std::{self, mem, ops};
use std::ffi::CStr;
use ffi;
use pointers::pptr;
use pointers::PyObjectPtr;
use python::{Python, ToPythonPointer};
use err::PyResult;
use native::PyNativeObject;
@ -86,10 +86,10 @@ exc_type!(UnicodeTranslateError, PyExc_UnicodeTranslateError);
impl UnicodeDecodeError {
pub fn new(py: Python, encoding: &CStr, input: &[u8], range: ops::Range<usize>, reason: &CStr)
-> PyResult<pptr> {
-> PyResult<PyObjectPtr> {
unsafe {
let input: &[c_char] = mem::transmute(input);
pptr::from_owned_ptr_or_err(
PyObjectPtr::from_owned_ptr_or_err(
py, ffi::PyUnicodeDecodeError_Create(
encoding.as_ptr(),
input.as_ptr(),
@ -101,7 +101,7 @@ impl UnicodeDecodeError {
}
pub fn new_utf8<'p>(py: Python, input: &[u8], err: std::str::Utf8Error)
-> PyResult<pptr>
-> PyResult<PyObjectPtr>
{
let pos = err.valid_up_to();
UnicodeDecodeError::new(py, cstr!("utf-8"), input, pos .. pos+1, cstr!("invalid utf-8"))

View File

@ -124,14 +124,14 @@ impl <T> ToPyObject for Vec<T> where T: ToPyObject {
impl <T> IntoPyObject for Vec<T> where T: IntoPyObject {
fn into_object(self, py: Python) -> ::pptr {
fn into_object(self, py: Python) -> ::PyObjectPtr {
unsafe {
let ptr = ffi::PyList_New(self.len() as Py_ssize_t);
for (i, e) in self.into_iter().enumerate() {
let obj = e.into_object(py).into_ptr();
ffi::PyList_SetItem(ptr, i as Py_ssize_t, obj);
}
::pptr::from_owned_ptr_or_panic(ptr)
::PyObjectPtr::from_owned_ptr_or_panic(ptr)
}
}
}

View File

@ -51,7 +51,7 @@ macro_rules! pyobject_nativetype(
impl<'p> $crate::native::PyBaseObject for $name<'p> {}
impl<'p> $crate::native::PyNativeObject<'p> for $name<'p> {
fn park(self) -> $crate::pptr {
fn park(self) -> $crate::PyObjectPtr {
unsafe { $crate::std::mem::transmute(self) }
}
fn as_object(self) -> $crate::PyObject<'p> {
@ -192,7 +192,7 @@ macro_rules! pyobject_nativetype(
impl<'a> $crate::IntoPyObject for $name<'a>
{
#[inline]
fn into_object(self, _py: $crate::Python) -> $crate::pptr
fn into_object(self, _py: $crate::Python) -> $crate::PyObjectPtr
{
unsafe { $crate::std::mem::transmute(self) }
}

View File

@ -15,92 +15,92 @@ use typeob::{PyTypeInfo, PyObjectAlloc};
#[allow(non_camel_case_types)]
pub struct pptr(*mut ffi::PyObject);
pub struct PyObjectPtr(*mut ffi::PyObject);
// `pptr` is thread-safe, because all operations on it require a Python<'p> token.
unsafe impl Send for pptr {}
unsafe impl Sync for pptr {}
// `PyObjectPtr` is thread-safe, because all operations on it require a Python<'p> token.
unsafe impl Send for PyObjectPtr {}
unsafe impl Sync for PyObjectPtr {}
impl pptr {
/// Creates a `pptr` instance for the given FFI pointer.
/// This moves ownership over the pointer into the `pptr`.
impl PyObjectPtr {
/// Creates a `PyObjectPtr` instance for the given FFI pointer.
/// This moves ownership over the pointer into the `PyObjectPtr`.
/// Undefined behavior if the pointer is NULL or invalid.
#[inline]
pub unsafe fn from_owned_ptr(ptr: *mut ffi::PyObject) -> pptr {
pub unsafe fn from_owned_ptr(ptr: *mut ffi::PyObject) -> PyObjectPtr {
debug_assert!(!ptr.is_null() && ffi::Py_REFCNT(ptr) > 0);
pptr(ptr)
PyObjectPtr(ptr)
}
/// Creates a `pptr` instance for the given FFI pointer.
/// This moves ownership over the pointer into the `pptr`.
/// Creates a `PyObjectPtr` instance for the given FFI pointer.
/// This moves ownership over the pointer into the `PyObjectPtr`.
/// Returns None for null pointers; undefined behavior if the pointer is invalid.
#[inline]
pub unsafe fn from_owned_ptr_or_opt(ptr: *mut ffi::PyObject) -> Option<pptr> {
pub unsafe fn from_owned_ptr_or_opt(ptr: *mut ffi::PyObject) -> Option<PyObjectPtr> {
if ptr.is_null() {
None
} else {
Some(pptr::from_owned_ptr(ptr))
Some(PyObjectPtr::from_owned_ptr(ptr))
}
}
/// Construct `pptr` from the result of a Python FFI call that
/// Construct `PyObjectPtr` from the result of a Python FFI call that
/// returns a new reference (owned pointer).
/// Returns `Err(PyErr)` if the pointer is `null`; undefined behavior if the
/// pointer is invalid.
pub unsafe fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult<pptr>
pub unsafe fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult<PyObjectPtr>
{
if ptr.is_null() {
Err(PyErr::fetch(py))
} else {
Ok(pptr::from_owned_ptr(ptr))
Ok(PyObjectPtr::from_owned_ptr(ptr))
}
}
/// Construct `pptr` instance for the given Python FFI pointer.
/// Construct `PyObjectPtr` instance for the given Python FFI pointer.
/// Panics if the pointer is `null`; undefined behavior if the pointer is invalid.
#[inline]
pub unsafe fn from_owned_ptr_or_panic(ptr: *mut ffi::PyObject) -> pptr
pub unsafe fn from_owned_ptr_or_panic(ptr: *mut ffi::PyObject) -> PyObjectPtr
{
if ptr.is_null() {
::err::panic_after_error();
} else {
pptr::from_owned_ptr(ptr)
PyObjectPtr::from_owned_ptr(ptr)
}
}
/// Creates a `pptr` instance for the given Python FFI pointer.
/// Creates a `PyObjectPtr` 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) -> pptr {
pub unsafe fn from_borrowed_ptr(ptr: *mut ffi::PyObject) -> PyObjectPtr {
debug_assert!(!ptr.is_null() && ffi::Py_REFCNT(ptr) > 0);
ffi::Py_INCREF(ptr);
pptr::from_owned_ptr(ptr)
PyObjectPtr::from_owned_ptr(ptr)
}
/// Creates a `pptr` instance for the given Python FFI pointer.
/// Creates a `PyObjectPtr` instance for the given Python FFI pointer.
/// Calls Py_INCREF() on the ptr.
/// Returns None for null pointers; undefined behavior if the pointer is invalid.
#[inline]
pub unsafe fn from_borrowed_ptr_or_opt(ptr: *mut ffi::PyObject) -> Option<pptr> {
pub unsafe fn from_borrowed_ptr_or_opt(ptr: *mut ffi::PyObject) -> Option<PyObjectPtr> {
if ptr.is_null() {
None
} else {
Some(pptr::from_borrowed_ptr(ptr))
Some(PyObjectPtr::from_borrowed_ptr(ptr))
}
}
/// Construct `pptr` instance for the given Python FFI pointer.
/// Construct `PyObjectPtr` instance for the given Python FFI pointer.
/// Calls Py_INCREF() on the ptr.
/// Panics if the pointer is `null`; undefined behavior if the pointer is invalid.
#[inline]
pub unsafe fn from_borrowed_ptr_or_panic(ptr: *mut ffi::PyObject) -> pptr
pub unsafe fn from_borrowed_ptr_or_panic(ptr: *mut ffi::PyObject) -> PyObjectPtr
{
if ptr.is_null() {
::err::panic_after_error();
} else {
pptr::from_borrowed_ptr(ptr)
PyObjectPtr::from_borrowed_ptr(ptr)
}
}
@ -116,7 +116,7 @@ impl pptr {
unsafe { std::mem::transmute(self) }
}
/// Converts `pptr` instance -> PyObject<'p>
/// Converts `PyObjectPtr` instance -> PyObject<'p>
/// Consumes `self` without calling `Py_DECREF()`
#[inline]
pub fn into_object<'p>(self, _py: Python<'p>) -> PyObject<'p> {
@ -125,11 +125,11 @@ impl pptr {
/// Clone self, Calls Py_INCREF() on the ptr.
#[inline]
pub fn clone_ref(&self, _py: Python) -> pptr {
unsafe { pptr::from_borrowed_ptr(self.0) }
pub fn clone_ref(&self, _py: Python) -> PyObjectPtr {
unsafe { PyObjectPtr::from_borrowed_ptr(self.0) }
}
/// Casts the `pptr` imstance to a concrete Python object type.
/// Casts the `PyObjectPtr` imstance to a concrete Python object type.
/// Fails with `PyDowncastError` if the object is not of the expected type.
#[inline]
pub fn cast_into<'p, D>(self, py: Python<'p>) -> Result<D, PyDowncastError<'p>>
@ -148,7 +148,7 @@ impl pptr {
// }
}
impl ToPythonPointer for pptr {
impl ToPythonPointer for PyObjectPtr {
/// Gets the underlying FFI pointer, returns a borrowed pointer.
#[inline]
fn as_ptr(&self) -> *mut ffi::PyObject {
@ -156,7 +156,7 @@ impl ToPythonPointer for pptr {
}
}
impl IntoPythonPointer for pptr {
impl IntoPythonPointer for PyObjectPtr {
/// Gets the underlying FFI pointer, returns a owned pointer.
#[inline]
#[must_use]
@ -167,27 +167,27 @@ impl IntoPythonPointer for pptr {
}
}
impl IntoPyObject for pptr {
impl IntoPyObject for PyObjectPtr {
#[inline]
fn into_object(self, _py: Python) -> pptr {
fn into_object(self, _py: Python) -> PyObjectPtr {
self
}
}
impl PartialEq for pptr {
impl PartialEq for PyObjectPtr {
#[inline]
fn eq(&self, o: &pptr) -> bool {
fn eq(&self, o: &PyObjectPtr) -> bool {
self.0 == o.0
}
}
/// Dropping a `pptr` instance decrements the reference count on the object by 1.
impl Drop for pptr {
/// Dropping a `PyObjectPtr` instance decrements the reference count on the object by 1.
impl Drop for PyObjectPtr {
fn drop(&mut self) {
unsafe {
println!("drop pptr: {:?} {} {:?}",
println!("drop PyObjectPtr: {:?} {} {:?}",
self.0, ffi::Py_REFCNT(self.0), &self as *const _);
}
let _gil_guard = Python::acquire_gil();
@ -203,10 +203,10 @@ pub struct PyPtr<T> {
impl<T> PyPtr<T> {
/// Converts PyPtr<T> -> pptr
/// Converts PyPtr<T> -> PyObjectPtr
/// Consumes `self` without calling `Py_INCREF()`
#[inline]
pub fn park(self) -> pptr {
pub fn park(self) -> PyObjectPtr {
unsafe { std::mem::transmute(self) }
}
@ -298,7 +298,7 @@ impl<T> ToPyObject for PyPtr<T> {
impl<T> IntoPyObject for PyPtr<T> {
#[inline]
fn into_object<'a>(self, _py: Python) -> pptr {
fn into_object<'a>(self, _py: Python) -> PyObjectPtr {
self.park()
}
}
@ -498,7 +498,7 @@ impl <'a, T> ToPyObject for Py<'a, T> {
impl<'p, T> IntoPyObject for Py<'p, T> {
#[inline]
default fn into_object(self, _py: Python) -> pptr {
default fn into_object(self, _py: Python) -> PyObjectPtr {
unsafe { std::mem::transmute(self) }
}
}

View File

@ -12,7 +12,7 @@ use typeob::{PyTypeInfo, PyTypeObject, PyObjectAlloc};
use token::{PythonToken};
use objects::{PyObject, PyType, PyBool, PyDict, PyModule};
use err::{PyErr, PyResult, PyDowncastError};
use pointers::{Py, pptr};
use pointers::{Py, PyObjectPtr};
use pythonrun::GILGuard;
@ -200,8 +200,8 @@ impl<'p> Python<'p> {
/// Gets the Python builtin value `None`.
#[allow(non_snake_case)] // the Python keyword starts with uppercase
#[inline]
pub fn None(self) -> pptr {
unsafe { pptr::from_borrowed_ptr(ffi::Py_None()) }
pub fn None(self) -> PyObjectPtr {
unsafe { PyObjectPtr::from_borrowed_ptr(ffi::Py_None()) }
}
/// Gets the Python builtin value `True`.
@ -221,8 +221,8 @@ impl<'p> Python<'p> {
/// Gets the Python builtin value `NotImplemented`.
#[allow(non_snake_case)] // the Python keyword starts with uppercase
#[inline]
pub fn NotImplemented(self) -> pptr {
unsafe { pptr::from_borrowed_ptr(ffi::Py_NotImplemented()) }
pub fn NotImplemented(self) -> PyObjectPtr {
unsafe { PyObjectPtr::from_borrowed_ptr(ffi::Py_NotImplemented()) }
}
/// Execute closure `F` with Python instance.

View File

@ -316,7 +316,7 @@ impl StaticMethod {
#[py::class]
struct GCIntegration {
self_ref: RefCell<pptr>,
self_ref: RefCell<PyObjectPtr>,
dropped: TestDropCall,
token: PythonToken<GCIntegration>,
}
@ -804,7 +804,7 @@ impl PyObjectProtocol for RichComparisons2 {
}
fn __richcmp__(&self, py: Python,
other: &'p PyObject<'p>, op: CompareOp) -> PyResult<pptr> {
other: &'p PyObject<'p>, op: CompareOp) -> PyResult<PyObjectPtr> {
match op {
CompareOp::Eq => Ok(true.to_object(py).park()),
CompareOp::Ne => Ok(false.to_object(py).park()),

View File

@ -26,7 +26,7 @@ struct Test {}
#[py::proto]
impl<'p> PyMappingProtocol<'p> for Test
{
fn __getitem__(&self, py: Python, idx: PyObject<'p>) -> PyResult<pptr> {
fn __getitem__(&self, py: Python, idx: PyObject<'p>) -> PyResult<PyObjectPtr> {
if let Ok(slice) = idx.cast_as::<PySlice>() {
let indices = slice.indices(1000)?;
if indices.start == 100 && indices.stop == 200 && indices.step == 1 {