2001-08-02 06:15:00 +02:00
|
|
|
#include "Python.h"
|
2001-08-15 20:09:11 +02:00
|
|
|
#include "structmember.h"
|
2001-08-02 06:15:00 +02:00
|
|
|
|
2002-06-13 22:33:02 +02:00
|
|
|
PyDoc_STRVAR(xxsubtype__doc__,
|
2001-12-10 23:53:30 +01:00
|
|
|
"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"
|
2002-06-13 22:33:02 +02:00
|
|
|
"test suite, you can recompile Python without Modules/xxsubtype.c.");
|
2001-08-02 06:15:00 +02:00
|
|
|
|
2001-12-17 02:27:01 +01:00
|
|
|
/* 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
|
|
|
|
|
2001-08-02 06:15:00 +02:00
|
|
|
/* 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;
|
2007-12-02 15:31:20 +01:00
|
|
|
return PyLong_FromLong(self->state);
|
2001-08-02 06:15:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-03-28 16:49:54 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-08-02 06:15:00 +02:00
|
|
|
static PyMethodDef spamlist_methods[] = {
|
|
|
|
{"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
|
2002-08-14 03:45:37 +02:00
|
|
|
PyDoc_STR("getstate() -> state")},
|
2001-08-02 06:15:00 +02:00
|
|
|
{"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
|
2002-08-14 03:45:37 +02:00
|
|
|
PyDoc_STR("setstate(state)")},
|
2002-03-28 16:49:54 +01:00
|
|
|
/* 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,
|
2002-08-14 03:45:37 +02:00
|
|
|
PyDoc_STR("classmeth(*args, **kw)")},
|
2002-03-28 16:49:54 +01:00
|
|
|
{"staticmeth", (PyCFunction)spamlist_specialmeth,
|
|
|
|
METH_VARARGS | METH_KEYWORDS | METH_STATIC,
|
2002-08-14 03:45:37 +02:00
|
|
|
PyDoc_STR("staticmeth(*args, **kw)")},
|
2001-08-02 06:15:00 +02:00
|
|
|
{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;
|
|
|
|
}
|
|
|
|
|
2001-08-15 20:18:58 +02:00
|
|
|
static PyObject *
|
|
|
|
spamlist_state_get(spamlistobject *self)
|
|
|
|
{
|
2007-12-02 15:31:20 +01:00
|
|
|
return PyLong_FromLong(self->state);
|
2001-08-15 20:18:58 +02:00
|
|
|
}
|
|
|
|
|
2001-09-20 23:45:26 +02:00
|
|
|
static PyGetSetDef spamlist_getsets[] = {
|
|
|
|
{"state", (getter)spamlist_state_get, NULL,
|
2002-08-14 03:45:37 +02:00
|
|
|
PyDoc_STR("an int variable for demonstration purposes")},
|
2001-08-15 20:18:58 +02:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2001-08-02 06:15:00 +02:00
|
|
|
static PyTypeObject spamlist_type = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 19:22:18 +02:00
|
|
|
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
|
2001-08-16 11:10:42 +02:00
|
|
|
"xxsubtype.spamlist",
|
2001-08-02 06:15:00 +02:00
|
|
|
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 */
|
2001-08-15 20:18:58 +02:00
|
|
|
spamlist_getsets, /* tp_getset */
|
2001-12-17 02:27:01 +01:00
|
|
|
DEFERRED_ADDRESS(&PyList_Type), /* tp_base */
|
2001-08-02 06:15:00 +02:00
|
|
|
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;
|
2007-12-02 15:31:20 +01:00
|
|
|
return PyLong_FromLong(self->state);
|
2001-08-02 06:15:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2002-08-14 03:45:37 +02:00
|
|
|
PyDoc_STR("getstate() -> state")},
|
2001-08-02 06:15:00 +02:00
|
|
|
{"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
|
2002-08-14 03:45:37 +02:00
|
|
|
PyDoc_STR("setstate(state)")},
|
2001-08-02 06:15:00 +02:00
|
|
|
{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;
|
|
|
|
}
|
|
|
|
|
2001-09-20 22:46:19 +02:00
|
|
|
static PyMemberDef spamdict_members[] = {
|
|
|
|
{"state", T_INT, offsetof(spamdictobject, state), READONLY,
|
2002-08-14 03:45:37 +02:00
|
|
|
PyDoc_STR("an int variable for demonstration purposes")},
|
2001-08-15 20:09:11 +02:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2001-08-02 06:15:00 +02:00
|
|
|
static PyTypeObject spamdict_type = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 19:22:18 +02:00
|
|
|
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
|
2001-08-16 11:10:42 +02:00
|
|
|
"xxsubtype.spamdict",
|
2001-08-02 06:15:00 +02:00
|
|
|
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 */
|
2001-08-15 20:09:11 +02:00
|
|
|
spamdict_members, /* tp_members */
|
2001-08-02 06:15:00 +02:00
|
|
|
0, /* tp_getset */
|
2001-12-17 02:27:01 +01:00
|
|
|
DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
|
2001-08-02 06:15:00 +02:00
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
2001-10-22 00:26:43 +02:00
|
|
|
static PyObject *
|
2001-08-02 06:15:00 +02:00
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
2008-06-11 07:26:20 +02:00
|
|
|
static struct PyModuleDef xxsubtypemodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"xxsubtype",
|
|
|
|
xxsubtype__doc__,
|
|
|
|
-1,
|
|
|
|
xxsubtype_functions,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-08-02 04:27:13 +02:00
|
|
|
PyMODINIT_FUNC
|
2008-06-11 07:26:20 +02:00
|
|
|
PyInit_xxsubtype(void)
|
2001-08-02 06:15:00 +02:00
|
|
|
{
|
2002-03-12 22:49:44 +01:00
|
|
|
PyObject *m;
|
2001-08-02 06:15:00 +02:00
|
|
|
|
2001-12-17 19:26:19 +01:00
|
|
|
/* 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. */
|
2001-12-17 02:27:01 +01:00
|
|
|
spamdict_type.tp_base = &PyDict_Type;
|
2001-12-17 18:25:53 +01:00
|
|
|
if (PyType_Ready(&spamdict_type) < 0)
|
2008-06-11 07:26:20 +02:00
|
|
|
return NULL;
|
2001-12-17 02:27:01 +01:00
|
|
|
|
|
|
|
spamlist_type.tp_base = &PyList_Type;
|
2001-12-17 18:25:53 +01:00
|
|
|
if (PyType_Ready(&spamlist_type) < 0)
|
2008-06-11 07:26:20 +02:00
|
|
|
return NULL;
|
2001-12-17 02:27:01 +01:00
|
|
|
|
2008-06-11 07:26:20 +02:00
|
|
|
m = PyModule_Create(&xxsubtypemodule);
|
2001-08-02 06:15:00 +02:00
|
|
|
if (m == NULL)
|
2008-06-11 07:26:20 +02:00
|
|
|
return NULL;
|
2001-08-02 06:15:00 +02:00
|
|
|
|
2001-08-07 19:24:28 +02:00
|
|
|
if (PyType_Ready(&spamlist_type) < 0)
|
2008-06-11 07:26:20 +02:00
|
|
|
return NULL;
|
2001-08-07 19:24:28 +02:00
|
|
|
if (PyType_Ready(&spamdict_type) < 0)
|
2008-06-11 07:26:20 +02:00
|
|
|
return NULL;
|
2001-08-02 06:15:00 +02:00
|
|
|
|
|
|
|
Py_INCREF(&spamlist_type);
|
2002-03-12 22:49:44 +01:00
|
|
|
if (PyModule_AddObject(m, "spamlist",
|
|
|
|
(PyObject *) &spamlist_type) < 0)
|
2008-06-11 07:26:20 +02:00
|
|
|
return NULL;
|
2001-08-02 06:15:00 +02:00
|
|
|
|
|
|
|
Py_INCREF(&spamdict_type);
|
2002-03-12 22:49:44 +01:00
|
|
|
if (PyModule_AddObject(m, "spamdict",
|
|
|
|
(PyObject *) &spamdict_type) < 0)
|
2008-06-11 07:26:20 +02:00
|
|
|
return NULL;
|
|
|
|
return m;
|
2001-08-02 06:15:00 +02:00
|
|
|
}
|