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.
On Thu, Sep 27, 2012 at 2:39 AM, Horace Abenga <horaceabe...@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 *'.
> 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).
> 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.
Uaually this is an error of reasoning, as a non-trival constructor will be non-trivial outside __cinit__ as well. So calling it from __cinit__ cannot be more difficult than calling it from anywhere else.
For each cdef class create a global cdef function that acts as a constructor, CefResponse is a C++ object, PyResponse a python equivalent of a c++ object: