Debian Codegraph @

Doc/includes/newtypes/custom3.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, "|UUi", kwlist,
50 &first, &last,
51 &self->number))
52 return -1;
53
54 if (first) {
55 Py_SETREF(self->first, Py_NewRef(first));
56 }
57 if (last) {
58 Py_SETREF(self->last, Py_NewRef(last));
59 }
60 return 0;
61 }
62
63 static PyMemberDef Custom_members[] = {
64 {"number", Py_T_INT, offsetof(CustomObject, number), 0,
65 "custom number"},
66 {NULL} /* Sentinel */
67 };
68
69 static PyObject *
70 Custom_getfirst(PyObject *op, void *closure)
71 {
72 CustomObject *self = (CustomObject *) op;
73 return Py_NewRef(self->first);
74 }
75
76 static int
77 Custom_setfirst(PyObject *op, PyObject *value, void *closure)
78 {
79 CustomObject *self = (CustomObject *) op;
80 if (value == NULL) {
81 PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
82 return -1;
83 }
84 if (!PyUnicode_Check(value)) {
85 PyErr_SetString(PyExc_TypeError,
86 "The first attribute value must be a string");
87 return -1;
88 }
89 Py_SETREF(self->first, Py_NewRef(value));
90 return 0;
91 }
92
93 static PyObject *
94 Custom_getlast(PyObject *op, void *closure)
95 {
96 CustomObject *self = (CustomObject *) op;
97 return Py_NewRef(self->last);
98 }
99
100 static int
101 Custom_setlast(PyObject *op, PyObject *value, void *closure)
102 {
103 CustomObject *self = (CustomObject *) op;
104 if (value == NULL) {
105 PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
106 return -1;
107 }
108 if (!PyUnicode_Check(value)) {
109 PyErr_SetString(PyExc_TypeError,
110 "The last attribute value must be a string");
111 return -1;
112 }
113 Py_SETREF(self->last, Py_NewRef(value));
114 return 0;
115 }
116
117 static PyGetSetDef Custom_getsetters[] = {
118 {"first", Custom_getfirst, Custom_setfirst,
119 "first name", NULL},
120 {"last", Custom_getlast, Custom_setlast,
121 "last name", NULL},
122 {NULL} /* Sentinel */
123 };
124
125 static PyObject *
126 Custom_name(PyObject *op, PyObject *Py_UNUSED(dummy))
127 {
128 CustomObject *self = (CustomObject *) op;
129 return PyUnicode_FromFormat("%S %S", self->first, self->last);
130 }
131
132 static PyMethodDef Custom_methods[] = {
133 {"name", Custom_name, METH_NOARGS,
134 "Return the name, combining the first and last name"
135 },
136 {NULL} /* Sentinel */
137 };
138
139 static PyTypeObject CustomType = {
140 .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
141 .tp_name = "custom3.Custom",
142 .tp_doc = PyDoc_STR("Custom objects"),
143 .tp_basicsize = sizeof(CustomObject),
144 .tp_itemsize = 0,
145 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
146 .tp_new = Custom_new,
147 .tp_init = Custom_init,
148 .tp_dealloc = Custom_dealloc,
149 .tp_members = Custom_members,
150 .tp_methods = Custom_methods,
151 .tp_getset = Custom_getsetters,
152 };
153
154 static int
155 custom_module_exec(PyObject *m)
156 {
157 if (PyType_Ready(&CustomType) < 0) {
158 return -1;
159 }
160
161 if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
162 return -1;
163 }
164
165 return 0;
166 }
167
168 static PyModuleDef_Slot custom_module_slots[] = {
169 {Py_mod_exec, custom_module_exec},
170 {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
171 {0, NULL}
172 };
173
174 static PyModuleDef custom_module = {
175 .m_base = PyModuleDef_HEAD_INIT,
176 .m_name = "custom3",
177 .m_doc = "Example module that creates an extension type.",
178 .m_size = 0,
179 .m_slots = custom_module_slots,
180 };
181
182 PyMODINIT_FUNC
183 PyInit_custom3(void)
184 {
185 return PyModuleDef_Init(&custom_module);
186 }