Class members in cdef classes
authorRobert Bradshaw <robertwb@math.washington.edu>
Wed, 19 Sep 2007 00:56:11 +0000 (17:56 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Wed, 19 Sep 2007 00:56:11 +0000 (17:56 -0700)
Cython/Compiler/Nodes.py
Cython/Compiler/Symtab.py

index ac52ce68bbaec74f8aadd0177bfcfe4636a6c2fd..0aa2aa746917b1bc91a1bb56caed13dd8a070fab 100644 (file)
@@ -1370,9 +1370,8 @@ class CClassDefNode(StatNode):
         env.allocate_vtable_names(self.entry)
         
     def analyse_expressions(self, env):
-        scope = self.entry.type.scope
         if self.body:
-            self.body.analyse_expressions(scope)
+            self.body.analyse_expressions(env)
     
     def generate_function_definitions(self, env, code):
         if self.body:
index bd828ab48f3f616de6a5e4eb13ed633a617d1a84..29e6f69edb1a1ef49545c5b75d5075ffc6f06edd 100644 (file)
@@ -1050,45 +1050,56 @@ class CClassScope(ClassScope):
 
     def declare_var(self, name, type, pos, 
             cname = None, visibility = 'private', is_cdef = 0):
-        # Add an entry for an attribute.
-        if self.defined:
-            error(pos,
-                "C attributes cannot be added in implementation part of"
-                " extension type")
-        if get_special_method_signature(name):
-            error(pos, 
-                "The name '%s' is reserved for a special method."
-                    % name)
-        if not cname:
-            cname = name
-        entry = self.declare(name, cname, type, pos)
-        entry.visibility = visibility
-        entry.is_variable = 1
-        self.var_entries.append(entry)
-        if type.is_pyobject:
-            self.has_pyobject_attrs = 1
-        if visibility not in ('private', 'public', 'readonly'):
-            error(pos,
-                "Attribute of extension type cannot be declared %s" % visibility)
-        if visibility in ('public', 'readonly'):
-            if type.pymemberdef_typecode:
-                self.public_attr_entries.append(entry)
-                if name == "__weakref__":
-                    error(pos, "Special attribute __weakref__ cannot be exposed to Python")
-            else:
+        if is_cdef:
+            # Add an entry for an attribute.
+            if self.defined:
                 error(pos,
-                    "C attribute of type '%s' cannot be accessed from Python" % type)
-        if visibility == 'public' and type.is_extension_type:
-            error(pos,
-                "Non-generic Python attribute cannot be exposed for writing from Python")
-        return entry
+                    "C attributes cannot be added in implementation part of"
+                    " extension type")
+            if get_special_method_signature(name):
+                error(pos, 
+                    "The name '%s' is reserved for a special method."
+                        % name)
+            if not cname:
+                cname = name
+            entry = self.declare(name, cname, type, pos)
+            entry.visibility = visibility
+            entry.is_variable = 1
+            self.var_entries.append(entry)
+            if type.is_pyobject:
+                self.has_pyobject_attrs = 1
+            if visibility not in ('private', 'public', 'readonly'):
+                error(pos,
+                    "Attribute of extension type cannot be declared %s" % visibility)
+            if visibility in ('public', 'readonly'):
+                if type.pymemberdef_typecode:
+                    self.public_attr_entries.append(entry)
+                    if name == "__weakref__":
+                        error(pos, "Special attribute __weakref__ cannot be exposed to Python")
+                else:
+                    error(pos,
+                        "C attribute of type '%s' cannot be accessed from Python" % type)
+            if visibility == 'public' and type.is_extension_type:
+                error(pos,
+                    "Non-generic Python attribute cannot be exposed for writing from Python")
+            return entry
+        else:
+            # Add an entry for a class attribute.
+            entry = Scope.declare_var(self, name, type, pos, 
+                cname, visibility, is_cdef)
+            entry.is_pyglobal = 1
+            entry.namespace_cname = "(PyObject *)%s" % self.parent_type.typeptr_cname
+            if Options.intern_names:
+                entry.interned_cname = self.intern(name)
+            return entry
+            
 
     def declare_pyfunction(self, name, pos):
         # Add an entry for a method.
         if name in ('__eq__', '__ne__', '__lt__', '__gt__', '__le__', '__ge__'):
             error(pos, "Special method %s must be implemented via __richcmp__" 
 % name)
-        entry = self.declare_var(name, py_object_type, pos)
+        entry = self.declare(name, name, py_object_type, pos)
         special_sig = get_special_method_signature(name)
         if special_sig:
             # Special methods get put in the method table with a particular