Skip to content

Add support for cpdef enum #45

Closed
wants to merge 2 commits into from
View
4 Cython/Compiler/Nodes.py
@@ -390,7 +390,7 @@ def analyse_expressions(self, env):
pass
def generate_execution_code(self, code):
- pass
+ self.body.generate_execution_code(code)
def annotate(self, code):
self.body.annotate(code)
@@ -1093,7 +1093,7 @@ def analyse_expressions(self, env):
pass
def generate_execution_code(self, code):
- if self.visibility == 'public' or self.api:
+ if self.visibility == 'public' or self.api or self.is_overridable:
temp = code.funcstate.allocate_temp(PyrexTypes.py_object_type, manage_ref=True)
for item in self.entry.enum_values:
code.putln("%s = PyInt_FromLong(%s); %s" % (
View
2 Cython/Compiler/ParseTreeTransforms.py
@@ -1530,7 +1530,7 @@ def visit_CBaseTypeNode(self, node):
return None
def visit_CEnumDefNode(self, node):
- if node.visibility == 'public':
+ if node.visibility == 'public' or node.is_overridable:
return node
else:
return None
View
7 Cython/Compiler/Parsing.py
@@ -2413,10 +2413,10 @@ def p_cdef_statement(s, ctx):
elif s.sy == 'IDENT' and s.systring in ("struct", "union", "enum", "packed"):
if ctx.level not in ('module', 'module_pxd'):
error(pos, "C struct/union/enum definition not allowed here")
- if ctx.overridable:
- error(pos, "C struct/union/enum cannot be declared cpdef")
if s.systring == "enum":
return p_c_enum_definition(s, pos, ctx)
+ if ctx.overridable:
+ error(pos, "C struct/union cannot be declared cpdef")
else:
return p_c_struct_or_union_definition(s, pos, ctx)
else:
@@ -2472,7 +2472,8 @@ def p_c_enum_definition(s, pos, ctx):
return Nodes.CEnumDefNode(
pos, name = name, cname = cname, items = items,
typedef_flag = ctx.typedef_flag, visibility = ctx.visibility,
- api = ctx.api, in_pxd = ctx.level == 'module_pxd')
+ api = ctx.api, in_pxd = ctx.level == 'module_pxd',
+ is_overridable = ctx.overridable)
def p_c_enum_line(s, ctx, items):
if s.sy != 'pass':
View
1 tests/run/cpdef_enum.h
@@ -0,0 +1 @@
+#define BAR 2
View
19 tests/run/cpdef_enum.pyx
@@ -0,0 +1,19 @@
+"""
+>>> import cpdef_enum
+>>> FOO
+1
+>>> cpdef_enum.FOO
+1
+>>> BAR
+2
+>>> cpdef_enum.BAR
+2
+"""
+import sys
+
+cpdef enum:
+ FOO = 1
+
+cdef extern from "cpdef_enum.h":
+ cpdef enum:
+ BAR
Something went wrong with that request. Please try again.