Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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++; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13 | return PyLong_FromLong(self->state); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | PyObject_HEAD_INIT(NULL) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | "shoddy.Shoddy", /* tp_name */ |
| 36 | sizeof(Shoddy), /* tp_basicsize */ |
| 37 | 0, /* tp_itemsize */ |
| 38 | 0, /* tp_dealloc */ |
| 39 | 0, /* tp_print */ |
| 40 | 0, /* tp_getattr */ |
| 41 | 0, /* tp_setattr */ |
Mark Dickinson | 9f98926 | 2009-02-02 21:29:40 +0000 | [diff] [blame] | 42 | 0, /* tp_reserved */ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | 0, /* tp_repr */ |
| 44 | 0, /* tp_as_number */ |
| 45 | 0, /* tp_as_sequence */ |
| 46 | 0, /* tp_as_mapping */ |
| 47 | 0, /* tp_hash */ |
| 48 | 0, /* tp_call */ |
| 49 | 0, /* tp_str */ |
| 50 | 0, /* tp_getattro */ |
| 51 | 0, /* tp_setattro */ |
| 52 | 0, /* tp_as_buffer */ |
| 53 | Py_TPFLAGS_DEFAULT | |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 54 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | 0, /* tp_doc */ |
| 56 | 0, /* tp_traverse */ |
| 57 | 0, /* tp_clear */ |
| 58 | 0, /* tp_richcompare */ |
| 59 | 0, /* tp_weaklistoffset */ |
| 60 | 0, /* tp_iter */ |
| 61 | 0, /* tp_iternext */ |
| 62 | Shoddy_methods, /* tp_methods */ |
| 63 | 0, /* tp_members */ |
| 64 | 0, /* tp_getset */ |
| 65 | 0, /* tp_base */ |
| 66 | 0, /* tp_dict */ |
| 67 | 0, /* tp_descr_get */ |
| 68 | 0, /* tp_descr_set */ |
| 69 | 0, /* tp_dictoffset */ |
| 70 | (initproc)Shoddy_init, /* tp_init */ |
| 71 | 0, /* tp_alloc */ |
| 72 | 0, /* tp_new */ |
| 73 | }; |
| 74 | |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 75 | static PyModuleDef shoddymodule = { |
| 76 | PyModuleDef_HEAD_INIT, |
| 77 | "shoddy", |
| 78 | "Shoddy module", |
| 79 | -1, |
| 80 | NULL, NULL, NULL, NULL, NULL |
| 81 | }; |
| 82 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | PyMODINIT_FUNC |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 84 | PyInit_shoddy(void) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | { |
| 86 | PyObject *m; |
| 87 | |
| 88 | ShoddyType.tp_base = &PyList_Type; |
| 89 | if (PyType_Ready(&ShoddyType) < 0) |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 90 | return NULL; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 92 | m = PyModule_Create(&shoddymodule); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | if (m == NULL) |
Georg Brandl | 913b2a3 | 2008-12-05 15:12:15 +0000 | [diff] [blame] | 94 | return NULL; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
| 96 | Py_INCREF(&ShoddyType); |
| 97 | PyModule_AddObject(m, "Shoddy", (PyObject *) &ShoddyType); |
Georg Brandl | 2c52e2f | 2009-09-01 08:03:26 +0000 | [diff] [blame] | 98 | return m; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | } |