cpdef enums can't be shared

485 views
Skip to first unread message

Guillaume Horel

unread,
Jun 29, 2016, 1:33:39 PM6/29/16
to cython-users
Hi,

I'm trying to cimport an enum from a pxd file. If I only have one file as below, everthing works fine.

test.pxd:

cpdef public enum Fruit:
    apple
    pear
    apricot

test.pyx:

from test cimport Fruit

and Fruit gets exported to the python namespace as an Enum which is nice.

But if I bring a second file into the mix:

test2.pyx:

from test cimport Fruit

Then it fails to compile with this error message:

Error compiling Cython file:
------------------------------------------------------------
...




cdef dict __Pyx_globals = globals()
if PY_VERSION_HEX >= 0x03040000:
                 ^
------------------------------------------------------------

EnumType:51:18: undeclared name not builtin: PY_VERSION_HEX

Error compiling Cython file:
------------------------------------------------------------
...


cdef dict __Pyx_globals = globals()
if PY_VERSION_HEX >= 0x03040000:

    Fruit = __Pyx_EnumBase('Fruit', __Pyx_OrderedDict([
                         ^
------------------------------------------------------------

EnumType:53:26: undeclared name not builtin: __Pyx_EnumBase

Error compiling Cython file:
------------------------------------------------------------
...


cdef dict __Pyx_globals = globals()
if PY_VERSION_HEX >= 0x03040000:

    Fruit = __Pyx_EnumBase('Fruit', __Pyx_OrderedDict([
                                                    ^
------------------------------------------------------------

EnumType:53:53: undeclared name not builtin: __Pyx_OrderedDict
Traceback (most recent call last):
  File "setup.py", line 8, in <module>
    ext_modules = cythonize(extensions))
  File "/usr/lib/python3.5/site-packages/Cython/Build/Dependencies.py", line 912, in cythonize
    cythonize_one(*args)
  File "/usr/lib/python3.5/site-packages/Cython/Build/Dependencies.py", line 1034, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: test2.pyx


Am I using it incorrectly? It's a bit surprising since usually declarations in pxd files should be able to be cimported from other files.

Guillaume

Alex Dippel

unread,
Jun 29, 2016, 2:37:11 PM6/29/16
to cython-users
Hey,

right at the moment I am struggling with the same problems. The documentation of the cpdef enum is quite insufficient and the error from the compiler is also not of any interest.

I've been doing a lot of tests and the only possible way without an error is to use a cpdef enum inside a .pyx file. As soon as your are declaring a cpdef enum in a .pxd file, you get the mentioned error.

A possible workaround might be to put a plain cdef enum test: pass inside a .pxd file and use this for reference in e.g. C-Function declarations. For the Python part, you use the cpdef enum. I think this might work.

Another error in connection with cpdef enum occurs when using a C enum like the following:
typedef enum { A, B, C, D } test;
This should result in a few compiler errors. Maybe you can check that too so that I can make sure I am not the only one with these compile errors.
When using a normal cdef enum these compile errors do not occur.

#lib.h
typedef enum { A, B, C, D } test;

#test.pyx
cdef extern from "lib.h":
cpdef enum test:
A, B, C, D
 
It is working when the C enum is declared like this: typedef enum test { A, B, C, D} test; or typedef enum test { A, B, C, D};
 
I think that cpdef enum is not working quite correctly ... 

Alex

Robert Bradshaw

unread,
Jun 30, 2016, 4:57:47 AM6/30/16
to cython...@googlegroups.com
Seems there's a bug cimporting cpdef enums...looking into it.
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

slonik

unread,
Jul 17, 2016, 12:34:58 PM7/17/16
to cython-users
On Thursday, June 30, 2016 at 4:57:47 AM UTC-4, Robert Bradshaw wrote:
Seems there's a bug cimporting cpdef enums...looking into it.

Robert, while you are working on a fix is there a workaround? I seem to run into exactly the same problem but in a different context, see https://groups.google.com/d/topic/cython-users/fSfEWRUgq5w/discussion

--Leo

Stefan Behnel

unread,
Jul 18, 2016, 11:01:12 AM7/18/16
to cython...@googlegroups.com
Guillaume Horel schrieb am 29.06.2016 um 19:20:
> I'm trying to cimport an enum from a pxd file. If I only have one file as
> below, everthing works fine.
>
> test.pxd:
>
> cpdef public enum Fruit:
> apple
> pear
> apricot
>
> test.pyx:
>
> from test cimport Fruit
>
> and Fruit gets exported to the python namespace as an Enum which is nice.

Note that the reason it's being exported is not the cimport but the name of
the .pxd file. In fact, the cimport is entirely redundant. Only the fact
that the .pxd file has the same name as the current .pyx file joins the two
into one namespace and enables the "cpdef" export of the enum.


> But if I bring a second file into the mix:
>
> test2.pyx:
>
> from test cimport Fruit
>
> Then it fails to compile with this error message:
> [...]

The rest indicates a bug, as Robert said.

Stefan

Robert Bradshaw

unread,
Jul 18, 2016, 11:54:11 AM7/18/16
to cython...@googlegroups.com
Use cdef enums for now, and if you need them accessible from Python
write something like

globals()['value'] = value
Reply all
Reply to author
Forward
0 new messages