Python C/API: converting C struct to Py class

Started by
0 comments, last by Eckos 15 years, 10 months ago
Hi, we are converting a struct

typedef struct {
          PyObject_HEAD
          int first, second;
      } intpair;


to a python class (similar to an example 25.2 in "Python in a Nutshell, 2nd Edition"). We, after PyInitialize() and importing the module in python, obtain a class, but __dict__ shows an empty doc string and also does not show the members "first" and "second". The init function intpair_init (see below) does not seem to be called, since no printf debug statements are printed. 1) We suspect that we either initialise wrongly 2) or a problem with the fields in PyTypeObject being in the wrong order, something was left out etc.) 3) or ??? cheers David, Juergen We are using the PyTypeObject definition from /usr/lib/python2.5/doc/commontex/typestruct.h (see below): and the functions intpair_dealloc,intpair_str, intpair_init.

static int intpair_init(PyObject *self , PyObject *args, PyObject *kwds)
{
    static char* nams[] = {"first","second",NULL};
    int first, second;
    printf("1 in init\n");fflush(stdout);
    if(!PyArg_ParseTupleAndKeywords(args, kwds, "ii", nams, &first, &second))
        return -1;
    ((intpair*)self)->first = 101;
    ((intpair*)self)->second = 102;
    printf("in init\n");
    return 0;
}


with intpair_members:

static PyMemberDef intpair_members[] = {
    {"first", T_INT, offsetof(intpair, first), 0, "first item" },
    {"second", T_INT, offsetof(intpair, second), 0, "second item" },
    {NULL}
};


the initialisation is done with:

void initintpair(void)
{
    static PyMethodDef no_methods[] = { {NULL} };
    PyObject* this_module = Py_InitModule("intpair", no_methods);
    PyType_Ready(&t_intpair);
    PyObject_SetAttrString(this_module, "intpair", (PyObject*)&t_intpair);
}



static PyTypeObject t_intpair = {
   PyObject_HEAD_INIT(NULL)
    0,
    "intpair.intpair", /* For printing, in format "<module>.<name>" */
    sizeof(intpair),
    0, /* For allocation */
    /* Methods to implement standard operations */
    intpair_dealloc,
    0,
    0,
    0, 
    0,
    intpair_str,
   /* Method suites for standard classes */
    0,
    0,
    0, 
    /* More standard operations (here for binary compatibility) */
    0,
    0,
    0,
    PyObject_GenericGetAttr,
    PyObject_GenericSetAttr,
    /* Functions to access object as input/output buffer */
    0,
    /* Flags to define presence of optional/expanded features */
    Py_TPFLAGS_DEFAULT,
    "two ints (first,second)", /* Documentation string */
    /* Assigned meaning in release 2.0 */
    /* call function for all accessible objects */
    0,
    /* delete references to contained objects */
    0, 
    /* Assigned meaning in release 2.1 */
    /* rich comparisons */
    0,
    /* weak reference enabler */
    0,
    /* Added in release 2.2 */
    /* Iterators */
    0,
    0, 
    /* Attribute descriptor and subclassing stuff */
    0,
    intpair_members,
    0,
    0,
    0,
    0,
    0,
    0, 
    intpair_init,
    PyType_GenericAlloc, 
    PyType_GenericNew ,
    _PyObject_Del, /* Low-level free-memory routine */
    0,
    0,
    0,
    0,
    0,
    0
};


[Edited by - juergenhaas on June 24, 2008 5:59:27 AM]
Advertisement
wrap your code around source tags. It's hard to read it that way

This topic is closed to new replies.

Advertisement