Skip to content

Commit 9d2e1f2

Browse files
gh-56612: Document sys.displayhook use
1 parent 2359c37 commit 9d2e1f2

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎Doc/builtins/functions.rst‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,9 @@ are always available. They are listed here in alphabetical order.
352352
The *mode* argument specifies what kind of code must be compiled; it can be
353353
``'exec'`` if *source* consists of a sequence of statements, ``'eval'`` if it
354354
consists of a single expression, or ``'single'`` if it consists of a single
355-
interactive statement (in the latter case, expression statements that
356-
evaluate to something other than ``None`` will be printed).
355+
interactive statement. When the resulting code object is executed,
356+
expression statements that evaluate to something other than ``None`` are
357+
displayed using :func:`sys.displayhook`.
357358
It can also be ``'func_type'`` if *source* consists of a
358359
function type signature, for example ``(int) -> str``; see
359360
:func:`ast.parse` for more information. This mode requires passing

‎Doc/library/code.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ The ``code`` module provides facilities to implement read-eval-print loops in
1212
Python. Two classes and convenience functions are included which can be used to
1313
build applications which provide an interactive interpreter prompt.
1414

15+
When code compiled in ``'single'`` mode is executed, expression results other
16+
than ``None`` are displayed using :func:`sys.displayhook`, which writes to
17+
:data:`sys.stdout` by default. Syntax errors and tracebacks are displayed using
18+
:meth:`InteractiveInterpreter.write`, which writes to :data:`sys.stderr` by
19+
default. If :func:`sys.excepthook` has been overridden, it is used to display
20+
syntax errors and tracebacks instead. The console's banner and exit message are
21+
also written using :meth:`InteractiveInterpreter.write`.
22+
1523

1624
.. class:: InteractiveInterpreter(locals=None)
1725

0 commit comments

Comments
 (0)