jeremy
jeremy

Reputation: 4681

Cython error: declaration does not declare anything

I'm writing some cython code, and I've come across an odd problem. When I try to pass an object straight from python to C as a struct, cython generates the code fine, but gcc doesn't like the code output and gives me the following error: error: declaration does not declare anything. Here is my test code:

// cake.h
using Cake = struct CakeStruct {
    int a, b, c;
};

void bake(Cake batter);

and the cython:

# test.pyx

cdef extern from "cake.h":
    void bake(Cake batter)
    ctypedef struct Cake:
        int a
        int b
        int c

def make_one(batter):
    cdef Cake more_batter;
    more_batter.a = 5
    more_batter.b = 10
    print(more_batter.a + more_batter.b)

    bake(more_batter)
    bake(batter)  # <- this line generates bad code

if you look at the generated code, the bad line looks like this:

...

Cake; // this is the error
static Cake __pyx_convert__from_py_Cake(PyObject *);

...

I'm using cython 0.21 directly from Anaconda and gcc 4.8.2 shipped with Ubuntu 14.04. Cython code is generated using cython --cplus test.pyx and syntax checked by:

gcc -std=c++11 -fsyntax-only -I`...python include dir...` test.cpp

--

Can anyone please tell me what I am doing wrong in my .pyx file? Or is this a cython bug that I have tripped on?

Upvotes: 0

Views: 269

Answers (1)

Jon Lund Steffensen
Jon Lund Steffensen

Reputation: 630

Yes, I think you are right that this is a bug in Cython 0.21. First, testing with Cython 0.20 (because that is what my linux distribution ships) and it gives me

cake.pyx:16:15: Cannot convert Python object to 'Cake'

I suppose this is because the conversion feature was missing or incomplete in 0.20, although I cannot find anything in the release notes about this (https://github.com/cython/cython/blob/master/CHANGES.rst).

Next, I tested with the master branch from the Github repository (https://github.com/cython/cython) This worked perfectly, with no errors from your supplied cython or gcc command. With the 0.21 version I can reproduce the error that you saw.

Running a git bisect shows that the bug seems to be fixed in commit fd56551. This is after the 0.21.1 (the latest version at this time) so upgrading to this version would not fix the bug. It appears you will have to go with the development branch or wait for the next Cython release.

Upvotes: 2

Related Questions