summaryrefslogtreecommitdiff
path: root/Cython/StringIOTree.py
diff options
context:
space:
mode:
Diffstat (limited to 'Cython/StringIOTree.py')
-rw-r--r--Cython/StringIOTree.py70
1 files changed, 68 insertions, 2 deletions
diff --git a/Cython/StringIOTree.py b/Cython/StringIOTree.py
index d8239efed..798009758 100644
--- a/Cython/StringIOTree.py
+++ b/Cython/StringIOTree.py
@@ -23,6 +23,9 @@ EXAMPLE:
>>> b.getvalue().split()
['second', 'alpha', 'beta', 'gamma']
+>>> try: from cStringIO import StringIO
+... except ImportError: from io import StringIO
+
>>> i = StringIOTree()
>>> d.insert(i)
>>> _= i.write('inserted\n')
@@ -54,11 +57,23 @@ class StringIOTree(object):
self.write = stream.write
self.markers = []
+ def empty(self):
+ if self.stream.tell():
+ return False
+ return all([child.empty() for child in self.prepended_children]) if self.prepended_children else True
+
def getvalue(self):
- content = [x.getvalue() for x in self.prepended_children]
- content.append(self.stream.getvalue())
+ content = []
+ self._collect_in(content)
return "".join(content)
+ def _collect_in(self, target_list):
+ for x in self.prepended_children:
+ x._collect_in(target_list)
+ stream_content = self.stream.getvalue()
+ if stream_content:
+ target_list.append(stream_content)
+
def copyto(self, target):
"""Potentially cheaper than getvalue as no string concatenation
needs to happen."""
@@ -78,6 +93,12 @@ class StringIOTree(object):
self.stream = StringIO()
self.write = self.stream.write
+ def reset(self):
+ self.prepended_children = []
+ self.markers = []
+ self.stream = StringIO()
+ self.write = self.stream.write
+
def insert(self, iotree):
"""
Insert a StringIOTree (and all of its contents) at this location.
@@ -106,3 +127,48 @@ class StringIOTree(object):
def allmarkers(self):
children = self.prepended_children
return [m for c in children for m in c.allmarkers()] + self.markers
+
+ """
+ # Print the result of allmarkers in a nice human-readable form. Use it only for debugging.
+ # Prints e.g.
+ # /path/to/source.pyx:
+ # cython line 2 maps to 3299-3343
+ # cython line 4 maps to 2236-2245 2306 3188-3201
+ # /path/to/othersource.pyx:
+ # cython line 3 maps to 1234-1270
+ # ...
+ # Note: In the example above, 3343 maps to line 2, 3344 does not.
+ def print_hr_allmarkers(self):
+ from collections import defaultdict
+ markers = self.allmarkers()
+ totmap = defaultdict(lambda: defaultdict(list))
+ for c_lineno, (cython_desc, cython_lineno) in enumerate(markers):
+ if cython_lineno > 0 and cython_desc.filename is not None:
+ totmap[cython_desc.filename][cython_lineno].append(c_lineno + 1)
+ reprstr = ""
+ if totmap == 0:
+ reprstr += "allmarkers is empty\n"
+ try:
+ sorted(totmap.items())
+ except:
+ print(totmap)
+ print(totmap.items())
+ for cython_path, filemap in sorted(totmap.items()):
+ reprstr += cython_path + ":\n"
+ for cython_lineno, c_linenos in sorted(filemap.items()):
+ reprstr += "\tcython line " + str(cython_lineno) + " maps to "
+ i = 0
+ while i < len(c_linenos):
+ reprstr += str(c_linenos[i])
+ flag = False
+ while i+1 < len(c_linenos) and c_linenos[i+1] == c_linenos[i]+1:
+ i += 1
+ flag = True
+ if flag:
+ reprstr += "-" + str(c_linenos[i]) + " "
+ i += 1
+ reprstr += "\n"
+
+ import sys
+ sys.stdout.write(reprstr)
+ """