blob: 9984a3d0d1a0c286b48b0715641d7dcdafd80260 [file] [log] [blame]
Jim Fultonaed0a4a2003-05-07 19:48:13 +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);
Jim Fulton1f325562003-05-16 13:53:43 +000016 self->ob_type->tp_free((PyObject*)self);
Jim Fultonaed0a4a2003-05-07 19:48:13 +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) {
26 self->first = PyString_FromString("");
27 if (self->first == NULL)
28 {
29 Py_DECREF(self);
30 return NULL;
31 }
32
33 self->last = PyString_FromString("");
34 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
Jim Fulton7050e922003-06-28 11:54:03 +000046static int
Jim Fultonaed0a4a2003-05-07 19:48:13 +000047Noddy_init(Noddy *self, PyObject *args, PyObject *kwds)
48{
49 PyObject *first=NULL, *last=NULL;
50
51 static char *kwlist[] = {"first", "last", "number", NULL};
52
53 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
54 &first, &last,
55 &self->number))
Jim Fulton7050e922003-06-28 11:54:03 +000056 return -1;
Jim Fultonaed0a4a2003-05-07 19:48:13 +000057
58 if (first) {
59 Py_DECREF(self->first);
60 Py_INCREF(first);
61 self->first = first;
62 }
63
64 if (last) {
65 Py_DECREF(self->last);
66 Py_INCREF(last);
67 self->last = last;
68 }
69
Jim Fulton7050e922003-06-28 11:54:03 +000070 return 0;
Jim Fultonaed0a4a2003-05-07 19:48:13 +000071}
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{
89 if (value == NULL) {
90 PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
91 return -1;
92 }
93
94 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;
105}
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{
117 if (value == NULL) {
118 PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
119 return -1;
120 }
121
122 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;
133}
134
135static PyGetSetDef Noddy_getseters[] = {
136 {"first",
137 (getter)Noddy_getfirst, (setter)Noddy_setfirst,
138 "first name",
139 NULL},
140 {"last",
141 (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) {
154 format = PyString_FromString("%s %s");
155 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
163 result = PyString_Format(format, args);
164 Py_DECREF(args);
165
166 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 = {
177 PyObject_HEAD_INIT(NULL)
178 0, /*ob_size*/
179 "noddy.Noddy", /*tp_name*/
180 sizeof(Noddy), /*tp_basicsize*/
181 0, /*tp_itemsize*/
182 (destructor)Noddy_dealloc, /*tp_dealloc*/
183 0, /*tp_print*/
184 0, /*tp_getattr*/
185 0, /*tp_setattr*/
186 0, /*tp_compare*/
187 0, /*tp_repr*/
188 0, /*tp_as_number*/
189 0, /*tp_as_sequence*/
190 0, /*tp_as_mapping*/
191 0, /*tp_hash */
192 0, /*tp_call*/
193 0, /*tp_str*/
194 0, /*tp_getattro*/
195 0, /*tp_setattro*/
196 0, /*tp_as_buffer*/
197 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
198 "Noddy objects", /* tp_doc */
199 0, /* tp_traverse */
200 0, /* tp_clear */
201 0, /* tp_richcompare */
202 0, /* tp_weaklistoffset */
203 0, /* tp_iter */
204 0, /* tp_iternext */
205 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
Jim Fulton1f325562003-05-16 13:53:43 +0000222#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
223#define PyMODINIT_FUNC void
224#endif
Jim Fultonaed0a4a2003-05-07 19:48:13 +0000225PyMODINIT_FUNC
226initnoddy3(void)
227{
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)
237 return;
238
Raymond Hettinger8fb665a2003-05-25 17:59:38 +0000239 Py_INCREF(&NoddyType);
Jim Fultonaed0a4a2003-05-07 19:48:13 +0000240 PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
241}