cpython: 976de10bf731 (original) (raw)
--- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -283,6 +283,36 @@ class PrettyPrinter: _dispatch[str.repr] = _pprint_str
- def _pprint_bytes(self, object, stream, indent, allowance, context, level):
write = stream.write[](#l1.8)
if len(object) <= 4:[](#l1.9)
write(repr(object))[](#l1.10)
return[](#l1.11)
parens = level == 1[](#l1.12)
if parens:[](#l1.13)
indent += 1[](#l1.14)
allowance += 1[](#l1.15)
write('(')[](#l1.16)
delim = ''[](#l1.17)
for rep in _wrap_bytes_repr(object, self._width - indent, allowance):[](#l1.18)
write(delim)[](#l1.19)
write(rep)[](#l1.20)
if not delim:[](#l1.21)
delim = '\n' + ' '*indent[](#l1.22)
if parens:[](#l1.23)
write(')')[](#l1.24)
- def _pprint_bytearray(self, object, stream, indent, allowance, context, level):
write = stream.write[](#l1.29)
write('bytearray(')[](#l1.30)
self._pprint_bytes(bytes(object), stream, indent + 10,[](#l1.31)
allowance + 1, context, level + 1)[](#l1.32)
write(')')[](#l1.33)
+ def _format_dict_items(self, items, stream, indent, allowance, context, level): write = stream.write @@ -463,5 +493,22 @@ def _perfcheck(object=None): print("_safe_repr:", t2 - t1) print("pformat:", t3 - t2) +def _wrap_bytes_repr(object, width, allowance):
- current = b''
- last = len(object) // 4 * 4
- for i in range(0, len(object), 4):
part = object[i: i+4][](#l1.48)
candidate = current + part[](#l1.49)
if i == last:[](#l1.50)
width -= allowance[](#l1.51)
if len(repr(candidate)) > width:[](#l1.52)
if current:[](#l1.53)
yield repr(current)[](#l1.54)
current = part[](#l1.55)
else:[](#l1.56)
current = candidate[](#l1.57)
- if current:
yield repr(current)[](#l1.59)
+ if name == "main": _perfcheck()
--- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -658,6 +658,106 @@ frozenset2({0, self.assertLessEqual(maxwidth, w) self.assertGreater(maxwidth, w - 3)
- def test_bytes_wrap(self):
self.assertEqual(pprint.pformat(b'', width=1), "b''")[](#l2.8)
self.assertEqual(pprint.pformat(b'abcd', width=1), "b'abcd'")[](#l2.9)
letters = b'abcdefghijklmnopqrstuvwxyz'[](#l2.10)
self.assertEqual(pprint.pformat(letters, width=29), repr(letters))[](#l2.11)
self.assertEqual(pprint.pformat(letters, width=19), """\[](#l2.12)
- b'mnopqrstuvwx'
- b'yz')""")
special = bytes(range(16))[](#l2.23)
self.assertEqual(pprint.pformat(special, width=61), repr(special))[](#l2.24)
self.assertEqual(pprint.pformat(special, width=48), """\[](#l2.25)
+(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b'
- b'\x04\x05\x06\x07\x08\t\n\x0b'
- b'\x0c\r\x0e\x0f')""")
self.assertEqual(pprint.pformat(special, width=1), """\[](#l2.32)
- b'\x04\x05\x06\x07'
- b'\x08\t\n\x0b'
- b'\x0c\r\x0e\x0f')""")
self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},[](#l2.37)
width=21), """\[](#l2.38)
- 'b': b'abcdefghijkl'
b'mnopqrstuvwx'[](#l2.41)
b'yz',[](#l2.42)
- 'c': 2}""")
self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},[](#l2.44)
width=20), """\[](#l2.45)
- 'b': b'abcdefgh'
b'ijklmnop'[](#l2.48)
b'qrstuvwxyz',[](#l2.49)
- 'c': 2}""")
self.assertEqual(pprint.pformat([[[[[[letters]]]]]], width=25), """\[](#l2.51)
b'qrstuvwxyz']]]]]]""")[](#l2.53)
self.assertEqual(pprint.pformat([[[[[[special]]]]]], width=41), """\[](#l2.54)
+[[[[[[b'\x00\x01\x02\x03\x04\x05\x06\x07'
b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f']]]]]]""")[](#l2.56)
# Check that the pprint is a usable repr[](#l2.57)
for width in range(1, 64):[](#l2.58)
formatted = pprint.pformat(special, width=width)[](#l2.59)
self.assertEqual(eval(formatted), special)[](#l2.60)
formatted = pprint.pformat([special] * 2, width=width)[](#l2.61)
self.assertEqual(eval(formatted), [special] * 2)[](#l2.62)
- def test_bytearray_wrap(self):
self.assertEqual(pprint.pformat(bytearray(), width=1), "bytearray(b'')")[](#l2.65)
letters = bytearray(b'abcdefghijklmnopqrstuvwxyz')[](#l2.66)
self.assertEqual(pprint.pformat(letters, width=40), repr(letters))[](#l2.67)
self.assertEqual(pprint.pformat(letters, width=28), """\[](#l2.68)
b'mnopqrstuvwxyz')""")[](#l2.70)
self.assertEqual(pprint.pformat(letters, width=27), """\[](#l2.71)
b'mnopqrstuvwx'[](#l2.73)
b'yz')""")[](#l2.74)
self.assertEqual(pprint.pformat(letters, width=25), """\[](#l2.75)
b'mnopqrstuvwx'[](#l2.77)
b'yz')""")[](#l2.78)
special = bytearray(range(16))[](#l2.79)
self.assertEqual(pprint.pformat(special, width=72), repr(special))[](#l2.80)
self.assertEqual(pprint.pformat(special, width=57), """\[](#l2.81)
+bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b'
b'\\x0c\\r\\x0e\\x0f')""")[](#l2.83)
self.assertEqual(pprint.pformat(special, width=41), """\[](#l2.84)
+bytearray(b'\x00\x01\x02\x03'
b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'[](#l2.86)
b'\\x0c\\r\\x0e\\x0f')""")[](#l2.87)
self.assertEqual(pprint.pformat(special, width=1), """\[](#l2.88)
+bytearray(b'\x00\x01\x02\x03'
b'\\x04\\x05\\x06\\x07'[](#l2.90)
b'\\x08\\t\\n\\x0b'[](#l2.91)
b'\\x0c\\r\\x0e\\x0f')""")[](#l2.92)
self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},[](#l2.93)
width=31), """\[](#l2.94)
- 'b': bytearray(b'abcdefghijkl'
b'mnopqrstuvwx'[](#l2.97)
b'yz'),[](#l2.98)
- 'c': 2}""")
self.assertEqual(pprint.pformat([[[[[letters]]]]], width=37), """\[](#l2.100)
+[[[[[bytearray(b'abcdefghijklmnop'
b'qrstuvwxyz')]]]]]""")[](#l2.102)
self.assertEqual(pprint.pformat([[[[[special]]]]], width=50), """\[](#l2.103)
+[[[[[bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07'
b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""")[](#l2.105)
+ class DottedPrettyPrinter(pprint.PrettyPrinter):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -26,6 +26,8 @@ Core and Builtins Library ------- +- Issue #17530: pprint now wraps long bytes objects and bytearrays. +