This commit was manufactured by cvs2svn to create tag 'r22a4-fork'.
[python/dscho.git] / Modules / xxsubtype.c
blobae2b619dd3f64953ead49b2b26c859c6063fdeb4
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 PyGetSetDef spamlist_getsets[] = {
59 {"state", (getter)spamlist_state_get, NULL,
60 "an int variable for demonstration purposes"},
61 {0}
64 static PyTypeObject spamlist_type = {
65 PyObject_HEAD_INIT(&PyType_Type)
67 "xxsubtype.spamlist",
68 sizeof(spamlistobject),
70 0, /* tp_dealloc */
71 0, /* tp_print */
72 0, /* tp_getattr */
73 0, /* tp_setattr */
74 0, /* tp_compare */
75 0, /* tp_repr */
76 0, /* tp_as_number */
77 0, /* tp_as_sequence */
78 0, /* tp_as_mapping */
79 0, /* tp_hash */
80 0, /* tp_call */
81 0, /* tp_str */
82 0, /* tp_getattro */
83 0, /* tp_setattro */
84 0, /* tp_as_buffer */
85 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
86 0, /* tp_doc */
87 0, /* tp_traverse */
88 0, /* tp_clear */
89 0, /* tp_richcompare */
90 0, /* tp_weaklistoffset */
91 0, /* tp_iter */
92 0, /* tp_iternext */
93 spamlist_methods, /* tp_methods */
94 0, /* tp_members */
95 spamlist_getsets, /* tp_getset */
96 &PyList_Type, /* tp_base */
97 0, /* tp_dict */
98 0, /* tp_descr_get */
99 0, /* tp_descr_set */
100 0, /* tp_dictoffset */
101 (initproc)spamlist_init, /* tp_init */
102 0, /* tp_alloc */
103 0, /* tp_new */
106 /* spamdict -- a dict subtype */
108 typedef struct {
109 PyDictObject dict;
110 int state;
111 } spamdictobject;
113 static PyObject *
114 spamdict_getstate(spamdictobject *self, PyObject *args)
116 if (!PyArg_ParseTuple(args, ":getstate"))
117 return NULL;
118 return PyInt_FromLong(self->state);
121 static PyObject *
122 spamdict_setstate(spamdictobject *self, PyObject *args)
124 int state;
126 if (!PyArg_ParseTuple(args, "i:setstate", &state))
127 return NULL;
128 self->state = state;
129 Py_INCREF(Py_None);
130 return Py_None;
133 static PyMethodDef spamdict_methods[] = {
134 {"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
135 "getstate() -> state"},
136 {"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
137 "setstate(state)"},
138 {NULL, NULL},
141 staticforward PyTypeObject spamdict_type;
143 static int
144 spamdict_init(spamdictobject *self, PyObject *args, PyObject *kwds)
146 if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
147 return -1;
148 self->state = 0;
149 return 0;
152 static PyMemberDef spamdict_members[] = {
153 {"state", T_INT, offsetof(spamdictobject, state), READONLY,
154 "an int variable for demonstration purposes"},
158 static PyTypeObject spamdict_type = {
159 PyObject_HEAD_INIT(&PyType_Type)
161 "xxsubtype.spamdict",
162 sizeof(spamdictobject),
164 0, /* tp_dealloc */
165 0, /* tp_print */
166 0, /* tp_getattr */
167 0, /* tp_setattr */
168 0, /* tp_compare */
169 0, /* tp_repr */
170 0, /* tp_as_number */
171 0, /* tp_as_sequence */
172 0, /* tp_as_mapping */
173 0, /* tp_hash */
174 0, /* tp_call */
175 0, /* tp_str */
176 0, /* tp_getattro */
177 0, /* tp_setattro */
178 0, /* tp_as_buffer */
179 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
180 0, /* tp_doc */
181 0, /* tp_traverse */
182 0, /* tp_clear */
183 0, /* tp_richcompare */
184 0, /* tp_weaklistoffset */
185 0, /* tp_iter */
186 0, /* tp_iternext */
187 spamdict_methods, /* tp_methods */
188 spamdict_members, /* tp_members */
189 0, /* tp_getset */
190 &PyDict_Type, /* tp_base */
191 0, /* tp_dict */
192 0, /* tp_descr_get */
193 0, /* tp_descr_set */
194 0, /* tp_dictoffset */
195 (initproc)spamdict_init, /* tp_init */
196 0, /* tp_alloc */
197 0, /* tp_new */
200 PyObject *
201 spam_bench(PyObject *self, PyObject *args)
203 PyObject *obj, *name, *res;
204 int n = 1000;
205 time_t t0, t1;
207 if (!PyArg_ParseTuple(args, "OS|i", &obj, &name, &n))
208 return NULL;
209 t0 = clock();
210 while (--n >= 0) {
211 res = PyObject_GetAttr(obj, name);
212 if (res == NULL)
213 return NULL;
214 Py_DECREF(res);
216 t1 = clock();
217 return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
220 static PyMethodDef xxsubtype_functions[] = {
221 {"bench", spam_bench, METH_VARARGS},
222 {NULL, NULL} /* sentinel */
225 DL_EXPORT(void)
226 initxxsubtype(void)
228 PyObject *m, *d;
230 m = Py_InitModule("xxsubtype", xxsubtype_functions);
231 if (m == NULL)
232 return;
234 if (PyType_Ready(&spamlist_type) < 0)
235 return;
236 if (PyType_Ready(&spamdict_type) < 0)
237 return;
239 d = PyModule_GetDict(m);
240 if (d == NULL)
241 return;
243 Py_INCREF(&spamlist_type);
244 if (PyDict_SetItemString(d, "spamlist",
245 (PyObject *) &spamlist_type) < 0)
246 return;
248 Py_INCREF(&spamdict_type);
249 if (PyDict_SetItemString(d, "spamdict",
250 (PyObject *) &spamdict_type) < 0)
251 return;