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
18 changes: 18 additions & 0 deletions Doc/c-api/object.rst
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,24 @@ Object Protocol
This function now includes a debug assertion to help ensure that it
does not silently discard an active exception.

.. c:function:: PyObject* PyObject_Pretty(PyObject *o, PyObject *printer)

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``.

*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`.

Raise :exc:`TypeError` if *printer* returns a :class:`str` rather than an
iterable of lines, or if any line is not a :class:`str`.

.. versionadded:: next


.. c:function:: PyObject* PyObject_Bytes(PyObject *o)

.. index:: pair: built-in function; bytes
Expand Down
4 changes: 4 additions & 0 deletions Doc/data/refcounts.dat
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,10 @@ PyObject_Size:PyObject*:o:0:
PyObject_Str:PyObject*::+1:
PyObject_Str:PyObject*:o:0:

PyObject_Pretty:PyObject*::+1:
PyObject_Pretty:PyObject*:o:0:
PyObject_Pretty:PyObject*:printer:0:

Py_TYPE:PyObject*::0:
Py_TYPE:PyObject*:ob:0:

Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ extern "C" {
assert(_PyObject_CheckConsistency(obj, 1)); */
extern int _PyObject_CheckConsistency(PyObject *op, int check_content);

// Export for shared _testinternalcapi extension
PyAPI_FUNC(PyObject *) _PyObject_Pretty(PyObject *obj, PyObject *printer);
extern PyObject *_PyObject_CallPprintHelper(PyObject *obj, const char *name);
#define _PyObject_PPRINT_METHOD(func, helper) \
static PyObject *func(PyObject *self, PyObject *Py_UNUSED(ignored)) \
Expand Down
1 change: 1 addition & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
/* Generic operations on objects */
PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_Pretty(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
Expand Down
Loading
Loading