]>
Commit | Line | Data |
---|---|---|
7eb75bcc DM |
1 | #include "Python.h"\r |
2 | #include "structmember.h"\r | |
3 | \r | |
4 | PyDoc_STRVAR(xxsubtype__doc__,\r | |
5 | "xxsubtype is an example module showing how to subtype builtin types from C.\n"\r | |
6 | "test_descr.py in the standard test suite requires it in order to complete.\n"\r | |
7 | "If you don't care about the examples, and don't intend to run the Python\n"\r | |
8 | "test suite, you can recompile Python without Modules/xxsubtype.c.");\r | |
9 | \r | |
10 | /* We link this module statically for convenience. If compiled as a shared\r | |
11 | library instead, some compilers don't allow addresses of Python objects\r | |
12 | defined in other libraries to be used in static initializers here. The\r | |
13 | DEFERRED_ADDRESS macro is used to tag the slots where such addresses\r | |
14 | appear; the module init function must fill in the tagged slots at runtime.\r | |
15 | The argument is for documentation -- the macro ignores it.\r | |
16 | */\r | |
17 | #define DEFERRED_ADDRESS(ADDR) 0\r | |
18 | \r | |
19 | /* spamlist -- a list subtype */\r | |
20 | \r | |
21 | typedef struct {\r | |
22 | PyListObject list;\r | |
23 | int state;\r | |
24 | } spamlistobject;\r | |
25 | \r | |
26 | static PyObject *\r | |
27 | spamlist_getstate(spamlistobject *self, PyObject *args)\r | |
28 | {\r | |
29 | if (!PyArg_ParseTuple(args, ":getstate"))\r | |
30 | return NULL;\r | |
31 | return PyInt_FromLong(self->state);\r | |
32 | }\r | |
33 | \r | |
34 | static PyObject *\r | |
35 | spamlist_setstate(spamlistobject *self, PyObject *args)\r | |
36 | {\r | |
37 | int state;\r | |
38 | \r | |
39 | if (!PyArg_ParseTuple(args, "i:setstate", &state))\r | |
40 | return NULL;\r | |
41 | self->state = state;\r | |
42 | Py_INCREF(Py_None);\r | |
43 | return Py_None;\r | |
44 | }\r | |
45 | \r | |
46 | static PyObject *\r | |
47 | spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)\r | |
48 | {\r | |
49 | PyObject *result = PyTuple_New(3);\r | |
50 | \r | |
51 | if (result != NULL) {\r | |
52 | if (self == NULL)\r | |
53 | self = Py_None;\r | |
54 | if (kw == NULL)\r | |
55 | kw = Py_None;\r | |
56 | Py_INCREF(self);\r | |
57 | PyTuple_SET_ITEM(result, 0, self);\r | |
58 | Py_INCREF(args);\r | |
59 | PyTuple_SET_ITEM(result, 1, args);\r | |
60 | Py_INCREF(kw);\r | |
61 | PyTuple_SET_ITEM(result, 2, kw);\r | |
62 | }\r | |
63 | return result;\r | |
64 | }\r | |
65 | \r | |
66 | static PyMethodDef spamlist_methods[] = {\r | |
67 | {"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,\r | |
68 | PyDoc_STR("getstate() -> state")},\r | |
69 | {"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,\r | |
70 | PyDoc_STR("setstate(state)")},\r | |
71 | /* These entries differ only in the flags; they are used by the tests\r | |
72 | in test.test_descr. */\r | |
73 | {"classmeth", (PyCFunction)spamlist_specialmeth,\r | |
74 | METH_VARARGS | METH_KEYWORDS | METH_CLASS,\r | |
75 | PyDoc_STR("classmeth(*args, **kw)")},\r | |
76 | {"staticmeth", (PyCFunction)spamlist_specialmeth,\r | |
77 | METH_VARARGS | METH_KEYWORDS | METH_STATIC,\r | |
78 | PyDoc_STR("staticmeth(*args, **kw)")},\r | |
79 | {NULL, NULL},\r | |
80 | };\r | |
81 | \r | |
82 | static int\r | |
83 | spamlist_init(spamlistobject *self, PyObject *args, PyObject *kwds)\r | |
84 | {\r | |
85 | if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)\r | |
86 | return -1;\r | |
87 | self->state = 0;\r | |
88 | return 0;\r | |
89 | }\r | |
90 | \r | |
91 | static PyObject *\r | |
92 | spamlist_state_get(spamlistobject *self)\r | |
93 | {\r | |
94 | return PyInt_FromLong(self->state);\r | |
95 | }\r | |
96 | \r | |
97 | static PyGetSetDef spamlist_getsets[] = {\r | |
98 | {"state", (getter)spamlist_state_get, NULL,\r | |
99 | PyDoc_STR("an int variable for demonstration purposes")},\r | |
100 | {0}\r | |
101 | };\r | |
102 | \r | |
103 | static PyTypeObject spamlist_type = {\r | |
104 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)\r | |
105 | "xxsubtype.spamlist",\r | |
106 | sizeof(spamlistobject),\r | |
107 | 0,\r | |
108 | 0, /* tp_dealloc */\r | |
109 | 0, /* tp_print */\r | |
110 | 0, /* tp_getattr */\r | |
111 | 0, /* tp_setattr */\r | |
112 | 0, /* tp_compare */\r | |
113 | 0, /* tp_repr */\r | |
114 | 0, /* tp_as_number */\r | |
115 | 0, /* tp_as_sequence */\r | |
116 | 0, /* tp_as_mapping */\r | |
117 | 0, /* tp_hash */\r | |
118 | 0, /* tp_call */\r | |
119 | 0, /* tp_str */\r | |
120 | 0, /* tp_getattro */\r | |
121 | 0, /* tp_setattro */\r | |
122 | 0, /* tp_as_buffer */\r | |
123 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */\r | |
124 | 0, /* tp_doc */\r | |
125 | 0, /* tp_traverse */\r | |
126 | 0, /* tp_clear */\r | |
127 | 0, /* tp_richcompare */\r | |
128 | 0, /* tp_weaklistoffset */\r | |
129 | 0, /* tp_iter */\r | |
130 | 0, /* tp_iternext */\r | |
131 | spamlist_methods, /* tp_methods */\r | |
132 | 0, /* tp_members */\r | |
133 | spamlist_getsets, /* tp_getset */\r | |
134 | DEFERRED_ADDRESS(&PyList_Type), /* tp_base */\r | |
135 | 0, /* tp_dict */\r | |
136 | 0, /* tp_descr_get */\r | |
137 | 0, /* tp_descr_set */\r | |
138 | 0, /* tp_dictoffset */\r | |
139 | (initproc)spamlist_init, /* tp_init */\r | |
140 | 0, /* tp_alloc */\r | |
141 | 0, /* tp_new */\r | |
142 | };\r | |
143 | \r | |
144 | /* spamdict -- a dict subtype */\r | |
145 | \r | |
146 | typedef struct {\r | |
147 | PyDictObject dict;\r | |
148 | int state;\r | |
149 | } spamdictobject;\r | |
150 | \r | |
151 | static PyObject *\r | |
152 | spamdict_getstate(spamdictobject *self, PyObject *args)\r | |
153 | {\r | |
154 | if (!PyArg_ParseTuple(args, ":getstate"))\r | |
155 | return NULL;\r | |
156 | return PyInt_FromLong(self->state);\r | |
157 | }\r | |
158 | \r | |
159 | static PyObject *\r | |
160 | spamdict_setstate(spamdictobject *self, PyObject *args)\r | |
161 | {\r | |
162 | int state;\r | |
163 | \r | |
164 | if (!PyArg_ParseTuple(args, "i:setstate", &state))\r | |
165 | return NULL;\r | |
166 | self->state = state;\r | |
167 | Py_INCREF(Py_None);\r | |
168 | return Py_None;\r | |
169 | }\r | |
170 | \r | |
171 | static PyMethodDef spamdict_methods[] = {\r | |
172 | {"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,\r | |
173 | PyDoc_STR("getstate() -> state")},\r | |
174 | {"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,\r | |
175 | PyDoc_STR("setstate(state)")},\r | |
176 | {NULL, NULL},\r | |
177 | };\r | |
178 | \r | |
179 | static int\r | |
180 | spamdict_init(spamdictobject *self, PyObject *args, PyObject *kwds)\r | |
181 | {\r | |
182 | if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)\r | |
183 | return -1;\r | |
184 | self->state = 0;\r | |
185 | return 0;\r | |
186 | }\r | |
187 | \r | |
188 | static PyMemberDef spamdict_members[] = {\r | |
189 | {"state", T_INT, offsetof(spamdictobject, state), READONLY,\r | |
190 | PyDoc_STR("an int variable for demonstration purposes")},\r | |
191 | {0}\r | |
192 | };\r | |
193 | \r | |
194 | static PyTypeObject spamdict_type = {\r | |
195 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)\r | |
196 | "xxsubtype.spamdict",\r | |
197 | sizeof(spamdictobject),\r | |
198 | 0,\r | |
199 | 0, /* tp_dealloc */\r | |
200 | 0, /* tp_print */\r | |
201 | 0, /* tp_getattr */\r | |
202 | 0, /* tp_setattr */\r | |
203 | 0, /* tp_compare */\r | |
204 | 0, /* tp_repr */\r | |
205 | 0, /* tp_as_number */\r | |
206 | 0, /* tp_as_sequence */\r | |
207 | 0, /* tp_as_mapping */\r | |
208 | 0, /* tp_hash */\r | |
209 | 0, /* tp_call */\r | |
210 | 0, /* tp_str */\r | |
211 | 0, /* tp_getattro */\r | |
212 | 0, /* tp_setattro */\r | |
213 | 0, /* tp_as_buffer */\r | |
214 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */\r | |
215 | 0, /* tp_doc */\r | |
216 | 0, /* tp_traverse */\r | |
217 | 0, /* tp_clear */\r | |
218 | 0, /* tp_richcompare */\r | |
219 | 0, /* tp_weaklistoffset */\r | |
220 | 0, /* tp_iter */\r | |
221 | 0, /* tp_iternext */\r | |
222 | spamdict_methods, /* tp_methods */\r | |
223 | spamdict_members, /* tp_members */\r | |
224 | 0, /* tp_getset */\r | |
225 | DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */\r | |
226 | 0, /* tp_dict */\r | |
227 | 0, /* tp_descr_get */\r | |
228 | 0, /* tp_descr_set */\r | |
229 | 0, /* tp_dictoffset */\r | |
230 | (initproc)spamdict_init, /* tp_init */\r | |
231 | 0, /* tp_alloc */\r | |
232 | 0, /* tp_new */\r | |
233 | };\r | |
234 | \r | |
235 | static PyObject *\r | |
236 | spam_bench(PyObject *self, PyObject *args)\r | |
237 | {\r | |
238 | PyObject *obj, *name, *res;\r | |
239 | int n = 1000;\r | |
240 | time_t t0, t1;\r | |
241 | \r | |
242 | if (!PyArg_ParseTuple(args, "OS|i", &obj, &name, &n))\r | |
243 | return NULL;\r | |
244 | t0 = clock();\r | |
245 | while (--n >= 0) {\r | |
246 | res = PyObject_GetAttr(obj, name);\r | |
247 | if (res == NULL)\r | |
248 | return NULL;\r | |
249 | Py_DECREF(res);\r | |
250 | }\r | |
251 | t1 = clock();\r | |
252 | return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);\r | |
253 | }\r | |
254 | \r | |
255 | static PyMethodDef xxsubtype_functions[] = {\r | |
256 | {"bench", spam_bench, METH_VARARGS},\r | |
257 | {NULL, NULL} /* sentinel */\r | |
258 | };\r | |
259 | \r | |
260 | PyMODINIT_FUNC\r | |
261 | initxxsubtype(void)\r | |
262 | {\r | |
263 | PyObject *m;\r | |
264 | \r | |
265 | /* Fill in deferred data addresses. This must be done before\r | |
266 | PyType_Ready() is called. Note that PyType_Ready() automatically\r | |
267 | initializes the ob.ob_type field to &PyType_Type if it's NULL,\r | |
268 | so it's not necessary to fill in ob_type first. */\r | |
269 | spamdict_type.tp_base = &PyDict_Type;\r | |
270 | if (PyType_Ready(&spamdict_type) < 0)\r | |
271 | return;\r | |
272 | \r | |
273 | spamlist_type.tp_base = &PyList_Type;\r | |
274 | if (PyType_Ready(&spamlist_type) < 0)\r | |
275 | return;\r | |
276 | \r | |
277 | m = Py_InitModule3("xxsubtype",\r | |
278 | xxsubtype_functions,\r | |
279 | xxsubtype__doc__);\r | |
280 | if (m == NULL)\r | |
281 | return;\r | |
282 | \r | |
283 | if (PyType_Ready(&spamlist_type) < 0)\r | |
284 | return;\r | |
285 | if (PyType_Ready(&spamdict_type) < 0)\r | |
286 | return;\r | |
287 | \r | |
288 | Py_INCREF(&spamlist_type);\r | |
289 | if (PyModule_AddObject(m, "spamlist",\r | |
290 | (PyObject *) &spamlist_type) < 0)\r | |
291 | return;\r | |
292 | \r | |
293 | Py_INCREF(&spamdict_type);\r | |
294 | if (PyModule_AddObject(m, "spamdict",\r | |
295 | (PyObject *) &spamdict_type) < 0)\r | |
296 | return;\r | |
297 | }\r |