stringutil: emit multiple chunks when pretty printing
authorGregory Szorc <gregory.szorc@gmail.com>
Mon, 27 Aug 2018 09:05:56 -0700
changeset 39354 5ed7c6caf24d
parent 39353 0d21b1f1722c
child 39355 bca0546a44bf
stringutil: emit multiple chunks when pretty printing This avoids concatenating output inside pprintgen() itself. But the real reason for this is it will make it easier to add indentation, as we'll need to account for indentation when emitting each individual object in a collection. The verbosity of this code compared to the original is a bit unfortunate. But I suppose this is the price to pay for having nice things (streaming and indenting). We could probably abstract the "print a collection" bits into a generic function to avoid some duplication. But I'm not overly inclined to do this. Differential Revision: https://phab.mercurial-scm.org/D4398
mercurial/utils/stringutil.py
--- a/mercurial/utils/stringutil.py	Mon Aug 27 09:02:39 2018 -0700
+++ b/mercurial/utils/stringutil.py	Mon Aug 27 09:05:56 2018 -0700
@@ -60,19 +60,97 @@
         # without coercion.
         yield "bytearray['%s']" % escapestr(bytes(o))
     elif isinstance(o, list):
-        yield '[%s]' % (b', '.join(pprint(a, bprefix=bprefix) for a in o))
+        if not o:
+            yield '[]'
+            return
+
+        yield '['
+
+        for i, a in enumerate(o):
+            for chunk in pprintgen(a, bprefix=bprefix):
+                yield chunk
+
+            if i + 1 < len(o):
+                yield ', '
+
+        yield ']'
     elif isinstance(o, dict):
-        yield '{%s}' % (b', '.join(
-            '%s: %s' % (pprint(k, bprefix=bprefix),
-                        pprint(v, bprefix=bprefix))
-            for k, v in sorted(o.items())))
+        if not o:
+            yield '{}'
+            return
+
+        yield '{'
+
+        for i, (k, v) in enumerate(sorted(o.items())):
+            for chunk in pprintgen(k, bprefix=bprefix):
+                yield chunk
+
+            yield ': '
+
+            for chunk in pprintgen(v, bprefix=bprefix):
+                yield chunk
+
+            if i + 1 < len(o):
+                yield ', '
+
+        yield '}'
     elif isinstance(o, set):
-        yield 'set([%s])' % (b', '.join(
-            pprint(k, bprefix=bprefix) for k in sorted(o)))
+        if not o:
+            yield 'set([])'
+            return
+
+        yield 'set(['
+
+        for i, k in enumerate(sorted(o)):
+            for chunk in pprintgen(k, bprefix=bprefix):
+                yield chunk
+
+            if i + 1 < len(o):
+                yield ', '
+
+        yield '])'
     elif isinstance(o, tuple):
-        yield '(%s)' % (b', '.join(pprint(a, bprefix=bprefix) for a in o))
+        if not o:
+            yield '()'
+            return
+
+        yield '('
+
+        for i, a in enumerate(o):
+            for chunk in pprintgen(a, bprefix=bprefix):
+                yield chunk
+
+            if i + 1 < len(o):
+                yield ', '
+
+        yield ')'
     elif isinstance(o, types.GeneratorType):
-        yield 'gen[%s]' % (b', '.join(pprint(a, bprefix=bprefix) for a in o))
+        # Special case of empty generator.
+        try:
+            nextitem = next(o)
+        except StopIteration:
+            yield 'gen[]'
+            return
+
+        yield 'gen['
+
+        last = False
+
+        while not last:
+            current = nextitem
+
+            try:
+                nextitem = next(o)
+            except StopIteration:
+                last = True
+
+            for chunk in pprintgen(current, bprefix=bprefix):
+                yield chunk
+
+            if not last:
+                yield ', '
+
+        yield ']'
     else:
         yield pycompat.byterepr(o)