Ezio Melotti <ezio.melo...@gmail.com> added the comment: This seems to fix the problem: ------------------------------ import sys import builtins
def hook(message): if message is None: return builtins._ = message try: print(repr(message)) except UnicodeEncodeError: print(ascii(message)) sys.displayhook = hook ------------------------------ Just to clarify: * The current Py3 behavior works fine in UTF8 terminals * It doesn't work on non-UTF8 terminals if they can't encode the chars (they raise an error) * It only affects the interactive interpreter * This new patch escapes the chars instead of raise an error only on non-UTF8 terminal and only when printed as ">>> foo" (without print()) and leaves the other behaviors unchanged * This is related to Py3 only Apparently the patch provided by Victor always escapes the non-ascii chars. This new hook function prints the Unicode chars if possible and escapes them if not. On a UTF8 terminal the behavior is unchanged, on a non-UTF8 terminal all the chars that can not be encoded will now be escaped. This only changes the behavior of ">>> foo", so it can not lead to confusion ("It works in the interpreter but not in the script"). In a script one can't write "foo" alone but "print(foo)" and the behavior of "print(foo)" is the same in both the interpreter and the scripts (with the patch applied): >>> ['\u2620'] ['\u2620'] >>> print(['\u2620']) UnicodeEncodeError: 'charmap' codec can't encode character '\u2620' in position 2: character maps to <undefined> I think that the PEP3138 didn't consider this issue. Its purpose is to have a better output (Unicode chars instead of escaped chars), but it only works with UTF8 terminals, on non-UTF8 terminals the output is worse (UnicodeEncodeError instead of escaped chars). This is an improvement and I can't see any negative side-effect. Attached there's a txt with more example, on Py2 and Py3, on Windows(non-UTF8 terminal) and Linux (UTF8 terminal), with and without my patch. Added file: http://bugs.python.org/file12900/issue5110.txt _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue5110> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com