Skip to content

Commit dc4be56

Browse files
committed
Initial implementation.
1 parent 3758955 commit dc4be56

38 files changed

Lines changed: 693 additions & 126 deletions

‎Doc/builtins/stdtypes.rst‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3236,12 +3236,21 @@ brackets (``{}``).
32363236
Each replacement field must contain an expression, optionally followed by:
32373237

32383238
* a *debug specifier* -- an equal sign (``=``);
3239-
* a *conversion specifier* -- ``!s``, ``!r`` or ``!a``; and/or
3239+
* a *conversion specifier* -- ``!s``, ``!r``, ``!a`` or ``!p``; and/or
32403240
* a *format specifier* prefixed with a colon (``:``).
32413241

32423242
See the :ref:`Lexical Analysis section on f-strings <f-strings>` for details
32433243
on the syntax of these fields.
32443244

3245+
The ``!p`` conversion calls :c:func:`PyObject_Pretty`. Its format specifier is
3246+
passed as the ``printer`` argument without converting a replacement field to a
3247+
string. The format specifier must be a single replacement field whose value is
3248+
callable. If it is omitted or empty, ``NULL`` is passed as the printer argument
3249+
to :c:func:`PyObject_Pretty`, which uses a default printer that calls
3250+
``__pprint__()`` when available and interprets its iterable as a description
3251+
of the object's layout and contents. If the method is absent, the default
3252+
printer falls back to :func:`repr`.
3253+
32453254
Debug specifier
32463255
^^^^^^^^^^^^^^^
32473256

‎Doc/c-api/object.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,15 @@ Object Protocol
397397
This function now includes a debug assertion to help ensure that it
398398
does not silently discard an active exception.
399399
400+
.. c:function:: PyObject* PyObject_Pretty(PyObject *o, PyObject *printer)
401+
402+
Return the result of calling *printer* with *o*. If *printer* is ``NULL``,
403+
a default printer is created, called with *o*, and discarded. The default
404+
printer is implemented in the :mod:`_pprint` module. It calls *o*'s
405+
``__pprint__()`` method if present and interprets the returned stream as a
406+
description of the object's layout and contents. If the method is absent,
407+
the default printer returns the result of :c:func:`PyObject_Repr`.
408+
400409
401410
.. c:function:: PyObject* PyObject_Bytes(PyObject *o)
402411

‎Doc/data/refcounts.dat‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,10 @@ PyObject_Size:PyObject*:o:0:
19161916
PyObject_Str:PyObject*::+1:
19171917
PyObject_Str:PyObject*:o:0:
19181918

1919+
PyObject_Pretty:PyObject*::+1:
1920+
PyObject_Pretty:PyObject*:o:0:
1921+
PyObject_Pretty:PyObject*:printer:0:
1922+
19191923
Py_TYPE:PyObject*::0:
19201924
Py_TYPE:PyObject*:ob:0:
19211925

‎Doc/data/stable_abi.dat‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Doc/reference/lexical_analysis.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ After the expression, replacement fields may optionally contain:
11451145

11461146
* a *debug specifier* -- an equal sign (``=``), optionally surrounded by
11471147
whitespace on one or both sides;
1148-
* a *conversion specifier* -- ``!s``, ``!r`` or ``!a``; and/or
1148+
* a *conversion specifier* -- ``!s``, ``!r``, ``!a`` or ``!p``; and/or
11491149
* a *format specifier* prefixed with a colon (``:``).
11501150

11511151
See the :ref:`Standard Library section on f-strings <stdtypes-fstrings>`

‎Include/ceval.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
128128
#define FVC_STR 0x1
129129
#define FVC_REPR 0x2
130130
#define FVC_ASCII 0x3
131+
#define FVC_PRETTY 0x4
131132
#define FVS_MASK 0x4
132133
#define FVS_HAVE_SPEC 0x4
133134

‎Include/internal/pycore_global_objects_fini_generated.h‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_global_strings.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ struct _Py_global_strings {
205205
STRUCT_FOR_ID(__path__)
206206
STRUCT_FOR_ID(__pos__)
207207
STRUCT_FOR_ID(__pow__)
208+
STRUCT_FOR_ID(__pprint__)
208209
STRUCT_FOR_ID(__prepare__)
209210
STRUCT_FOR_ID(__qualname__)
210211
STRUCT_FOR_ID(__radd__)

‎Include/internal/pycore_magic_number.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ Known values:
304304
Python 3.16a1 3704 (Replace DELETE_ATTR with PUSH_NULL; STORE_ATTR)
305305
Python 3.16a1 3705 (Add INTRINSIC_ADD_CONDITIONAL_ANNOTATION)
306306
Python 3.16a1 3706 (Add INTRINSIC_MATCH_CLASS_ISINSTANCE)
307+
Python 3.16a1 3707 (Add FORMAT_PRETTY opcode)
307308
308309
Python 3.17 will start with 3750
309310
@@ -313,7 +314,7 @@ Known values:
313314
314315
*/
315316

316-
#define PYC_MAGIC_NUMBER 3706
317+
#define PYC_MAGIC_NUMBER 3707
317318
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
318319
(little-endian) and then appending b'\r\n'. */
319320
#define PYC_MAGIC_NUMBER_TOKEN \

‎Include/internal/pycore_opcode_metadata.h‎

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)