fix the declaration/initilization/cleanup of module global cdef Python objects
authorLisandro Dalcin <dalcinl@gmail.com>
Thu, 16 Oct 2008 23:26:34 +0000 (20:26 -0300)
committerLisandro Dalcin <dalcinl@gmail.com>
Thu, 16 Oct 2008 23:26:34 +0000 (20:26 -0300)
Cython/Compiler/ModuleNode.py
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/Symtab.py
tests/compile/globvardef.pyx

index 035efbde9047acd7e417294eb82389ed3cab363d..4c76c25973e9f2676cd303b75e337933473a5087 100644 (file)
@@ -1633,7 +1633,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
             rev_entries.reverse()
             for entry in rev_entries:
                 if entry.visibility != 'extern':
-                    if entry.type.is_pyobject:
+                    if entry.type.is_pyobject and entry.used:
                         code.put_var_decref_clear(entry)
         if Options.generate_cleanup_code >= 3:
             code.putln("/*--- Type import cleanup code ---*/")
@@ -1734,7 +1734,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         # variables to None.
         for entry in env.var_entries:
             if entry.visibility != 'extern':
-                if entry.type.is_pyobject:
+                if entry.type.is_pyobject and entry.used:
                     code.put_init_var_to_py_none(entry)
 
     def generate_c_function_export_code(self, env, code):
index a32c169afb1ba54e5dd766d890340d6f5fe469ca..6cb72bf2daa510a2d9acd680d72fd34680b6e1a8 100644 (file)
@@ -183,9 +183,10 @@ class PostParse(CythonTransform):
                                     handler(decl)
                                     continue # Remove declaration
                             raise PostParseError(decl.pos, ERR_CDEF_INCLASS)
+                        first_assignment = self.scope_type != 'module'
                         stats.append(SingleAssignmentNode(node.pos,
                             lhs=NameNode(node.pos, name=declbase.name),
-                            rhs=declbase.default, first=True))
+                            rhs=declbase.default, first=first_assignment))
                         declbase.default = None
                 newdecls.append(decl)
             node.declarators = newdecls
index c166953b775d72880f09a8c63a7f59a0a34a1b75..3348e4349a2e6668b344ec801dae025965674a61 100644 (file)
@@ -934,6 +934,8 @@ class ModuleScope(Scope):
             entry.is_pyglobal = 1
         else:
             entry.is_cglobal = 1
+            if entry.type.is_pyobject:
+                entry.init = 0
             self.var_entries.append(entry)
         return entry
     
index 1e3debb69158422c750b1d6a3096420cb017fea6..9349016a2ac036b3e5fc29c47bf3257210c4c105 100644 (file)
@@ -2,4 +2,6 @@ cdef int a_global_int
 cdef a_global_pyobject
 
 a_global_int = 0
-a_global_pyobject = None
\ No newline at end of file
+a_global_pyobject = None
+
+cdef object unused