diff -r 64640a02b0ca Doc/library/code.rst --- a/Doc/library/code.rst Mon Aug 27 10:03:23 2012 +0300 +++ b/Doc/library/code.rst Mon Aug 27 23:06:15 2012 +1000 @@ -59,6 +59,33 @@ invalid literal. +Overriding Console Output +------------------------- + +The output for :class:`InteractiveConsole` and :class:`InteractiveInterpreter` +is written using one of :data:`sys.stderr`, :func:`sys.displayhook` +or :func:`sys.excepthook`. Which output mechanism is used depends on the +reason for producing output: + +* 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*. +* If the default :func:`sys.excepthook` has not been replaced, syntax errors + and exception tracebacks are printed by calling the + :meth:`write ` method on :data:`sys.stderr`. +* If :func:`sys.excepthook` has been replaced with a user defined function + :func:`sys.excepthook` is called to handle printing of exception tracebacks + and syntax errors. + +Additionally, :class:`InteractiveConsole` will print banner information to +:data:`sys.stderr` if *banner* was passed to the constructor. + +To customize the console output a user should replace :func:`sys.displayhook` +and :func:`sys.excepthook` with custom implementations. Alternatively, users +can replace the :meth:`InteractiveConsole.write` method on a subclass +instead of replacing :func:`sys.excepthook` to control the printing of errors. + + .. _interpreter-objects: Interactive Interpreter Objects