I tried to create metaclass in c extension:
static PyTypeObject MyMetaClass = {
PyVarObject_HEAD_INIT(NULL, 0)
"MyMetaClass", // tp_name
sizeof(MyMetaClassObject), // tp_basicsize
0, // tp_itemsize
(destructor)MyMetaClass_del, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_reserved
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
(getattrofunc)MyMetaClass_getattr, // tp_getattro
(setattrofunc)MyMetaClass_setattr, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
"my metaclass", // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
0, // tp_methods
0, // tp_members
0, // tp_getset
&PyType_Type, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
0, // tp_init
0, // tp_alloc
(newfunc)MyMetaClass_new, // tp_new
};
And the new function use PyType_type.tp_new to create. The classes created from the function only change tp_getattro, tp_setattro, tp_dealloc. However, the classes can create their instance only on unix. On windows, the creating for the class is successful but when create the instance, it segfault.