mirror of
https://github.com/python/cpython.git
synced 2024-11-24 18:34:43 +08:00
1a3faba9f1
* Remove '#include "structmember.h"'. * If needed, add <stddef.h> to get offsetof() function. * Update Parser/asdl_c.py to regenerate Python/Python-ast.c. * Replace: * T_SHORT => Py_T_SHORT * T_INT => Py_T_INT * T_LONG => Py_T_LONG * T_FLOAT => Py_T_FLOAT * T_DOUBLE => Py_T_DOUBLE * T_STRING => Py_T_STRING * T_OBJECT => _Py_T_OBJECT * T_CHAR => Py_T_CHAR * T_BYTE => Py_T_BYTE * T_UBYTE => Py_T_UBYTE * T_USHORT => Py_T_USHORT * T_UINT => Py_T_UINT * T_ULONG => Py_T_ULONG * T_STRING_INPLACE => Py_T_STRING_INPLACE * T_BOOL => Py_T_BOOL * T_OBJECT_EX => Py_T_OBJECT_EX * T_LONGLONG => Py_T_LONGLONG * T_ULONGLONG => Py_T_ULONGLONG * T_PYSSIZET => Py_T_PYSSIZET * T_NONE => _Py_T_NONE * READONLY => Py_READONLY * PY_AUDIT_READ => Py_AUDIT_READ * READ_RESTRICTED => Py_AUDIT_READ * PY_WRITE_RESTRICTED => _Py_WRITE_RESTRICTED * RESTRICTED => (READ_RESTRICTED | _Py_WRITE_RESTRICTED)
310 lines
11 KiB
C
310 lines
11 KiB
C
#include "Python.h"
|
|
#include <stddef.h> // offsetof()
|
|
|
|
|
|
PyDoc_STRVAR(xxsubtype__doc__,
|
|
"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"
|
|
"test suite, you can recompile Python without Modules/xxsubtype.c.");
|
|
|
|
/* 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
|
|
|
|
/* 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 PyLong_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;
|
|
return Py_NewRef(Py_None);
|
|
}
|
|
|
|
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;
|
|
PyTuple_SET_ITEM(result, 0, Py_NewRef(self));
|
|
PyTuple_SET_ITEM(result, 1, Py_NewRef(args));
|
|
PyTuple_SET_ITEM(result, 2, Py_NewRef(kw));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static PyMethodDef spamlist_methods[] = {
|
|
{"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. */
|
|
{"classmeth", _PyCFunction_CAST(spamlist_specialmeth),
|
|
METH_VARARGS | METH_KEYWORDS | METH_CLASS,
|
|
PyDoc_STR("classmeth(*args, **kw)")},
|
|
{"staticmeth", _PyCFunction_CAST(spamlist_specialmeth),
|
|
METH_VARARGS | METH_KEYWORDS | METH_STATIC,
|
|
PyDoc_STR("staticmeth(*args, **kw)")},
|
|
{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;
|
|
}
|
|
|
|
static PyObject *
|
|
spamlist_state_get(spamlistobject *self, void *Py_UNUSED(ignored))
|
|
{
|
|
return PyLong_FromLong(self->state);
|
|
}
|
|
|
|
static PyGetSetDef spamlist_getsets[] = {
|
|
{"state", (getter)spamlist_state_get, NULL,
|
|
PyDoc_STR("an int variable for demonstration purposes")},
|
|
{0}
|
|
};
|
|
|
|
static PyTypeObject spamlist_type = {
|
|
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
|
|
"xxsubtype.spamlist",
|
|
sizeof(spamlistobject),
|
|
0,
|
|
0, /* tp_dealloc */
|
|
0, /* tp_vectorcall_offset */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_as_async */
|
|
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 */
|
|
};
|
|
|
|
/* 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 PyLong_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;
|
|
return Py_NewRef(Py_None);
|
|
}
|
|
|
|
static PyMethodDef spamdict_methods[] = {
|
|
{"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
|
|
PyDoc_STR("getstate() -> state")},
|
|
{"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
|
|
PyDoc_STR("setstate(state)")},
|
|
{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;
|
|
}
|
|
|
|
static PyMemberDef spamdict_members[] = {
|
|
{"state", Py_T_INT, offsetof(spamdictobject, state), Py_READONLY,
|
|
PyDoc_STR("an int variable for demonstration purposes")},
|
|
{0}
|
|
};
|
|
|
|
static PyTypeObject spamdict_type = {
|
|
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
|
|
"xxsubtype.spamdict",
|
|
sizeof(spamdictobject),
|
|
0,
|
|
0, /* tp_dealloc */
|
|
0, /* tp_vectorcall_offset */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_as_async */
|
|
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 */
|
|
};
|
|
|
|
static PyObject *
|
|
spam_bench(PyObject *self, PyObject *args)
|
|
{
|
|
PyObject *obj, *name, *res;
|
|
int n = 1000;
|
|
time_t t0 = 0, t1 = 0;
|
|
|
|
if (!PyArg_ParseTuple(args, "OU|i", &obj, &name, &n))
|
|
return NULL;
|
|
#ifdef HAVE_CLOCK
|
|
t0 = clock();
|
|
while (--n >= 0) {
|
|
res = PyObject_GetAttr(obj, name);
|
|
if (res == NULL)
|
|
return NULL;
|
|
Py_DECREF(res);
|
|
}
|
|
t1 = clock();
|
|
#endif
|
|
return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
|
|
}
|
|
|
|
static PyMethodDef xxsubtype_functions[] = {
|
|
{"bench", spam_bench, METH_VARARGS},
|
|
{NULL, NULL} /* sentinel */
|
|
};
|
|
|
|
static int
|
|
xxsubtype_exec(PyObject* m)
|
|
{
|
|
/* 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)
|
|
return -1;
|
|
|
|
spamlist_type.tp_base = &PyList_Type;
|
|
if (PyType_Ready(&spamlist_type) < 0)
|
|
return -1;
|
|
|
|
if (PyType_Ready(&spamlist_type) < 0)
|
|
return -1;
|
|
if (PyType_Ready(&spamdict_type) < 0)
|
|
return -1;
|
|
|
|
if (PyModule_AddObjectRef(m, "spamlist", (PyObject *)&spamlist_type) < 0)
|
|
return -1;
|
|
|
|
if (PyModule_AddObjectRef(m, "spamdict", (PyObject *)&spamdict_type) < 0)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
static struct PyModuleDef_Slot xxsubtype_slots[] = {
|
|
{Py_mod_exec, xxsubtype_exec},
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
|
{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);
|
|
}
|