disallow names as keys in struct dict literals
authorRobert Bradshaw <robertwb@math.washington.edu>
Sun, 19 Oct 2008 07:06:47 +0000 (00:06 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sun, 19 Oct 2008 07:06:47 +0000 (00:06 -0700)
Cython/Compiler/ExprNodes.py
tests/run/struct_conversion.pyx

index d9faada43d17432e87f49e3efb66f006fa6f98bc..02b9c55bd6e7643862203de0b884f31eea145cd8 100644 (file)
@@ -1880,7 +1880,7 @@ class CallNode(ExprNode):
             args, kwds = self.explicit_args_kwds()
             items = []
             for arg, member in zip(args, type.scope.var_entries):
-                items.append(DictItemNode(pos=arg.pos, key=NameNode(pos=arg.pos, name=member.name), value=arg))
+                items.append(DictItemNode(pos=arg.pos, key=IdentifierStringNode(pos=arg.pos, value=member.name), value=arg))
             if kwds:
                 items += kwds.key_value_pairs
             self.key_value_pairs = items
@@ -2947,15 +2947,13 @@ class DictNode(ExprNode):
             for item in self.key_value_pairs:
                 if isinstance(item.key, CoerceToPyTypeNode):
                     item.key = item.key.arg
-                if isinstance(item.key, (StringNode, IdentifierStringNode)):
-                    item.key = NameNode(pos=item.key.pos, name=item.key.value)
-                if not isinstance(item.key, NameNode):
-                    print item.key
-                    error(item.key.pos, "Struct field must be a name")
+                if not isinstance(item.key, (StringNode, IdentifierStringNode)):
+                    error(item.key.pos, "Invalid struct field identifier")
+                    item.key = IdentifierStringNode(item.key.pos, value="<error>")
                 else:
-                    member = dst_type.scope.lookup_here(item.key.name)
+                    member = dst_type.scope.lookup_here(item.key.value)
                     if not member:
-                        error(item.key.pos, "struct '%s' has no field '%s'" % (dst_type, item.key.name))
+                        error(item.key.pos, "struct '%s' has no field '%s'" % (dst_type, item.key.value))
                     else:
                         value = item.value
                         if isinstance(value, CoerceToPyTypeNode):
@@ -3003,7 +3001,7 @@ class DictNode(ExprNode):
             else:
                 code.putln("%s.%s = %s;" % (
                         self.result(),
-                        item.key.name,
+                        item.key.value,
                         item.value.result()))
             item.generate_disposal_code(code)
             
index 3fd12ef418ce991cb2900fdf7e694c75855523cd..39921d0fe01d20a5811b40d8b5ebca6099f049e8 100644 (file)
@@ -40,7 +40,7 @@ def test_constructor_kwds(x, y, color):
     return p
     
 def test_dict_construction(x, y, color):
-    cdef Point p = {color: color, x: x, y: y}
+    cdef Point p = {'color': color, 'x': x, 'y': y}
     return p
 
 cdef union int_or_float:
@@ -53,8 +53,8 @@ cdef struct with_pointers:
     void* ptr
     
 def test_pointers(int n, double x):
-    cdef with_pointers a = [True, {n: n}, NULL]
-    cdef with_pointers b = with_pointers(False, {x: x}, NULL)
+    cdef with_pointers a = [True, {'n': n}, NULL]
+    cdef with_pointers b = with_pointers(False, {'x': x}, NULL)
     print a.data.n
     print b.data.x
-    print a.ptr == b.ptr == NULL
\ No newline at end of file
+    print a.ptr == b.ptr == NULL