From dd58c55502dbd3f7bf3d6a75887481782856c2da 2019-05-31 01:07:37 From: Matthias Bussonnier Date: 2019-05-31 01:07:37 Subject: [PATCH] Merge pull request #11711 from pierstitus/unicodeerror Fix UnicodeEncodeError and crash. Fixes #11706 --- diff --git a/IPython/core/displayhook.py b/IPython/core/displayhook.py index 1cce4f1..d4ff9a6 100644 --- a/IPython/core/displayhook.py +++ b/IPython/core/displayhook.py @@ -187,7 +187,12 @@ class DisplayHook(Configurable): # But avoid extraneous empty lines. result_repr = '\n' + result_repr - print(result_repr) + try: + print(result_repr) + except UnicodeEncodeError: + # If a character is not supported by the terminal encoding replace + # it with its \u or \x representation + print(result_repr.encode(sys.stdout.encoding,'backslashreplace').decode(sys.stdout.encoding)) def update_user_ns(self, result): """Update user_ns with various things like _, __, _1, etc."""