1 | n/a | #include <Python.h> |
---|
2 | n/a | |
---|
3 | n/a | typedef struct { |
---|
4 | n/a | PyListObject list; |
---|
5 | n/a | int state; |
---|
6 | n/a | } Shoddy; |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | static PyObject * |
---|
10 | n/a | Shoddy_increment(Shoddy *self, PyObject *unused) |
---|
11 | n/a | { |
---|
12 | n/a | self->state++; |
---|
13 | n/a | return PyLong_FromLong(self->state); |
---|
14 | n/a | } |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | static PyMethodDef Shoddy_methods[] = { |
---|
18 | n/a | {"increment", (PyCFunction)Shoddy_increment, METH_NOARGS, |
---|
19 | n/a | PyDoc_STR("increment state counter")}, |
---|
20 | n/a | {NULL, NULL}, |
---|
21 | n/a | }; |
---|
22 | n/a | |
---|
23 | n/a | static int |
---|
24 | n/a | Shoddy_init(Shoddy *self, PyObject *args, PyObject *kwds) |
---|
25 | n/a | { |
---|
26 | n/a | if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0) |
---|
27 | n/a | return -1; |
---|
28 | n/a | self->state = 0; |
---|
29 | n/a | return 0; |
---|
30 | n/a | } |
---|
31 | n/a | |
---|
32 | n/a | |
---|
33 | n/a | static PyTypeObject ShoddyType = { |
---|
34 | n/a | PyObject_HEAD_INIT(NULL) |
---|
35 | n/a | "shoddy.Shoddy", /* tp_name */ |
---|
36 | n/a | sizeof(Shoddy), /* tp_basicsize */ |
---|
37 | n/a | 0, /* tp_itemsize */ |
---|
38 | n/a | 0, /* tp_dealloc */ |
---|
39 | n/a | 0, /* tp_print */ |
---|
40 | n/a | 0, /* tp_getattr */ |
---|
41 | n/a | 0, /* tp_setattr */ |
---|
42 | n/a | 0, /* tp_reserved */ |
---|
43 | n/a | 0, /* tp_repr */ |
---|
44 | n/a | 0, /* tp_as_number */ |
---|
45 | n/a | 0, /* tp_as_sequence */ |
---|
46 | n/a | 0, /* tp_as_mapping */ |
---|
47 | n/a | 0, /* tp_hash */ |
---|
48 | n/a | 0, /* tp_call */ |
---|
49 | n/a | 0, /* tp_str */ |
---|
50 | n/a | 0, /* tp_getattro */ |
---|
51 | n/a | 0, /* tp_setattro */ |
---|
52 | n/a | 0, /* tp_as_buffer */ |
---|
53 | n/a | Py_TPFLAGS_DEFAULT | |
---|
54 | n/a | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
---|
55 | n/a | 0, /* tp_doc */ |
---|
56 | n/a | 0, /* tp_traverse */ |
---|
57 | n/a | 0, /* tp_clear */ |
---|
58 | n/a | 0, /* tp_richcompare */ |
---|
59 | n/a | 0, /* tp_weaklistoffset */ |
---|
60 | n/a | 0, /* tp_iter */ |
---|
61 | n/a | 0, /* tp_iternext */ |
---|
62 | n/a | Shoddy_methods, /* tp_methods */ |
---|
63 | n/a | 0, /* tp_members */ |
---|
64 | n/a | 0, /* tp_getset */ |
---|
65 | n/a | 0, /* tp_base */ |
---|
66 | n/a | 0, /* tp_dict */ |
---|
67 | n/a | 0, /* tp_descr_get */ |
---|
68 | n/a | 0, /* tp_descr_set */ |
---|
69 | n/a | 0, /* tp_dictoffset */ |
---|
70 | n/a | (initproc)Shoddy_init, /* tp_init */ |
---|
71 | n/a | 0, /* tp_alloc */ |
---|
72 | n/a | 0, /* tp_new */ |
---|
73 | n/a | }; |
---|
74 | n/a | |
---|
75 | n/a | static PyModuleDef shoddymodule = { |
---|
76 | n/a | PyModuleDef_HEAD_INIT, |
---|
77 | n/a | "shoddy", |
---|
78 | n/a | "Shoddy module", |
---|
79 | n/a | -1, |
---|
80 | n/a | NULL, NULL, NULL, NULL, NULL |
---|
81 | n/a | }; |
---|
82 | n/a | |
---|
83 | n/a | PyMODINIT_FUNC |
---|
84 | n/a | PyInit_shoddy(void) |
---|
85 | n/a | { |
---|
86 | n/a | PyObject *m; |
---|
87 | n/a | |
---|
88 | n/a | ShoddyType.tp_base = &PyList_Type; |
---|
89 | n/a | if (PyType_Ready(&ShoddyType) < 0) |
---|
90 | n/a | return NULL; |
---|
91 | n/a | |
---|
92 | n/a | m = PyModule_Create(&shoddymodule); |
---|
93 | n/a | if (m == NULL) |
---|
94 | n/a | return NULL; |
---|
95 | n/a | |
---|
96 | n/a | Py_INCREF(&ShoddyType); |
---|
97 | n/a | PyModule_AddObject(m, "Shoddy", (PyObject *) &ShoddyType); |
---|
98 | n/a | return m; |
---|
99 | n/a | } |
---|