| 1 |
#include <Python.h> |
| 2 |
#include "structmember.h" |
| 3 |
|
| 4 |
typedef struct { |
| 5 |
PyObject_HEAD |
| 6 |
PyObject *first; |
| 7 |
PyObject *last; |
| 8 |
int number; |
| 9 |
} Noddy; |
| 10 |
|
| 11 |
static void |
| 12 |
Noddy_dealloc(Noddy* self) |
| 13 |
{ |
| 14 |
Py_XDECREF(self->first); |
| 15 |
Py_XDECREF(self->last); |
| 16 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 17 |
} |
| 18 |
|
| 19 |
static PyObject * |
| 20 |
Noddy_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 |
Py_DECREF(self); |
| 29 |
return NULL; |
| 30 |
} |
| 31 |
|
| 32 |
self->last = PyString_FromString(""); |
| 33 |
if (self->last == NULL) { |
| 34 |
Py_DECREF(self); |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
|
| 38 |
self->number = 0; |
| 39 |
} |
| 40 |
|
| 41 |
return (PyObject *)self; |
| 42 |
} |
| 43 |
|
| 44 |
static int |
| 45 |
Noddy_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 |
|
| 51 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSi", kwlist, |
| 52 |
&first, &last, |
| 53 |
&self->number)) |
| 54 |
return -1; |
| 55 |
|
| 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 |
|
| 73 |
static PyMemberDef Noddy_members[] = { |
| 74 |
{"number", T_INT, offsetof(Noddy, number), 0, |
| 75 |
"noddy number"}, |
| 76 |
{NULL} |
| 77 |
}; |
| 78 |
|
| 79 |
static PyObject * |
| 80 |
Noddy_getfirst(Noddy *self, void *closure) |
| 81 |
{ |
| 82 |
Py_INCREF(self->first); |
| 83 |
return self->first; |
| 84 |
} |
| 85 |
|
| 86 |
static int |
| 87 |
Noddy_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 |
|
| 107 |
static PyObject * |
| 108 |
Noddy_getlast(Noddy *self, void *closure) |
| 109 |
{ |
| 110 |
Py_INCREF(self->last); |
| 111 |
return self->last; |
| 112 |
} |
| 113 |
|
| 114 |
static int |
| 115 |
Noddy_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 |
|
| 135 |
static 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} |
| 145 |
}; |
| 146 |
|
| 147 |
static PyObject * |
| 148 |
Noddy_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 |
|
| 169 |
static PyMethodDef Noddy_methods[] = { |
| 170 |
{"name", (PyCFunction)Noddy_name, METH_NOARGS, |
| 171 |
"Return the name, combining the first and last name" |
| 172 |
}, |
| 173 |
{NULL} |
| 174 |
}; |
| 175 |
|
| 176 |
static PyTypeObject NoddyType = { |
| 177 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 178 |
"noddy.Noddy", |
| 179 |
sizeof(Noddy), |
| 180 |
0, |
| 181 |
(destructor)Noddy_dealloc, |
| 182 |
0, |
| 183 |
0, |
| 184 |
0, |
| 185 |
0, |
| 186 |
0, |
| 187 |
0, |
| 188 |
0, |
| 189 |
0, |
| 190 |
0, |
| 191 |
0, |
| 192 |
0, |
| 193 |
0, |
| 194 |
0, |
| 195 |
0, |
| 196 |
Py_TPFLAGS_DEFAULT | |
| 197 |
Py_TPFLAGS_BASETYPE, |
| 198 |
"Noddy objects", |
| 199 |
0, |
| 200 |
0, |
| 201 |
0, |
| 202 |
0, |
| 203 |
0, |
| 204 |
0, |
| 205 |
Noddy_methods, |
| 206 |
Noddy_members, |
| 207 |
Noddy_getseters, |
| 208 |
0, |
| 209 |
0, |
| 210 |
0, |
| 211 |
0, |
| 212 |
0, |
| 213 |
(initproc)Noddy_init, |
| 214 |
0, |
| 215 |
Noddy_new, |
| 216 |
}; |
| 217 |
|
| 218 |
static PyMethodDef module_methods[] = { |
| 219 |
{NULL} |
| 220 |
}; |
| 221 |
|
| 222 |
#ifndef PyMODINIT_FUNC |
| 223 |
#define PyMODINIT_FUNC void |
| 224 |
#endif |
| 225 |
PyMODINIT_FUNC |
| 226 |
initnoddy3(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 |
|
| 239 |
Py_INCREF(&NoddyType); |
| 240 |
PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType); |
| 241 |
} |