Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

python - Accessing the underlying struct of a PyObject

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    
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your PyArg_ParseTuple should not use format O but O! (see the docs):

O! (object) [typeobject, PyObject *]

Store a Python object in a C object pointer. This is similar to O, but takes two C arguments: the first is the address of a Python type object, the second is the address of the C variable (of type PyObject*) into which the object pointer is stored. If the Python object does not have the required type, TypeError is raised.

Once you've done that, you know that in your function's body (PointObject*)point will be a correct and valid pointer to a PointObject, and therefore its ->my_point will be the Point* you seek. With a plain format O you'd have to do the type checking yourself.

Edit: the OP in a comments asks for the source...:

static PyObject*
set_point(PyObject* self, PyObject* args)
{
    PyObject* point; 

    if (!PyArg_ParseTuple(args, "O!", &PointType, &point))
    {
        return NULL;
    }

    Point* pp = ((PointObject*)point)->my_point;

    // ... use pp as the pointer to Point you were looking for...

    // ... and incidentally don't forget to return a properly incref'd
    // PyObject*, of course;-)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...