On Thu, Sep 27, 2012 at 2:39 AM, Horace Abenga <
horace...@gmail.com> wrote:
> Is it possible to pass pointers to defined types in Cython to constructors,
> for example:
>
> cdef extern from "Foo.h":
> cdef cppclass Bar:
> pass
>
> cdef class PyClass:
> cdef Bar *bar
>
> def __cinit__(self, Bar *b)
> bar = b
>
>
> When you do this, you get a Cannot convert Python object to 'Bar *'.
>
> (Copied from
>
http://stackoverflow.com/questions/12204441/passing-c-pointer-as-argument-into-cython-function,
> the top answer there is convoluted and seems to be just wrapping a double,
> what I'm trying to do is a bit more complicated)
>
> I am using such a construct to implement a "Has-A" (composition)
> relationship between two types that are defined as structs in a C Library.
>
> In my code, the Class Bar has a non-trivial constructor, and the
> "constructor" for PyClass in C should receive a pointer to an initialized
> Bar object.
>
> Is there a simple way of doing this?
Python class construction requires a Python call, hence Python object
arguments. The two answers given on stackoverflow (writing a global
method that constructs the object then sets the pointer, or creating a
wrapper class for Bar you an pass around in Python space) are the
typical ways of handling this. There's not really anything "simpler"
(though that's pretty simple).
- Robert