mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 03:44:55 +00:00 
			
		
		
		
	* Add a note to the PyModule_AddObject docs. * Correct example usages of PyModule_AddObject. * Whitespace. * Clean up wording. * 📜🤖 Added by blurb_it. * First code review. * Add < 0 in the tests with PyModule_AddObject
		
			
				
	
	
		
			69 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#define PY_SSIZE_T_CLEAN
 | 
						|
#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",
 | 
						|
    .tp_doc = "SubList objects",
 | 
						|
    .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;
 | 
						|
 | 
						|
    Py_INCREF(&SubListType);
 | 
						|
    if (PyModule_AddObject(m, "SubList", (PyObject *) &SubListType) < 0) {
 | 
						|
        Py_DECREF(&SubListType);
 | 
						|
        Py_DECREF(m);
 | 
						|
        return NULL;
 | 
						|
    }
 | 
						|
 | 
						|
    return m;
 | 
						|
}
 |