2019-04-12 22:46:21 -03:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
2018-04-07 13:14:03 -03:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyListObject list;
|
|
|
|
int state;
|
|
|
|
} SubListObject;
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
SubList_increment(SubListObject *self, PyObject *unused)
|
|
|
|
{
|
|
|
|
self->state++;
|
|
|
|
return PyLong_FromLong(self->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef SubList_methods[] = {
|
|
|
|
{"increment", (PyCFunction) SubList_increment, METH_NOARGS,
|
|
|
|
PyDoc_STR("increment state counter")},
|
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
SubList_init(SubListObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
if (PyList_Type.tp_init((PyObject *) self, args, kwds) < 0)
|
|
|
|
return -1;
|
|
|
|
self->state = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyTypeObject SubListType = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
.tp_name = "sublist.SubList",
|
2022-04-18 00:39:32 -03:00
|
|
|
.tp_doc = PyDoc_STR("SubList objects"),
|
2018-04-07 13:14:03 -03:00
|
|
|
.tp_basicsize = sizeof(SubListObject),
|
|
|
|
.tp_itemsize = 0,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
|
|
|
.tp_init = (initproc) SubList_init,
|
|
|
|
.tp_methods = SubList_methods,
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyModuleDef sublistmodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "sublist",
|
|
|
|
.m_doc = "Example module that creates an extension type.",
|
|
|
|
.m_size = -1,
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit_sublist(void)
|
|
|
|
{
|
|
|
|
PyObject *m;
|
|
|
|
SubListType.tp_base = &PyList_Type;
|
|
|
|
if (PyType_Ready(&SubListType) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
m = PyModule_Create(&sublistmodule);
|
|
|
|
if (m == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2023-07-25 08:34:49 -03:00
|
|
|
if (PyModule_AddObjectRef(m, "SubList", (PyObject *) &SubListType) < 0) {
|
2019-09-12 09:11:20 -03:00
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:14:03 -03:00
|
|
|
return m;
|
|
|
|
}
|