Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Doc/includes/custom.c
12 views
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 PyModuleDef custommodule = {
20
.m_base = 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
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
47