blob: 89f3a7712e35a9aa7924027ba3874e8a4d9a663c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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);
Georg Brandl913b2a32008-12-05 15:12:15 +000016 Py_TYPE(self)->tp_free((PyObject*)self);
Georg Brandl116aa622007-08-15 14:28:22 +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) {
Neal Norwitzbed67842007-10-27 04:00:45 +000026 self->first = PyUnicode_FromString("");
Georg Brandl116aa622007-08-15 14:28:22 +000027 if (self->first == NULL)
28 {
29 Py_DECREF(self);
30 return NULL;
31 }
32
Neal Norwitzbed67842007-10-27 04:00:45 +000033 self->last = PyUnicode_FromString("");
Georg Brandl116aa622007-08-15 14:28:22 +000034 if (self->last == NULL)
35 {
36 Py_DECREF(self);
37 return NULL;
38 }
39
40 self->number = 0;
41 }
42
43 return (PyObject *)self;
44}
45
46static int
47Noddy_init(Noddy *self, PyObject *args, PyObject *kwds)
48{
49 PyObject *first=NULL, *last=NULL, *tmp;
50
51 static char *kwlist[] = {"first", "last", "number", NULL};
52
53 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSi", kwlist,
54 &first, &last,
55 &self->number))
56 return -1;
57
58 if (first) {
59 tmp = self->first;
60 Py_INCREF(first);
61 self->first = first;
62 Py_DECREF(tmp);
63 }
64
65 if (last) {
66 tmp = self->last;
67 Py_INCREF(last);
68 self->last = last;
69 Py_DECREF(tmp);
70 }
71
72 return 0;
73}
74
75static PyMemberDef Noddy_members[] = {
76 {"number", T_INT, offsetof(Noddy, number), 0,
77 "noddy number"},
78 {NULL} /* Sentinel */
79};
80
81static PyObject *
82Noddy_getfirst(Noddy *self, void *closure)
83{
84 Py_INCREF(self->first);
85 return self->first;
86}
87
88static int
89Noddy_setfirst(Noddy *self, PyObject *value, void *closure)
90{
91 if (value == NULL) {
92 PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
93 return -1;
94 }
95
Neal Norwitzbed67842007-10-27 04:00:45 +000096 if (! PyUnicode_Check(value)) {
Georg Brandl116aa622007-08-15 14:28:22 +000097 PyErr_SetString(PyExc_TypeError,
98 "The first attribute value must be a string");
99 return -1;
100 }
101
102 Py_DECREF(self->first);
103 Py_INCREF(value);
104 self->first = value;
105
106 return 0;
107}
108
109static PyObject *
110Noddy_getlast(Noddy *self, void *closure)
111{
112 Py_INCREF(self->last);
113 return self->last;
114}
115
116static int
117Noddy_setlast(Noddy *self, PyObject *value, void *closure)
118{
119 if (value == NULL) {
120 PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
121 return -1;
122 }
123
Neal Norwitzbed67842007-10-27 04:00:45 +0000124 if (! PyUnicode_Check(value)) {
Georg Brandl116aa622007-08-15 14:28:22 +0000125 PyErr_SetString(PyExc_TypeError,
126 "The last attribute value must be a string");
127 return -1;
128 }
129
130 Py_DECREF(self->last);
131 Py_INCREF(value);
132 self->last = value;
133
134 return 0;
135}
136
137static PyGetSetDef Noddy_getseters[] = {
138 {"first",
139 (getter)Noddy_getfirst, (setter)Noddy_setfirst,
140 "first name",
141 NULL},
142 {"last",
143 (getter)Noddy_getlast, (setter)Noddy_setlast,
144 "last name",
145 NULL},
146 {NULL} /* Sentinel */
147};
148
149static PyObject *
150Noddy_name(Noddy* self)
151{
152 static PyObject *format = NULL;
153 PyObject *args, *result;
154
155 if (format == NULL) {
Neal Norwitzbed67842007-10-27 04:00:45 +0000156 format = PyUnicode_FromString("%s %s");
Georg Brandl116aa622007-08-15 14:28:22 +0000157 if (format == NULL)
158 return NULL;
159 }
160
161 args = Py_BuildValue("OO", self->first, self->last);
162 if (args == NULL)
163 return NULL;
164
Neal Norwitzbed67842007-10-27 04:00:45 +0000165 result = PyUnicode_Format(format, args);
Georg Brandl116aa622007-08-15 14:28:22 +0000166 Py_DECREF(args);
167
168 return result;
169}
170
171static PyMethodDef Noddy_methods[] = {
172 {"name", (PyCFunction)Noddy_name, METH_NOARGS,
173 "Return the name, combining the first and last name"
174 },
175 {NULL} /* Sentinel */
176};
177
178static PyTypeObject NoddyType = {
Georg Brandl51335292009-05-05 07:55:26 +0000179 PyVarObject_HEAD_INIT(NULL, 0)
Georg Brandl913b2a32008-12-05 15:12:15 +0000180 "noddy.Noddy", /* tp_name */
181 sizeof(Noddy), /* tp_basicsize */
182 0, /* tp_itemsize */
183 (destructor)Noddy_dealloc, /* tp_dealloc */
184 0, /* tp_print */
185 0, /* tp_getattr */
186 0, /* tp_setattr */
Mark Dickinson9f989262009-02-02 21:29:40 +0000187 0, /* tp_reserved */
Georg Brandl913b2a32008-12-05 15:12:15 +0000188 0, /* tp_repr */
189 0, /* tp_as_number */
190 0, /* tp_as_sequence */
191 0, /* tp_as_mapping */
192 0, /* tp_hash */
193 0, /* tp_call */
194 0, /* tp_str */
195 0, /* tp_getattro */
196 0, /* tp_setattro */
197 0, /* tp_as_buffer */
198 Py_TPFLAGS_DEFAULT |
199 Py_TPFLAGS_BASETYPE, /* tp_flags */
Georg Brandl116aa622007-08-15 14:28:22 +0000200 "Noddy objects", /* tp_doc */
201 0, /* tp_traverse */
202 0, /* tp_clear */
203 0, /* tp_richcompare */
204 0, /* tp_weaklistoffset */
205 0, /* tp_iter */
206 0, /* tp_iternext */
207 Noddy_methods, /* tp_methods */
208 Noddy_members, /* tp_members */
209 Noddy_getseters, /* tp_getset */
210 0, /* tp_base */
211 0, /* tp_dict */
212 0, /* tp_descr_get */
213 0, /* tp_descr_set */
214 0, /* tp_dictoffset */
215 (initproc)Noddy_init, /* tp_init */
216 0, /* tp_alloc */
217 Noddy_new, /* tp_new */
218};
219
Georg Brandl913b2a32008-12-05 15:12:15 +0000220static PyModuleDef noddy3module = {
221 PyModuleDef_HEAD_INIT,
222 "noddy3",
223 "Example module that creates an extension type.",
224 -1,
225 NULL, NULL, NULL, NULL, NULL
Georg Brandl116aa622007-08-15 14:28:22 +0000226};
227
Georg Brandl116aa622007-08-15 14:28:22 +0000228PyMODINIT_FUNC
Georg Brandl913b2a32008-12-05 15:12:15 +0000229PyInit_noddy3(void)
Georg Brandl116aa622007-08-15 14:28:22 +0000230{
231 PyObject* m;
232
233 if (PyType_Ready(&NoddyType) < 0)
Georg Brandl913b2a32008-12-05 15:12:15 +0000234 return NULL;
Georg Brandl116aa622007-08-15 14:28:22 +0000235
Georg Brandl913b2a32008-12-05 15:12:15 +0000236 m = PyModule_Create(&noddy3module);
Georg Brandl116aa622007-08-15 14:28:22 +0000237 if (m == NULL)
Georg Brandl913b2a32008-12-05 15:12:15 +0000238 return NULL;
Georg Brandl116aa622007-08-15 14:28:22 +0000239
240 Py_INCREF(&NoddyType);
241 PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
Benjamin Peterson71e30a02008-12-24 16:27:25 +0000242 return m;
Georg Brandl116aa622007-08-15 14:28:22 +0000243}