23 pycompat, |
23 pycompat, |
24 ) |
24 ) |
25 |
25 |
26 def pprint(o): |
26 def pprint(o): |
27 """Pretty print an object.""" |
27 """Pretty print an object.""" |
28 if isinstance(o, (bytes, bytearray)): |
28 if isinstance(o, bytes): |
29 return "b'%s'" % escapestr(o) |
29 return "b'%s'" % escapestr(o) |
|
30 elif isinstance(o, bytearray): |
|
31 # codecs.escape_encode() can't handle bytearray, so escapestr fails |
|
32 # without coercion. |
|
33 return "bytearray['%s']" % escapestr(bytes(o)) |
30 elif isinstance(o, list): |
34 elif isinstance(o, list): |
31 return '[%s]' % (b', '.join(pprint(a) for a in o)) |
35 return '[%s]' % (b', '.join(pprint(a) for a in o)) |
32 elif isinstance(o, dict): |
36 elif isinstance(o, dict): |
33 return '{%s}' % (b', '.join( |
37 return '{%s}' % (b', '.join( |
34 '%s: %s' % (pprint(k), pprint(v)) for k, v in sorted(o.items()))) |
38 '%s: %s' % (pprint(k), pprint(v)) for k, v in sorted(o.items()))) |
35 elif isinstance(o, bool): |
39 elif isinstance(o, bool): |
36 return b'True' if o else b'False' |
40 return b'True' if o else b'False' |
|
41 elif isinstance(o, int): |
|
42 return '%d' % o |
|
43 elif isinstance(o, float): |
|
44 return '%f' % o |
37 else: |
45 else: |
38 raise error.ProgrammingError('do not know how to format %r' % o) |
46 raise error.ProgrammingError('do not know how to format %r' % o) |
39 |
47 |
40 def binary(s): |
48 def binary(s): |
41 """return true if a string is binary data""" |
49 """return true if a string is binary data""" |