simplify (and centralise) module cleanup code generation for tuple constants
authorStefan Behnel <scoder@users.berlios.de>
Wed, 17 Nov 2010 05:30:56 +0000 (06:30 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Wed, 17 Nov 2010 05:30:56 +0000 (06:30 +0100)
Cython/Compiler/Code.py
Cython/Compiler/ExprNodes.py

index fae568881b3f22c4293ec1b49c2ec604c43068b5..7592de39c9984096769fad067b74b30138628b71 100644 (file)
@@ -557,9 +557,6 @@ class GlobalState(object):
     def get_cached_constants_writer(self):
         return self.parts['cached_constants']
 
-    def get_globals_cleanup_writer(self):
-        return self.parts['cleanup_globals']
-
     def get_int_const(self, str_value, longness=False):
         longness = bool(longness)
         try:
@@ -568,9 +565,14 @@ class GlobalState(object):
             c = self.new_int_const(str_value, longness)
         return c
 
-    def get_py_const(self, type, prefix=''):
+    def get_py_const(self, type, prefix='', cleanup_level=None):
         # create a new Python object constant
-        return self.new_py_const(type, prefix)
+        const = self.new_py_const(type, prefix)
+        if cleanup_level is not None \
+               and cleanup_level >= Options.generate_cleanup_code:
+            cleanup_writer = self.parts['cleanup_globals']
+            cleanup_writer.put_xdecref_clear(const.cname, type, nanny=False)
+        return const
 
     def get_string_const(self, text):
         # return a C string constant, creating a new one if necessary
@@ -962,8 +964,8 @@ class CCodeWriter(object):
     def get_py_num(self, str_value, longness):
         return self.globalstate.get_int_const(str_value, longness).cname
 
-    def get_py_const(self, type, prefix=''):
-        return self.globalstate.get_py_const(type, prefix).cname
+    def get_py_const(self, type, prefix='', cleanup_level=None):
+        return self.globalstate.get_py_const(type, prefix, cleanup_level).cname
 
     def get_string_const(self, text):
         return self.globalstate.get_string_const(text).cname
@@ -983,9 +985,6 @@ class CCodeWriter(object):
     def get_cached_constants_writer(self):
         return self.globalstate.get_cached_constants_writer()
 
-    def get_globals_cleanup_writer(self):
-        return self.globalstate.get_globals_cleanup_writer()
-
     # code generation
 
     def putln(self, code = "", safe=False):
index 4eb84f91b726b8a022c3e8714dd0a116195b1e90..bad9b5f6450a089c6ec75e346e14af02af12f1ab 100755 (executable)
@@ -3963,10 +3963,7 @@ class TupleNode(SequenceNode):
         if self.is_literal:
             # non-empty cached tuple => result is global constant,
             # creation code goes into separate code writer
-            self.result_code = code.get_py_const(py_object_type, 'tuple_')
-            if Options.generate_cleanup_code >= 2:
-                cleanup_writer = code.get_globals_cleanup_writer()
-                cleanup_writer.put_xdecref_clear(self.result(), py_object_type, nanny=False)
+            self.result_code = code.get_py_const(py_object_type, 'tuple_', cleanup_level=2)
             code = code.get_cached_constants_writer()
             code.mark_pos(self.pos)