comparison mercurial/utils/stringutil.py @ 39381:5ed7c6caf24d

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
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 27 Aug 2018 09:05:56 -0700
parents 0d21b1f1722c
children 0f549da54379
comparison
equal deleted inserted replaced
39380:0d21b1f1722c 39381:5ed7c6caf24d
58 elif isinstance(o, bytearray): 58 elif isinstance(o, bytearray):
59 # codecs.escape_encode() can't handle bytearray, so escapestr fails 59 # codecs.escape_encode() can't handle bytearray, so escapestr fails
60 # without coercion. 60 # without coercion.
61 yield "bytearray['%s']" % escapestr(bytes(o)) 61 yield "bytearray['%s']" % escapestr(bytes(o))
62 elif isinstance(o, list): 62 elif isinstance(o, list):
63 yield '[%s]' % (b', '.join(pprint(a, bprefix=bprefix) for a in o)) 63 if not o:
64 yield '[]'
65 return
66
67 yield '['
68
69 for i, a in enumerate(o):
70 for chunk in pprintgen(a, bprefix=bprefix):
71 yield chunk
72
73 if i + 1 < len(o):
74 yield ', '
75
76 yield ']'
64 elif isinstance(o, dict): 77 elif isinstance(o, dict):
65 yield '{%s}' % (b', '.join( 78 if not o:
66 '%s: %s' % (pprint(k, bprefix=bprefix), 79 yield '{}'
67 pprint(v, bprefix=bprefix)) 80 return
68 for k, v in sorted(o.items()))) 81
82 yield '{'
83
84 for i, (k, v) in enumerate(sorted(o.items())):
85 for chunk in pprintgen(k, bprefix=bprefix):
86 yield chunk
87
88 yield ': '
89
90 for chunk in pprintgen(v, bprefix=bprefix):
91 yield chunk
92
93 if i + 1 < len(o):
94 yield ', '
95
96 yield '}'
69 elif isinstance(o, set): 97 elif isinstance(o, set):
70 yield 'set([%s])' % (b', '.join( 98 if not o:
71 pprint(k, bprefix=bprefix) for k in sorted(o))) 99 yield 'set([])'
100 return
101
102 yield 'set(['
103
104 for i, k in enumerate(sorted(o)):
105 for chunk in pprintgen(k, bprefix=bprefix):
106 yield chunk
107
108 if i + 1 < len(o):
109 yield ', '
110
111 yield '])'
72 elif isinstance(o, tuple): 112 elif isinstance(o, tuple):
73 yield '(%s)' % (b', '.join(pprint(a, bprefix=bprefix) for a in o)) 113 if not o:
114 yield '()'
115 return
116
117 yield '('
118
119 for i, a in enumerate(o):
120 for chunk in pprintgen(a, bprefix=bprefix):
121 yield chunk
122
123 if i + 1 < len(o):
124 yield ', '
125
126 yield ')'
74 elif isinstance(o, types.GeneratorType): 127 elif isinstance(o, types.GeneratorType):
75 yield 'gen[%s]' % (b', '.join(pprint(a, bprefix=bprefix) for a in o)) 128 # Special case of empty generator.
129 try:
130 nextitem = next(o)
131 except StopIteration:
132 yield 'gen[]'
133 return
134
135 yield 'gen['
136
137 last = False
138
139 while not last:
140 current = nextitem
141
142 try:
143 nextitem = next(o)
144 except StopIteration:
145 last = True
146
147 for chunk in pprintgen(current, bprefix=bprefix):
148 yield chunk
149
150 if not last:
151 yield ', '
152
153 yield ']'
76 else: 154 else:
77 yield pycompat.byterepr(o) 155 yield pycompat.byterepr(o)
78 156
79 def prettyrepr(o): 157 def prettyrepr(o):
80 """Pretty print a representation of a possibly-nested object""" 158 """Pretty print a representation of a possibly-nested object"""