diff --git a/Doc/library/code.rst b/Doc/library/code.rst index 538e5afc7822aa..c76c1df349a44d 100644 --- a/Doc/library/code.rst +++ b/Doc/library/code.rst @@ -65,6 +65,22 @@ build applications which provide an interactive interpreter prompt. raises :exc:`OverflowError` or :exc:`ValueError` if the command contains an invalid literal. +Overriding Console Output +------------------------- + +All output is printed to :data:`sys.stderr` by default. A user can override +:meth:`InteractiveConsole.write` in a derived class to change that. +Alternatively, it is possible to selectively redirect parts of the output: + +* The return values of successfully interpreted Python statements are printed + with :func:`sys.displayhook`. +* Exception tracebacks and syntax errors can be redirected by setting + :func:`sys.excepthook`. + +Additionally, :class:`InteractiveConsole` will print banner information to +:data:`sys.stderr` if *banner* was passed to the constructor. + + .. _interpreter-objects: diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 7d8a669f9e16d4..2cdb9c95c6e48c 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -288,8 +288,9 @@ are always available. They are listed here in alphabetical order. The *mode* argument specifies what kind of code must be compiled; it can be ``'exec'`` if *source* consists of a sequence of statements, ``'eval'`` if it consists of a single expression, or ``'single'`` if it consists of a single - interactive statement (in the latter case, expression statements that - evaluate to something other than ``None`` will be printed). + interactive statement. In the ``'single'`` case, expression statements that + evaluate to something other than ``None`` will be printed using + :func:`sys.displayhook`. The optional arguments *flags* and *dont_inherit* control which :ref:`compiler options ` should be activated diff --git a/Misc/NEWS.d/next/Documentation/2021-05-25-20-35-37.bpo-12403.8BFtwS.rst b/Misc/NEWS.d/next/Documentation/2021-05-25-20-35-37.bpo-12403.8BFtwS.rst new file mode 100644 index 00000000000000..beef052bf8d0f6 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2021-05-25-20-35-37.bpo-12403.8BFtwS.rst @@ -0,0 +1 @@ +Update documentation of :mod:`code` and :func:`compile` to explain how their outputs can be redirected.