xxsubtype.c 7.42 KB
Newer Older
1
#include "Python.h"
2
#include "structmember.h"
3

4
PyDoc_STRVAR(xxsubtype__doc__,
5 6 7
"xxsubtype is an example module showing how to subtype builtin types from C.\n"
"test_descr.py in the standard test suite requires it in order to complete.\n"
"If you don't care about the examples, and don't intend to run the Python\n"
8
"test suite, you can recompile Python without Modules/xxsubtype.c.");
9

10 11 12 13 14 15 16 17 18
/* We link this module statically for convenience.  If compiled as a shared
   library instead, some compilers don't allow addresses of Python objects
   defined in other libraries to be used in static initializers here.  The
   DEFERRED_ADDRESS macro is used to tag the slots where such addresses
   appear; the module init function must fill in the tagged slots at runtime.
   The argument is for documentation -- the macro ignores it.
*/
#define DEFERRED_ADDRESS(ADDR) 0

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/* spamlist -- a list subtype */

typedef struct {
	PyListObject list;
	int state;
} spamlistobject;

static PyObject *
spamlist_getstate(spamlistobject *self, PyObject *args)
{
	if (!PyArg_ParseTuple(args, ":getstate"))
		return NULL;
	return PyInt_FromLong(self->state);
}

static PyObject *
spamlist_setstate(spamlistobject *self, PyObject *args)
{
	int state;

	if (!PyArg_ParseTuple(args, "i:setstate", &state))
		return NULL;
	self->state = state;
	Py_INCREF(Py_None);
	return Py_None;
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
static PyObject *
spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)
{
	PyObject *result = PyTuple_New(3);

	if (result != NULL) {
		if (self == NULL)
			self = Py_None;
		if (kw == NULL)
			kw = Py_None;
		Py_INCREF(self);
		PyTuple_SET_ITEM(result, 0, self);
		Py_INCREF(args);
		PyTuple_SET_ITEM(result, 1, args);
		Py_INCREF(kw);
		PyTuple_SET_ITEM(result, 2, kw);
	}
	return result;
}

66 67
static PyMethodDef spamlist_methods[] = {
	{"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
68
	 	PyDoc_STR("getstate() -> state")},
69
	{"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
70
	 	PyDoc_STR("setstate(state)")},
71 72 73 74
	/* These entries differ only in the flags; they are used by the tests
	   in test.test_descr. */
	{"classmeth", (PyCFunction)spamlist_specialmeth,
		METH_VARARGS | METH_KEYWORDS | METH_CLASS,
75
	 	PyDoc_STR("classmeth(*args, **kw)")},
76 77
	{"staticmeth", (PyCFunction)spamlist_specialmeth,
		METH_VARARGS | METH_KEYWORDS | METH_STATIC,
78
	 	PyDoc_STR("staticmeth(*args, **kw)")},
79 80 81 82 83 84 85 86 87 88 89 90
	{NULL,	NULL},
};

static int
spamlist_init(spamlistobject *self, PyObject *args, PyObject *kwds)
{
	if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)
		return -1;
	self->state = 0;
	return 0;
}

91 92 93 94 95 96
static PyObject *
spamlist_state_get(spamlistobject *self)
{
	return PyInt_FromLong(self->state);
}

97 98
static PyGetSetDef spamlist_getsets[] = {
	{"state", (getter)spamlist_state_get, NULL,
99
	 PyDoc_STR("an int variable for demonstration purposes")},
100 101 102
	{0}
};

103
static PyTypeObject spamlist_type = {
104
	PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
105
	"xxsubtype.spamlist",
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	sizeof(spamlistobject),
	0,
	0,					/* tp_dealloc */
	0,					/* tp_print */
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	0,					/* tp_compare */
	0,					/* tp_repr */
	0,					/* tp_as_number */
	0,					/* tp_as_sequence */
	0,					/* tp_as_mapping */
	0,					/* tp_hash */
	0,					/* tp_call */
	0,					/* tp_str */
	0,					/* tp_getattro */
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
	0,					/* tp_doc */
	0,					/* tp_traverse */
	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	spamlist_methods,			/* tp_methods */
	0,					/* tp_members */
133
	spamlist_getsets,			/* tp_getset */
134
	DEFERRED_ADDRESS(&PyList_Type),		/* tp_base */
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	(initproc)spamlist_init,		/* tp_init */
	0,					/* tp_alloc */
	0,					/* tp_new */
};

