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     PyVarObject_HEAD_INIT(NULL, 0)
11     .tp_name = "custom.Custom",
12     .tp_doc = "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 PyModuleDef custommodule = {
20     PyModuleDef_HEAD_INIT,
21     .m_name = "custom",
22     .m_doc = "Example module that creates an extension type.",
23     .m_size = -1,
24 };
25 
26 PyMODINIT_FUNC
PyInit_custom(void)27 PyInit_custom(void)
28 {
29     PyObject *m;
30     if (PyType_Ready(&CustomType) < 0)
31         return NULL;
32 
33     m = PyModule_Create(&custommodule);
34     if (m == NULL)
35         return NULL;
36 
37     Py_INCREF(&CustomType);
38     if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
39         Py_DECREF(&CustomType);
40         Py_DECREF(m);
41         return NULL;
42     }
43 
44     return m;
45 }
46