pyo3/src/class/typeob.rs

314 lines
9.6 KiB
Rust
Raw Normal View History

2017-05-16 18:58:18 +00:00
// Copyright (c) 2017-present PyO3 Project and Contributors
use std::mem;
use std::ffi::CString;
use std::collections::HashMap;
use ::{ffi, class, PyErr, Python, PyResult, Py};
2017-05-19 04:35:08 +00:00
use objects::PyType;
2017-05-17 06:43:39 +00:00
use callback::AbortOnDrop;
use class::{BaseObject, PyMethodDefType};
2017-05-16 18:58:18 +00:00
2017-05-19 04:35:08 +00:00
/// Trait implemented by Python object types that have a corresponding type object.
pub trait PyTypeObject {
2017-05-16 18:58:18 +00:00
2017-05-19 04:35:08 +00:00
/// Retrieves the type object for this Python object type.
fn type_object(py: Python) -> PyType;
2017-05-16 18:58:18 +00:00
2017-05-19 04:35:08 +00:00
}
2017-05-19 04:35:08 +00:00
/// Trait implemented by object that generated by py::class macro
pub trait PyTypeObjectInfo {
type Type;
2017-05-19 04:35:08 +00:00
2017-05-22 05:22:45 +00:00
fn size() -> usize;
fn offset() -> usize;
2017-05-19 04:35:08 +00:00
fn type_name() -> &'static str;
fn type_object() -> &'static mut ffi::PyTypeObject;
}
2017-05-22 05:22:45 +00:00
impl<'a, T: ?Sized> PyTypeObjectInfo for &'a T where T: PyTypeObjectInfo {
type Type = T::Type;
#[inline]
default fn size() -> usize {
<T as PyTypeObjectInfo>::size()
}
#[inline]
default fn offset() -> usize {
<T as PyTypeObjectInfo>::offset()
}
#[inline]
default fn type_name() -> &'static str {
<T as PyTypeObjectInfo>::type_name()
}
#[inline]
default fn type_object() -> &'static mut ffi::PyTypeObject {
<T as PyTypeObjectInfo>::type_object()
}
}
impl<'a, T> PyTypeObjectInfo for Py<'a, T> where T: PyTypeObjectInfo {
type Type = T::Type;
2017-05-22 05:22:45 +00:00
#[inline]
default fn size() -> usize {
<T as PyTypeObjectInfo>::size()
}
#[inline]
default fn offset() -> usize {
<T as PyTypeObjectInfo>::offset()
}
#[inline]
default fn type_name() -> &'static str {
<T as PyTypeObjectInfo>::type_name()
}
#[inline]
default fn type_object() -> &'static mut ffi::PyTypeObject {
<T as PyTypeObjectInfo>::type_object()
}
}
impl<T> PyTypeObject for T where T: BaseObject + PyTypeObjectInfo {
2017-05-16 18:58:18 +00:00
2017-05-19 04:35:08 +00:00
#[inline]
fn type_object(py: Python) -> PyType {
let mut ty = <T as PyTypeObjectInfo>::type_object();
2017-05-16 18:58:18 +00:00
2017-05-19 04:35:08 +00:00
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(
format!("An error occurred while initializing class {}",
<T as PyTypeObjectInfo>::type_name()).as_ref());
unsafe { PyType::from_type_ptr(py, ty) }
}
}
2017-05-16 18:58:18 +00:00
}
2017-05-18 23:57:39 +00:00
pub fn initialize_type<T>(py: Python, module_name: Option<&str>, type_name: &str,
2017-05-17 06:43:39 +00:00
type_object: &mut ffi::PyTypeObject) -> PyResult<PyType>
2017-05-22 05:22:45 +00:00
where T: BaseObject + PyTypeObjectInfo
2017-05-17 06:43:39 +00:00
{
// type name
let name = match module_name {
2017-05-18 23:57:39 +00:00
Some(module_name) => CString::new(format!("{}.{}", module_name, type_name)),
2017-05-20 06:14:59 +00:00
None => CString::new(type_name)
2017-05-17 06:43:39 +00:00
};
let name = name.expect(
"Module name/type name must not contain NUL byte").into_raw();
type_object.tp_name = name;
// dealloc
type_object.tp_dealloc = Some(tp_dealloc_callback::<T>);
// type size
type_object.tp_basicsize = <T as PyTypeObjectInfo>::size() as ffi::Py_ssize_t;
2017-05-17 06:43:39 +00:00
2017-05-22 05:22:45 +00:00
// GC support
// <T as class::gc::PyGCProtocolImpl>::update_type_object(type_object);
2017-05-17 06:43:39 +00:00
// descriptor protocol
2017-05-22 05:22:45 +00:00
// <T as class::descr::PyDescrProtocolImpl>::tp_as_descr(type_object);
2017-05-17 06:43:39 +00:00
2017-05-18 22:57:38 +00:00
// iterator methods
2017-05-22 05:22:45 +00:00
// <T as class::iter::PyIterProtocolImpl>::tp_as_iter(type_object);
2017-05-18 22:57:38 +00:00
// basic methods
<T as class::basic::PyObjectProtocolImpl>::tp_as_object(type_object);
2017-05-17 06:43:39 +00:00
// number methods
2017-05-22 05:22:45 +00:00
/*if let Some(meth) = <T as class::number::PyNumberProtocolImpl>::tp_as_number() {
2017-05-17 06:43:39 +00:00
static mut NB_METHODS: ffi::PyNumberMethods = ffi::PyNumberMethods_INIT;
*(unsafe { &mut NB_METHODS }) = meth;
type_object.tp_as_number = unsafe { &mut NB_METHODS };
2017-05-18 22:57:38 +00:00
mem::forget(meth);
2017-05-17 06:43:39 +00:00
} else {
type_object.tp_as_number = 0 as *mut ffi::PyNumberMethods;
2017-05-16 18:58:18 +00:00
}
2017-05-17 06:43:39 +00:00
// mapping methods
2017-05-17 23:33:18 +00:00
if let Some(meth) = <T as class::mapping::PyMappingProtocolImpl>::tp_as_mapping() {
2017-05-17 06:43:39 +00:00
static mut MP_METHODS: ffi::PyMappingMethods = ffi::PyMappingMethods_INIT;
*(unsafe { &mut MP_METHODS }) = meth;
type_object.tp_as_mapping = unsafe { &mut MP_METHODS };
2017-05-18 22:57:38 +00:00
mem::forget(meth);
2017-05-17 06:43:39 +00:00
} else {
type_object.tp_as_mapping = 0 as *mut ffi::PyMappingMethods;
}
2017-05-16 18:58:18 +00:00
2017-05-17 06:43:39 +00:00
// sequence methods
2017-05-20 19:15:28 +00:00
if let Some(meth) = <T as class::sequence::PySequenceProtocolImpl>::tp_as_sequence() {
2017-05-17 06:43:39 +00:00
static mut SQ_METHODS: ffi::PySequenceMethods = ffi::PySequenceMethods_INIT;
*(unsafe { &mut SQ_METHODS }) = meth;
type_object.tp_as_sequence = unsafe { &mut SQ_METHODS };
2017-05-18 22:57:38 +00:00
mem::forget(meth);
2017-05-17 06:43:39 +00:00
} else {
type_object.tp_as_sequence = 0 as *mut ffi::PySequenceMethods;
}
2017-05-16 18:58:18 +00:00
2017-05-17 06:43:39 +00:00
// async methods
2017-05-18 01:25:26 +00:00
if let Some(meth) = <T as class::async::PyAsyncProtocolImpl>::tp_as_async() {
2017-05-17 06:43:39 +00:00
static mut ASYNC_METHODS: ffi::PyAsyncMethods = ffi::PyAsyncMethods_INIT;
*(unsafe { &mut ASYNC_METHODS }) = meth;
type_object.tp_as_async = unsafe { &mut ASYNC_METHODS };
2017-05-18 22:57:38 +00:00
mem::forget(meth);
2017-05-17 06:43:39 +00:00
} else {
type_object.tp_as_async = 0 as *mut ffi::PyAsyncMethods;
}
2017-05-16 18:58:18 +00:00
2017-05-17 06:43:39 +00:00
// buffer protocol
if let Some(meth) = ffi::PyBufferProcs::new::<T>() {
static mut BUFFER_PROCS: ffi::PyBufferProcs = ffi::PyBufferProcs_INIT;
*(unsafe { &mut BUFFER_PROCS }) = meth;
type_object.tp_as_buffer = unsafe { &mut BUFFER_PROCS };
2017-05-18 22:57:38 +00:00
mem::forget(meth);
2017-05-17 06:43:39 +00:00
} else {
type_object.tp_as_buffer = 0 as *mut ffi::PyBufferProcs;
}
2017-05-16 18:58:18 +00:00
2017-05-17 06:43:39 +00:00
// normal methods
2017-05-20 06:14:59 +00:00
let (new, call, mut methods) = py_class_method_defs::<T>();
2017-05-17 06:43:39 +00:00
if !methods.is_empty() {
methods.push(ffi::PyMethodDef_INIT);
type_object.tp_methods = methods.as_ptr() as *mut _;
2017-05-16 18:58:18 +00:00
2017-05-17 06:43:39 +00:00
static mut METHODS: *const ffi::PyMethodDef = 0 as *const _;
*(unsafe { &mut METHODS }) = methods.as_ptr();
2017-05-18 22:57:38 +00:00
mem::forget(methods);
2017-05-17 06:43:39 +00:00
}
2017-05-20 06:14:59 +00:00
// __new__ method
type_object.tp_new = new;
// __call__ method
type_object.tp_call = call;
2017-05-16 18:58:18 +00:00
2017-05-17 06:43:39 +00:00
// properties
let mut props = py_class_properties::<T>();
if !props.is_empty() {
props.push(ffi::PyGetSetDef_INIT);
let props = props.into_boxed_slice();
type_object.tp_getset = props.as_ptr() as *mut _;
2017-05-16 18:58:18 +00:00
2017-05-17 06:43:39 +00:00
static mut PROPS: *const ffi::PyGetSetDef = 0 as *const _;
*(unsafe { &mut PROPS }) = props.as_ptr();
2017-05-16 18:58:18 +00:00
2017-05-17 06:43:39 +00:00
// strange
mem::forget(props);
2017-05-22 05:22:45 +00:00
}*/
2017-05-16 18:58:18 +00:00
2017-05-17 06:43:39 +00:00
// register type object
unsafe {
if ffi::PyType_Ready(type_object) == 0 {
Ok(PyType::from_type_ptr(py, type_object))
} else {
Err(PyErr::fetch(py))
2017-05-16 18:58:18 +00:00
}
}
}
2017-05-22 05:22:45 +00:00
unsafe extern "C" fn tp_dealloc_callback<T>(obj: *mut ffi::PyObject)
where T: BaseObject + PyTypeObjectInfo
2017-05-16 18:58:18 +00:00
{
let guard = AbortOnDrop("Cannot unwind out of tp_dealloc");
let py = Python::assume_gil_acquired();
2017-05-22 05:22:45 +00:00
let r = <T as BaseObject>::dealloc(py, obj);
2017-05-16 18:58:18 +00:00
mem::forget(guard);
r
}
2017-05-20 06:14:59 +00:00
fn py_class_method_defs<T>() -> (Option<ffi::newfunc>,
Option<ffi::PyCFunctionWithKeywords>,
Vec<ffi::PyMethodDef>) {
2017-05-16 18:58:18 +00:00
let mut defs = Vec::new();
2017-05-20 06:14:59 +00:00
let mut call = None;
let mut new = None;
2017-05-16 18:58:18 +00:00
for def in <T as class::methods::PyMethodsProtocolImpl>::py_methods() {
match def {
2017-05-20 06:14:59 +00:00
&PyMethodDefType::New(ref def) => {
if let class::methods::PyMethodType::PyNewFunc(meth) = def.ml_meth {
new = Some(meth)
}
},
&PyMethodDefType::Call(ref def) => {
if let class::methods::PyMethodType::PyCFunctionWithKeywords(meth) = def.ml_meth {
call = Some(meth)
} else {
panic!("Method type is not supoorted by tp_call slot")
}
}
&PyMethodDefType::Method(ref def) => {
defs.push(def.as_method_def())
}
2017-05-16 18:58:18 +00:00
_ => (),
}
}
for def in <T as class::basic::PyObjectProtocolImpl>::methods() {
defs.push(def.as_method_def())
}
2017-05-19 18:48:05 +00:00
for def in <T as class::async::PyAsyncProtocolImpl>::methods() {
defs.push(def.as_method_def())
}
2017-05-20 06:14:59 +00:00
for def in <T as class::context::PyContextProtocolImpl>::methods() {
defs.push(def.as_method_def())
2017-05-20 19:15:28 +00:00
}
for def in <T as class::mapping::PyMappingProtocolImpl>::methods() {
defs.push(def.as_method_def())
}
for def in <T as class::number::PyNumberProtocolImpl>::methods() {
defs.push(def.as_method_def())
}
for def in <T as class::descr::PyDescrProtocolImpl>::methods() {
defs.push(def.as_method_def())
2017-05-20 06:14:59 +00:00
}
2017-05-16 18:58:18 +00:00
2017-05-20 06:14:59 +00:00
(new, call, defs)
2017-05-16 18:58:18 +00:00
}
fn py_class_properties<T>() -> Vec<ffi::PyGetSetDef> {
let mut defs = HashMap::new();
for def in <T as class::methods::PyMethodsProtocolImpl>::py_methods() {
match def {
&PyMethodDefType::Getter(ref getter) => {
let name = getter.name.to_string();
if !defs.contains_key(&name) {
let _ = defs.insert(name.clone(), ffi::PyGetSetDef_INIT);
}
let def = defs.get_mut(&name).unwrap();
getter.copy_to(def);
},
&PyMethodDefType::Setter(ref setter) => {
let name = setter.name.to_string();
if !defs.contains_key(&name) {
let _ = defs.insert(name.clone(), ffi::PyGetSetDef_INIT);
}
let def = defs.get_mut(&name).unwrap();
setter.copy_to(def);
},
_ => (),
}
}
defs.values().map(|i| i.clone()).collect()
}