From 518b87b57b25f7dd7bd87e494566f2ba955791f3 Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Sat, 29 Nov 2008 20:31:46 +0100 Subject: [PATCH] Remove use of TempNode from ForInStatNode to simplify temp flow slightly --- Cython/Compiler/ExprNodes.py | 27 +++++++++++++++++---------- Cython/Compiler/Nodes.py | 2 ++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index b7b328cf..9c5a7b2e 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1324,6 +1324,10 @@ class ImportNode(ExprNode): class IteratorNode(ExprNode): # Used as part of for statement implementation. + # + # allocate_counter_temp/release_counter_temp needs to be called + # by parent (ForInStatNode) + # # Implements result = iter(sequence) # # sequence ExprNode @@ -1336,16 +1340,19 @@ class IteratorNode(ExprNode): self.type = py_object_type self.gil_check(env) self.is_temp = 1 - - self.counter = TempNode(self.pos, PyrexTypes.c_py_ssize_t_type, env) - self.counter.allocate_temp(env) gil_message = "Iterating over Python object" def release_temp(self, env): env.release_temp(self.result()) - self.counter.release_temp(env) - + + def allocate_counter_temp(self, code): + self.counter_cname = code.funcstate.allocate_temp( + PyrexTypes.c_py_ssize_t_type, manage_ref=False) + + def release_counter_temp(self, code): + code.funcstate.release_temp(self.counter_cname) + def generate_result_code(self, code): is_builtin_sequence = self.sequence.type is list_type or \ self.sequence.type is tuple_type @@ -1359,7 +1366,7 @@ class IteratorNode(ExprNode): self.sequence.py_result())) code.putln( "%s = 0; %s = %s; Py_INCREF(%s);" % ( - self.counter.result(), + self.counter_cname, self.result(), self.sequence.py_result(), self.result())) @@ -1370,7 +1377,7 @@ class IteratorNode(ExprNode): code.error_goto(self.pos)) else: code.putln("%s = -1; %s = PyObject_GetIter(%s); %s" % ( - self.counter.result(), + self.counter_cname, self.result(), self.sequence.py_result(), code.error_goto_if_null(self.result(), self.pos))) @@ -1406,7 +1413,7 @@ class NextNode(AtomicExprNode): prefix, self.iterator.py_result())) code.putln( "if (%s >= Py%s_GET_SIZE(%s)) break;" % ( - self.iterator.counter.result(), + self.iterator.counter_cname, prefix, self.iterator.py_result())) code.putln( @@ -1414,9 +1421,9 @@ class NextNode(AtomicExprNode): self.result(), prefix, self.iterator.py_result(), - self.iterator.counter.result(), + self.iterator.counter_cname, self.result(), - self.iterator.counter.result())) + self.iterator.counter_cname)) if len(type_checks) > 1: code.put("} else ") if len(type_checks) == 1: diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 7982bfb5..6c2c595c 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -3737,6 +3737,7 @@ class ForInStatNode(LoopNode, StatNode): def generate_execution_code(self, code): old_loop_labels = code.new_loop_labels() + self.iterator.allocate_counter_temp(code) self.iterator.generate_evaluation_code(code) code.putln( "for (;;) {") @@ -3753,6 +3754,7 @@ class ForInStatNode(LoopNode, StatNode): self.else_clause.generate_execution_code(code) code.putln("}") code.put_label(break_label) + self.iterator.release_counter_temp(code) self.iterator.generate_disposal_code(code) def annotate(self, code): -- 2.26.2