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
9 changes: 5 additions & 4 deletions Doc/c-api/object.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,14 @@ Object Protocol

Pretty print *o* with *printer*, and return the result as a string. This
is the equivalent of the Python expression ``f'{o!p:{printer}}'``, or
``f'{o!p}'`` when *printer* is ``NULL``.
``f'{o!p}'`` when *printer* is ``NULL`` or ``None``.

*printer* is called with *o*, and must return an iterable of strings, one
for each line of output. The lines are joined with ``'\n'``, with no
trailing newline. If *printer* is ``NULL``, a default printer is used. It
calls *o*'s ``__pprint__()`` method if there is one, and interprets the
description that it returns. Otherwise it uses :c:func:`PyObject_Repr`.
trailing newline. If *printer* is ``NULL`` or ``None``, a default printer
is used. It calls *o*'s ``__pprint__()`` method if there is one, and
interprets the description that it returns. Otherwise it uses
:c:func:`PyObject_Repr`.

Raise :exc:`TypeError` if *printer* returns a :class:`str` rather than an
iterable of lines, or if any line is not a :class:`str`.
Expand Down
5 changes: 3 additions & 2 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

from abc import ABCMeta, abstractmethod
import sys
lazy from _pprint import Layout as _Layout

GenericAlias = type(list[int])
EllipsisType = type(...)
Expand Down Expand Up @@ -840,8 +841,8 @@ def __repr__(self):
return '{0.__class__.__name__}({0._mapping!r})'.format(self)

def __pprint__(self):
from pprint import _mapping_view_pprint
return _mapping_view_pprint(self)
yield _Layout(open=f'{type(self).__name__}(', close=')', hug=True)
yield self._mapping

__class_getitem__ = classmethod(GenericAlias)

Expand Down
Loading
Loading