Repository: ipython/ipython

Issue:
UnicodeDecodeError
I am receiving an error running ipython from the shell. Everything was running fine a few days ago but recently I have been unable to get it running. 

I'm on osx mavericks and have pip installed everything in a virualenv. I have also created a new virtualenv and have the same error. Tried both V 1.1 and 1.0 but no luck

this is my python version

Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
here is what is installed

Jinja2==2.7.1
MarkupSafe==0.18
brewer2mpl==1.3.2
ipython==1.1.0
matplotlib==1.3.1
nose==1.3.0
numpy==1.8.0
pandas==0.12.0
prettyplotlib==0.1.3
pyparsing==2.0.1
python-dateutil==2.2
pytz==2013.8
pyzmq==14.0.0
seaborn==0.1
six==1.4.1
tornado==3.1.1
wsgiref==0.1.2

-----------------error from terminal-------------------------------------

(pyData)Zunayeds-MacBook-Pro:pyData zunayed$ ipython
Traceback (most recent call last):
  File "/Users/zunayed/pyData/bin/ipython", line 9, in <module>
    load_entry_point('ipython==1.1.0', 'console_scripts', 'ipython')()
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/**init**.py", line 118, in start_ipython
    return launch_new_instance(argv=argv, *_kwargs)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/config/application.py", line 544, in launch_instance
    app.initialize(argv)
  File "<string>", line 2, in initialize
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/config/application.py", line 89, in catch_config_error
    return method(app, *args, *_kwargs)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 323, in initialize
    self.init_shell()
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 339, in init_shell
    ipython_dir=self.ipython_dir, user_ns=self.user_ns)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/config/configurable.py", line 349, in instance
    inst = cls(_args, *_kwargs)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 424, in **new**
    value.instance_init(inst)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 255, in instance_init
    self.set_default_value(obj)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 275, in set_default_value
    newdv = self._validate(obj, dv)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 323, in _validate
    return self.validate(obj, value)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 1028, in validate
    return unicode(value)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 7: ordinal not in range(128)

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    c.Application.verbose_crash=True



Discussion:
Have you set any options in IPython config files? Does the working directory have any non-ascii characters in?

I can replicate the error in a clean virtualenv with no files in the workding directory other then the files created form virtualenv. Also I did not modify the Ipython config files

 An interesting thing is you can still run a fully functional ipython notebook using : ipython notebook -pylab inline

what is the output of `ipython --debug`?

```
(pyData)Zunayeds-MacBook-Pro:pyData zunayed$ ipython --debug
[TerminalIPythonApp] Config changed:
[TerminalIPythonApp] {'TerminalIPythonApp': {'log_level': 10}}
[TerminalIPythonApp] Using existing profile dir: u'/Users/zunayed/.ipython/profile_default'
[TerminalIPythonApp] Searching path [u'/Users/zunayed/pyData', u'/Users/zunayed/.ipython/profile_default'] for config files
[TerminalIPythonApp] Attempting to load config file: ipython_config.py
[TerminalIPythonApp] Config file ipython_config.py not found
Traceback (most recent call last):
  File "/Users/zunayed/pyData/bin/ipython", line 9, in <module>
    load_entry_point('ipython==1.1.0', 'console_scripts', 'ipython')()
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/__init__.py", line 118, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/config/application.py", line 544, in launch_instance
    app.initialize(argv)
  File "<string>", line 2, in initialize
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/config/application.py", line 89, in catch_config_error
    return method(app, *args, **kwargs)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 323, in initialize
    self.init_shell()
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 339, in init_shell
    ipython_dir=self.ipython_dir, user_ns=self.user_ns)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/config/configurable.py", line 349, in instance
    inst = cls(*args, **kwargs)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 424, in __new__
    value.instance_init(inst)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 255, in instance_init
    self.set_default_value(obj)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 275, in set_default_value
    newdv = self._validate(obj, dv)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 323, in _validate
    return self.validate(obj, value)
  File "/Users/zunayed/pyData/lib/python2.7/site-packages/IPython/utils/traitlets.py", line 1028, in validate
    return unicode(value)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 7: ordinal not in range(128)

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    c.Application.verbose_crash=True
```

Do you have `$EDITOR` set?

Yes! In my .bashrc file I have 

export EDITOR="subl -w"  

Commenting that line out lets python again! Thanks takluyver! Curious why this  happens.

Does the value of EDITOR have any non-ascii characters in? I.e., if you uncomment that again, and in Python, do:

```
import os
os.environ['EDITOR']
```

What do you see

So I ran those commands

```
>>> import os
>>> os.environ['EDITOR']
'subl -w\xc2\xa0\xc2\xa0'
>>>
```

Turns out there was some spaces after the
export EDITOR="subl -w"  

Removing them now allows yields 

```
>>> import os
>>> os.environ['EDITOR']
'subl -w'
```

That pretty much solved all my issues! 

Reopening because I'm going to work on at least having a better error message for this ;-)


Resolving patch:
diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py
--- a/IPython/terminal/interactiveshell.py
+++ b/IPython/terminal/interactiveshell.py
@@ -41,13 +41,19 @@
 def get_default_editor():
     try:
         ed = os.environ['EDITOR']
+        if not py3compat.PY3:
+            ed = ed.decode()
+        return ed
     except KeyError:
-        if os.name == 'posix':
-            ed = 'vi'  # the only one guaranteed to be there!
-        else:
-            ed = 'notepad' # same in Windows!
-    return ed
-
+        pass
+    except UnicodeError:
+        warn("$EDITOR environment variable is not pure ASCII. Using platform "
+             "default editor.")
+
+    if os.name == 'posix':
+        return 'vi'  # the only one guaranteed to be there!
+    else:
+        return 'notepad' # same in Windows!
 
 def get_pasted_lines(sentinel, l_input=py3compat.input):
     """ Yield pasted lines until the user enters the given sentinel value.
diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py
--- a/IPython/utils/traitlets.py
+++ b/IPython/utils/traitlets.py
@@ -1024,7 +1024,11 @@ def validate(self, obj, value):
         if isinstance(value, py3compat.unicode_type):
             return value
         if isinstance(value, bytes):
-            return py3compat.unicode_type(value)
+            try:
+                return value.decode('ascii', 'strict')
+            except UnicodeDecodeError:
+                msg = "Could not decode {!r} for unicode trait '{}' of {} instance."
+                raise TraitError(msg.format(value, self.name, class_of(obj)))
         self.error(obj, value)
 
 


Test patch: