From 9a9ed5660f5b9c630c1a3af8a7a6e75e3b327d15 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Wed, 17 Nov 2010 06:30:56 +0100 Subject: [PATCH] simplify (and centralise) module cleanup code generation for tuple constants --- Cython/Compiler/Code.py | 19 +++++++++---------- Cython/Compiler/ExprNodes.py | 5 +---- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py index fae56888..7592de39 100644 --- a/Cython/Compiler/Code.py +++ b/Cython/Compiler/Code.py @@ -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): diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 4eb84f91..bad9b5f6 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -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) -- 2.26.2