43 return pat |
43 return pat |
44 return pat.encode('latin1') |
44 return pat.encode('latin1') |
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""" |
75 lines = [] |
81 lines = [] |
76 rs = pycompat.byterepr(o) |
82 rs = pycompat.byterepr(o) |