fix C++ namespace for enum items
authorLisandro Dalcin <dalcinl@gmail.com>
Thu, 22 Jul 2010 14:54:21 +0000 (11:54 -0300)
committerLisandro Dalcin <dalcinl@gmail.com>
Thu, 22 Jul 2010 14:54:21 +0000 (11:54 -0300)
Cython/Compiler/Parsing.py
tests/compile/cpp_enums.h [new file with mode: 0644]
tests/compile/cpp_enums.pyx [new file with mode: 0644]

index f36fbdd90d66b47a9d96454e325ab5455a895072..d41e883793607ef0bd05a1614bb95251ec7e186e 100644 (file)
@@ -2216,34 +2216,36 @@ def p_c_enum_definition(s, pos, ctx):
     s.expect(':')
     items = []
     if s.sy != 'NEWLINE':
-        p_c_enum_line(s, items)
+        p_c_enum_line(s, ctx, items)
     else:
         s.next() # 'NEWLINE'
         s.expect_indent()
         while s.sy not in ('DEDENT', 'EOF'):
-            p_c_enum_line(s, items)
+            p_c_enum_line(s, ctx, items)
         s.expect_dedent()
     return Nodes.CEnumDefNode(
         pos, name = name, cname = cname, items = items,
         typedef_flag = ctx.typedef_flag, visibility = ctx.visibility,
         in_pxd = ctx.level == 'module_pxd')
 
-def p_c_enum_line(s, items):
+def p_c_enum_line(s, ctx, items):
     if s.sy != 'pass':
-        p_c_enum_item(s, items)
+        p_c_enum_item(s, ctx, items)
         while s.sy == ',':
             s.next()
             if s.sy in ('NEWLINE', 'EOF'):
                 break
-            p_c_enum_item(s, items)
+            p_c_enum_item(s, ctx, items)
     else:
         s.next()
     s.expect_newline("Syntax error in enum item list")
 
-def p_c_enum_item(s, items):
+def p_c_enum_item(s, ctx, items):
     pos = s.position()
     name = p_ident(s)
     cname = p_opt_cname(s)
+    if cname is None and ctx.namespace is not None:
+        cname = ctx.namespace + "::" + name
     value = None
     if s.sy == '=':
         s.next()
diff --git a/tests/compile/cpp_enums.h b/tests/compile/cpp_enums.h
new file mode 100644 (file)
index 0000000..4ea444e
--- /dev/null
@@ -0,0 +1,11 @@
+enum Enum1 {
+  Item1,
+  Item2
+};
+
+namespace Namespace1 {
+  enum Enum2 {
+    Item3,
+    Item4
+  };
+}
diff --git a/tests/compile/cpp_enums.pyx b/tests/compile/cpp_enums.pyx
new file mode 100644 (file)
index 0000000..bccfacd
--- /dev/null
@@ -0,0 +1,24 @@
+cdef extern from "cpp_enums.h":
+    cdef enum Enum1:
+        Item1
+        Item2
+
+a = Item1
+b = Item2
+
+cdef Enum1 x, y
+x = Item1
+y = Item2
+
+cdef extern from "cpp_enums.h" namespace "Namespace1":
+    cdef enum Enum2:
+        Item3
+        Item4
+
+c = Item3
+d = Item4
+
+cdef Enum2 z, w
+z = Item3
+w = Item4
+