copy ModuleNode changes to new file
authorRobert Bradshaw <robertwb@math.washington.edu>
Thu, 7 Jun 2007 04:16:25 +0000 (21:16 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Thu, 7 Jun 2007 04:16:25 +0000 (21:16 -0700)
Cython/Compiler/ModuleNode.py
Cython/Compiler/Nodes.py

index 3760e5c546cb471c312b5a479cdacdcf8fb274d4..1d7358e1b260c294d768029f9509849814651258 100644 (file)
@@ -22,7 +22,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
     #  body      StatListNode
     
     def analyse_declarations(self, env):
-        env.doc = self.doc
+        if Options.embed_pos_in_docstring:
+            env.doc = 'File: %s (starting at line %s)'%relative_position(self.pos)
+            if not self.doc is None:
+                env.doc = env.doc + '\\n' + self.doc
+        else:
+            env.doc = self.doc
         self.body.analyse_declarations(env)
     
     def process_implementation(self, env, result):
@@ -97,6 +102,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         self.generate_const_definitions(env, code)
         self.generate_interned_name_decls(env, code)
         self.generate_py_string_decls(env, code)
+        self.generate_cached_builtins_decls(env, code)
         self.body.generate_function_definitions(env, code)
         self.generate_interned_name_table(env, code)
         self.generate_py_string_table(env, code)
@@ -129,11 +135,19 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         code.putln('/* Generated by Pyrex %s on %s */' % (
             Version.version, time.asctime()))
         code.putln('')
+        code.putln('#define PY_SSIZE_T_CLEAN')
         for filename in env.python_include_files:
             code.putln('#include "%s"' % filename)
         code.putln("#ifndef PY_LONG_LONG")
         code.putln("  #define PY_LONG_LONG LONG_LONG")
         code.putln("#endif")
+        code.putln("#if PY_VERSION_HEX < 0x02050000")
+        code.putln("  typedef int Py_ssize_t;")
+        code.putln("  #define PY_SSIZE_T_MAX INT_MAX")
+        code.putln("  #define PY_SSIZE_T_MIN INT_MIN")
+        code.putln("  #define PyInt_FromSsize_t(z) PyInt_FromLong(z)")
+        code.putln("  #define PyInt_AsSsize_t(o)   PyInt_AsLong(o)")
+        code.putln("#endif")
         self.generate_extern_c_macro_definition(code)
         code.putln("%s double pow(double, double);" % Naming.extern_c_macro)
         self.generate_includes(env, cimported_modules, code)
@@ -445,7 +459,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
                 type.vtabptr_cname))
         for entry in py_attrs:
             if entry.name == "__weakref__":
-                code.putln("p->%s = 0;" % entry.cname)
+                code.putln("p->%s = NULL;" % entry.cname)
             else:
                 code.put_init_var_to_py_none(entry, "p->%s")
         entry = scope.lookup_here("__new__")
@@ -577,12 +591,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         # a __getitem__ method is present. It converts its
         # argument to a Python integer and calls mp_subscript.
         code.putln(
-            "static PyObject *%s(PyObject *o, int i) {" %
+            "static PyObject *%s(PyObject *o, Py_ssize_t i) {" %
                 scope.mangle_internal("sq_item"))
         code.putln(
                 "PyObject *r;")
         code.putln(
-                "PyObject *x = PyInt_FromLong(i); if(!x) return 0;")
+                "PyObject *x = PyInt_FromSsize(i); if(!x) return 0;")
         code.putln(
                 "r = o->ob_type->tp_as_mapping->mp_subscript(o, x);")
         code.putln(
@@ -668,7 +682,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         del_entry = scope.lookup_here("__delslice__")
         code.putln("")
         code.putln(
-            "static int %s(PyObject *o, int i, int j, PyObject *v) {" %
+            "static int %s(PyObject *o, Py_ssize_t i, Py_ssize_t j, PyObject *v) {" %
                 scope.mangle_internal("sq_ass_slice"))
         code.putln(
                 "if (v) {")
@@ -925,7 +939,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
             "0, /*ob_size*/")
         code.putln(
             '"%s.%s", /*tp_name*/' % (
-                modname, scope.class_name))
+                self.full_module_name, scope.class_name))
         if type.typedef_flag:
             objstruct = type.objstruct_cname
         else:
@@ -1054,6 +1068,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         self.generate_intern_code(env, code)
         #code.putln("/*--- String init code ---*/")
         self.generate_string_init_code(env, code)
+        #code.putln("/*--- Builtin init code ---*/")
+        self.generate_builtin_init_code(env, code)
         #code.putln("/*--- Global init code ---*/")
         self.generate_global_init_code(env, code)
 
@@ -1121,6 +1137,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
                 "if (__Pyx_InitStrings(%s) < 0) %s;" % (
                     Naming.stringtab_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.builtin_scope().cached_entries:
+                if Options.intern_names:
+                    #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)))
+                else:
+                    code.putln(
+                        '%s = __Pyx_GetName(%s, "%s"); if (!%s) %s' % (
+                        entry.cname,
+                        Naming.builtins_cname,
+                        self.entry.name,
+                        entry.cname, 
+                        code.error_goto(entry.pos)))
     
     def generate_global_init_code(self, env, code):
         # Generate code to initialise global PyObject *
index 298ab1b08ab3c74e833d2c83e2f0e3c464669546..933211697cf6618ec4ecb769a4ec2b712011a352 100644 (file)
@@ -115,6 +115,14 @@ class BlockNode:
                 code.putln(
                     "static PyObject *%s;" % entry.pystring_cname)
 
+    def generate_cached_builtins_decls(self, env, code):
+        entries = env.builtin_scope().undeclared_cached_entries
+        if len(entries) > 0:
+            code.putln("")
+        for entry in entries:
+            code.putln("static PyObject *%s;" % entry.cname)
+        del entries[:]
+
 
 class StatListNode(Node):
     # stats     a list of StatNode