This question is essentially a follow up regarding this question. Specifically, I am wondering about enum visibility across shared modules.
Essentially, I would like to make a wrapped C enum part of a python API (using cython). The simplest way to wrap some existing C code would be a simple
lib.pyx
(for the sake of simplicity I included the header verbatim)
cdef extern from *:
"""
typedef enum Status {GOOD, BAD} Status;
"""
cpdef enum Status:
GOOD,
BAD
If I compile this file into a shared library (say lib.so
), I can use the enum directly, i.e. I can type python3 -c "import lib; print(lib.Status)"
and see <enum 'Status'>
, which is indeed the expected behavior.
Now assume that I would like to split the code into a header (aka lib.pxd
)
containing the code from above as well as an implementation file (aka lib.pyx
) cimport
ing the header:
from lib cimport Status
def test():
print Status.GOOD
I can again compile the project, however, the results are quite different.
I can access the function test
, but python3 -c "import lib; lib.test()"
just returns 0
, rather than <Status.GOOD: 0>
.
What is more, python3 -c "import lib; print(lib.Status)"
now gives me
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: module 'lib' has no attribute 'Status'
In short: The enum declared in the pxd file is not included in the final module, even though it is declared as cpdef
.
Is this behavior intentional? Is there any way to include the declaration into the generated module?