blob: 91ba0f1204b35663c46faa5bac15044eabebac58 [file] [log] [blame]
Tim Peters6d6c1a32001-08-02 04:15:00 +00001#include "Python.h"
Guido van Rossume6b90ea2001-08-15 18:09:11 +00002#include "structmember.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00003
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004PyDoc_STRVAR(xxsubtype__doc__,
Tim Peters65760b22001-12-10 22:53:30 +00005"xxsubtype is an example module showing how to subtype builtin types from C.\n"
6"test_descr.py in the standard test suite requires it in order to complete.\n"
7"If you don't care about the examples, and don't intend to run the Python\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008"test suite, you can recompile Python without Modules/xxsubtype.c.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00009
Tim Petersdd5c05f2001-12-17 01:27:01 +000010/* We link this module statically for convenience. If compiled as a shared
11 library instead, some compilers don't allow addresses of Python objects
12 defined in other libraries to be used in static initializers here. The
13 DEFERRED_ADDRESS macro is used to tag the slots where such addresses
14 appear; the module init function must fill in the tagged slots at runtime.
15 The argument is for documentation -- the macro ignores it.
16*/
17#define DEFERRED_ADDRESS(ADDR) 0
18
Tim Peters6d6c1a32001-08-02 04:15:00 +000019/* spamlist -- a list subtype */
20
21typedef struct {
22 PyListObject list;
23 int state;
24} spamlistobject;
25
26static PyObject *
27spamlist_getstate(spamlistobject *self, PyObject *args)
28{
29 if (!PyArg_ParseTuple(args, ":getstate"))
30 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +000031 return PyLong_FromLong(self->state);
Tim Peters6d6c1a32001-08-02 04:15:00 +000032}
33
34static PyObject *
35spamlist_setstate(spamlistobject *self, PyObject *args)
36{
37 int state;
38
39 if (!PyArg_ParseTuple(args, "i:setstate", &state))
40 return NULL;
41 self->state = state;
42 Py_INCREF(Py_None);
43 return Py_None;
44}
45
Fred Drakef841aa62002-03-28 15:49:54 +000046static PyObject *
47spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)
48{
49 PyObject *result = PyTuple_New(3);
50
51 if (result != NULL) {
52 if (self == NULL)
53 self = Py_None;
54 if (kw == NULL)
55 kw = Py_None;
56 Py_INCREF(self);
57 PyTuple_SET_ITEM(result, 0, self);
58 Py_INCREF(args);
59 PyTuple_SET_ITEM(result, 1, args);
60 Py_INCREF(kw);
61 PyTuple_SET_ITEM(result, 2, kw);
62 }
63 return result;
64}
65
Tim Peters6d6c1a32001-08-02 04:15:00 +000066static PyMethodDef spamlist_methods[] = {
67 {"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
Skip Montanaro23b59182002-08-14 01:45:37 +000068 PyDoc_STR("getstate() -> state")},
Tim Peters6d6c1a32001-08-02 04:15:00 +000069 {"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
Skip Montanaro23b59182002-08-14 01:45:37 +000070 PyDoc_STR("setstate(state)")},
Fred Drakef841aa62002-03-28 15:49:54 +000071 /* These entries differ only in the flags; they are used by the tests
72 in test.test_descr. */
73 {"classmeth", (PyCFunction)spamlist_specialmeth,
74 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
Skip Montanaro23b59182002-08-14 01:45:37 +000075 PyDoc_STR("classmeth(*args, **kw)")},
Fred Drakef841aa62002-03-28 15:49:54 +000076 {"staticmeth", (PyCFunction)spamlist_specialmeth,
77 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
Skip Montanaro23b59182002-08-14 01:45:37 +000078 PyDoc_STR("staticmeth(*args, **kw)")},
Tim Peters6d6c1a32001-08-02 04:15:00 +000079 {NULL, NULL},
80};
81
Tim Peters6d6c1a32001-08-02 04:15:00 +000082static int
83spamlist_init(spamlistobject *self, PyObject *args, PyObject *kwds)
84{
85 if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)
86 return -1;
87 self->state = 0;
88 return 0;
89}
90
Guido van Rossum88dcf032001-08-15 18:18:58 +000091static PyObject *
92spamlist_state_get(spamlistobject *self)
93{
Christian Heimes217cfd12007-12-02 14:31:20 +000094 return PyLong_FromLong(self->state);
Guido van Rossum88dcf032001-08-15 18:18:58 +000095}
96
Guido van Rossum32d34c82001-09-20 21:45:26 +000097static PyGetSetDef spamlist_getsets[] = {
98 {"state", (getter)spamlist_state_get, NULL,
Skip Montanaro23b59182002-08-14 01:45:37 +000099 PyDoc_STR("an int variable for demonstration purposes")},
Guido van Rossum88dcf032001-08-15 18:18:58 +0000100 {0}
101};
102
Tim Peters6d6c1a32001-08-02 04:15:00 +0000103static PyTypeObject spamlist_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000104 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum83f56cb2001-08-16 09:10:42 +0000105 "xxsubtype.spamlist",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000106 sizeof(spamlistobject),
107 0,
108 0, /* tp_dealloc */
109 0, /* tp_print */
110 0, /* tp_getattr */
111 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000112 0, /* tp_reserved */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000113 0, /* tp_repr */
114 0, /* tp_as_number */
115 0, /* tp_as_sequence */
116 0, /* tp_as_mapping */
117 0, /* tp_hash */
118 0, /* tp_call */
119 0, /* tp_str */
120 0, /* tp_getattro */
121 0, /* tp_setattro */
122 0, /* tp_as_buffer */
123 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
124 0, /* tp_doc */
125 0, /* tp_traverse */
126 0, /* tp_clear */
127 0, /* tp_richcompare */
128 0, /* tp_weaklistoffset */
129 0, /* tp_iter */
130 0, /* tp_iternext */
131 spamlist_methods, /* tp_methods */
132 0, /* tp_members */
Guido van Rossum88dcf032001-08-15 18:18:58 +0000133 spamlist_getsets, /* tp_getset */
Tim Petersdd5c05f2001-12-17 01:27:01 +0000134 DEFERRED_ADDRESS(&PyList_Type), /* tp_base */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000135 0, /* tp_dict */
136 0, /* tp_descr_get */
137 0, /* tp_descr_set */
138 0, /* tp_dictoffset */
139 (initproc)spamlist_init, /* tp_init */
140 0, /* tp_alloc */
141 0, /* tp_new */
142};
143
144/* spamdict -- a dict subtype */
145
146typedef struct {
147 PyDictObject dict;
148 int state;
149} spamdictobject;
150
151static PyObject *
152spamdict_getstate(spamdictobject *self, PyObject *args)
153{
154 if (!PyArg_ParseTuple(args, ":getstate"))
155 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +0000156 return PyLong_FromLong(self->state);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000157}
158
159static PyObject *
160spamdict_setstate(spamdictobject *self, PyObject *args)
161{
162 int state;
163
164 if (!PyArg_ParseTuple(args, "i:setstate", &state))
165 return NULL;
166 self->state = state;
167 Py_INCREF(Py_None);
168 return Py_None;
169}
170
171static PyMethodDef spamdict_methods[] = {
172 {"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
Skip Montanaro23b59182002-08-14 01:45:37 +0000173 PyDoc_STR("getstate() -> state")},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000174 {"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
Skip Montanaro23b59182002-08-14 01:45:37 +0000175 PyDoc_STR("setstate(state)")},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000176 {NULL, NULL},
177};
178
Tim Peters6d6c1a32001-08-02 04:15:00 +0000179static int
180spamdict_init(spamdictobject *self, PyObject *args, PyObject *kwds)
181{
182 if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
183 return -1;
184 self->state = 0;
185 return 0;
186}
187
Guido van Rossum6f799372001-09-20 20:46:19 +0000188static PyMemberDef spamdict_members[] = {
189 {"state", T_INT, offsetof(spamdictobject, state), READONLY,
Skip Montanaro23b59182002-08-14 01:45:37 +0000190 PyDoc_STR("an int variable for demonstration purposes")},
Guido van Rossume6b90ea2001-08-15 18:09:11 +0000191 {0}
192};
193
Tim Peters6d6c1a32001-08-02 04:15:00 +0000194static PyTypeObject spamdict_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000195 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Guido van Rossum83f56cb2001-08-16 09:10:42 +0000196 "xxsubtype.spamdict",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000197 sizeof(spamdictobject),
198 0,
199 0, /* tp_dealloc */
200 0, /* tp_print */
201 0, /* tp_getattr */
202 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000203 0, /* tp_reserved */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000204 0, /* tp_repr */
205 0, /* tp_as_number */
206 0, /* tp_as_sequence */
207 0, /* tp_as_mapping */
208 0, /* tp_hash */
209 0, /* tp_call */
210 0, /* tp_str */
211 0, /* tp_getattro */
212 0, /* tp_setattro */
213 0, /* tp_as_buffer */
214 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
215 0, /* tp_doc */
216 0, /* tp_traverse */
217 0, /* tp_clear */
218 0, /* tp_richcompare */
219 0, /* tp_weaklistoffset */
220 0, /* tp_iter */
221 0, /* tp_iternext */
222 spamdict_methods, /* tp_methods */
Guido van Rossume6b90ea2001-08-15 18:09:11 +0000223 spamdict_members, /* tp_members */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000224 0, /* tp_getset */
Tim Petersdd5c05f2001-12-17 01:27:01 +0000225 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000226 0, /* tp_dict */
227 0, /* tp_descr_get */
228 0, /* tp_descr_set */
229 0, /* tp_dictoffset */
230 (initproc)spamdict_init, /* tp_init */
231 0, /* tp_alloc */
232 0, /* tp_new */
233};
234
Neil Schemenauer26775122001-10-21 22:26:43 +0000235static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000236spam_bench(PyObject *self, PyObject *args)
237{
238 PyObject *obj, *name, *res;
239 int n = 1000;
240 time_t t0, t1;
241
242 if (!PyArg_ParseTuple(args, "OS|i", &obj, &name, &n))
243 return NULL;
244 t0 = clock();
245 while (--n >= 0) {
246 res = PyObject_GetAttr(obj, name);
247 if (res == NULL)
248 return NULL;
249 Py_DECREF(res);
250 }
251 t1 = clock();
252 return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
253}
254
255static PyMethodDef xxsubtype_functions[] = {
256 {"bench", spam_bench, METH_VARARGS},
257 {NULL, NULL} /* sentinel */
258};
259
Martin v. Löwis1a214512008-06-11 05:26:20 +0000260static struct PyModuleDef xxsubtypemodule = {
261 PyModuleDef_HEAD_INIT,
262 "xxsubtype",
263 xxsubtype__doc__,
264 -1,
265 xxsubtype_functions,
266 NULL,
267 NULL,
268 NULL,
269 NULL
270};
271
272
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000273PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000274PyInit_xxsubtype(void)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000275{
Fred Drake1de5a722002-03-12 21:49:44 +0000276 PyObject *m;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000277
Tim Peters4befff92001-12-17 18:26:19 +0000278 /* Fill in deferred data addresses. This must be done before
279 PyType_Ready() is called. Note that PyType_Ready() automatically
280 initializes the ob.ob_type field to &PyType_Type if it's NULL,
281 so it's not necessary to fill in ob_type first. */
Tim Petersdd5c05f2001-12-17 01:27:01 +0000282 spamdict_type.tp_base = &PyDict_Type;
Guido van Rossuma7b9b3c2001-12-17 17:25:53 +0000283 if (PyType_Ready(&spamdict_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000284 return NULL;
Tim Petersdd5c05f2001-12-17 01:27:01 +0000285
Tim Petersdd5c05f2001-12-17 01:27:01 +0000286 spamlist_type.tp_base = &PyList_Type;
Guido van Rossuma7b9b3c2001-12-17 17:25:53 +0000287 if (PyType_Ready(&spamlist_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000288 return NULL;
Tim Petersdd5c05f2001-12-17 01:27:01 +0000289
Martin v. Löwis1a214512008-06-11 05:26:20 +0000290 m = PyModule_Create(&xxsubtypemodule);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000291 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000292 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000293
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000294 if (PyType_Ready(&spamlist_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000295 return NULL;
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000296 if (PyType_Ready(&spamdict_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000297 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000298
Tim Peters6d6c1a32001-08-02 04:15:00 +0000299 Py_INCREF(&spamlist_type);
Fred Drake1de5a722002-03-12 21:49:44 +0000300 if (PyModule_AddObject(m, "spamlist",
301 (PyObject *) &spamlist_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000302 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000303
304 Py_INCREF(&spamdict_type);
Fred Drake1de5a722002-03-12 21:49:44 +0000305 if (PyModule_AddObject(m, "spamdict",
306 (PyObject *) &spamdict_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000307 return NULL;
308 return m;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000309}