rename PyTypeObjectInfo to PyTypeInfo; use PyTypeInfo::offset() for struct offset

This commit is contained in:
Nikolay Kim 2017-05-22 23:45:28 -07:00
parent 579c1280e9
commit 5fdad40caa
9 changed files with 70 additions and 83 deletions

View file

@ -32,22 +32,21 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident) -> Tokens {
let cls_name = quote! { #cls }.as_str().to_string();
quote! {
impl _pyo3::class::typeob::PyTypeObjectInfo for #cls {
impl _pyo3::class::typeob::PyTypeInfo for #cls {
type Type = #cls;
#[inline]
fn size() -> usize {
Self::offset() + std::mem::size_of::<#cls>()
Self::offset() as usize + std::mem::size_of::<#cls>()
}
#[inline]
fn offset() -> usize {
fn offset() -> isize {
let align = std::mem::align_of::<#cls>();
let bs = <#base as _pyo3::class::typeob::PyTypeObjectInfo>::size();
let bs = <#base as _pyo3::class::typeob::PyTypeInfo>::size();
// round base_size up to next multiple of align
(bs + align - 1) / align * align
((bs + align - 1) / align * align) as isize
}
#[inline]

View file

@ -61,7 +61,7 @@ pub trait PyObjectProtocol<'a> : Sized + 'static {
pub trait PyObjectGetAttrProtocol<'a>: PyObjectProtocol<'a> {
type Name: ::FromPyObj<'a> + ::class::typeob::PyTypeObjectInfo;
type Name: ::FromPyObj<'a> + ::class::typeob::PyTypeInfo;
type Result: Into<PyResult<()>>;
}
@ -71,7 +71,7 @@ pub trait PyObjectGetAttrProtocol<'a>: PyObjectProtocol<'a> {
// type Result: Into<PyResult<Self::Success>>;
//}
pub trait PyObjectSetAttrProtocol<'a>: PyObjectProtocol<'a> {
type Name: FromPyObject<'a> + ::class::typeob::PyTypeObjectInfo + ::class::typeob::PyTypeObject + ::PythonObject;
type Name: FromPyObject<'a> + ::class::typeob::PyTypeInfo + ::class::typeob::PyTypeObject + ::PythonObject;
type Value: FromPyObject<'a>;
type Result: Into<PyResult<()>>;
}
@ -177,14 +177,14 @@ use callback::CallbackConverter;
impl<'a, T> PyObjectGetAttrProtocolImpl for T
where T: PyObjectGetAttrProtocol<'a> + ::class::typeob::PyTypeObjectInfo
where T: PyObjectGetAttrProtocol<'a> + ::class::typeob::PyTypeInfo
{
#[inline]
fn tp_getattro() -> Option<ffi::binaryfunc> {
//py_binary_func_!(PyObjectGetAttrProtocol, T::__getattr__, PyObjectCallbackConverter)
unsafe extern "C" fn wrap<'a, T>(slf: *mut ffi::PyObject,
arg: *mut ffi::PyObject) -> *mut ffi::PyObject
where T: PyObjectGetAttrProtocol<'a> + ::class::typeob::PyTypeObjectInfo
where T: PyObjectGetAttrProtocol<'a> + ::class::typeob::PyTypeInfo
{
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
{

View file

@ -69,7 +69,7 @@ pub trait BaseObject {
/// A PythonObject that is usable as a base type for #[class]
impl<T> BaseObject for T where T : typeob::PyTypeObjectInfo {
impl<T> BaseObject for T where T : typeob::PyTypeInfo {
type Type = T::Type;
/// Allocates a new object (usually by calling ty->tp_alloc),
@ -78,10 +78,10 @@ impl<T> BaseObject for T where T : typeob::PyTypeObjectInfo {
/// must be of type `ty`.
unsafe fn alloc(_py: Python, value: T::Type) -> PyResult<*mut ffi::PyObject> {
let obj = ffi::PyType_GenericAlloc(
<Self as typeob::PyTypeObjectInfo>::type_object(), 0);
<Self as typeob::PyTypeInfo>::type_object(), 0);
let offset = <Self as typeob::PyTypeObjectInfo>::offset();
let ptr = (obj as *mut u8).offset(offset as isize) as *mut Self::Type;
let offset = <Self as typeob::PyTypeInfo>::offset();
let ptr = (obj as *mut u8).offset(offset) as *mut Self::Type;
std::ptr::write(ptr, value);
Ok(obj)
@ -92,7 +92,7 @@ impl<T> BaseObject for T where T : typeob::PyTypeObjectInfo {
/// This function is used as tp_dealloc implementation.
unsafe fn dealloc(_py: Python, obj: *mut ffi::PyObject) {
let ptr = (obj as *mut u8).offset(
<Self as typeob::PyTypeObjectInfo>::offset() as isize) as *mut Self::Type;
<Self as typeob::PyTypeInfo>::offset() as isize) as *mut Self::Type;
std::ptr::drop_in_place(ptr);
let ty = ffi::Py_TYPE(obj);

View file

@ -20,12 +20,12 @@ pub trait PyTypeObject {
/// Trait implemented by object that generated by py::class macro
pub trait PyTypeObjectInfo {
pub trait PyTypeInfo {
type Type;
fn size() -> usize;
fn offset() -> usize;
fn offset() -> isize;
fn type_name() -> &'static str;
@ -34,69 +34,69 @@ pub trait PyTypeObjectInfo {
}
impl<'a, T: ?Sized> PyTypeObjectInfo for &'a T where T: PyTypeObjectInfo {
impl<'a, T: ?Sized> PyTypeInfo for &'a T where T: PyTypeInfo {
type Type = T::Type;
#[inline]
default fn size() -> usize {
<T as PyTypeObjectInfo>::size()
<T as PyTypeInfo>::size()
}
#[inline]
default fn offset() -> usize {
<T as PyTypeObjectInfo>::offset()
default fn offset() -> isize {
<T as PyTypeInfo>::offset()
}
#[inline]
default fn type_name() -> &'static str {
<T as PyTypeObjectInfo>::type_name()
<T as PyTypeInfo>::type_name()
}
#[inline]
default fn type_object() -> &'static mut ffi::PyTypeObject {
<T as PyTypeObjectInfo>::type_object()
<T as PyTypeInfo>::type_object()
}
}
impl<'a, T> PyTypeObjectInfo for Py<'a, T> where T: PyTypeObjectInfo {
impl<'a, T> PyTypeInfo for Py<'a, T> where T: PyTypeInfo {
type Type = T::Type;
#[inline]
default fn size() -> usize {
<T as PyTypeObjectInfo>::size()
<T as PyTypeInfo>::size()
}
#[inline]
default fn offset() -> usize {
<T as PyTypeObjectInfo>::offset()
default fn offset() -> isize {
<T as PyTypeInfo>::offset()
}
#[inline]
default fn type_name() -> &'static str {
<T as PyTypeObjectInfo>::type_name()
<T as PyTypeInfo>::type_name()
}
#[inline]
default fn type_object() -> &'static mut ffi::PyTypeObject {
<T as PyTypeObjectInfo>::type_object()
<T as PyTypeInfo>::type_object()
}
}
impl<T> PyTypeObject for T where T: BaseObject + PyTypeObjectInfo {
impl<T> PyTypeObject for T where T: BaseObject + PyTypeInfo {
#[inline]
fn type_object(py: Python) -> PyType {
let mut ty = <T as PyTypeObjectInfo>::type_object();
let mut ty = <T as PyTypeInfo>::type_object();
if (ty.tp_flags & ffi::Py_TPFLAGS_READY) != 0 {
unsafe { PyType::from_type_ptr(py, ty) }
} else {
// automatically initialize the class on-demand
initialize_type::<T>(py, None, <T as PyTypeObjectInfo>::type_name(), ty).expect(
initialize_type::<T>(py, None, <T as PyTypeInfo>::type_name(), ty).expect(
format!("An error occurred while initializing class {}",
<T as PyTypeObjectInfo>::type_name()).as_ref());
<T as PyTypeInfo>::type_name()).as_ref());
unsafe { PyType::from_type_ptr(py, ty) }
}
}
@ -104,7 +104,7 @@ impl<T> PyTypeObject for T where T: BaseObject + PyTypeObjectInfo {
pub fn initialize_type<T>(py: Python, module_name: Option<&str>, type_name: &str,
type_object: &mut ffi::PyTypeObject) -> PyResult<PyType>
where T: BaseObject + PyTypeObjectInfo
where T: BaseObject + PyTypeInfo
{
// type name
let name = match module_name {
@ -120,7 +120,7 @@ pub fn initialize_type<T>(py: Python, module_name: Option<&str>, type_name: &str
type_object.tp_dealloc = Some(tp_dealloc_callback::<T>);
// type size
type_object.tp_basicsize = <T as PyTypeObjectInfo>::size() as ffi::Py_ssize_t;
type_object.tp_basicsize = <T as PyTypeInfo>::size() as ffi::Py_ssize_t;
// GC support
// <T as class::gc::PyGCProtocolImpl>::update_type_object(type_object);
@ -225,7 +225,7 @@ pub fn initialize_type<T>(py: Python, module_name: Option<&str>, type_name: &str
}
unsafe extern "C" fn tp_dealloc_callback<T>(obj: *mut ffi::PyObject)
where T: BaseObject + PyTypeObjectInfo
where T: PyTypeInfo
{
let guard = AbortOnDrop("Cannot unwind out of tp_dealloc");
let py = Python::assume_gil_acquired();

View file

@ -117,7 +117,7 @@ pub trait FromPyObject<'source> : Sized {
pub trait FromPyObj<'source> : Sized {
/// Extracts `Self` from the source `PyObject`.
fn extr<S>(py: &'source Py<'source, S>) -> PyResult<Self>
where S: ::class::typeob::PyTypeObjectInfo;
where S: ::class::typeob::PyTypeInfo;
}
pub trait RefFromPyObject {

View file

@ -93,7 +93,7 @@ macro_rules! pyobject_newtype(
($name: ident, $checkfunction: ident, $typeobject: ident) => (
pyobject_newtype!($name, $checkfunction);
impl $crate::class::typeob::PyTypeObjectInfo for $name {
impl $crate::class::typeob::PyTypeInfo for $name {
type Type = ();
#[inline]
@ -102,7 +102,7 @@ macro_rules! pyobject_newtype(
}
#[inline]
fn offset() -> usize {
fn offset() -> isize {
0
}
@ -155,16 +155,16 @@ macro_rules! pyobject_newtype_(
}
}
impl $crate::class::typeob::PyTypeObjectInfo for $name {
impl $crate::class::typeob::PyTypeInfo for $name {
type Type = ();
#[inline]
fn size() -> usize {
0
$crate::std::mem::size_of::<$name>()
}
#[inline]
fn offset() -> usize {
fn offset() -> isize {
0
}
@ -201,12 +201,12 @@ pub mod exc;
use std;
use ffi;
use class::typeob::PyTypeObjectInfo;
use class::typeob::PyTypeInfo;
pub struct PyObj;
impl PyTypeObjectInfo for PyObj {
impl PyTypeInfo for PyObj {
type Type = ();
#[inline]
@ -215,7 +215,7 @@ impl PyTypeObjectInfo for PyObj {
}
#[inline]
fn offset() -> usize {
fn offset() -> isize {
0
}
@ -230,7 +230,7 @@ impl PyTypeObjectInfo for PyObj {
}
}
impl PyTypeObjectInfo for PyObject {
impl PyTypeInfo for PyObject {
type Type = ();
#[inline]
@ -239,7 +239,7 @@ impl PyTypeObjectInfo for PyObject {
}
#[inline]
fn offset() -> usize {
fn offset() -> isize {
0
}

View file

@ -110,10 +110,10 @@ impl PyModule {
/// sets `new_type.__module__` to this module's name,
/// and adds the type to this module.
pub fn add_class<'p, T>(&self, py: Python<'p>) -> PyResult<()>
where T: ::class::BaseObject + PythonObject + ::class::typeob::PyTypeObjectInfo
where T: ::class::BaseObject + PythonObject + ::class::typeob::PyTypeInfo
{
let mut ty = <T as ::class::typeob::PyTypeObjectInfo>::type_object();
let type_name = <T as ::class::typeob::PyTypeObjectInfo>::type_name();
let mut ty = <T as ::class::typeob::PyTypeInfo>::type_object();
let type_name = <T as ::class::typeob::PyTypeInfo>::type_name();
let ty = if (ty.tp_flags & ffi::Py_TPFLAGS_READY) != 0 {
unsafe { PyType::from_type_ptr(py, ty) }
@ -123,7 +123,7 @@ impl PyModule {
::class::typeob::initialize_type::<T>(
py, Some(name), type_name, ty).expect(
format!("An error occurred while initializing class {}",
<T as ::class::typeob::PyTypeObjectInfo>::type_name()).as_ref());
<T as ::class::typeob::PyTypeInfo>::type_name()).as_ref());
unsafe { PyType::from_type_ptr(py, ty) }
};

View file

@ -12,7 +12,7 @@ use err::{self, PyResult};
use python::Python;
use class::BaseObject;
use objects::PyObject;
use class::typeob::PyTypeObjectInfo;
use class::typeob::PyTypeInfo;
#[derive(Debug)]
@ -46,7 +46,7 @@ impl<T> PyPtr<T> {
ptr
}
/// Gets the reference count of this Py object.
/// Gets the reference count of this PyPtr object.
#[inline]
pub fn get_refcnt(&self) -> usize {
unsafe { ffi::Py_REFCNT(self.inner) as usize }
@ -161,7 +161,7 @@ impl<'p, T> Py<'p, T>
}
impl<'p, T> Py<'p, T> where T: PyTypeObjectInfo
impl<'p, T> Py<'p, T> where T: PyTypeInfo
{
/// Create new python object and move T instance under python management
pub fn new(py: Python<'p>, value: T) -> PyResult<Py<'p, T>> where T: BaseObject<Type=T>
@ -218,28 +218,20 @@ impl<'p, T> Py<'p, T> where T: PyTypeObjectInfo
#[inline]
pub fn as_ref(&self) -> &T {
let align = std::mem::align_of::<T>();
let bs = <T as PyTypeObjectInfo>::size();
// round base_size up to next multiple of align
let offset = (bs + align - 1) / align * align;
let offset = <T as PyTypeInfo>::offset();
unsafe {
let ptr = (self.inner as *mut u8).offset(offset as isize) as *mut T;
let ptr = (self.inner as *mut u8).offset(offset) as *mut T;
ptr.as_ref().unwrap()
}
}
#[inline]
pub fn as_mut(&self) -> &mut T {
let align = std::mem::align_of::<T>();
let bs = <T as PyTypeObjectInfo>::size();
// round base_size up to next multiple of align
let offset = (bs + align - 1) / align * align;
let offset = <T as PyTypeInfo>::offset();
unsafe {
let ptr = (self.inner as *mut u8).offset(offset as isize) as *mut T;
let ptr = (self.inner as *mut u8).offset(offset) as *mut T;
ptr.as_mut().unwrap()
}
}
@ -279,13 +271,9 @@ impl<'p, T> Py<'p, T> where T: PyTypeObjectInfo
/// Undefined behavior if the input object does not have the expected type.
#[inline]
pub unsafe fn unchecked_downcast_borrow_from<'a, S>(py: &'a Py<'a, S>) -> &'a T {
let align = std::mem::align_of::<T>();
let bs = <T as PyTypeObjectInfo>::size();
let offset = <T as PyTypeInfo>::offset();
// round base_size up to next multiple of align
let offset = (bs + align - 1) / align * align;
let ptr = (py.inner as *mut u8).offset(offset as isize) as *mut T;
let ptr = (py.inner as *mut u8).offset(offset) as *mut T;
ptr.as_ref().unwrap()
}
@ -319,7 +307,7 @@ impl<'p, T> Clone for Py<'p, T> {
}
}
impl<'p, T> Deref for Py<'p, T> where T: PyTypeObjectInfo {
impl<'p, T> Deref for Py<'p, T> where T: PyTypeInfo {
type Target = T;
fn deref(&self) -> &T {
@ -327,21 +315,21 @@ impl<'p, T> Deref for Py<'p, T> where T: PyTypeObjectInfo {
}
}
impl<'p, T> AsRef<T> for Py<'p, T> where T: PyTypeObjectInfo {
impl<'p, T> AsRef<T> for Py<'p, T> where T: PyTypeInfo {
#[inline]
fn as_ref(&self) -> &T {
self.as_ref()
}
}
impl<'p, T> AsMut<T> for Py<'p, T> where T: PyTypeObjectInfo {
impl<'p, T> AsMut<T> for Py<'p, T> where T: PyTypeInfo {
#[inline]
fn as_mut(&mut self) -> &mut T {
Py::<T>::as_mut(self)
}
}
impl<'p, T> ::PyWithCheckedDowncast<'p> for T where T: PyTypeObjectInfo
impl<'p, T> ::PyWithCheckedDowncast<'p> for T where T: PyTypeInfo
{
#[inline]
default fn downcast_from<S>(ob: Py<'p, S>)
@ -360,7 +348,7 @@ impl<'p, T> ::PyWithCheckedDowncast<'p> for T where T: PyTypeObjectInfo
#[inline]
default fn downcast_borrow_from<'source, S>(
ob: &'source Py<'p, S>) -> Result<&'source T, ::PythonObjectDowncastError<'p>>
where S: PyTypeObjectInfo
where S: PyTypeInfo
{
println!("downcast borrow from {:?}", ob);
let checked = unsafe { ffi::PyObject_TypeCheck(ob.inner, T::type_object()) != 0 };
@ -376,22 +364,22 @@ impl<'p, T> ::PyWithCheckedDowncast<'p> for T where T: PyTypeObjectInfo
}
impl<'source, T> ::FromPyObj<'source> for &'source T
where T: PyTypeObjectInfo
where T: PyTypeInfo
{
#[inline]
default fn extr<S>(py: &'source Py<'source, S>) -> PyResult<&'source T>
where S: PyTypeObjectInfo
where S: PyTypeInfo
{
Ok(::PyWithCheckedDowncast::downcast_borrow_from(py)?)
}
}
impl<'source, T> ::FromPyObj<'source> for Py<'source, T>
where T: PyTypeObjectInfo
where T: PyTypeInfo
{
#[inline]
default fn extr<S>(py: &'source Py<'source, S>) -> PyResult<Py<'source, T>>
where S: PyTypeObjectInfo
where S: PyTypeInfo
{
Ok(::PyWithCheckedDowncast::downcast_from(py.clone())?)
}
@ -416,7 +404,7 @@ impl<'p, T> ToPyObject for Py<'p, T> {
}
}
impl<'p, T> fmt::Debug for Py<'p, T> where T: PyTypeObjectInfo {
impl<'p, T> fmt::Debug for Py<'p, T> where T: PyTypeInfo {
fn fmt(&self, f : &mut fmt::Formatter) -> Result<(), fmt::Error> {
let repr_obj = try!(unsafe {
err::result_cast_from_owned_ptr::<::PyString>(self.py(), ffi::PyObject_Repr(self.as_ptr()))

View file

@ -97,7 +97,7 @@ pub trait PyWithCheckedDowncast<'p> : Sized {
/// Cast from PyObject to a concrete Python object type.
fn downcast_borrow_from<'source, S>(&'source ::Py<'p, S>)
-> Result<&'source Self, PythonObjectDowncastError<'p>>
where S: ::class::typeob::PyTypeObjectInfo;
where S: ::class::typeob::PyTypeInfo;
}
impl<T> PythonObjectWithCheckedDowncast for T where T: PyTypeObject + PythonObject {