| 1 |
#include <Python.h> |
| 2 |
|
| 3 |
typedef struct { |
| 4 |
PyListObject list; |
| 5 |
int state; |
| 6 |
} Shoddy; |
| 7 |
|
| 8 |
|
| 9 |
static PyObject * |
| 10 |
Shoddy_increment(Shoddy *self, PyObject *unused) |
| 11 |
{ |
| 12 |
self->state++; |
| 13 |
return PyInt_FromLong(self->state); |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
static PyMethodDef Shoddy_methods[] = { |
| 18 |
{"increment", (PyCFunction)Shoddy_increment, METH_NOARGS, |
| 19 |
PyDoc_STR("increment state counter")}, |
| 20 |
{NULL, NULL}, |
| 21 |
}; |
| 22 |
|
| 23 |
static int |
| 24 |
Shoddy_init(Shoddy *self, PyObject *args, PyObject *kwds) |
| 25 |
{ |
| 26 |
if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0) |
| 27 |
return -1; |
| 28 |
self->state = 0; |
| 29 |
return 0; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
static PyTypeObject ShoddyType = { |
| 34 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 35 |
"shoddy.Shoddy", |
| 36 |
sizeof(Shoddy), |
| 37 |
0, |
| 38 |
0, |
| 39 |
0, |
| 40 |
0, |
| 41 |
0, |
| 42 |
0, |
| 43 |
0, |
| 44 |
0, |
| 45 |
0, |
| 46 |
0, |
| 47 |
0, |
| 48 |
0, |
| 49 |
0, |
| 50 |
0, |
| 51 |
0, |
| 52 |
0, |
| 53 |
Py_TPFLAGS_DEFAULT | |
| 54 |
Py_TPFLAGS_BASETYPE, |
| 55 |
0, |
| 56 |
0, |
| 57 |
0, |
| 58 |
0, |
| 59 |
0, |
| 60 |
0, |
| 61 |
0, |
| 62 |
Shoddy_methods, |
| 63 |
0, |
| 64 |
0, |
| 65 |
0, |
| 66 |
0, |
| 67 |
0, |
| 68 |
0, |
| 69 |
0, |
| 70 |
(initproc)Shoddy_init, |
| 71 |
0, |
| 72 |
0, |
| 73 |
}; |
| 74 |
|
| 75 |
PyMODINIT_FUNC |
| 76 |
initshoddy(void) |
| 77 |
{ |
| 78 |
PyObject *m; |
| 79 |
|
| 80 |
ShoddyType.tp_base = &PyList_Type; |
| 81 |
if (PyType_Ready(&ShoddyType) < 0) |
| 82 |
return; |
| 83 |
|
| 84 |
m = Py_InitModule3("shoddy", NULL, "Shoddy module"); |
| 85 |
if (m == NULL) |
| 86 |
return; |
| 87 |
|
| 88 |
Py_INCREF(&ShoddyType); |
| 89 |
PyModule_AddObject(m, "Shoddy", (PyObject *) &ShoddyType); |
| 90 |
} |