blob: 43ec42b682cfde52ca960b25bd57d5179916aee7 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001#include <Python.h>
2#include "structmember.h"
3
4typedef struct {
5 PyObject_HEAD
6 PyObject *first;
7 PyObject *last;
8 int number;
9} Noddy;
10
11static void
12Noddy_dealloc(Noddy* self)
13{
14 Py_XDECREF(self->first);
15 Py_XDECREF(self->last);
INADA Naoki9436bbd2017-02-21 21:12:03 +090016 Py_TYPE(self)->tp_free((PyObject*)self);
Georg Brandl8ec7f652007-08-15 14:28:01 +000017}
18
19static PyObject *
20Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
21{
22 Noddy *self;
23
24 self = (Noddy *)type->tp_alloc(type, 0);
25 if (self != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000026 self->first = PyString_FromString("");
INADA Naoki9436bbd2017-02-21 21:12:03 +090027 if (self->first == NULL) {
Georg Brandl8ec7f652007-08-15 14:28:01 +000028 Py_DECREF(self);
29 return NULL;
INADA Naoki9436bbd2017-02-21 21:12:03 +090030 }
31
Gregory P. Smithdd96db62008-06-09 04:58:54 +000032 self->last = PyString_FromString("");
INADA Naoki9436bbd2017-02-21 21:12:03 +090033 if (self->last == NULL) {
Georg Brandl8ec7f652007-08-15 14:28:01 +000034 Py_DECREF(self);
35 return NULL;
INADA Naoki9436bbd2017-02-21 21:12:03 +090036 }
Georg Brandl8ec7f652007-08-15 14:28:01 +000037
38 self->number = 0;
39 }
40
41 return (PyObject *)self;
42}
43
44static int
45Noddy_init(Noddy *self, PyObject *args, PyObject *kwds)
46{
47 PyObject *first=NULL, *last=NULL, *tmp;
48
49 static char *kwlist[] = {"first", "last", "number", NULL};
50
INADA Naoki9436bbd2017-02-21 21:12:03 +090051 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSi", kwlist,
52 &first, &last,
Georg Brandl8ec7f652007-08-15 14:28:01 +000053 &self->number))
INADA Naoki9436bbd2017-02-21 21:12:03 +090054 return -1;
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
56 if (first) {
57 tmp = self->first;
58 Py_INCREF(first);
59 self->first = first;
60 Py_DECREF(tmp);
61 }
62
63 if (last) {
64 tmp = self->last;
65 Py_INCREF(last);
66 self->last = last;
67 Py_DECREF(tmp);
68 }
69
70 return 0;
71}
72
73static PyMemberDef Noddy_members[] = {
74 {"number", T_INT, offsetof(Noddy, number), 0,
75 "noddy number"},
76 {NULL} /* Sentinel */
77};
78
79static PyObject *
80Noddy_getfirst(Noddy *self, void *closure)
81{
82 Py_INCREF(self->first);
83 return self->first;
84}
85
86static int
87Noddy_setfirst(Noddy *self, PyObject *value, void *closure)
88{
INADA Naoki9436bbd2017-02-21 21:12:03 +090089 if (value == NULL) {
90 PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
91 return -1;
92 }
Georg Brandl8ec7f652007-08-15 14:28:01 +000093
INADA Naoki9436bbd2017-02-21 21:12:03 +090094 if (! PyString_Check(value)) {
95 PyErr_SetString(PyExc_TypeError,
96 "The first attribute value must be a string");
97 return -1;
98 }
99
100 Py_DECREF(self->first);
101 Py_INCREF(value);
102 self->first = value;
103
104 return 0;
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105}
106
107static PyObject *
108Noddy_getlast(Noddy *self, void *closure)
109{
110 Py_INCREF(self->last);
111 return self->last;
112}
113
114static int
115Noddy_setlast(Noddy *self, PyObject *value, void *closure)
116{
INADA Naoki9436bbd2017-02-21 21:12:03 +0900117 if (value == NULL) {
118 PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
119 return -1;
120 }
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121
INADA Naoki9436bbd2017-02-21 21:12:03 +0900122 if (! PyString_Check(value)) {
123 PyErr_SetString(PyExc_TypeError,
124 "The last attribute value must be a string");
125 return -1;
126 }
127
128 Py_DECREF(self->last);
129 Py_INCREF(value);
130 self->last = value;
131
132 return 0;
Georg Brandl8ec7f652007-08-15 14:28:01 +0000133}
134
135static PyGetSetDef Noddy_getseters[] = {
INADA Naoki9436bbd2017-02-21 21:12:03 +0900136 {"first",
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137 (getter)Noddy_getfirst, (setter)Noddy_setfirst,
138 "first name",
139 NULL},
INADA Naoki9436bbd2017-02-21 21:12:03 +0900140 {"last",
Georg Brandl8ec7f652007-08-15 14:28:01 +0000141 (getter)Noddy_getlast, (setter)Noddy_setlast,
142 "last name",
143 NULL},
144 {NULL} /* Sentinel */
145};
146
147static PyObject *
148Noddy_name(Noddy* self)
149{
150 static PyObject *format = NULL;
151 PyObject *args, *result;
152
153 if (format == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000154 format = PyString_FromString("%s %s");
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155 if (format == NULL)
156 return NULL;
157 }
158
159 args = Py_BuildValue("OO", self->first, self->last);
160 if (args == NULL)
161 return NULL;
162
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000163 result = PyString_Format(format, args);
Georg Brandl8ec7f652007-08-15 14:28:01 +0000164 Py_DECREF(args);
INADA Naoki9436bbd2017-02-21 21:12:03 +0900165
Georg Brandl8ec7f652007-08-15 14:28:01 +0000166 return result;
167}
168
169static PyMethodDef Noddy_methods[] = {
170 {"name", (PyCFunction)Noddy_name, METH_NOARGS,
171 "Return the name, combining the first and last name"
172 },
173 {NULL} /* Sentinel */
174};
175
176static PyTypeObject NoddyType = {
INADA Naoki9436bbd2017-02-21 21:12:03 +0900177 PyVarObject_HEAD_INIT(NULL, 0)
178 "noddy.Noddy", /* tp_name */
179 sizeof(Noddy), /* tp_basicsize */
180 0, /* tp_itemsize */
181 (destructor)Noddy_dealloc, /* tp_dealloc */
182 0, /* tp_print */
183 0, /* tp_getattr */
184 0, /* tp_setattr */
185 0, /* tp_compare */
186 0, /* tp_repr */
187 0, /* tp_as_number */
188 0, /* tp_as_sequence */
189 0, /* tp_as_mapping */
190 0, /* tp_hash */
191 0, /* tp_call */
192 0, /* tp_str */
193 0, /* tp_getattro */
194 0, /* tp_setattro */
195 0, /* tp_as_buffer */
196 Py_TPFLAGS_DEFAULT |
197 Py_TPFLAGS_BASETYPE, /* tp_flags */
Georg Brandl8ec7f652007-08-15 14:28:01 +0000198 "Noddy objects", /* tp_doc */
INADA Naoki9436bbd2017-02-21 21:12:03 +0900199 0, /* tp_traverse */
200 0, /* tp_clear */
201 0, /* tp_richcompare */
202 0, /* tp_weaklistoffset */
203 0, /* tp_iter */
204 0, /* tp_iternext */
Georg Brandl8ec7f652007-08-15 14:28:01 +0000205 Noddy_methods, /* tp_methods */
206 Noddy_members, /* tp_members */
207 Noddy_getseters, /* tp_getset */
208 0, /* tp_base */
209 0, /* tp_dict */
210 0, /* tp_descr_get */
211 0, /* tp_descr_set */
212 0, /* tp_dictoffset */
213 (initproc)Noddy_init, /* tp_init */
214 0, /* tp_alloc */
215 Noddy_new, /* tp_new */
216};
217
218static PyMethodDef module_methods[] = {
219 {NULL} /* Sentinel */
220};
221
222#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
223#define PyMODINIT_FUNC void
224#endif
225PyMODINIT_FUNC
INADA Naoki9436bbd2017-02-21 21:12:03 +0900226initnoddy3(void)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000227{
228 PyObject* m;
229
230 if (PyType_Ready(&NoddyType) < 0)
231 return;
232
233 m = Py_InitModule3("noddy3", module_methods,
234 "Example module that creates an extension type.");
235
236 if (m == NULL)
INADA Naoki9436bbd2017-02-21 21:12:03 +0900237 return;
Georg Brandl8ec7f652007-08-15 14:28:01 +0000238
239 Py_INCREF(&NoddyType);
240 PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
241}