Debian Codegraph @

Doc/includes/newtypes/custom2.c

raw
1 #define PY_SSIZE_T_CLEAN
2 #include <Python.h>
3 #include <stddef.h> /* for offsetof() */
4
5 typedef struct {
6 PyObject_HEAD
7 PyObject *first; /* first name */
8 PyObject *last; /* last name */
9 int number;
10 } CustomObject;
11
12 static void
13 Custom_dealloc(PyObject *op)
14 {
15 CustomObject *self = (CustomObject *) op;
16 Py_XDECREF(self->first);
17 Py_XDECREF(self->last);
18 Py_TYPE(self)->tp_free(self);
19 }
20
21 static PyObject *
22 Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23 {
24 CustomObject *self;
25 self = (CustomObject *) type->tp_alloc(type, 0);
26 if (self != NULL) {
27 self->first = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
28 if (self->first == NULL) {
29 Py_DECREF(self);
30 return NULL;
31 }
32 self->last = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
33 if (self->last == NULL) {
34 Py_DECREF(self);
35 return NULL;
36 }
37 self->number = 0;
38 }
39 return (PyObject *) self;
40 }
41
42 static int
43 Custom_init(PyObject *op, PyObject *args, PyObject *kwds)
44 {
45 CustomObject *self = (CustomObject *) op;
46 static char *kwlist[] = {"first", "last", "number", NULL};
47 PyObject *first = NULL, *last = NULL;
48
49 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
50 &first, &last,
51 &self->number))
52 return -1;
53
54 if (first) {
55 Py_XSETREF(self->first, Py_NewRef(first));
56 }
57 if (last) {
58 Py_XSETREF(self->last, Py_NewRef(last));
59 }
60 return 0;
61 }
62
63 static PyMemberDef Custom_members[] = {
64 {"first", Py_T_OBJECT_EX, offsetof(CustomObject, first), 0,
65 "first name"},
66 {"last", Py_T_OBJECT_EX, offsetof(CustomObject, last), 0,
67 "last name"},
68 {"number", Py_T_INT, offsetof(CustomObject, number), 0,
69 "custom number"},
70 {NULL} /* Sentinel */
71 };
72
73 static PyObject *
74 Custom_name(PyObject *op, PyObject *Py_UNUSED(dummy))
75 {
76 CustomObject *self = (CustomObject *) op;
77 if (self->first == NULL) {
78 PyErr_SetString(PyExc_AttributeError, "first");
79 return NULL;
80 }
81 if (self->last == NULL) {
82 PyErr_SetString(PyExc_AttributeError, "last");
83 return NULL;
84 }
85 return PyUnicode_FromFormat("%S %S", self->first, self->last);
86 }
87
88 static PyMethodDef Custom_methods[] = {
89 {"name", Custom_name, METH_NOARGS,
90 "Return the name, combining the first and last name"
91 },
92 {NULL} /* Sentinel */
93 };
94
95 static PyTypeObject CustomType = {
96 .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
97 .tp_name = "custom2.Custom",
98 .tp_doc = PyDoc_STR("Custom objects"),
99 .tp_basicsize = sizeof(CustomObject),
100 .tp_itemsize = 0,
101 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
102 .tp_new = Custom_new,
103 .tp_init = Custom_init,
104 .tp_dealloc = Custom_dealloc,
105 .tp_members = Custom_members,
106 .tp_methods = Custom_methods,
107 };
108
109 static int
110 custom_module_exec(PyObject *m)
111 {
112 if (PyType_Ready(&CustomType) < 0) {
113 return -1;
114 }
115
116 if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
117 return -1;
118 }
119
120 return 0;
121 }
122
123 static PyModuleDef_Slot custom_module_slots[] = {
124 {Py_mod_exec, custom_module_exec},
125 {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
126 {0, NULL}
127 };
128
129 static PyModuleDef custom_module = {
130 .m_base = PyModuleDef_HEAD_INIT,
131 .m_name = "custom2",
132 .m_doc = "Example module that creates an extension type.",
133 .m_size = 0,
134 .m_slots = custom_module_slots,
135 };
136
137 PyMODINIT_FUNC
138 PyInit_custom2(void)
139 {
140 return PyModuleDef_Init(&custom_module);
141 }