added dump_pos() method to Node class as a quick debugging way to figure out where...
authorStefan Behnel <stefan_ml@behnel.de>
Fri, 28 Sep 2012 16:04:00 +0000 (18:04 +0200)
committerStefan Behnel <stefan_ml@behnel.de>
Fri, 28 Sep 2012 16:04:00 +0000 (18:04 +0200)
Cython/Compiler/Nodes.py

index 07a3d44..5ed855d 100644 (file)
@@ -240,6 +240,8 @@ class Node(object):
             return pos
 
     def dump(self, level=0, filter_out=("pos",), cutoff=100, encountered=None):
+        """Debug helper method that returns a recursive string representation of this node.
+        """
         if cutoff == 0:
             return "<...nesting level cutoff...>"
         if encountered is None:
@@ -268,6 +270,24 @@ class Node(object):
             res += "%s>" % indent
             return res
 
+    def dump_pos(self, mark_column=False, marker='(#)'):
+        """Debug helper method that returns the source code context of this node as a string.
+        """
+        if not self.pos:
+            return u''
+        source_desc, line, col = self.pos
+        contents = source_desc.get_lines(encoding='ASCII',
+                                         error_handling='ignore')
+        # line numbers start at 1
+        lines = contents[max(0,line-3):line]
+        current = lines[-1]
+        if mark_column:
+            current = current[:col] + marker + current[col:]
+        lines[-1] = current.rstrip() + u'             # <<<<<<<<<<<<<<\n'
+        lines += contents[line:line+2]
+        return u'"%s":%d:%d\n%s\n' % (
+            source_desc.get_escaped_description(), line, col, u''.join(lines))
+
 class CompilerDirectivesNode(Node):
     """
     Sets compiler directives for the children nodes