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
26 changes: 19 additions & 7 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
data structures.

"""
import shutil

import collections as _collections
import dataclasses as _dataclasses
Expand All @@ -45,7 +46,7 @@
"PrettyPrinter", "pp"]


def pprint(object, stream=None, indent=1, width=80, depth=None, *,
def pprint(object, stream=None, indent=1, width=None, depth=None, *,
compact=False, sort_dicts=True, underscore_numbers=False):
"""Pretty-print a Python object to a stream [default is sys.stdout]."""
printer = PrettyPrinter(
Expand All @@ -54,7 +55,7 @@ def pprint(object, stream=None, indent=1, width=80, depth=None, *,
underscore_numbers=underscore_numbers)
printer.pprint(object)

def pformat(object, indent=1, width=80, depth=None, *,
def pformat(object, indent=1, width=None, depth=None, *,
compact=False, sort_dicts=True, underscore_numbers=False):
"""Format a Python object into a pretty-printed representation."""
return PrettyPrinter(indent=indent, width=width, depth=depth,
Expand Down Expand Up @@ -104,7 +105,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, sort_dicts=True, underscore_numbers=False):
"""Handle pretty printing operations onto a stream using a set of
configured parameters.
Expand All @@ -130,7 +131,21 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,

"""
indent = int(indent)
if stream is not None:
self._stream = stream
else:
self._stream = _sys.stdout

if width is None:
if self._stream is _sys.stdout:
# Detect using shutil
width = shutil.get_terminal_size(fallback=(80, 80)).columns
else:
# Default to 80
width = 80

width = int(width)

if indent < 0:
raise ValueError('indent must be >= 0')
if depth is not None and depth <= 0:
Expand All @@ -140,10 +155,7 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
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._compact = bool(compact)
self._sort_dicts = sort_dicts
self._underscore_numbers = underscore_numbers
Expand Down
13 changes: 12 additions & 1 deletion Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import collections
import contextlib
import unittest.mock
import dataclasses
import io
import itertools
import os
import pprint
import random
import shutil

import test.support
import test.test_set
import types
Expand Down Expand Up @@ -143,6 +147,14 @@ def test_init(self):
self.assertRaises(ValueError, pprint.PrettyPrinter, depth=-1)
self.assertRaises(ValueError, pprint.PrettyPrinter, width=0)

def test_init_width(self):
self.assertEqual(pprint.PrettyPrinter(width=53)._width, 53)
terminal_width = shutil.get_terminal_size((80, 80)).columns
self.assertEqual(pprint.PrettyPrinter(width=None)._width, terminal_width)
mocked_size = os.terminal_size((120, 120))
with unittest.mock.patch('shutil.get_terminal_size', return_value=mocked_size):
self.assertEqual(pprint.PrettyPrinter(width=None)._width, 120)

def test_basic(self):
# Verify .isrecursive() and .isreadable() w/o recursion
pp = pprint.PrettyPrinter()
Expand Down Expand Up @@ -1222,7 +1234,6 @@ def test_user_string(self):
'jumped over a '
'lazy dog'}""")


class DottedPrettyPrinter(pprint.PrettyPrinter):

def format(self, object, context, maxlevels, level):
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ Daniel Fortunov
Evens Fortuné
Chris Foster
John Fouhy
Tom Forbes
Andrew Francis
Matt Frank
Stefan Franke
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Change the default ``width`` argument of
:meth:`pprint.pprint`, :meth:`pprint.pformat`
and :class:`pprint.PrettyPrinter` to use the
terminal width when available.