| 1 |
#define PY_SSIZE_T_CLEAN |
| 2 |
#include <Python.h> |
| 3 |
|
| 4 |
typedef struct { |
| 5 |
PyObject_HEAD |
| 6 |
/* Type-specific fields go here. */ |
| 7 |
} CustomObject; |
| 8 |
|
| 9 |
static PyTypeObject CustomType = { |
| 10 |
.ob_base = PyVarObject_HEAD_INIT(NULL, 0) |
| 11 |
.tp_name = "custom.Custom", |
| 12 |
.tp_doc = PyDoc_STR("Custom objects"), |
| 13 |
.tp_basicsize = sizeof(CustomObject), |
| 14 |
.tp_itemsize = 0, |
| 15 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 16 |
.tp_new = PyType_GenericNew, |
| 17 |
}; |
| 18 |
|
| 19 |
static int |
| 20 |
custom_module_exec(PyObject *m) |
| 21 |
{ |
| 22 |
if (PyType_Ready(&CustomType) < 0) { |
| 23 |
return -1; |
| 24 |
} |
| 25 |
|
| 26 |
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) { |
| 27 |
return -1; |
| 28 |
} |
| 29 |
|
| 30 |
return 0; |
| 31 |
} |
| 32 |
|
| 33 |
static PyModuleDef_Slot custom_module_slots[] = { |
| 34 |
{Py_mod_exec, custom_module_exec}, |
| 35 |
// Just use this while using static types |
| 36 |
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED}, |
| 37 |
{0, NULL} |
| 38 |
}; |
| 39 |
|
| 40 |
static PyModuleDef custom_module = { |
| 41 |
.m_base = PyModuleDef_HEAD_INIT, |
| 42 |
.m_name = "custom", |
| 43 |
.m_doc = "Example module that creates an extension type.", |
| 44 |
.m_size = 0, |
| 45 |
.m_slots = custom_module_slots, |
| 46 |
}; |
| 47 |
|
| 48 |
PyMODINIT_FUNC |
| 49 |
PyInit_custom(void) |
| 50 |
{ |
| 51 |
return PyModuleDef_Init(&custom_module); |
| 52 |
} |