2 #include "structmember.h"
4 /* Examples showing how to subtype the builtin list and dict types from C. */
6 /* spamlist -- a list subtype */
14 spamlist_getstate(spamlistobject
*self
, PyObject
*args
)
16 if (!PyArg_ParseTuple(args
, ":getstate"))
18 return PyInt_FromLong(self
->state
);
22 spamlist_setstate(spamlistobject
*self
, PyObject
*args
)
26 if (!PyArg_ParseTuple(args
, "i:setstate", &state
))
33 static PyMethodDef spamlist_methods
[] = {
34 {"getstate", (PyCFunction
)spamlist_getstate
, METH_VARARGS
,
35 "getstate() -> state"},
36 {"setstate", (PyCFunction
)spamlist_setstate
, METH_VARARGS
,
41 staticforward PyTypeObject spamlist_type
;
44 spamlist_init(spamlistobject
*self
, PyObject
*args
, PyObject
*kwds
)
46 if (PyList_Type
.tp_init((PyObject
*)self
, args
, kwds
) < 0)
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
},
63 static PyTypeObject spamlist_type
= {
64 PyObject_HEAD_INIT(&PyType_Type
)
67 sizeof(spamlistobject
),
76 0, /* tp_as_sequence */
77 0, /* tp_as_mapping */
84 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
88 0, /* tp_richcompare */
89 0, /* tp_weaklistoffset */
92 spamlist_methods
, /* tp_methods */
94 spamlist_getsets
, /* tp_getset */
95 &PyList_Type
, /* tp_base */
99 0, /* tp_dictoffset */
100 (initproc
)spamlist_init
, /* tp_init */
105 /* spamdict -- a dict subtype */
113 spamdict_getstate(spamdictobject
*self
, PyObject
*args
)
115 if (!PyArg_ParseTuple(args
, ":getstate"))
117 return PyInt_FromLong(self
->state
);
121 spamdict_setstate(spamdictobject
*self
, PyObject
*args
)
125 if (!PyArg_ParseTuple(args
, "i:setstate", &state
))
132 static PyMethodDef spamdict_methods
[] = {
133 {"getstate", (PyCFunction
)spamdict_getstate
, METH_VARARGS
,
134 "getstate() -> state"},
135 {"setstate", (PyCFunction
)spamdict_setstate
, METH_VARARGS
,
140 staticforward PyTypeObject spamdict_type
;
143 spamdict_init(spamdictobject
*self
, PyObject
*args
, PyObject
*kwds
)
145 if (PyDict_Type
.tp_init((PyObject
*)self
, args
, kwds
) < 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
),
168 0, /* tp_as_number */
169 0, /* tp_as_sequence */
170 0, /* tp_as_mapping */
176 0, /* tp_as_buffer */
177 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
181 0, /* tp_richcompare */
182 0, /* tp_weaklistoffset */
185 spamdict_methods
, /* tp_methods */
186 spamdict_members
, /* tp_members */
188 &PyDict_Type
, /* tp_base */
190 0, /* tp_descr_get */
191 0, /* tp_descr_set */
192 0, /* tp_dictoffset */
193 (initproc
)spamdict_init
, /* tp_init */
199 spam_bench(PyObject
*self
, PyObject
*args
)
201 PyObject
*obj
, *name
, *res
;
205 if (!PyArg_ParseTuple(args
, "OS|i", &obj
, &name
, &n
))
209 res
= PyObject_GetAttr(obj
, name
);
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 */
228 m
= Py_InitModule("xxsubtype", xxsubtype_functions
);
232 if (PyType_Ready(&spamlist_type
) < 0)
234 if (PyType_Ready(&spamdict_type
) < 0)
237 d
= PyModule_GetDict(m
);
241 Py_INCREF(&spamlist_type
);
242 if (PyDict_SetItemString(d
, "spamlist",
243 (PyObject
*) &spamlist_type
) < 0)
246 Py_INCREF(&spamdict_type
);
247 if (PyDict_SetItemString(d
, "spamdict",
248 (PyObject
*) &spamdict_type
) < 0)