/* spamdict -- a dict subtype */

typedef struct {
	PyDictObject dict;
	int state;
} spamdictobject;

static PyObject *
spamdict_getstate(spamdictobject *self, PyObject *args)
{
	if (!PyArg_ParseTuple(args, ":getstate"))
		return NULL;
	return PyInt_FromLong(self->state);
}

static PyObject *
spamdict_setstate(spamdictobject *self, PyObject *args)
{
	int state;

	if (!PyArg_ParseTuple(args, "i:setstate", &state))
		return NULL;
	self->state = state;
	Py_INCREF(Py_None);
	return Py_None;
}

static PyMethodDef spamdict_methods[] = {
	{"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
173
	 	PyDoc_STR("getstate() -> state")},
174
	{"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
175
	 	PyDoc_STR("setstate(state)")},
176 177 178 179 180 181 182 183 184 185 186 187
	{NULL,	NULL},
};

static int
spamdict_init(spamdictobject *self, PyObject *args, PyObject *kwds)
{
	if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
		return -1;
	self->state = 0;
	return 0;
}

188 189
static PyMemberDef spamdict_members[] = {
	{"state", T_INT, offsetof(spamdictobject, state), READONLY,
190
	 PyDoc_STR("an int variable for demonstration purposes")},
191 192 193
	{0}
};

194
static PyTypeObject spamdict_type = {
195
	PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
196
	"xxsubtype.spamdict",
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	sizeof(spamdictobject),
	0,
	0,					/* tp_dealloc */
	0,					/* tp_print */
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	0,					/* tp_compare */
	0,					/* tp_repr */
	0,					/* tp_as_number */
	0,					/* tp_as_sequence */
	0,					/* tp_as_mapping */
	0,					/* tp_hash */
	0,					/* tp_call */
	0,					/* tp_str */
	0,					/* tp_getattro */
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
	0,					/* tp_doc */
	0,					/* tp_traverse */
	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	spamdict_methods,			/* tp_methods */
223
	spamdict_members,			/* tp_members */
224
	0,					/* tp_getset */
225
	DEFERRED_ADDRESS(&PyDict_Type),		/* tp_base */
226 227 228 229 230 231 232 233 234
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	(initproc)spamdict_init,		/* tp_init */
	0,					/* tp_alloc */
	0,					/* tp_new */
};

235
static PyObject *
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
spam_bench(PyObject *self, PyObject *args)
{
	PyObject *obj, *name, *res;
	int n = 1000;
	time_t t0, t1;

	if (!PyArg_ParseTuple(args, "OS|i", &obj, &name, &n))
		return NULL;
	t0 = clock();
	while (--n >= 0) {
		res = PyObject_GetAttr(obj, name);
		if (res == NULL)
			return NULL;
		Py_DECREF(res);
	}
	t1 = clock();
	return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
}

static PyMethodDef xxsubtype_functions[] = {
	{"bench",	spam_bench, 	METH_VARARGS},
	{NULL,		NULL}		/* sentinel */
};

260
PyMODINIT_FUNC
261 262
initxxsubtype(void)
{
263
	PyObject *m;
264

265 266 267 268
	/* Fill in deferred data addresses.  This must be done before
	   PyType_Ready() is called.  Note that PyType_Ready() automatically
	   initializes the ob.ob_type field to &PyType_Type if it's NULL,
	   so it's not necessary to fill in ob_type first. */
269
	spamdict_type.tp_base = &PyDict_Type;
270 271
	if (PyType_Ready(&spamdict_type) < 0)
		return;
272 273

	spamlist_type.tp_base = &PyList_Type;
274 275
	if (PyType_Ready(&spamlist_type) < 0)
		return;
276

277 278 279
	m = Py_InitModule3("xxsubtype",
			   xxsubtype_functions,
			   xxsubtype__doc__);
280 281 282
	if (m == NULL)
		return;

283
	if (PyType_Ready(&spamlist_type) < 0)
284
		return;
285
	if (PyType_Ready(&spamdict_type) < 0)
286 287 288
		return;

	Py_INCREF(&spamlist_type);
289 290
	if (PyModule_AddObject(m, "spamlist",
			       (PyObject *) &spamlist_type) < 0)
291 292 293
		return;

	Py_INCREF(&spamdict_type);
294 295
	if (PyModule_AddObject(m, "spamdict",
			       (PyObject *) &spamdict_type) < 0)
296 297
		return;
}