pub fn initialize_type<'p, T>(py: Python<'p>, module_name: Option<&str>) -> PyResult<()>
    where T: PyObjectAlloc<T> + PyTypeInfo
{
    // type name
    let name = match module_name {
        Some(module_name) => CString::new(format!("{}.{}", module_name, T::NAME)),
        None => CString::new(T::NAME)
    };
    let name = name.expect(
        "Module name/type name must not contain NUL byte").into_raw();

    let type_object: &mut ffi::PyTypeObject = unsafe{&mut *T::type_object()};
    let base_type_object: &mut ffi::PyTypeObject = unsafe {
        &mut *<T::BaseType as PyTypeInfo>::type_object() };

    type_object.tp_name = name;
    type_object.tp_doc = T::DESCRIPTION.as_ptr() as *const _;
    type_object.tp_base = base_type_object;

    // dealloc
    type_object.tp_dealloc = Some(tp_dealloc_callback::<T>);

    // type size
    type_object.tp_basicsize = <T as PyTypeInfo>::SIZE as ffi::Py_ssize_t;

    let mut offset = T::SIZE;
    // weakref support (check py3cls::py_class::impl_class)
    if T::FLAGS & PY_TYPE_FLAG_WEAKREF != 0 {
        offset -= std::mem::size_of::<*const ffi::PyObject>();
        type_object.tp_weaklistoffset = offset as isize;
    }

    // __dict__ support
    if T::FLAGS & PY_TYPE_FLAG_DICT != 0 {
        offset -= std::mem::size_of::<*const ffi::PyObject>();
        type_object.tp_dictoffset = offset as isize;
    }

    // GC support
    <T as class::gc::PyGCProtocolImpl>::update_type_object(type_object);

    // descriptor protocol
    <T as class::descr::PyDescrProtocolImpl>::tp_as_descr(type_object);

    // iterator methods
    <T as class::iter::PyIterProtocolImpl>::tp_as_iter(type_object);

    // basic methods
    <T as class::basic::PyObjectProtocolImpl>::tp_as_object(type_object);

    // number methods
    if let Some(meth) = <T as class::number::PyNumberProtocolImpl>::tp_as_number() {
        type_object.tp_as_number = Box::into_raw(Box::new(meth));
    } else {
        type_object.tp_as_number = ::std::ptr::null_mut()
    }

    // mapping methods
    if let Some(meth) = <T as class::mapping::PyMappingProtocolImpl>::tp_as_mapping() {
        type_object.tp_as_mapping = Box::into_raw(Box::new(meth));
    } else {
        type_object.tp_as_mapping = ::std::ptr::null_mut()
    }

    // sequence methods
    if let Some(meth) = <T as class::sequence::PySequenceProtocolImpl>::tp_as_sequence() {
        type_object.tp_as_sequence = Box::into_raw(Box::new(meth));
    } else {
        type_object.tp_as_sequence = ::std::ptr::null_mut()
    }

    // async methods
    async_methods::<T>(type_object);

    // buffer protocol
    if let Some(meth) = <T as class::buffer::PyBufferProtocolImpl>::tp_as_buffer() {
        type_object.tp_as_buffer = Box::into_raw(Box::new(meth));
    } else {
        type_object.tp_as_buffer = ::std::ptr::null_mut()
    }

    // normal methods
    let (new, init, call, mut methods) = py_class_method_defs::<T>()?;
    if !methods.is_empty() {
        methods.push(ffi::PyMethodDef_INIT);
        type_object.tp_methods = methods.as_mut_ptr();
        mem::forget(methods);
    }

    match (new, init) {
        (None, Some(_)) => {
            panic!("{}.__new__ method is required if __init__ method defined", T::NAME);
        }
        _ => ()
    }

    // __new__ method
    type_object.tp_new = new;
    // __init__ method
    type_object.tp_init = init;
    // __call__ method
    type_object.tp_call = call;

    // properties
    let mut props = py_class_properties::<T>();
    if !props.is_empty() {
        props.push(ffi::PyGetSetDef_INIT);
        type_object.tp_getset = props.as_mut_ptr();
        mem::forget(props);
    }

    // set type flags
    py_class_flags::<T>(type_object);
    if type_object.tp_base != unsafe{std::mem::transmute(&ffi::PyBaseObject_Type)} {
        type_object.tp_flags = type_object.tp_flags | ffi::Py_TPFLAGS_HEAPTYPE
    }

    // register type object
    unsafe {
        if ffi::PyType_Ready(type_object) == 0 {
            Ok(())
        } else {
            PyErr::fetch(py).into()
        }
    }
}
/// Register new type in python object system.
Register new type in python object system.
Register new type in python object system .
pub fn initialize_type<'p, T>(py: Python<'p>, module_name: Option<&str>) -> PyResult<()>
    where T: PyObjectAlloc<T> + PyTypeInfo
{
    // type name
    let name = match module_name {
        Some(module_name) => CString::new(format!("{}.{}", module_name, T::NAME)),
        None => CString::new(T::NAME)
    };
    let name = name.expect(
        "Module name/type name must not contain NUL byte").into_raw();
    let type_object: &mut ffi::PyTypeObject = unsafe{&mut *T::type_object()};
    let base_type_object: &mut ffi::PyTypeObject = unsafe {
        &mut *<T::BaseType as PyTypeInfo>::type_object() };
    type_object.tp_name = name;
    type_object.tp_doc = T::DESCRIPTION.as_ptr() as *const _;
    type_object.tp_base = base_type_object;
    // dealloc
    type_object.tp_dealloc = Some(tp_dealloc_callback::<T>);
    // type size
    type_object.tp_basicsize = <T as PyTypeInfo>::SIZE as ffi::Py_ssize_t;
    let mut offset = T::SIZE;
    // weakref support (check py3cls::py_class::impl_class)
    if T::FLAGS & PY_TYPE_FLAG_WEAKREF != 0 {
        offset -= std::mem::size_of::<*const ffi::PyObject>();
        type_object.tp_weaklistoffset = offset as isize;
    }
    // __dict__ support
    if T::FLAGS & PY_TYPE_FLAG_DICT != 0 {
        offset -= std::mem::size_of::<*const ffi::PyObject>();
        type_object.tp_dictoffset = offset as isize;
    }
    // GC support
    <T as class::gc::PyGCProtocolImpl>::update_type_object(type_object);
    // descriptor protocol
    <T as class::descr::PyDescrProtocolImpl>::tp_as_descr(type_object);
    // iterator methods
    <T as class::iter::PyIterProtocolImpl>::tp_as_iter(type_object);
    // basic methods
    <T as class::basic::PyObjectProtocolImpl>::tp_as_object(type_object);
    // number methods
    if let Some(meth) = <T as class::number::PyNumberProtocolImpl>::tp_as_number() {
        type_object.tp_as_number = Box::into_raw(Box::new(meth));
    } else {
        type_object.tp_as_number = ::std::ptr::null_mut()
    }
    // mapping methods
    if let Some(meth) = <T as class::mapping::PyMappingProtocolImpl>::tp_as_mapping() {
        type_object.tp_as_mapping = Box::into_raw(Box::new(meth));
    } else {
        type_object.tp_as_mapping = ::std::ptr::null_mut()
    }
    // sequence methods
    if let Some(meth) = <T as class::sequence::PySequenceProtocolImpl>::tp_as_sequence() {
        type_object.tp_as_sequence = Box::into_raw(Box::new(meth));
    } else {
        type_object.tp_as_sequence = ::std::ptr::null_mut()
    }
    // async methods
    async_methods::<T>(type_object);
    // buffer protocol
    if let Some(meth) = <T as class::buffer::PyBufferProtocolImpl>::tp_as_buffer() {
        type_object.tp_as_buffer = Box::into_raw(Box::new(meth));
    } else {
        type_object.tp_as_buffer = ::std::ptr::null_mut()
    }
    // normal methods
    let (new, init, call, mut methods) = py_class_method_defs::<T>()?;
    if !methods.is_empty() {
        methods.push(ffi::PyMethodDef_INIT);
        type_object.tp_methods = methods.as_mut_ptr();
        mem::forget(methods);
    }
    match (new, init) {
        (None, Some(_)) => {
            panic!("{}.__new__ method is required if __init__ method defined", T::NAME);
        }
        _ => ()
    }
    // __new__ method
    type_object.tp_new = new;
    // __init__ method
    type_object.tp_init = init;
    // __call__ method
    type_object.tp_call = call;
    // properties
    let mut props = py_class_properties::<T>();
    if !props.is_empty() {
        props.push(ffi::PyGetSetDef_INIT);
        type_object.tp_getset = props.as_mut_ptr();
        mem::forget(props);
    }
    // set type flags
    py_class_flags::<T>(type_object);
    if type_object.tp_base != unsafe{std::mem::transmute(&ffi::PyBaseObject_Type)} {
        type_object.tp_flags = type_object.tp_flags | ffi::Py_TPFLAGS_HEAPTYPE
    }
    // register type object
    unsafe {
        if ffi::PyType_Ready(type_object) == 0 {
            Ok(())
        } else {
            PyErr::fetch(py).into()
        }
    }
}
pub fn initialize_type < ' p , T > ( py : Python < ' p > , module_name : Option < & str > ) -> PyResult < ( ) > where T : PyObjectAlloc < T > + PyTypeInfo { // type name let name = match module_name { Some ( module_name ) => CString :: new ( format ! ( " " module_name T NAME ) ) , None => CString :: new ( T :: NAME ) } ; let name = name . expect ( " " ) . into_raw ( ) ; let type_object : & mut ffi :: PyTypeObject = unsafe { & mut * T :: type_object ( ) } ; let base_type_object : & mut ffi :: PyTypeObject = unsafe { & mut * < T :: BaseType as PyTypeInfo > :: type_object ( ) } ; type_object . tp_name = name ; type_object . tp_doc = T :: DESCRIPTION . as_ptr ( ) as * const _ ; type_object . tp_base = base_type_object ; // dealloc type_object . tp_dealloc = Some ( tp_dealloc_callback :: < T > ) ; // type size type_object . tp_basicsize = < T as PyTypeInfo > :: SIZE as ffi :: Py_ssize_t ; let mut offset = T :: SIZE ; // weakref support (check py3cls::py_class::impl_class) if T :: FLAGS & PY_TYPE_FLAG_WEAKREF != 0 { offset -= std :: mem :: size_of :: < * const ffi :: PyObject > ( ) ; type_object . tp_weaklistoffset = offset as isize ; } // __dict__ support if T :: FLAGS & PY_TYPE_FLAG_DICT != 0 { offset -= std :: mem :: size_of :: < * const ffi :: PyObject > ( ) ; type_object . tp_dictoffset = offset as isize ; } // GC support < T as class :: gc :: PyGCProtocolImpl > :: update_type_object ( type_object ) ; // descriptor protocol < T as class :: descr :: PyDescrProtocolImpl > :: tp_as_descr ( type_object ) ; // iterator methods < T as class :: iter :: PyIterProtocolImpl > :: tp_as_iter ( type_object ) ; // basic methods < T as class :: basic :: PyObjectProtocolImpl > :: tp_as_object ( type_object ) ; // number methods if let Some ( meth ) = < T as class :: number :: PyNumberProtocolImpl > :: tp_as_number ( ) { type_object . tp_as_number = Box :: into_raw ( Box :: new ( meth ) ) ; } else { type_object . tp_as_number = :: std :: ptr :: null_mut ( ) } // mapping methods if let Some ( meth ) = < T as class :: mapping :: PyMappingProtocolImpl > :: tp_as_mapping ( ) { type_object . tp_as_mapping = Box :: into_raw ( Box :: new ( meth ) ) ; } else { type_object . tp_as_mapping = :: std :: ptr :: null_mut ( ) } // sequence methods if let Some ( meth ) = < T as class :: sequence :: PySequenceProtocolImpl > :: tp_as_sequence ( ) { type_object . tp_as_sequence = Box :: into_raw ( Box :: new ( meth ) ) ; } else { type_object . tp_as_sequence = :: std :: ptr :: null_mut ( ) } // async methods async_methods :: < T > ( type_object ) ; // buffer protocol if let Some ( meth ) = < T as class :: buffer :: PyBufferProtocolImpl > :: tp_as_buffer ( ) { type_object . tp_as_buffer = Box :: into_raw ( Box :: new ( meth ) ) ; } else { type_object . tp_as_buffer = :: std :: ptr :: null_mut ( ) } // normal methods let ( new , init , call , mut methods ) = py_class_method_defs :: < T > ( ) ? ; if ! methods . is_empty ( ) { methods . push ( ffi :: PyMethodDef_INIT ) ; type_object . tp_methods = methods . as_mut_ptr ( ) ; mem :: forget ( methods ) ; } match ( new , init ) { ( None , Some ( _ ) ) => { panic ! ( " " T NAME ) ; } _ => ( ) } // __new__ method type_object . tp_new = new ; // __init__ method type_object . tp_init = init ; // __call__ method type_object . tp_call = call ; // properties let mut props = py_class_properties :: < T > ( ) ; if ! props . is_empty ( ) { props . push ( ffi :: PyGetSetDef_INIT ) ; type_object . tp_getset = props . as_mut_ptr ( ) ; mem :: forget ( props ) ; } // set type flags py_class_flags :: < T > ( type_object ) ; if type_object . tp_base != unsafe { std :: mem :: transmute ( & ffi :: PyBaseObject_Type ) } { type_object . tp_flags = type_object . tp_flags | ffi :: Py_TPFLAGS_HEAPTYPE } // register type object unsafe { if ffi :: PyType_Ready ( type_object ) == 0 { Ok ( ( ) ) } else { PyErr :: fetch ( py ) . into ( ) } } }
Register new type in python object system.
Register new type in python object system .
py
Python<'p>
module_name
Option<&str>
py
Python<'p>
module_name
Option<&str>