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

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


(sorry, accidentally hit 'send')

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.

You may want to leave these things to Cython:

    cdef class dbvalue:
       cdef unsigned int id
       cdef str type

       def __init__(self, index, type):
           self.id = index   # will raise an exception if not uint
           self.type = type.encode('ASCII')

    ...
    class ...:
        def set_input_from_db(self, someval, dbvalue value):
            print someval, value.id, value.type

Cython will translate this to efficient C code for you.

http://cython.org/

Stefan



More information about the capi-sig mailing list