mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Issue #17530: pprint now wraps long bytes objects and bytearrays.
This commit is contained in:
parent
8e2aa88a40
commit
022f20376a
3 changed files with 149 additions and 0 deletions
|
@ -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''")
|
||||
self.assertEqual(pprint.pformat(b'abcd', width=1), "b'abcd'")
|
||||
letters = b'abcdefghijklmnopqrstuvwxyz'
|
||||
self.assertEqual(pprint.pformat(letters, width=29), repr(letters))
|
||||
self.assertEqual(pprint.pformat(letters, width=19), """\
|
||||
(b'abcdefghijkl'
|
||||
b'mnopqrstuvwxyz')""")
|
||||
self.assertEqual(pprint.pformat(letters, width=18), """\
|
||||
(b'abcdefghijkl'
|
||||
b'mnopqrstuvwx'
|
||||
b'yz')""")
|
||||
self.assertEqual(pprint.pformat(letters, width=16), """\
|
||||
(b'abcdefghijkl'
|
||||
b'mnopqrstuvwx'
|
||||
b'yz')""")
|
||||
special = bytes(range(16))
|
||||
self.assertEqual(pprint.pformat(special, width=61), repr(special))
|
||||
self.assertEqual(pprint.pformat(special, width=48), """\
|
||||
(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
|
||||
b'\\x0c\\r\\x0e\\x0f')""")
|
||||
self.assertEqual(pprint.pformat(special, width=32), """\
|
||||
(b'\\x00\\x01\\x02\\x03'
|
||||
b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
|
||||
b'\\x0c\\r\\x0e\\x0f')""")
|
||||
self.assertEqual(pprint.pformat(special, width=1), """\
|
||||
(b'\\x00\\x01\\x02\\x03'
|
||||
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},
|
||||
width=21), """\
|
||||
{'a': 1,
|
||||
'b': b'abcdefghijkl'
|
||||
b'mnopqrstuvwx'
|
||||
b'yz',
|
||||
'c': 2}""")
|
||||
self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
|
||||
width=20), """\
|
||||
{'a': 1,
|
||||
'b': b'abcdefgh'
|
||||
b'ijklmnop'
|
||||
b'qrstuvwxyz',
|
||||
'c': 2}""")
|
||||
self.assertEqual(pprint.pformat([[[[[[letters]]]]]], width=25), """\
|
||||
[[[[[[b'abcdefghijklmnop'
|
||||
b'qrstuvwxyz']]]]]]""")
|
||||
self.assertEqual(pprint.pformat([[[[[[special]]]]]], width=41), """\
|
||||
[[[[[[b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
|
||||
b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f']]]]]]""")
|
||||
# Check that the pprint is a usable repr
|
||||
for width in range(1, 64):
|
||||
formatted = pprint.pformat(special, width=width)
|
||||
self.assertEqual(eval(formatted), special)
|
||||
formatted = pprint.pformat([special] * 2, width=width)
|
||||
self.assertEqual(eval(formatted), [special] * 2)
|
||||
|
||||
def test_bytearray_wrap(self):
|
||||
self.assertEqual(pprint.pformat(bytearray(), width=1), "bytearray(b'')")
|
||||
letters = bytearray(b'abcdefghijklmnopqrstuvwxyz')
|
||||
self.assertEqual(pprint.pformat(letters, width=40), repr(letters))
|
||||
self.assertEqual(pprint.pformat(letters, width=28), """\
|
||||
bytearray(b'abcdefghijkl'
|
||||
b'mnopqrstuvwxyz')""")
|
||||
self.assertEqual(pprint.pformat(letters, width=27), """\
|
||||
bytearray(b'abcdefghijkl'
|
||||
b'mnopqrstuvwx'
|
||||
b'yz')""")
|
||||
self.assertEqual(pprint.pformat(letters, width=25), """\
|
||||
bytearray(b'abcdefghijkl'
|
||||
b'mnopqrstuvwx'
|
||||
b'yz')""")
|
||||
special = bytearray(range(16))
|
||||
self.assertEqual(pprint.pformat(special, width=72), repr(special))
|
||||
self.assertEqual(pprint.pformat(special, width=57), """\
|
||||
bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
|
||||
b'\\x0c\\r\\x0e\\x0f')""")
|
||||
self.assertEqual(pprint.pformat(special, width=41), """\
|
||||
bytearray(b'\\x00\\x01\\x02\\x03'
|
||||
b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
|
||||
b'\\x0c\\r\\x0e\\x0f')""")
|
||||
self.assertEqual(pprint.pformat(special, width=1), """\
|
||||
bytearray(b'\\x00\\x01\\x02\\x03'
|
||||
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},
|
||||
width=31), """\
|
||||
{'a': 1,
|
||||
'b': bytearray(b'abcdefghijkl'
|
||||
b'mnopqrstuvwx'
|
||||
b'yz'),
|
||||
'c': 2}""")
|
||||
self.assertEqual(pprint.pformat([[[[[letters]]]]], width=37), """\
|
||||
[[[[[bytearray(b'abcdefghijklmnop'
|
||||
b'qrstuvwxyz')]]]]]""")
|
||||
self.assertEqual(pprint.pformat([[[[[special]]]]], width=50), """\
|
||||
[[[[[bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
|
||||
b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""")
|
||||
|
||||
|
||||
class DottedPrettyPrinter(pprint.PrettyPrinter):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue