[Pyrex] opaque types, constructors, assignment - help please

Stefan Behnel stefan_ml at behnel.de
Thu Feb 25 21:36:08 CET 2010


Jose Nazario, 25.02.2010 18:04:
> trying to get the following code to work:
> 
> cdef extern from "../src/capi.h":
>     ctypedef extern struct FastBitQueryHandle
>     ....
>     FastBitQueryHandle fastbit_build_query(char *selectClause, char
> *indexLocation, char *queryConditions)
>     ....
> 
> class Query:
>     # The Query class holds queries over the FastBit data set
>     def __init__(self, selectClause, indexLocation, queryConditions):
>         self.qh = <FastBitQueryHandle>fastbit_build_query(selectClause,
>                     indexLocation, queryConditions)
> 
> not surprisingly i get an assignment error:
> 
> /var/home/jose/fastbit-ibis1.1.6/python/fastbit.pyx:235:18: Cannot
> convert 'FastBitQueryHandle' to Python object
> 
> 
> FastBitQueryHandle is a complex C++ object, easier to leave it opaque

You are trying to assign a C++ object to a Python object attribute. This
requires a conversion to a Python object, which obviously fails. Instead,
use a cdef class that has a cdef attribute of the C++ type.

Note that Cython 0.13 will have much better support for C++ classes. See here:

http://wiki.cython.org/enhancements/cpp

This will make it a lot easier to handle C++ objects. You can get a
developer version with the described features from here:

http://hg.cython.org/cython-devel

Stefan




More information about the Pyrex mailing list