changeset 40275:f2fbd32c7664

stringutil: make level parameter of pprintgen() 0-origin I think this makes more sense in that the level is incremented where nesting goes one more deep.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 14 Oct 2018 07:18:19 +0200
parents a8b9174517c7
children be57c7019c70
files mercurial/utils/stringutil.py
diffstat 1 files changed, 22 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/utils/stringutil.py	Sun Oct 14 06:51:19 2018 +0200
+++ b/mercurial/utils/stringutil.py	Sun Oct 14 07:18:19 2018 +0200
@@ -47,7 +47,7 @@
     """Pretty print an object."""
     return b''.join(pprintgen(o, bprefix=bprefix, indent=indent))
 
-def pprintgen(o, bprefix=False, indent=0, _level=1):
+def pprintgen(o, bprefix=False, indent=0, _level=0):
     """Pretty print an object to a generator of atoms.
 
     ``bprefix`` is a flag influencing whether bytestrings are preferred with
@@ -75,12 +75,13 @@
         yield '['
 
         if indent:
+            _level += 1
             yield '\n'
             yield ' ' * (_level * indent)
 
         for i, a in enumerate(o):
             for chunk in pprintgen(a, bprefix=bprefix, indent=indent,
-                                   _level=_level + 1):
+                                   _level=_level):
                 yield chunk
 
             if i + 1 < len(o):
@@ -91,8 +92,9 @@
                     yield ', '
 
         if indent:
+            _level -= 1
             yield '\n'
-            yield ' ' * ((_level - 1) * indent)
+            yield ' ' * (_level * indent)
 
         yield ']'
     elif isinstance(o, dict):
@@ -103,18 +105,19 @@
         yield '{'
 
         if indent:
+            _level += 1
             yield '\n'
             yield ' ' * (_level * indent)
 
         for i, (k, v) in enumerate(sorted(o.items())):
             for chunk in pprintgen(k, bprefix=bprefix, indent=indent,
-                                   _level=_level + 1):
+                                   _level=_level):
                 yield chunk
 
             yield ': '
 
             for chunk in pprintgen(v, bprefix=bprefix, indent=indent,
-                                   _level=_level + 1):
+                                   _level=_level):
                 yield chunk
 
             if i + 1 < len(o):
@@ -125,8 +128,9 @@
                     yield ', '
 
         if indent:
+            _level -= 1
             yield '\n'
-            yield ' ' * ((_level - 1) * indent)
+            yield ' ' * (_level * indent)
 
         yield '}'
     elif isinstance(o, set):
@@ -137,12 +141,13 @@
         yield 'set(['
 
         if indent:
+            _level += 1
             yield '\n'
             yield ' ' * (_level * indent)
 
         for i, k in enumerate(sorted(o)):
             for chunk in pprintgen(k, bprefix=bprefix, indent=indent,
-                                   _level=_level + 1):
+                                   _level=_level):
                 yield chunk
 
             if i + 1 < len(o):
@@ -153,8 +158,9 @@
                     yield ', '
 
         if indent:
+            _level -= 1
             yield '\n'
-            yield ' ' * ((_level - 1) * indent)
+            yield ' ' * (_level * indent)
 
         yield '])'
     elif isinstance(o, tuple):
@@ -165,12 +171,13 @@
         yield '('
 
         if indent:
+            _level += 1
             yield '\n'
             yield ' ' * (_level * indent)
 
         for i, a in enumerate(o):
             for chunk in pprintgen(a, bprefix=bprefix, indent=indent,
-                                   _level=_level + 1):
+                                   _level=_level):
                 yield chunk
 
             if i + 1 < len(o):
@@ -181,8 +188,9 @@
                     yield ', '
 
         if indent:
+            _level -= 1
             yield '\n'
-            yield ' ' * ((_level - 1) * indent)
+            yield ' ' * (_level * indent)
 
         yield ')'
     elif isinstance(o, types.GeneratorType):
@@ -196,6 +204,7 @@
         yield 'gen['
 
         if indent:
+            _level += 1
             yield '\n'
             yield ' ' * (_level * indent)
 
@@ -210,7 +219,7 @@
                 last = True
 
             for chunk in pprintgen(current, bprefix=bprefix, indent=indent,
-                                   _level=_level + 1):
+                                   _level=_level):
                 yield chunk
 
             if not last:
@@ -221,8 +230,9 @@
                     yield ', '
 
         if indent:
+            _level -= 1
             yield '\n'
-            yield ' ' * ((_level -1) * indent)
+            yield ' ' * (_level * indent)
 
         yield ']'
     else: