cpython: 5a2296093645 (original) (raw)

--- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -14,8 +14,8 @@ The :mod:pprint module provides a capa Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable. This may be the case if objects -such as files, sockets, classes, or instances are included, as well as many -other built-in objects which are not representable as Python constants. +such as files, sockets or classes are included, as well as many other +objects which are not representable as Python literals. The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don't fit within the allowed width. @@ -65,7 +65,7 @@ The :mod:pprint module defines one cla ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...))))))) -The :class:PrettyPrinter class supports several derivative functions: +The :mod:pprint module also provides several shortcut functions: .. function:: pformat(object, indent=1, width=80, depth=None) @@ -193,101 +193,141 @@ Example ------- To demonstrate several uses of the :func:pprint function and its parameters, -let's fetch information about a project from PyPI:: +let's fetch information about a project from PyPI <https://pypi.python.org>_:: >>> import json >>> import pprint >>> from urllib.request import urlopen

In its basic form, :func:pprint shows the whole object::

The result can be limited to a certain depth (ellipsis is used for deeper contents)::

+ +Additionally, maximum character width can be suggested. If a long object +cannot be split, the specified width will be exceeded:: -Additionally, maximum width can be suggested. If a long object cannot be -split, the specified width will be exceeded:: -

--- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -34,6 +34,7 @@ saferepr() """ +import re import sys as _sys from collections import OrderedDict as _OrderedDict from io import StringIO as _StringIO @@ -158,13 +159,10 @@ class PrettyPrinter: return rep = self._repr(object, context, level - 1) typ = _type(object)

- if sepLines: r = getattr(typ, "repr", None) if issubclass(typ, dict): @@ -242,6 +240,37 @@ class PrettyPrinter: write(endchar) return

def _repr(self, object, context, level):

--- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -1,3 +1,5 @@ +# -- coding: utf-8 -- + import pprint import test.support import unittest @@ -475,6 +477,42 @@ class QueryTestCase(unittest.TestCase): self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)), '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id)))

+'the quick brown ' +'fox jumped over ' +'a lazy dog'""")

+{'a': 1,

+'Portons dix bons ' +'"whiskys"\n' +"à l'avocat " +'goujat\t qui ' +'fumait au zoo'""")

+ class DottedPrettyPrinter(pprint.PrettyPrinter):

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -294,6 +294,9 @@ Core and Builtins Library ------- +- Issue #17150: pprint now uses line continuations to wrap long string