Can Cython access static inline class member variables? If not, I would campaign, if not for a fix, at least for better error diagnostics and documentation (FAQ?). If so, can someone give me an example? I scoured the documentation at
cython.org, and
stackoverflow.com, and the web, to no avail.
Consider how to access the following member variable:
namespace my::name::space
{
class MyClass
{
public:
static inline int LIFE { 42 };
};
}
The Cython .pyx file:
cdef extern from "mycpp.h" namespace "my::name::space":
cdef cppclass MyClass:
int LIFE
cdef class MyMgr:
def my_failing_function(self) -> None:
cdef int life = MyClass.LIFE
This elicits the following diagnostic during generated C++ compilation:
src/dotbug/dotbug-mre.cpp: In function ‘PyObject* __pyx_pf_6dotbug_5MyMgr_my_failing_function(__pyx_obj_6dotbug_MyMgr*)’:
src/dotbug/dotbug-mre.cpp:2666:39: error: expected primary-expression before ‘.’ token
2666 | __pyx_t_1 = my::name::space::MyClass.LIFE;
| ^
This is not valid C++ syntax; the period should be the scoping operator, "::". If you change it in the generated code, and build, it works properly.
ChatGPT recommended (before it broke down and claimed Cython cannot access static member variables) that I use "static int LIFE" for my cdef definition, but this yields a different error, this one in Cython:
cdef extern from "mycpp.h" namespace "my::name::space":
cdef cppclass MyClass:
static int LIFE
^
------------------------------------------------------------
src/dotbug/dotbug-mre.pyx:3:19: Syntax error in C variable declaration
Is there something I am missing with respect to syntax? I note that if I convert this into a non-static variable, and instantiate it, I can access it just fine.
For a full set of buildable files, see the Github Gist at
https://gist.github.com/eewanco/f21cee1917532e87b1fe72a107728eae