stringutil: support more types with pprint()
bytearray wasn't working. Integers and floats were not being
formatted.
I /think/ %f is portable across both Python 2 and 3, as it should
default to 6 decimal points on each.
Differential Revision: https://phab.mercurial-scm.org/D3302
--- a/mercurial/utils/stringutil.py Thu Apr 12 14:27:13 2018 -0400
+++ b/mercurial/utils/stringutil.py Thu Apr 12 20:42:42 2018 -0700
@@ -25,8 +25,12 @@
def pprint(o):
"""Pretty print an object."""
- if isinstance(o, (bytes, bytearray)):
+ if isinstance(o, bytes):
return "b'%s'" % escapestr(o)
+ elif isinstance(o, bytearray):
+ # codecs.escape_encode() can't handle bytearray, so escapestr fails
+ # without coercion.
+ return "bytearray['%s']" % escapestr(bytes(o))
elif isinstance(o, list):
return '[%s]' % (b', '.join(pprint(a) for a in o))
elif isinstance(o, dict):
@@ -34,6 +38,10 @@
'%s: %s' % (pprint(k), pprint(v)) for k, v in sorted(o.items())))
elif isinstance(o, bool):
return b'True' if o else b'False'
+ elif isinstance(o, int):
+ return '%d' % o
+ elif isinstance(o, float):
+ return '%f' % o
else:
raise error.ProgrammingError('do not know how to format %r' % o)