I am working on creating a python c extension but am having difficulty finding documentation on what I want to do. I basically want to create a pointer to a cstruct and be able to have access that pointer. The sample code is below. Any help would be appreciated.
typedef struct{
int x;
int y;
} Point;
typedef struct {
PyObject_HEAD
Point* my_point;
} PointObject;
static PyTypeObject PointType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"point", /*tp_name*/
sizeof(PointObject), /*tp_basicsize*/
0, /*tp_itemsize*/
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, /*tp_flags*/
"point objects", /* tp_doc */
};
static PyObject* set_point(PyObject* self, PyObject* args)
{
PyObject* point;
if (!PyArg_ParseTuple(args, "O", &point))
{
return NULL;
}
//code to access my_point
}
Your
PyArg_ParseTupleshould not use formatObutO!(see the docs):Once you’ve done that, you know that in your function’s body
(PointObject*)pointwill be a correct and valid pointer to aPointObject, and therefore its->my_pointwill be thePoint*you seek. With a plain formatOyou’d have to do the type checking yourself.Edit: the OP in a comments asks for the source…: