Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
skip formatting altogether if stdout is None
  • Loading branch information
iritkatriel committed Jul 3, 2021
commit 214fb4fbc6ad8d1b6a8fc4a027c42baf4f333d24
12 changes: 4 additions & 8 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,16 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
self._width = width
if stream is not None:
self._stream = stream
elif _sys.stdout is not None:
self._stream = _sys.stdout
else:
class _NullStdout:
def write(self, s):
return 0
self._stream = _NullStdout()
self._stream = _sys.stdout
self._compact = bool(compact)
self._sort_dicts = sort_dicts
self._underscore_numbers = underscore_numbers

def pprint(self, object):
self._format(object, self._stream, 0, 0, {}, 0)
self._stream.write("\n")
if self._stream is not None:
self._format(object, self._stream, 0, 0, {}, 0)
self._stream.write("\n")

def pformat(self, object):
sio = _StringIO()
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ def test_basic(self):

def test_stdout_is_None(self):
with contextlib.redirect_stdout(None):
pprint.pprint('this should not fail')
# smoke test - there is no output to check
value = 'this should not fail'
pprint.pprint(value)
pprint.PrettyPrinter().pprint(value)

def test_knotted(self):
# Verify .isrecursive() and .isreadable() w/ recursion
Expand Down