[cython-users] Forward declarations in Cython > 0.11.2

602 views
Skip to first unread message

Saúl Ibarra

unread,
Mar 3, 2010, 1:42:25 PM3/3/10
to cython...@googlegroups.com
Hi,

This is my first to the mailing list so if this has already been asked
I'm sorry :) I've spent some time trying to understand why forward
declarations don't work as I expect on cython > 0.11.2 but I'm missing
something.

This is a simple example:

saghul@hal:~/work/cython$ cat test.pxd

cdef class Test(object)
cdef class Test2(object)


saghul@hal:~/work/cython$ cat test.pyx

include "test.1.pxi"
include "test.2.pxi"


saghul@hal:~/work/cython$ cat test.1.pxi

cdef class Test:
pass


saghul@hal:~/work/cython$ cat test.2.pxi

cdef class Test2:
pass


If I do cython -v test.pyx I'm getting the following:

/home/saghul/work/ag-projects/cython/test.pxd:3:5: C class 'Test' is
declared but not defined
/home/saghul/work/ag-projects/cython/test.pxd:4:5: C class 'Test2' is
declared but not defined

Any clue about how to do this properly?


Thanks in advance,

--
/Saúl
http://saghul.net | http://sipdoc.net

Kurt Smith

unread,
Mar 3, 2010, 2:10:14 PM3/3/10
to cython...@googlegroups.com
On Wed, Mar 3, 2010 at 12:42 PM, Saúl Ibarra <sag...@gmail.com> wrote:
> Hi,
>
> This is my first to the mailing list so if this has already been asked
> I'm sorry :) I've spent some time trying to understand why forward
> declarations don't work as I expect on cython > 0.11.2 but I'm missing
> something.
>
> This is a simple example:
>
> saghul@hal:~/work/cython$ cat test.pxd
>
> cdef class Test(object)
> cdef class Test2(object)

Here is the error: as the error message says, you declare the
extension types 'Test' and 'Test2', but you need to supply a
definition. Try this:

# test.pxd ---
cdef class Test(object)
cdef class Test2(object) # forward declarations

cdef class Test(object):
pass # fields go here

cdef class Test2(object):
pass # fields go here

#----------------------

You need to put the definition of the extension types' fields in the
pxd file (the fields that go in place of the 'pass' statements above).
In the pyx file, you would put the method definitions.

See the following docs:

http://docs.cython.org/src/userguide/sharing_declarations.html#sharing-extension-types
http://docs.cython.org/src/userguide/extension_types.html?highlight=forward#id1

Saúl Ibarra

unread,
Mar 3, 2010, 6:30:08 PM3/3/10
to cython...@googlegroups.com

Hi Kurt!

Thanks for your quick answer! :) That was it, I had it in front of my
eyes but was missing it :-S

Reply all
Reply to author
Forward
0 new messages