Accessing static inline class member variables

16 views
Skip to first unread message

Eric Ewanco

unread,
May 2, 2024, 1:23:24 AMMay 2
to cython-users
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

da-woods

unread,
May 2, 2024, 2:23:54 AMMay 2
to cython...@googlegroups.com
I don't think it can directly.

See libcpp::string::npos (https://github.com/cython/cython/blob/a6d810b970c21948c7fcdeec2cf28769e716e4a9/Cython/Includes/libcpp/string.pxd#L6) for an example of how we solved that problem ourself when wrapping the C++ standard library. Essentially you specify the class name as "namespace" and that gets the correct code generated.
For a full set of buildable files, see the Github Gist at https://gist.github.com/eewanco/f21cee1917532e87b1fe72a107728eae --

---
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/1caf5af1-f105-4d00-8269-14358765b635n%40googlegroups.com.


Reply all
Reply to author
Forward
0 new messages