Moved cached builtin init to Code.py for consistency
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Sun, 3 Aug 2008 19:20:44 +0000 (21:20 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Sun, 3 Aug 2008 19:20:44 +0000 (21:20 +0200)
Cython/Compiler/Code.py
Cython/Compiler/ModuleNode.py

index f41f4fb46e02e2bc1887e5a677f796df5a5b0b9a..7d4d3d0df7ec98e65596f1c6ccaf0aaa37b94f06 100644 (file)
@@ -168,8 +168,13 @@ class GlobalState(object):
         self.utildefwriter = rootwriter.new_writer()
         self.decls_writer = rootwriter.new_writer()
         self.pystring_table = rootwriter.new_writer()
+        self.init_cached_builtins_writer = rootwriter.new_writer()
         self.initwriter = rootwriter.new_writer()
 
+        if Options.cache_builtins:
+            self.init_cached_builtins_writer.enter_cfunc_scope()
+            self.init_cached_builtins_writer.putln("static int __Pyx_InitCachedBuiltins(void) {")
+
         self.initwriter.enter_cfunc_scope()
         self.initwriter.putln("").putln("static int __Pyx_InitGlobals(void) {")
 
@@ -197,19 +202,28 @@ class GlobalState(object):
                     Naming.stringtab_cname,
                     self.initwriter.error_goto(self.module_pos)))
 
+        if Options.cache_builtins:
+            (self.init_cached_builtins_writer
+             .putln("return 0;")
+             .put_label(self.init_cached_builtins_writer.error_label)
+             .putln("return -1;")
+             .putln("}")
+             .exit_cfunc_scope()
+             )
+
         (self.initwriter
          .putln("return 0;")
          .put_label(self.initwriter.error_label)
          .putln("return -1;")
          .putln("}")
+         .exit_cfunc_scope()
          )
-        self.initwriter.exit_cfunc_scope()
          
-    def insert_py_string_table_into(self, code):
+    def insert_initcode_into(self, code):
         if self.pystring_table_needed:
             code.insert(self.pystring_table)
-
-    def insert_initglobals_into(self, code):
+        if Options.cache_builtins:
+            code.insert(self.init_cached_builtins_writer)
         code.insert(self.initwriter)
 
     def put_pyobject_decl(self, entry):
@@ -268,8 +282,16 @@ class GlobalState(object):
             self.put_pyobject_decl(entry)
         
     def add_cached_builtin_decl(self, entry):
-        if self.should_declare(entry.cname, entry):
-            self.put_pyobject_decl(entry)
+        if Options.cache_builtins:
+            if self.should_declare(entry.cname, entry):
+                self.put_pyobject_decl(entry)
+                self.init_cached_builtins_writer.putln('%s = __Pyx_GetName(%s, %s); if (!%s) %s' % (
+                    entry.cname,
+                    Naming.builtins_cname,
+                    entry.interned_cname,
+                    entry.cname,
+                    self.init_cached_builtins_writer.error_goto(entry.pos)))
+
 
     #
     # File name state
index c31a781e7c1ba2d9bcb406842b4b23dd4062effe..e1b638e0801dd6f7de8012de127c008fde2204e8 100644 (file)
@@ -256,7 +256,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         self.generate_cached_builtins_decls(env, code)
         self.body.generate_function_definitions(env, code, options.transforms)
         code.mark_pos(None)
-        self.generate_py_string_table(env, code)
         self.generate_typeobj_definitions(env, code)
         self.generate_method_table(env, code)
         self.generate_filename_init_prototype(code)
@@ -1456,9 +1455,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
             code.putln(
                 "};")
 
-    def generate_py_string_table(self, env, code):
-        code.globalstate.insert_py_string_table_into(code)
-
     def generate_filename_init_prototype(self, code):
         code.putln("");
         code.putln("static void %s(void); /*proto*/" % Naming.fileinit_cname)
@@ -1527,7 +1523,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
 
     def generate_module_init_func(self, imported_modules, env, code):
         # Insert code stream of __Pyx_InitGlobals
-        code.globalstate.insert_initglobals_into(code)
+        code.globalstate.insert_initcode_into(code)
         
         code.enter_cfunc_scope()
         code.putln("")
@@ -1556,8 +1552,9 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
 
         if Options.cache_builtins:
             code.putln("/*--- Builtin init code ---*/")
-            self.generate_builtin_init_code(env, code)
-            
+            code.putln(code.error_goto_if_neg("__Pyx_InitCachedBuiltins()",
+                                              self.pos))
+                
         code.putln("%s = 0;" % Naming.skip_dispatch_cname);
 
         code.putln("/*--- Global init code ---*/")
@@ -1714,19 +1711,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
                     Naming.preimport_cname,
                     code.error_goto(self.pos)));
 
-    def generate_builtin_init_code(self, env, code):
-        # Lookup and cache builtin objects.
-        if Options.cache_builtins:
-            for entry in env.cached_builtins:
-                #assert entry.interned_cname is not None
-                code.putln(
-                    '%s = __Pyx_GetName(%s, %s); if (!%s) %s' % (
-                    entry.cname,
-                    Naming.builtins_cname,
-                    entry.interned_cname,
-                    entry.cname,
-                    code.error_goto(entry.pos)))
-    
     def generate_global_init_code(self, env, code):
         # Generate code to initialise global PyObject *
         # variables to None.