diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-12-04 17:24:33 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-12-04 17:24:33 (GMT) |
commit | 13d49ee7d6a44af656fd77713342e419ec57e4a5 (patch) | |
tree | 30250bac5be4f5e904d62f3628424e0ef6e22c2c /Doc | |
parent | 44588b45d2b9cedb9fd91f82c1b00cd781a56c94 (diff) | |
download | cpython-13d49ee7d6a44af656fd77713342e419ec57e4a5.zip cpython-13d49ee7d6a44af656fd77713342e419ec57e4a5.tar.gz cpython-13d49ee7d6a44af656fd77713342e419ec57e4a5.tar.bz2 |
Issue #10601: sys.displayhook uses 'backslashreplace' error handler on
UnicodeEncodeError.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/sys.rst | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index c7aa214..95d1cf9 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -99,13 +99,39 @@ always available. .. function:: displayhook(value) - If *value* is not ``None``, this function prints it to ``sys.stdout``, and saves - it in ``builtins._``. + If *value* is not ``None``, this function prints ``repr(value)`` to + ``sys.stdout``, and saves *value* in ``builtins._``. If ``repr(value)`` is + not encodable to ``sys.stdout.encoding`` with ``sys.stdout.errors`` error + handler (which is probably ``'strict'``), encode it to + ``sys.stdout.encoding`` with ``'backslashreplace'`` error handler. ``sys.displayhook`` is called on the result of evaluating an :term:`expression` entered in an interactive Python session. The display of these values can be customized by assigning another one-argument function to ``sys.displayhook``. + Pseudo-code:: + + def displayhook(value): + if value is None: + return + # Set '_' to None to avoid recursion + builtins._ = None + text = repr(value) + try: + sys.stdout.write(text) + except UnicodeEncodeError: + bytes = text.encode(sys.stdout.encoding, 'backslashreplace') + if hasattr(sys.stdout, 'buffer'): + sys.stdout.buffer.write(bytes) + else: + text = bytes.decode(sys.stdout.encoding, 'strict') + sys.stdout.write(text) + sys.stdout.write("\n") + builtins._ = value + + .. versionchanged:: 3.2 + Use ``'backslashreplace'`` error handler on :exc:`UnicodeEncodeError`. + .. function:: excepthook(type, value, traceback) |