What to do with extern enums in Cython 3

381 views
Skip to first unread message

Edward Sr

unread,
Nov 24, 2021, 1:16:28 PM11/24/21
to cython-users
Hi all,

In my project, I expose some C enums of a C library that I work with to the Python users using .pyx and .pxd files as follows:

dummy.pyx:
cdef extern from '<SOME_LIB/HEADER.h>':

    cpdef enum dummy:
        DUMMY_1
        DUMMY_2
        ETC

So far so good, but now with Cython3 there are no cpdef variables anymore and the compilation fails.
Changing the cpdef to cdef will no longer be accessible by python users...

What is the right thing to do in this case?
I thought of assigning a new module variables as a workaround - but that would be just lame and funny:

cdef extern from '<SOME_LIB/HEADER.h>':

    cpdef enum dummy:
        DUMMY_1_  "DUMMY_1"
        DUMMY_2_  "DUMMY_2"
        ETC_ "ETC"
DUMMY_1 = DUMMY_1_  
DUMMY_2 = DUMMY_2_
ETC = ETC_

What is the right way to fix this?

Thanks in advance!

da-woods

unread,
Nov 24, 2021, 3:56:19 PM11/24/21
to cython...@googlegroups.com
This sounds like an unintended change.

The reason for removing cpdef variables was that things like

cpdef int a

did the same as "cdef" but lots of people thought that it would behave differently and expose their variable to Python. However "cpdef" enums had a real purpose, so if they've been broken by the change then it was a mistake. Sorry!

So the answer is probably "hold on and hopefully it can be fixed in the near future". I'm slightly surprised we didn't have suitable tests for cpdef enums (and that should be fixed too).
--

---
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/b17eab6d-85c0-4334-8512-35182a578136n%40googlegroups.com.


da-woods

unread,
Nov 24, 2021, 3:56:34 PM11/24/21
to cython...@googlegroups.com
With that said we do have tests that superficially look a lot like your use-case:


So right now I'm not sure exactly what's stopped working

Edward Sr

unread,
Nov 25, 2021, 7:21:31 AM11/25/21
to cython-users
Now I see what happened!
I wasn't precise... the problem was not specifically as I described with the enums, but with extern defines as follows:

cdef extern from '<SOME_LIB/HEADER.h>':
    cpdef int DUMMY_1

This means I'm exposing some defined variable from HEADER.h, and not an enum.
This cpdef worked before (cython 0.29), but now (cython 3a) it doesn't - but I guess that's ok as it's not a new degradation, because even in 0.29 I had to manually add DUMMY_1_ = DUMMY_1 at the pxd module level to expose it (as DUMMY_1_) to python.

Thanks.

Reply all
Reply to author
Forward
0 new messages