Ticket #208: if_patch.txt

File if_patch.txt, 1.8 KB (added by mlh, 3 years ago)
Line 
1# HG changeset patch
2# User Magnus Lie Hetland <magnus@hetland.org>
3# Date 1234093755 -3600
4# Node ID ed31690f90c99699e8a4e5720a6a8d38e12eed10
5# Parent  3b522098d1335b9a7f0b60604c8ceac12424e40f
6Added if around for loop (for issue 208)
7
8diff -r 3b522098d133 -r ed31690f90c9 Cython/Compiler/Nodes.py
9--- a/Cython/Compiler/Nodes.py  Sun Feb 08 09:25:28 2009 +0100
10+++ b/Cython/Compiler/Nodes.py  Sun Feb 08 12:49:15 2009 +0100
11@@ -3903,6 +3903,7 @@
12             
13     def generate_execution_code(self, code):
14         old_loop_labels = code.new_loop_labels()
15+        from_range = getattr(self, "from_range", False)
16         self.bound1.generate_evaluation_code(code)
17         self.bound2.generate_evaluation_code(code)
18         offset, incop = self.relation_table[self.relation1]
19@@ -3916,6 +3917,12 @@
20             incop = "%s=%s" % (incop[0], step)
21             decop = "%s=%s" % (decop[0], step)
22         loopvar_name = self.loopvar_node.result()
23+        if from_range:
24+            # Skip the loop entirely (and avoid assigning to the loopvar) if
25+            # the loop is empty:
26+            code.putln("if (%s%s %s %s) {" % (
27+            self.bound1.result(), offset, self.relation2, self.bound2.result()
28+            ))
29         code.putln(
30             "for (%s = %s%s; %s %s %s; %s%s) {" % (
31                 loopvar_name,
32@@ -3927,9 +3934,11 @@
33             self.target.generate_assignment_code(self.py_loopvar_node, code)
34         self.body.generate_execution_code(code)
35         code.put_label(code.continue_label)
36-        if getattr(self, "from_range", False):
37+        if from_range:
38             # Undo last increment to maintain Python semantics:
39             code.putln("} %s%s;" % (loopvar_name, decop))
40+            # End the outer if statement:
41+            code.putln("} /* end if */")
42         else:
43             code.putln("}")
44         break_label = code.break_label