Skip to content

Commit 06073fe

Browse files
authored
Merge pull request RustPython#4196 from jopemachine/update-pprint
Update `pprint` lib and tests from CPython v3.12.0a0
2 parents ac25328 + 01aad3c commit 06073fe

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

Lib/pprint.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
149149
self._underscore_numbers = underscore_numbers
150150

151151
def pprint(self, object):
152-
self._format(object, self._stream, 0, 0, {}, 0)
153-
self._stream.write("\n")
152+
if self._stream is not None:
153+
self._format(object, self._stream, 0, 0, {}, 0)
154+
self._stream.write("\n")
154155

155156
def pformat(self, object):
156157
sio = _StringIO()
@@ -636,19 +637,6 @@ def _recursion(object):
636637
% (type(object).__name__, id(object)))
637638

638639

639-
def _perfcheck(object=None):
640-
import time
641-
if object is None:
642-
object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000
643-
p = PrettyPrinter()
644-
t1 = time.perf_counter()
645-
p._safe_repr(object, {}, None, 0, True)
646-
t2 = time.perf_counter()
647-
p.pformat(object)
648-
t3 = time.perf_counter()
649-
print("_safe_repr:", t2 - t1)
650-
print("pformat:", t3 - t2)
651-
652640
def _wrap_bytes_repr(object, width, allowance):
653641
current = b''
654642
last = len(object) // 4 * 4
@@ -665,6 +653,3 @@ def _wrap_bytes_repr(object, width, allowance):
665653
current = candidate
666654
if current:
667655
yield repr(current)
668-
669-
if __name__ == "__main__":
670-
_perfcheck()

Lib/test/test_pprint.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
import collections
4+
import contextlib
45
import dataclasses
56
import io
67
import itertools
@@ -159,6 +160,13 @@ def test_basic(self):
159160
self.assertTrue(pp.isreadable(safe),
160161
"expected isreadable for %r" % (safe,))
161162

163+
def test_stdout_is_None(self):
164+
with contextlib.redirect_stdout(None):
165+
# smoke test - there is no output to check
166+
value = 'this should not fail'
167+
pprint.pprint(value)
168+
pprint.PrettyPrinter().pprint(value)
169+
162170
def test_knotted(self):
163171
# Verify .isrecursive() and .isreadable() w/ recursion
164172
# Tie a knot.
@@ -195,7 +203,7 @@ def test_knotted(self):
195203
def test_unreadable(self):
196204
# Not recursive but not readable anyway
197205
pp = pprint.PrettyPrinter()
198-
for unreadable in type(3), pprint, pprint.isrecursive:
206+
for unreadable in object(), int, pprint, pprint.isrecursive:
199207
# module-level convenience functions
200208
self.assertFalse(pprint.isrecursive(unreadable),
201209
"expected not isrecursive for %r" % (unreadable,))

0 commit comments

Comments
 (0)