xref: /third_party/python/Doc/includes/custom.c (revision 7db96d56)
1#define PY_SSIZE_T_CLEAN
2#include <Python.h>
3
4typedef struct {
5    PyObject_HEAD
6    /* Type-specific fields go here. */
7} CustomObject;
8
9static PyTypeObject CustomType = {
10    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
19static 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
26PyMODINIT_FUNC
27PyInit_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