cache builtins at module level
authorRobert Bradshaw <robertwb@math.washington.edu>
Thu, 25 Oct 2007 04:35:02 +0000 (21:35 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Thu, 25 Oct 2007 04:35:02 +0000 (21:35 -0700)
Cython/Compiler/ModuleNode.py
Cython/Compiler/Nodes.py
Cython/Compiler/Symtab.py

index f541f1f1eefb5b507fe19a5cd0dee0291c081828..74708b2439bd53ee512e68ac9e6135daa9476a15 100644 (file)
@@ -1293,7 +1293,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
                 code.put_decref("((PyObject*)%s)" % type.typeptr_cname, PyrexTypes.py_object_type)
         if Options.cache_builtins:
             code.putln("/*--- Builtin cleanup code ---*/")
-            for entry in env.builtin_scope().cached_entries:
+            for entry in env.cached_builtins:
                 code.put_var_decref_clear(entry)
         code.putln("/*--- Intern cleanup code ---*/")
         for entry in env.pynum_entries:
@@ -1370,7 +1370,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
     def generate_builtin_init_code(self, env, code):
         # Lookup and cache builtin objects.
         if Options.cache_builtins:
-            for entry in env.builtin_scope().cached_entries:
+            for entry in env.cached_builtins:
                 if Options.intern_names:
                     #assert entry.interned_cname is not None
                     code.putln(
index 515ed39439d603f7c0cf1d26b14d8e3b008df3a7..00082716e58dfed99841b1c28a7e093519957043 100644 (file)
@@ -129,7 +129,7 @@ class BlockNode:
             del entries[:]
 
     def generate_cached_builtins_decls(self, env, code):
-        entries = env.builtin_scope().undeclared_cached_entries
+        entries = env.global_scope().undeclared_cached_builtins
         if len(entries) > 0:
             code.putln("")
         for entry in entries:
index 3ad1b4844a4ee36842f2dc23b1886e17309c012b..f1ae18ff3badebf2f95055c3d3466e638d145de4 100644 (file)
@@ -560,8 +560,6 @@ class BuiltinScope(Scope):
         for name, definition in self.builtin_entries.iteritems():
             cname, type = definition
             self.declare_var(name, type, None, cname)
-        self.cached_entries = []
-        self.undeclared_cached_entries = []
         
     def declare_builtin(self, name, pos):
         if not hasattr(__builtin__, name):
@@ -569,16 +567,6 @@ class BuiltinScope(Scope):
                 return self.outer_scope.declare_builtin(name, pos)
             else:
                 error(pos, "undeclared name not builtin: %s"%name)
-        entry = self.declare(name, name, py_object_type, pos)
-        if Options.cache_builtins:
-            entry.is_builtin = 1
-            entry.is_const = 1
-            entry.cname = Naming.builtin_prefix + name
-            self.cached_entries.append(entry)
-            self.undeclared_cached_entries.append(entry)
-        else:
-            entry.is_builtin = 1
-        return entry
     
     def declare_builtin_cfunction(self, name, type, cname, python_equiv = None,
             utility_code = None):
@@ -688,6 +676,8 @@ class ModuleScope(Scope):
         self.types_imported = {}
         self.pynum_entries = []
         self.has_extern_class = 0
+        self.cached_builtins = []
+        self.undeclared_cached_builtins = []
     
     def qualifying_scope(self):
         return self.parent_module
@@ -696,8 +686,20 @@ class ModuleScope(Scope):
         return self
     
     def declare_builtin(self, name, pos):
-        entry = Scope.declare_builtin(self, name, pos)
-        #entry.interned_cname = self.intern(name)
+        if not hasattr(__builtin__, name):
+            if self.outer_scope is not None:
+                return self.outer_scope.declare_builtin(name, pos)
+            else:
+                error(pos, "undeclared name not builtin: %s"%name)
+        entry = self.declare(name, name, py_object_type, pos)
+        if Options.cache_builtins:
+            entry.is_builtin = 1
+            entry.is_const = 1
+            entry.cname = Naming.builtin_prefix + name
+            self.cached_builtins.append(entry)
+            self.undeclared_cached_builtins.append(entry)
+        else:
+            entry.is_builtin = 1
         return entry
     
     def intern(self, name):