##// 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 def pprint(o, bprefix=False):
46 def pprint(o, bprefix=False):
47 """Pretty print an object."""
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 if isinstance(o, bytes):
53 if isinstance(o, bytes):
49 if bprefix:
54 if bprefix:
50 return "b'%s'" % escapestr(o)
55 yield "b'%s'" % escapestr(o)
51 return "'%s'" % escapestr(o)
56 else:
57 yield "'%s'" % escapestr(o)
52 elif isinstance(o, bytearray):
58 elif isinstance(o, bytearray):
53 # codecs.escape_encode() can't handle bytearray, so escapestr fails
59 # codecs.escape_encode() can't handle bytearray, so escapestr fails
54 # without coercion.
60 # without coercion.
55 return "bytearray['%s']" % escapestr(bytes(o))
61 yield "bytearray['%s']" % escapestr(bytes(o))
56 elif isinstance(o, list):
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 elif isinstance(o, dict):
64 elif isinstance(o, dict):
59 return '{%s}' % (b', '.join(
65 yield '{%s}' % (b', '.join(
60 '%s: %s' % (pprint(k, bprefix=bprefix),
66 '%s: %s' % (pprint(k, bprefix=bprefix),
61 pprint(v, bprefix=bprefix))
67 pprint(v, bprefix=bprefix))
62 for k, v in sorted(o.items())))
68 for k, v in sorted(o.items())))
63 elif isinstance(o, set):
69 elif isinstance(o, set):
64 return 'set([%s])' % (b', '.join(
70 yield 'set([%s])' % (b', '.join(
65 pprint(k, bprefix=bprefix) for k in sorted(o)))
71 pprint(k, bprefix=bprefix) for k in sorted(o)))
66 elif isinstance(o, tuple):
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 elif isinstance(o, types.GeneratorType):
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 else:
76 else:
71 return pycompat.byterepr(o)
77 yield pycompat.byterepr(o)
72
78
73 def prettyrepr(o):
79 def prettyrepr(o):
74 """Pretty print a representation of a possibly-nested object"""
80 """Pretty print a representation of a possibly-nested object"""
General Comments 0
You need to be logged in to leave comments. Login now