foo.h
typedef int bar;
typedef struct _foo
{
bar* b;
} foo;
extern foo* foo_new();
extern bar* foo_bar_new(foo* f);
foo.pxd
cdef extern from "foo.h":
ctypedef int bar;
ctypedef struct foo:
bar* b
foo* foo_new();
bar* foo_bar_new(foo* f);
foo.pyx
from foo cimport *
cdef class Foo:
cdef foo* _ptr
def __cinit__(self):
self._ptr = foo_new()
cdef class Bar:
cdef bar* _ptr
def __cinit__(self, f):
self._ptr = foo_bar_new(f._ptr) # error
^
foo.pyx:11:33: Cannot convert Python object to 'foo *'
cython foo.pyx throws an error at the marked line.
I'm not quite sure what I'm doing wrong. Changing _ptr in Foo to cpdef results in the same error.
== Update == I changed foo.pyx to the following and it works.
from foo cimport *
cdef class Foo:
cdef foo* _ptr
def __cinit__(self):
self._ptr = foo_new()
def new_bar(self):
cdef bar* b
b = foo_bar_new(self._ptr)
tmp = Bar()
tmp._set(b)
return tmp
cdef class Bar:
cdef bar* _ptr
cdef _set(self, bar* bptr):
self._ptr = bptr
def get(self):
return bar_get(self._ptr)
I can do f=Foo(); b=f.new_bar() but this doesn't seem ideal to me since I can't create with Bar(f) or similar. The problem is that I can't cdef __cinit__() since it's a special method. Can't cdef __init__() either. Any ideas on how to solve this problem?
foo*argument and return a newFooafter assigningFoo._ptrto the passed infoo*. This is needed as__init__can only accept python objects, not C types.