From c86e43f8cd0dc70b5c2345e04ba5893659510f35 2016-09-18 20:59:33 From: Matthias Bussonnier Date: 2016-09-18 20:59:33 Subject: [PATCH] Backport PR #9955: Catch UnicodeDecodeError from file lying about its encoding Closes gh-9954 I'd like to show a more useful error, but we're several layers away from the code that knows the filename, and I don't want to duplicate a load of code from the traceback module. This prevents it from crashing IPython, at least. --- diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py index 2e4268f..98e2cd5 100644 --- a/IPython/core/ultratb.py +++ b/IPython/core/ultratb.py @@ -1130,6 +1130,12 @@ class VerboseTB(TBTools): # problems, but it generates empty tracebacks for console errors # (5 blanks lines) where none should be returned. return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) + except UnicodeDecodeError: + # This can occur if a file's encoding magic comment is wrong. + # I can't see a way to recover without duplicating a bunch of code + # from the stdlib traceback module. --TK + error('\nUnicodeDecodeError while processing traceback.\n') + return None except: # FIXME: I've been getting many crash reports from python 2.3 # users, traceable to inspect.py. If I can find a small test-case