##// END OF EJS Templates
stringutil: refactor core of pprint so it emits chunks...
Gregory Szorc -
r39389:0d21b1f1 default
parent child Browse files
Show More
@@ -45,30 +45,36 b' def reescape(pat):'
45 45
46 46 def pprint(o, bprefix=False):
47 47 """Pretty print an object."""
48 return b''.join(pprintgen(o, bprefix=bprefix))
49
50 def pprintgen(o, bprefix=False):
51 """Pretty print an object to a generator of atoms."""
52
48 53 if isinstance(o, bytes):
49 54 if bprefix:
50 return "b'%s'" % escapestr(o)
51 return "'%s'" % escapestr(o)
55 yield "b'%s'" % escapestr(o)
56 else:
57 yield "'%s'" % escapestr(o)
52 58 elif isinstance(o, bytearray):
53 59 # codecs.escape_encode() can't handle bytearray, so escapestr fails
54 60 # without coercion.
55 return "bytearray['%s']" % escapestr(bytes(o))
61 yield "bytearray['%s']" % escapestr(bytes(o))
56 62 elif isinstance(o, list):
57 return '[%s]' % (b', '.join(pprint(a, bprefix=bprefix) for a in o))
63 yield '[%s]' % (b', '.join(pprint(a, bprefix=bprefix) for a in o))
58 64 elif isinstance(o, dict):
59 return '{%s}' % (b', '.join(
65 yield '{%s}' % (b', '.join(
60 66 '%s: %s' % (pprint(k, bprefix=bprefix),
61 67 pprint(v, bprefix=bprefix))
62 68 for k, v in sorted(o.items())))
63 69 elif isinstance(o, set):
64 return 'set([%s])' % (b', '.join(
70 yield 'set([%s])' % (b', '.join(
65 71 pprint(k, bprefix=bprefix) for k in sorted(o)))
66 72 elif isinstance(o, tuple):
67 return '(%s)' % (b', '.join(pprint(a, bprefix=bprefix) for a in o))
73 yield '(%s)' % (b', '.join(pprint(a, bprefix=bprefix) for a in o))
68 74 elif isinstance(o, types.GeneratorType):
69 return 'gen[%s]' % (b', '.join(pprint(a, bprefix=bprefix) for a in o))
75 yield 'gen[%s]' % (b', '.join(pprint(a, bprefix=bprefix) for a in o))
70 76 else:
71 return pycompat.byterepr(o)
77 yield pycompat.byterepr(o)
72 78
73 79 def prettyrepr(o):
74 80 """Pretty print a representation of a possibly-nested object"""
General Comments 0
You need to be logged in to leave comments. Login now