Ipython: crash with python files in wrong encoding

Created on 17 Sep 2016  ·  3Comments  ·  Source: ipython/ipython

%running a python file marked with # -*- coding: utf-8 -*- and saved in a non-utf8 format can crash ipython (return to shell).
To get the crash, the file must :

  • contain a non-ascii character
  • contain an error (when run with python3, a stack trace is generated, but no crash)
  • be saved with a wrong encoding

Here is a small script reproducing this, saved in ISO-8859-15, together with the stack trace:
files.zip

%run wrong_encoding.py
...
... <long trace>
...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 78: invalid continuation byte
 python3 -c "import IPython; print(IPython.sys_info())"
{'commit_hash': '5c9c918',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/usr/lib/python3.4/site-packages/IPython',
 'ipython_version': '5.1.0',
 'os_name': 'posix',
 'platform': 'Linux-3.16.7-42-desktop-x86_64-with-SuSE-13.2-x86_64',
 'sys_executable': '/usr/bin/python3',
 'sys_platform': 'linux',
 'sys_version': '3.4.5 (default, Jul 03 2016, 13:55:08) [GCC]'}

Of course, the file should have been saved in the correct format, in the first place.

But ideally the session would not be lost, and a more informative message would be issued.

Most helpful comment

I count thirteen chained tracebacks - I think getting that many without using recursion should get you some kind of prize. ;-)

All 3 comments

I count thirteen chained tracebacks - I think getting that many without using recursion should get you some kind of prize. ;-)

9955 is a rough fix for this; the error isn't very informative, but it doesn't crash IPython.

If the file name is not easy to reach, then here is the command I used to track down the culprit:

find . -name "*.py" -exec file {} \; | grep -v ASCII | grep -v UTF-8 | grep -v empty

Was this page helpful?
0 / 5 - 0 ratings