[capi-sig] Passing an object from script to C code

Stefan Behnel python_capi at behnel.de
Tue Jan 27 09:15:20 CET 2009


Gamze Tunali wrote:
> As a beginner, I have a question about passing a class instance from
> Python script to my C code.
> What I am doing is basically creating a class in the script:
>
> class dbvalue:
>   def __init__(self, index, type):
>     self.id = index    # unsigned integer
>     self.type = type   # string
>
> last_roi = dbvalue(id, type);
> bmdl_batch.set_input_from_db(1, last_roi); <--------
>
> I would like to call my C function with last_roi and able to recover id
> and type in the code. The C part is follows:

You might want to leave these things to Cython:

    cdef class dbvalue:
   def __init__(self, index, type):
>     self.id = index    # unsigned integer
>     self.type = type   # string


>
>
> PyObject *set_input_from_db(PyObject* self, PyObject *args)
> {
>   unsigned input;
>   PyObject *obj;
>
>   if (!PyArg_ParseArg(args, "iO:set_input_from_db", &input, &obj))
>     return NULL;
>
>   ....
> }
>
> I am not sure if PyArg_ParseArg() is the right method to get the object. I
> used "O:.."  to get it but after that, how do I acces index and type
> fields of the object. Do I create an equivalent class/struct in C code to?
> Again, all I want is pass the 2 values together in an object and able to
> recover then in C code.
>
> Thanks,
>
> Gamze
>
>
>
>
> _______________________________________________
> capi-sig mailing list
> capi-sig at python.org
> http://mail.python.org/mailman/listinfo/capi-sig
>



More information about the capi-sig mailing list