Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Doc/library/pprint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The :mod:`pprint` module defines one class:
.. First the implementation class:


.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
.. class:: PrettyPrinter(indent=1, width=None, depth=None, stream=None, *, \
compact=False)

Construct a :class:`PrettyPrinter` instance. This constructor understands
Expand All @@ -44,7 +44,9 @@ The :mod:`pprint` module defines one class:
controlled by *depth*; if the data structure being printed is too deep, the next
contained level is replaced by ``...``. By default, there is no constraint on
the depth of the objects being formatted. The desired output width is
constrained using the *width* parameter; the default is 80 characters. If a
constrained using the *width* parameter, or the width of the terminal if
an output stream is interactive (i.e., connected to a
terminal/tty device), or by 80 characters. If a
structure cannot be formatted within the constrained width, a best effort will
be made. If *compact* is false (the default) each item of a long sequence
will be formatted on a separate line. If *compact* is true, as many items
Expand Down Expand Up @@ -76,6 +78,9 @@ The :mod:`pprint` module defines one class:
>>> pp.pprint(tup)
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))

.. versionchanged:: 3.7
The width of the terminal is used if an output stream is interactive.


The :mod:`pprint` module also provides several shortcut functions:

Expand All @@ -89,7 +94,7 @@ The :mod:`pprint` module also provides several shortcut functions:
Added the *compact* parameter.


.. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
.. function:: pprint(object, stream=None, indent=1, width=None, depth=None, *, \
compact=False)

Prints the formatted representation of *object* on *stream*, followed by a
Expand All @@ -113,6 +118,9 @@ The :mod:`pprint` module also provides several shortcut functions:
'knights',
'ni']

.. versionchanged:: 3.7
The width of the terminal is used if an output stream is interactive.


.. function:: isreadable(object)

Expand Down
24 changes: 17 additions & 7 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
import sys as _sys
import types as _types
from io import StringIO as _StringIO
from os import get_terminal_size as _get_terminal_size

__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
"PrettyPrinter"]


def pprint(object, stream=None, indent=1, width=80, depth=None, *,
def pprint(object, stream=None, indent=1, width=None, depth=None, *,
compact=False):
"""Pretty-print a Python object to a stream [default is sys.stdout]."""
printer = PrettyPrinter(
Expand Down Expand Up @@ -96,7 +97,7 @@ def _safe_tuple(t):
return _safe_key(t[0]), _safe_key(t[1])

class PrettyPrinter:
def __init__(self, indent=1, width=80, depth=None, stream=None, *,
def __init__(self, indent=1, width=None, depth=None, stream=None, *,
compact=False):
"""Handle pretty printing operations onto a stream using a set of
configured parameters.
Expand All @@ -118,21 +119,30 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
If true, several items will be combined in one line.

"""
if stream is None:
stream = _sys.stdout
indent = int(indent)
width = int(width)
if indent < 0:
raise ValueError('indent must be >= 0')
if depth is not None and depth <= 0:
raise ValueError('depth must be > 0')
if width is None:
width = 80
if hasattr(stream, 'isatty') and stream.isatty():
try:
width = _get_terminal_size(stream.fileno()).columns
except (AttributeError, ValueError, OSError):
# stream doesn't have a fileno, or is closed, detached, or
# not a terminal, or os.get_terminal_size() is unsupported
pass
else:
width = int(width)
if not width:
raise ValueError('width must be != 0')
self._depth = depth
self._indent_per_level = indent
self._width = width
if stream is not None:
self._stream = stream
else:
self._stream = _sys.stdout
self._stream = stream
self._compact = bool(compact)

def pprint(self, object):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def test_subclassing(self):
exp = """\
{'names with spaces': 'should be presented using repr()',
others.should.not.be: like.this}"""
self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
self.assertEqual(DottedPrettyPrinter(width=80).pformat(o), exp)

def test_set_reprs(self):
self.assertEqual(pprint.pformat(set()), 'set()')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The width of the terminal is now used as default width in pprint() if the
output stream is connected to a terminal.