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 PyGetSetDef spamlist_getsets
[] = {
59 {"state", (getter
)spamlist_state_get
, NULL
,
60 "an int variable for demonstration purposes"},
64 static PyTypeObject spamlist_type
= {
65 PyObject_HEAD_INIT(&PyType_Type
)
68 sizeof(spamlistobject
),
77 0, /* tp_as_sequence */
78 0, /* tp_as_mapping */
85 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
89 0, /* tp_richcompare */
90 0, /* tp_weaklistoffset */
93 spamlist_methods
, /* tp_methods */
95 spamlist_getsets
, /* tp_getset */
96 &PyList_Type
, /* tp_base */
100 0, /* tp_dictoffset */
101 (initproc
)spamlist_init
, /* tp_init */
106 /* spamdict -- a dict subtype */
114 spamdict_getstate(spamdictobject
*self
, PyObject
*args
)
116 if (!PyArg_ParseTuple(args
, ":getstate"))
118 return PyInt_FromLong(self
->state
);
122 spamdict_setstate(spamdictobject
*self
, PyObject
*args
)
126 if (!PyArg_ParseTuple(args
, "i:setstate", &state
))
133 static PyMethodDef spamdict_methods
[] = {
134 {"getstate", (PyCFunction
)spamdict_getstate
, METH_VARARGS
,
135 "getstate() -> state"},
136 {"setstate", (PyCFunction
)spamdict_setstate
, METH_VARARGS
,
141 staticforward PyTypeObject spamdict_type
;
144 spamdict_init(spamdictobject
*self
, PyObject
*args
, PyObject
*kwds
)
146 if (PyDict_Type
.tp_init((PyObject
*)self
, args
, kwds
) < 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
),
170 0, /* tp_as_number */
171 0, /* tp_as_sequence */
172 0, /* tp_as_mapping */
178 0, /* tp_as_buffer */
179 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
183 0, /* tp_richcompare */
184 0, /* tp_weaklistoffset */
187 spamdict_methods
, /* tp_methods */
188 spamdict_members
, /* tp_members */
190 &PyDict_Type
, /* tp_base */
192 0, /* tp_descr_get */
193 0, /* tp_descr_set */
194 0, /* tp_dictoffset */
195 (initproc
)spamdict_init
, /* tp_init */
201 spam_bench(PyObject
*self
, PyObject
*args
)
203 PyObject
*obj
, *name
, *res
;
207 if (!PyArg_ParseTuple(args
, "OS|i", &obj
, &name
, &n
))
211 res
= PyObject_GetAttr(obj
, name
);
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 */
230 m
= Py_InitModule("xxsubtype", xxsubtype_functions
);
234 if (PyType_Ready(&spamlist_type
) < 0)
236 if (PyType_Ready(&spamdict_type
) < 0)
239 d
= PyModule_GetDict(m
);
243 Py_INCREF(&spamlist_type
);
244 if (PyDict_SetItemString(d
, "spamlist",
245 (PyObject
*) &spamlist_type
) < 0)
248 Py_INCREF(&spamdict_type
);
249 if (PyDict_SetItemString(d
, "spamdict",
250 (PyObject
*) &spamdict_type
) < 0)