Cython: declaring a typedefed enum

145 views
Skip to first unread message

Jernej Azarija

unread,
Dec 29, 2014, 9:27:16 AM12/29/14
to sage-...@googlegroups.com
Dear sage-devel,

I have another question related to Cython. I like to post them here instead of cython-users as it seems to me that the former is a much slower group. That said....

The definition of the Graph class in one of the libraries is :

===================================
class Graph : public AbstractGraph
{
public:
  typedef enum {
    shs_f = 0, shs_fs, shs_fl, shs_fm, shs_fsm, shs_flm
  } SplittingHeuristic;
// blah blah blah

}
===================================

Following Cython's guide on this subject http://docs.cython.org/src/userguide/external_C_code.html I figured my definition of the wrapped class should look like:

===================================
# blah blah blah
cdef cppclass Graph(AbstractGraph):
        ctypedef enum SplittingHeuristic:
           shs_f = 0
# blah blah blah
===================================



Unfortunately, this gives me a compilation error:

===================================
    cdef cppclass Graph(AbstractGraph):
        ctypedef enum SplittingHeuristic:
                     ^
------------------------------------------------------------

sage/graphs/bliss.pyx:45:22: Syntax error in C variable declaration
===================================

Now I think the pasted code completely isolates the issue since I am able to work with the Graph class (access methods) normally. The only issue appears when I introduce this enum thing. Hence I am wondering what exactly I am missing in this code?


Best,

Jernej

Volker Braun

unread,
Dec 29, 2014, 12:03:35 PM12/29/14
to sage-...@googlegroups.com
Don't redeclare the enum values. 

cdef cppclass Graph(AbstractGraph):
        ctypedef enum SplittingHeuristic:
           shs_f
           foo
           bar

Volker Braun

unread,
Dec 29, 2014, 12:33:19 PM12/29/14
to sage-...@googlegroups.com
Also, Cython doesn't support class-level typedefs so you'll probably need a version of the "literal" trick:

ctypedef enum Graph_SplittingHeuristic "Graph::SplittingHeuristic":
    shs_f
    foo
    bar

Jernej Azarija

unread,
Dec 29, 2014, 1:57:02 PM12/29/14
to Sage devel
Volker,

this gives me the exact same error:

========
    cdef cppclass Graph(AbstractGraph):

        ctypedef enum Graph_SplittingHeuristic "Graph::SplittingHeuristic":
                     ^
------------------------------------------------------------

sage/graphs/bliss.pyx:36:22: Syntax error in C variable declaration
========

--
You received this message because you are subscribed to a topic in the Google Groups "sage-devel" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-devel/1AzzUVqTITY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-devel+...@googlegroups.com.
To post to this group, send email to sage-...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Volker Braun

unread,
Dec 29, 2014, 3:45:09 PM12/29/14
to sage-...@googlegroups.com
You need to move the typedef outside of the class scope, see my second post.

Jernej Azarija

unread,
Dec 29, 2014, 4:10:20 PM12/29/14
to Sage devel
Looks like we're on the right track - though it now seems  to not recognize the scope of this enum  within the library?

=====
build/cythonized/sage/graphs/bliss.cpp: In function 'bliss::Graph* __pyx_f_4sage_6graphs_5bliss_bliss_graph(PyObject*, PyObject*, PyObject*, PyObject*)':
build/cythonized/sage/graphs/bliss.cpp:1895:38: error: 'shs_f' is not a member of 'bliss'
   __pyx_v_g->set_splitting_heuristic(bliss::shs_f);
                                      ^
error: command 'gcc' failed with exit status 1
====
Reply all
Reply to author
Forward
0 new messages