diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2021-07-19 09:19:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-19 09:19:02 (GMT) |
commit | aab1899c9d79083c1ff31d974ed8b562d3ca3b5d (patch) | |
tree | d243fa6ada2ea05367737fe9a35573409df6e5a5 /Lib | |
parent | 2c2055884420f22afb4d2045bbdab7aa1394cb63 (diff) | |
download | cpython-aab1899c9d79083c1ff31d974ed8b562d3ca3b5d.zip cpython-aab1899c9d79083c1ff31d974ed8b562d3ca3b5d.tar.gz cpython-aab1899c9d79083c1ff31d974ed8b562d3ca3b5d.tar.bz2 |
bpo-41546: make pprint (like print) not write to stdout when it is None (GH-26810)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pprint.py | 5 | ||||
-rw-r--r-- | Lib/test/test_pprint.py | 8 |
2 files changed, 11 insertions, 2 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py index 13819f3..60ce57e 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -148,8 +148,9 @@ class PrettyPrinter: 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() diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 6c714fd..c7b9893 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import collections +import contextlib import dataclasses import io import itertools @@ -159,6 +160,13 @@ class QueryTestCase(unittest.TestCase): 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. |