Skip to content

Commit 214fb4f

Browse files
committed
skip formatting altogether if stdout is None
1 parent 793fba5 commit 214fb4f

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

Lib/pprint.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,16 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
141141
self._width = width
142142
if stream is not None:
143143
self._stream = stream
144-
elif _sys.stdout is not None:
145-
self._stream = _sys.stdout
146144
else:
147-
class _NullStdout:
148-
def write(self, s):
149-
return 0
150-
self._stream = _NullStdout()
145+
self._stream = _sys.stdout
151146
self._compact = bool(compact)
152147
self._sort_dicts = sort_dicts
153148
self._underscore_numbers = underscore_numbers
154149

155150
def pprint(self, object):
156-
self._format(object, self._stream, 0, 0, {}, 0)
157-
self._stream.write("\n")
151+
if self._stream is not None:
152+
self._format(object, self._stream, 0, 0, {}, 0)
153+
self._stream.write("\n")
158154

159155
def pformat(self, object):
160156
sio = _StringIO()

Lib/test/test_pprint.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ def test_basic(self):
162162

163163
def test_stdout_is_None(self):
164164
with contextlib.redirect_stdout(None):
165-
pprint.pprint('this should not fail')
165+
# smoke test - there is no output to check
166+
value = 'this should not fail'
167+
pprint.pprint(value)
168+
pprint.PrettyPrinter().pprint(value)
166169

167170
def test_knotted(self):
168171
# Verify .isrecursive() and .isreadable() w/ recursion

0 commit comments

Comments
 (0)