3

I am trying to write a Cython wrapper around a C library. I am very new to Cython, so my apologies in advance if the problem is obvious.

In a file wrapper.pxd, I define a struct (reduced example):

cdef extern from "thiscouldbeyourlibrary.h":
    cdef struct foo:
        double **output

I then have a class:

cdef class Bar:
    cdef wrapper.foo __stuff

    cdef do_something(self):
        self.__stuff.output = NULL

This fails:

Cannot convert 'void *' to Python object.

Apparently, Cython cannot be sure that self.__stuff.output is always a pointer. But I declared its type and the class is a "cdef" class, so I don't really understand why.

4
  • Note that there are better tools for the specific task of generating Python wrappers, like SWIG (that's what pywin32 uses). Commented Sep 12, 2014 at 16:08
  • @ivan_pozdeev I know. There is actually already a wrapper for the specific library I need, based on SWIG. However, I was having some problems with it (and by "problems" I mean segfaults), and it doesn't do precisely what I want (NumPy interoperability). Commented Sep 12, 2014 at 16:17
  • Just a guess: use None instead of NULL - that looks more like a "Python object" :^) Commented Sep 12, 2014 at 23:17
  • @EyPandaBear did you check the answer below? Commented Oct 13, 2014 at 8:27

1 Answer 1

1

The problem is the incompatibility between NULL and double **. You could assign it to a char, int or void *, for example, doing:

wrapper.pyd:

cdef extern from "thiscouldbeyourlibrary.h":
    cdef struct foo:
        char a
        int b
        void *c
        double **output

thiscouldbeyourlibrary.h:

struct foo
{ 
    char a;
    int b;
    void *c;
    double **output;
};

main.pyx:

cimport wrapper

cdef class Bar:
    cdef wrapper.foo __stuff
    def __init__(self):
        self.__stuff.a = <char>NULL
        self.__stuff.b = <int>NULL
        self.__stuff.c = NULL

def main():
    bar = Bar()
    print bar.__stuff.a
    print bar.__stuff.b

If you had allocated the memory for output before, you could have done it:

self.__stuff.output[0] = NULL

without allocating it will crash...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.