Skip to content
Merged
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
21 changes: 3 additions & 18 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
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 Expand Up @@ -636,19 +637,6 @@ def _recursion(object):
% (type(object).__name__, id(object)))


def _perfcheck(object=None):
import time
if object is None:
object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000
p = PrettyPrinter()
t1 = time.perf_counter()
p._safe_repr(object, {}, None, 0, True)
t2 = time.perf_counter()
p.pformat(object)
t3 = time.perf_counter()
print("_safe_repr:", t2 - t1)
print("pformat:", t3 - t2)

def _wrap_bytes_repr(object, width, allowance):
current = b''
last = len(object) // 4 * 4
Expand All @@ -665,6 +653,3 @@ def _wrap_bytes_repr(object, width, allowance):
current = candidate
if current:
yield repr(current)

if __name__ == "__main__":
_perfcheck()
10 changes: 9 additions & 1 deletion Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import collections
import contextlib
import dataclasses
import io
import itertools
Expand Down Expand Up @@ -159,6 +160,13 @@ def test_basic(self):
self.assertTrue(pp.isreadable(safe),
"expected isreadable for %r" % (safe,))

def test_stdout_is_None(self):
with contextlib.redirect_stdout(None):
# 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
# Tie a knot.
Expand Down Expand Up @@ -195,7 +203,7 @@ def test_knotted(self):
def test_unreadable(self):
# Not recursive but not readable anyway
pp = pprint.PrettyPrinter()
for unreadable in type(3), pprint, pprint.isrecursive:
for unreadable in object(), int, pprint, pprint.isrecursive:
# module-level convenience functions
self.assertFalse(pprint.isrecursive(unreadable),
"expected not isrecursive for %r" % (unreadable,))
Expand Down