[Python-checkins] bpo-41546: make pprint (like print) not write to stdout when it is None (GH-26810)

iritkatriel webhook-mailer at python.org
Mon Jul 19 05:19:07 EDT 2021


https://github.com/python/cpython/commit/aab1899c9d79083c1ff31d974ed8b562d3ca3b5d
commit: aab1899c9d79083c1ff31d974ed8b562d3ca3b5d
branch: main
author: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2021-07-19T10:19:02+01:00
summary:

bpo-41546: make pprint (like print) not write to stdout when it is None (GH-26810)

files:
A Misc/NEWS.d/next/Library/2021-06-20-14-03-18.bpo-41546.lO1jXU.rst
M Doc/library/pprint.rst
M Lib/pprint.py
M Lib/test/test_pprint.py

diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst
index 8f374503725ad..3da5aa9389b17 100644
--- a/Doc/library/pprint.rst
+++ b/Doc/library/pprint.rst
@@ -46,6 +46,8 @@ The :mod:`pprint` module defines one class:
 
    *stream* (default ``sys.stdout``) is a :term:`file-like object` to
    which the output will be written by calling its :meth:`write` method.
+   If both *stream* and ``sys.stdout`` are ``None``, then
+   :meth:`~PrettyPrinter.pprint` silently returns.
 
    Other values configure the manner in which nesting of complex data
    structures is displayed.
@@ -84,6 +86,9 @@ The :mod:`pprint` module defines one class:
    .. versionchanged:: 3.10
       Added the *underscore_numbers* parameter.
 
+   .. versionchanged:: 3.11
+      No longer attempts to write to ``sys.stdout`` if it is ``None``.
+
       >>> import pprint
       >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
       >>> stuff.insert(0, stuff[:])
@@ -107,24 +112,13 @@ The :mod:`pprint` module defines one class:
       >>> pp.pprint(tup)
       ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
 
-
-The :mod:`pprint` module also provides several shortcut functions:
-
 .. function:: pformat(object, indent=1, width=80, depth=None, *, \
                       compact=False, sort_dicts=True, underscore_numbers=False)
 
    Return the formatted representation of *object* as a string.  *indent*,
-   *width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* will be passed to the
-   :class:`PrettyPrinter` constructor as formatting parameters.
-
-   .. versionchanged:: 3.4
-      Added the *compact* parameter.
-
-   .. versionchanged:: 3.8
-      Added the *sort_dicts* parameter.
-
-   .. versionchanged:: 3.10
-      Added the *underscore_numbers* parameter.
+   *width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* are
+   passed to the :class:`PrettyPrinter` constructor as formatting parameters
+   and their meanings are as described in its documentation above.
 
 
 .. function:: pp(object, *args, sort_dicts=False, **kwargs)
@@ -142,20 +136,15 @@ The :mod:`pprint` module also provides several shortcut functions:
                      compact=False, sort_dicts=True, underscore_numbers=False)
 
    Prints the formatted representation of *object* on *stream*, followed by a
-   newline.  If *stream* is ``None``, ``sys.stdout`` is used.  This may be used
+   newline.  If *stream* is ``None``, ``sys.stdout`` is used. This may be used
    in the interactive interpreter instead of the :func:`print` function for
    inspecting values (you can even reassign ``print = pprint.pprint`` for use
-   within a scope).  *indent*, *width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* will
-   be passed to the :class:`PrettyPrinter` constructor as formatting parameters.
-
-   .. versionchanged:: 3.4
-      Added the *compact* parameter.
-
-   .. versionchanged:: 3.8
-      Added the *sort_dicts* parameter.
+   within a scope).
 
-   .. versionchanged:: 3.10
-      Added the *underscore_numbers* parameter.
+   The configuration parameters *stream*, *indent*, *width*, *depth*,
+   *compact*, *sort_dicts* and *underscore_numbers* are passed to the
+   :class:`PrettyPrinter` constructor and their meanings are as
+   described in its documentation above.
 
       >>> import pprint
       >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
@@ -168,7 +157,6 @@ The :mod:`pprint` module also provides several shortcut functions:
        'knights',
        'ni']
 
-
 .. function:: isreadable(object)
 
    .. index:: builtin: eval
diff --git a/Lib/pprint.py b/Lib/pprint.py
index 13819f3fef212..60ce57e910ec4 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -148,8 +148,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()
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
index 6c714fd39e203..c7b9893943471 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 @@ 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.
diff --git a/Misc/NEWS.d/next/Library/2021-06-20-14-03-18.bpo-41546.lO1jXU.rst b/Misc/NEWS.d/next/Library/2021-06-20-14-03-18.bpo-41546.lO1jXU.rst
new file mode 100644
index 0000000000000..050da761570d4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-06-20-14-03-18.bpo-41546.lO1jXU.rst
@@ -0,0 +1 @@
+Make :mod:`pprint` (like the builtin ``print``) not attempt to write to ``stdout`` when it is ``None``.



More information about the Python-checkins mailing list