2001-08-02 12:15:00 +08:00
|
|
|
#include "Python.h"
|
2023-09-02 23:51:19 +08:00
|
|
|
|
2023-07-25 21:28:30 +08:00
|
|
|
#include <stddef.h> // offsetof()
|
2023-09-02 23:51:19 +08:00
|
|
|
#include <time.h> // clock()
|
2023-07-25 21:28:30 +08:00
|
|
|
|
2001-08-02 12:15:00 +08:00
|
|
|
|
2002-06-14 04:33:02 +08:00
|
|
|
PyDoc_STRVAR(xxsubtype__doc__,
|
2001-12-11 06:53:30 +08: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-14 04:33:02 +08:00
|
|
|
"test suite, you can recompile Python without Modules/xxsubtype.c.");
|
2001-08-02 12:15:00 +08:00
|
|
|
|
2001-12-17 09:27:01 +08: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 12:15:00 +08:00
|
|
|
/* spamlist -- a list subtype */
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 23:52:27 +08:00
|
|
|
PyListObject list;
|
|
|
|
int state;
|
2001-08-02 12:15:00 +08:00
|
|
|
} spamlistobject;
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
spamlist_getstate(spamlistobject *self, PyObject *args)
|
|
|
|
{
|
2010-05-09 23:52:27 +08:00
|
|
|
if (!PyArg_ParseTuple(args, ":getstate"))
|
|
|
|
return NULL;
|
|
|
|
return PyLong_FromLong(self->state);
|
2001-08-02 12:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
spamlist_setstate(spamlistobject *self, PyObject *args)
|
|
|
|
{
|
2010-05-09 23:52:27 +08:00
|
|
|
int state;
|
2001-08-02 12:15:00 +08:00
|
|
|
|
2010-05-09 23:52:27 +08:00
|
|
|
if (!PyArg_ParseTuple(args, "i:setstate", &state))
|
|
|
|
return NULL;
|
|
|
|
self->state = state;
|
2022-11-14 23:21:40 +08:00
|
|
|
return Py_NewRef(Py_None);
|
2001-08-02 12:15:00 +08:00
|
|
|
}
|
|
|
|
|
2002-03-28 23:49:54 +08:00
|
|
|
static PyObject *
|
|
|
|
spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)
|
|
|
|
{
|
2010-05-09 23:52:27 +08:00
|
|
|
PyObject *result = PyTuple_New(3);
|
|
|
|
|
|
|
|
if (result != NULL) {
|
|
|
|
if (self == NULL)
|
|
|
|
self = Py_None;
|
|
|
|
if (kw == NULL)
|
|
|
|
kw = Py_None;
|
2022-11-14 23:21:40 +08:00
|
|
|
PyTuple_SET_ITEM(result, 0, Py_NewRef(self));
|
|
|
|
PyTuple_SET_ITEM(result, 1, Py_NewRef(args));
|
|
|
|
PyTuple_SET_ITEM(result, 2, Py_NewRef(kw));
|
2010-05-09 23:52:27 +08:00
|
|
|
}
|
|
|
|
return result;
|
2002-03-28 23:49:54 +08:00
|
|
|
}
|
|
|
|
|
2001-08-02 12:15:00 +08:00
|
|
|
static PyMethodDef spamlist_methods[] = {
|
2010-05-09 23:52:27 +08:00
|
|
|
{"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
|
|
|
|
PyDoc_STR("getstate() -> state")},
|
|
|
|
{"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
|
|
|
|
PyDoc_STR("setstate(state)")},
|
|
|
|
/* These entries differ only in the flags; they are used by the tests
|
|
|
|
in test.test_descr. */
|
2022-05-04 03:42:14 +08:00
|
|
|
{"classmeth", _PyCFunction_CAST(spamlist_specialmeth),
|
2010-05-09 23:52:27 +08:00
|
|
|
METH_VARARGS | METH_KEYWORDS | METH_CLASS,
|
|
|
|
PyDoc_STR("classmeth(*args, **kw)")},
|
2022-05-04 03:42:14 +08:00
|
|
|
{"staticmeth", _PyCFunction_CAST(spamlist_specialmeth),
|
2010-05-09 23:52:27 +08:00
|
|
|
METH_VARARGS | METH_KEYWORDS | METH_STATIC,
|
|
|
|
PyDoc_STR("staticmeth(*args, **kw)")},
|
|
|
|
{NULL, NULL},
|
2001-08-02 12:15:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
spamlist_init(spamlistobject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 23:52:27 +08:00
|
|
|
if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)
|
|
|
|
return -1;
|
|
|
|
self->state = 0;
|
|
|
|
return 0;
|
2001-08-02 12:15:00 +08:00
|
|
|
}
|
|
|
|
|
2001-08-16 02:18:58 +08:00
|
|
|
static PyObject *
|
2018-11-28 01:34:35 +08:00
|
|
|
spamlist_state_get(spamlistobject *self, void *Py_UNUSED(ignored))
|
2001-08-16 02:18:58 +08:00
|
|
|
{
|
2010-05-09 23:52:27 +08:00
|
|
|
return PyLong_FromLong(self->state);
|
2001-08-16 02:18:58 +08:00
|
|
|
}
|
|
|
|
|
2001-09-21 05:45:26 +08:00
|
|
|
static PyGetSetDef spamlist_getsets[] = {
|
2010-05-09 23:52:27 +08:00
|
|
|
{"state", (getter)spamlist_state_get, NULL,
|
|
|
|
PyDoc_STR("an int variable for demonstration purposes")},
|
|
|
|
{0}
|
2001-08-16 02:18:58 +08:00
|
|
|
};
|
|
|
|
|
2001-08-02 12:15:00 +08:00
|
|
|
static PyTypeObject spamlist_type = {
|
2010-05-09 23:52:27 +08:00
|
|
|
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
|
|
|
|
"xxsubtype.spamlist",
|
|
|
|
sizeof(spamlistobject),
|
|
|
|
0,
|
|
|
|
0, /* tp_dealloc */
|
2019-05-31 10:13:39 +08:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 23:52:27 +08:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-31 10:13:39 +08:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 23:52:27 +08:00
|
|
|
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 */
|
|
|
|
spamlist_getsets, /* tp_getset */
|
|
|
|
DEFERRED_ADDRESS(&PyList_Type), /* tp_base */
|
|
|
|
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 */
|
2001-08-02 12:15:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* spamdict -- a dict subtype */
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 23:52:27 +08:00
|
|
|
PyDictObject dict;
|
|
|
|
int state;
|
2001-08-02 12:15:00 +08:00
|
|
|
} spamdictobject;
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
spamdict_getstate(spamdictobject *self, PyObject *args)
|
|
|
|
{
|
2010-05-09 23:52:27 +08:00
|
|
|
if (!PyArg_ParseTuple(args, ":getstate"))
|
|
|
|
return NULL;
|
|
|
|
return PyLong_FromLong(self->state);
|
2001-08-02 12:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
spamdict_setstate(spamdictobject *self, PyObject *args)
|
|
|
|
{
|
2010-05-09 23:52:27 +08:00
|
|
|
int state;
|
2001-08-02 12:15:00 +08:00
|
|
|
|
2010-05-09 23:52:27 +08:00
|
|
|
if (!PyArg_ParseTuple(args, "i:setstate", &state))
|
|
|
|
return NULL;
|
|
|
|
self->state = state;
|
2022-11-14 23:21:40 +08:00
|
|
|
return Py_NewRef(Py_None);
|
2001-08-02 12:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef spamdict_methods[] = {
|
2010-05-09 23:52:27 +08:00
|
|
|
{"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
|
|
|
|
PyDoc_STR("getstate() -> state")},
|
|
|
|
{"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
|
|
|
|
PyDoc_STR("setstate(state)")},
|
|
|
|
{NULL, NULL},
|
2001-08-02 12:15:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
spamdict_init(spamdictobject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 23:52:27 +08:00
|
|
|
if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
|
|
|
|
return -1;
|
|
|
|
self->state = 0;
|
|
|
|
return 0;
|
2001-08-02 12:15:00 +08:00
|
|
|
}
|
|
|
|
|
2001-09-21 04:46:19 +08:00
|
|
|
static PyMemberDef spamdict_members[] = {
|
2023-07-25 21:28:30 +08:00
|
|
|
{"state", Py_T_INT, offsetof(spamdictobject, state), Py_READONLY,
|
2010-05-09 23:52:27 +08:00
|
|
|
PyDoc_STR("an int variable for demonstration purposes")},
|
|
|
|
{0}
|
2001-08-16 02:09:11 +08:00
|
|
|
};
|
|
|
|
|
2001-08-02 12:15:00 +08:00
|
|
|
static PyTypeObject spamdict_type = {
|
2010-05-09 23:52:27 +08:00
|
|
|
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
|
|
|
|
"xxsubtype.spamdict",
|
|
|
|
sizeof(spamdictobject),
|
|
|
|
0,
|
|
|
|
0, /* tp_dealloc */
|
2019-05-31 10:13:39 +08:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 23:52:27 +08:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-31 10:13:39 +08:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 23:52:27 +08:00
|
|
|
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 */
|
|
|
|
spamdict_members, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
|
|
|
|
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-08-02 12:15:00 +08:00
|
|
|
};
|
|
|
|
|
2001-10-22 06:26:43 +08:00
|
|
|
static PyObject *
|
2001-08-02 12:15:00 +08:00
|
|
|
spam_bench(PyObject *self, PyObject *args)
|
|
|
|
{
|
2010-05-09 23:52:27 +08:00
|
|
|
PyObject *obj, *name, *res;
|
|
|
|
int n = 1000;
|
2022-01-13 16:46:04 +08:00
|
|
|
time_t t0 = 0, t1 = 0;
|
2010-05-09 23:52:27 +08:00
|
|
|
|
2018-04-10 13:21:18 +08:00
|
|
|
if (!PyArg_ParseTuple(args, "OU|i", &obj, &name, &n))
|
2010-05-09 23:52:27 +08:00
|
|
|
return NULL;
|
2022-01-13 16:46:04 +08:00
|
|
|
#ifdef HAVE_CLOCK
|
2010-05-09 23:52:27 +08:00
|
|
|
t0 = clock();
|
|
|
|
while (--n >= 0) {
|
|
|
|
res = PyObject_GetAttr(obj, name);
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
t1 = clock();
|
2022-01-13 16:46:04 +08:00
|
|
|
#endif
|
2010-05-09 23:52:27 +08:00
|
|
|
return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
|
2001-08-02 12:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef xxsubtype_functions[] = {
|
2010-05-09 23:52:27 +08:00
|
|
|
{"bench", spam_bench, METH_VARARGS},
|
|
|
|
{NULL, NULL} /* sentinel */
|
2001-08-02 12:15:00 +08:00
|
|
|
};
|
|
|
|
|
2015-05-23 20:24:10 +08:00
|
|
|
static int
|
|
|
|
xxsubtype_exec(PyObject* m)
|
2001-08-02 12:15:00 +08:00
|
|
|
{
|
2010-05-09 23:52:27 +08: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. */
|
|
|
|
spamdict_type.tp_base = &PyDict_Type;
|
|
|
|
if (PyType_Ready(&spamdict_type) < 0)
|
2015-05-23 20:24:10 +08:00
|
|
|
return -1;
|
2010-05-09 23:52:27 +08:00
|
|
|
|
|
|
|
spamlist_type.tp_base = &PyList_Type;
|
|
|
|
if (PyType_Ready(&spamlist_type) < 0)
|
2015-05-23 20:24:10 +08:00
|
|
|
return -1;
|
2010-05-09 23:52:27 +08:00
|
|
|
|
|
|
|
if (PyType_Ready(&spamlist_type) < 0)
|
2015-05-23 20:24:10 +08:00
|
|
|
return -1;
|
2010-05-09 23:52:27 +08:00
|
|
|
if (PyType_Ready(&spamdict_type) < 0)
|
2015-05-23 20:24:10 +08:00
|
|
|
return -1;
|
2010-05-09 23:52:27 +08:00
|
|
|
|
2023-07-18 14:00:22 +08:00
|
|
|
if (PyModule_AddObjectRef(m, "spamlist", (PyObject *)&spamlist_type) < 0)
|
2015-05-23 20:24:10 +08:00
|
|
|
return -1;
|
2010-05-09 23:52:27 +08:00
|
|
|
|
2023-07-18 14:00:22 +08:00
|
|
|
if (PyModule_AddObjectRef(m, "spamdict", (PyObject *)&spamdict_type) < 0)
|
2015-05-23 20:24:10 +08:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct PyModuleDef_Slot xxsubtype_slots[] = {
|
|
|
|
{Py_mod_exec, xxsubtype_exec},
|
2023-05-06 05:11:27 +08:00
|
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
2024-05-03 23:30:55 +08:00
|
|
|
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
2015-05-23 20:24:10 +08:00
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct PyModuleDef xxsubtypemodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"xxsubtype",
|
|
|
|
xxsubtype__doc__,
|
|
|
|
0,
|
|
|
|
xxsubtype_functions,
|
|
|
|
xxsubtype_slots,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit_xxsubtype(void)
|
|
|
|
{
|
|
|
|
return PyModuleDef_Init(&xxsubtypemodule);
|
2001-08-02 12:15:00 +08:00
|
|
|
}
|