stringutil: support more types with pprint()
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 12 Apr 2018 20:42:42 -0700
changeset 37619 68132a95df31
parent 37618 1edf3738e000
child 37620 fd1dd79cff20
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
mercurial/utils/stringutil.py
--- 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)