From e2f43afd009d32b6c58369e90c50bea9d57a4d08 Mon Sep 17 00:00:00 2001
From: Lisandro Dalcin <dalcinl@gmail.com>
Date: Thu, 16 Oct 2008 20:26:34 -0300
Subject: [PATCH] fix the declaration/initilization/cleanup of module global
 cdef Python objects

---
 Cython/Compiler/ModuleNode.py          | 4 ++--
 Cython/Compiler/ParseTreeTransforms.py | 3 ++-
 Cython/Compiler/Symtab.py              | 2 ++
 tests/compile/globvardef.pyx           | 4 +++-
 4 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py
index 035efbde9..4c76c2597 100644
--- a/Cython/Compiler/ModuleNode.py
+++ b/Cython/Compiler/ModuleNode.py
@@ -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):
diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py
index a32c169af..6cb72bf2d 100644
--- a/Cython/Compiler/ParseTreeTransforms.py
+++ b/Cython/Compiler/ParseTreeTransforms.py
@@ -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
diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py
index c166953b7..3348e4349 100644
--- a/Cython/Compiler/Symtab.py
+++ b/Cython/Compiler/Symtab.py
@@ -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
     
diff --git a/tests/compile/globvardef.pyx b/tests/compile/globvardef.pyx
index 1e3debb69..9349016a2 100644
--- a/tests/compile/globvardef.pyx
+++ b/tests/compile/globvardef.pyx
@@ -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
-- 
2.30.9