New \grammartoken markup, similar to \token but allowed everywhere.
[python/dscho.git] / Modules / xxsubtype.c
blobc20a1c711403f5a112b09bc81dd68f0aa9afb654
1 #include "Python.h"
2 #include "structmember.h"
4 /* Examples showing how to subtype the builtin list and dict types from C. */
6 /* spamlist -- a list subtype */
8 typedef struct {
9 PyListObject list;
10 int state;
11 } spamlistobject;
13 static PyObject *
14 spamlist_getstate(spamlistobject *self, PyObject *args)
16 if (!PyArg_ParseTuple(args, ":getstate"))
17 return NULL;
18 return PyInt_FromLong(self->state);
21 static PyObject *
22 spamlist_setstate(spamlistobject *self, PyObject *args)
24 int state;
26 if (!PyArg_ParseTuple(args, "i:setstate", &state))
27 return NULL;
28 self->state = state;
29 Py_INCREF(Py_None);
30 return Py_None;
33 static PyMethodDef spamlist_methods[] = {
34 {"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
35 "getstate() -> state"},
36 {"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
37 "setstate(state)"},
38 {NULL, NULL},
41 staticforward PyTypeObject spamlist_type;
43 static int
44 spamlist_init(spamlistobject *self, PyObject *args, PyObject *kwds)
46 if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)
47 return -1;
48 self->state = 0;
49 return 0;
52 static PyObject *
53 spamlist_state_get(spamlistobject *self)
55 return PyInt_FromLong(self->state);
58 static struct getsetlist spamlist_getsets[] = {
59 {"state", (getter)spamlist_state_get, NULL, NULL},
60 {0}
63 static PyTypeObject spamlist_type = {
64 PyObject_HEAD_INIT(&PyType_Type)
66 "xxsubtype.spamlist",
67 sizeof(spamlistobject),
69 0, /* tp_dealloc */
70 0, /* tp_print */
71 0, /* tp_getattr */
72 0, /* tp_setattr */
73 0, /* tp_compare */
74 0, /* tp_repr */
75 0, /* tp_as_number */
76 0, /* tp_as_sequence */
77 0, /* tp_as_mapping */
78 0, /* tp_hash */
79 0, /* tp_call */
80 0, /* tp_str */
81 0, /* tp_getattro */
82 0, /* tp_setattro */
83 0, /* tp_as_buffer */
84 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
85 0, /* tp_doc */
86 0, /* tp_traverse */
87 0, /* tp_clear */
88 0, /* tp_richcompare */
89 0, /* tp_weaklistoffset */
90 0, /* tp_iter */
91 0, /* tp_iternext */
92 spamlist_methods, /* tp_methods */
93 0, /* tp_members */
94 spamlist_getsets, /* tp_getset */
95 &PyList_Type, /* tp_base */
96 0, /* tp_dict */
97 0, /* tp_descr_get */
98 0, /* tp_descr_set */
99 0, /* tp_dictoffset */
100 (initproc)spamlist_init, /* tp_init */
101 0, /* tp_alloc */
102 0, /* tp_new */
105 /* spamdict -- a dict subtype */
107 typedef struct {
108 PyDictObject dict;
109 int state;
110 } spamdictobject;
112 static PyObject *
113 spamdict_getstate(spamdictobject *self, PyObject *args)
115 if (!PyArg_ParseTuple(args, ":getstate"))
116 return NULL;
117 return PyInt_FromLong(self->state);
120 static PyObject *
121 spamdict_setstate(spamdictobject *self, PyObject *args)
123 int state;
125 if (!PyArg_ParseTuple(args, "i:setstate", &state))
126 return NULL;
127 self->state = state;
128 Py_INCREF(Py_None);
129 return Py_None;
132 static PyMethodDef spamdict_methods[] = {
133 {"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
134 "getstate() -> state"},
135 {"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
136 "setstate(state)"},
137 {NULL, NULL},
140 staticforward PyTypeObject spamdict_type;
142 static int
143 spamdict_init(spamdictobject *self, PyObject *args, PyObject *kwds)
145 if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
146 return -1;
147 self->state = 0;
148 return 0;
151 static struct memberlist spamdict_members[] = {
152 {"state", T_INT, offsetof(spamdictobject, state), READONLY},
156 static PyTypeObject spamdict_type = {
157 PyObject_HEAD_INIT(&PyType_Type)
159 "xxsubtype.spamdict",
160 sizeof(spamdictobject),
162 0, /* tp_dealloc */
163 0, /* tp_print */
164 0, /* tp_getattr */
165 0, /* tp_setattr */
166 0, /* tp_compare */
167 0, /* tp_repr */
168 0, /* tp_as_number */
169 0, /* tp_as_sequence */
170 0, /* tp_as_mapping */
171 0, /* tp_hash */
172 0, /* tp_call */
173 0, /* tp_str */
174 0, /* tp_getattro */
175 0, /* tp_setattro */
176 0, /* tp_as_buffer */
177 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
178 0, /* tp_doc */
179 0, /* tp_traverse */
180 0, /* tp_clear */
181 0, /* tp_richcompare */
182 0, /* tp_weaklistoffset */
183 0, /* tp_iter */
184 0, /* tp_iternext */
185 spamdict_methods, /* tp_methods */
186 spamdict_members, /* tp_members */
187 0, /* tp_getset */
188 &PyDict_Type, /* tp_base */
189 0, /* tp_dict */
190 0, /* tp_descr_get */
191 0, /* tp_descr_set */
192 0, /* tp_dictoffset */
193 (initproc)spamdict_init, /* tp_init */
194 0, /* tp_alloc */
195 0, /* tp_new */
198 PyObject *
199 spam_bench(PyObject *self, PyObject *args)
201 PyObject *obj, *name, *res;
202 int n = 1000;
203 time_t t0, t1;
205 if (!PyArg_ParseTuple(args, "OS|i", &obj, &name, &n))
206 return NULL;
207 t0 = clock();
208 while (--n >= 0) {
209 res = PyObject_GetAttr(obj, name);
210 if (res == NULL)
211 return NULL;
212 Py_DECREF(res);
214 t1 = clock();
215 return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
218 static PyMethodDef xxsubtype_functions[] = {
219 {"bench", spam_bench, METH_VARARGS},
220 {NULL, NULL} /* sentinel */
223 DL_EXPORT(void)
224 initxxsubtype(void)
226 PyObject *m, *d;
228 m = Py_InitModule("xxsubtype", xxsubtype_functions);
229 if (m == NULL)
230 return;
232 if (PyType_Ready(&spamlist_type) < 0)
233 return;
234 if (PyType_Ready(&spamdict_type) < 0)
235 return;
237 d = PyModule_GetDict(m);
238 if (d == NULL)
239 return;
241 Py_INCREF(&spamlist_type);
242 if (PyDict_SetItemString(d, "spamlist",
243 (PyObject *) &spamlist_type) < 0)
244 return;
246 Py_INCREF(&spamdict_type);
247 if (PyDict_SetItemString(d, "spamdict",
248 (PyObject *) &spamdict_type) < 0)
249 return;