From 27a95b9bdcdc45e95209ec92e546c0e439ecd2c9 Mon Sep 17 00:00:00 2001
From: Robert Bradshaw <robertwb@gmail.com>
Date: Tue, 14 Jan 2014 00:17:47 -0800
Subject: [PATCH] Fix cdef class ordering in the presence of pxi includes.

---
 Cython/Compiler/Nodes.py | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py
index 2a17c18d0..448530fd9 100644
--- a/Cython/Compiler/Nodes.py
+++ b/Cython/Compiler/Nodes.py
@@ -380,7 +380,20 @@ class StatListNode(Node):
     def analyse_declarations(self, env):
         #print "StatListNode.analyse_declarations" ###
         base_classes = {}
-        for stat in self.stats:
+        def flatten(stats):
+            # Common case is trivial flatten.
+            if not [stat for stat in stats if isinstance(stat, StatListNode)]:
+                return stats
+            else:
+                all = []
+                for stat in stats:
+                    if isinstance(stat, StatListNode):
+                        all.extend(flatten(stat.stats))
+                    else:
+                        all.append(stat)
+                return all
+        flattened = flatten(self.stats)
+        for stat in flattened:
             if isinstance(stat, CClassDefNode) and not stat.base_class_module:
                 base_classes[stat.class_name] = stat.base_class_name
         @cached_function
@@ -391,7 +404,7 @@ class StatListNode(Node):
             else:
                 return depth(base_class) + 1
         keyed_stats = []
-        for ix, stat in enumerate(self.stats):
+        for ix, stat in enumerate(flattened):
             if isinstance(stat, CClassDefNode):
                 key = 20, depth(stat.class_name), ix
             else:
-- 
2.30.9