changeset 37619:68132a95df31

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
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 12 Apr 2018 20:42:42 -0700
parents 1edf3738e000
children fd1dd79cff20
files mercurial/utils/stringutil.py
diffstat 1 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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)