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