Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-12403: Mention sys.displayhook in code module docs and the compile builtin docs #26217

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
19 changes: 8 additions & 11 deletions Doc/library/code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,14 @@ build applications which provide an interactive interpreter prompt.
Overriding Console Output
-------------------------

To customize console output a user can replace :func:`sys.displayhook`
and/or :func:`sys.excepthook`. Alternatively, users can override
:meth:`InteractiveConsole.write` in a derived class.

The destination of console output is controlled as follows:

* The return value from successfully interpreted Python statements are printed
by calling :func:`sys.displayhook`. This is a side effect of
:func:`compile_command` passing ``'single'`` for *symbol*.
* Exception tracebacks and syntax errors are written to :data:`sys.stderr` by
default but can be redirected by setting :func:`sys.excepthook`.
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap this so that no line is more than 80 chars.

Before we make this documented behavior, let's make sure that there are unit tests ensuring it is true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are tests for excepthook, but not for displayhook:
https://github.com/python/cpython/blob/main/Lib/test/test_code_module.py

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.
Expand Down