diff -r 20be90a3a714 Lib/pprint.py --- a/Lib/pprint.py Wed May 01 16:20:00 2013 +0300 +++ b/Lib/pprint.py Sat May 04 13:25:50 2013 +0300 @@ -34,6 +34,7 @@ """ +import base64 as _base64 import re import sys as _sys from collections import OrderedDict as _OrderedDict @@ -101,6 +102,10 @@ "Helper function for comparing 2-tuples" return _safe_key(t[0]), _safe_key(t[1]) +def _chunks(data, size): + for i in range(0, len(data), size): + yield data[i: i + size] + class PrettyPrinter: def __init__(self, indent=1, width=80, depth=None, stream=None): """Handle pretty printing operations onto a stream using a set of @@ -271,6 +276,25 @@ write('\n' + ' '*indent) write(rep) return + + if (issubclass(typ, bytes) and len(object) > 0 and r is bytes.__repr__ and + re.search(b'[^\t\n\r -~]', object)): + write(rep) + write('\n') + # max width is chunksize * 4 + 4 + chunksize = (16 if self._width - indent >= 68 else + 8 if self._width - indent >= 36 else + 4) + for chunk in _chunks(object, chunksize): + write(' ' * indent) + hexes = b' '.join(_chunks(_base64.b16encode(chunk), 2)) + chars = re.sub(b'[^ -~]', b'.', chunk) + write('# %-*s | %s\n' % (chunksize * 3 - 1, + hexes.decode('ascii'), + chars.decode('ascii'))) + write(' ' * indent) + return + write(rep) def _repr(self, object, context, level): diff -r 20be90a3a714 Lib/test/test_pprint.py --- a/Lib/test/test_pprint.py Wed May 01 16:20:00 2013 +0300 +++ b/Lib/test/test_pprint.py Sat May 04 13:25:50 2013 +0300 @@ -513,6 +513,33 @@ formatted = pprint.pformat(special, width=width) self.assertEqual(eval("(" + formatted + ")"), special) + def test_bytes_hex(self): + data = bytes(range(0, 256, 10)) + self.assertEqual(pprint.pformat(data, width=78), +r"""b'\x00\n\x14\x1e(2