Repository: ipython/ipython

Issue:
No sys.stdout.encoding in kernel based IPython
As originally reported by @tkf in #2207.
### Kernel-based IPython

``` python
% ./ipython.py console                       
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
Type "copyright", "credits" or "license" for more information.

IPython 0.14.dev -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-8616.json

In [1]: import sys

In [2]: sys.stdout.encoding
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-18fba92f6a6c> in <module>()
----> 1 sys.stdout.encoding

AttributeError: 'OutStream' object has no attribute 'encoding'
```
### Normal IPython

Same in pure Python console.

``` python
% ./ipython.py        
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
Type "copyright", "credits" or "license" for more information.

IPython 0.14.dev -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import sys

In [2]: sys.stdout.encoding
Out[2]: 'UTF-8'
```



Discussion:
Seems like this has stalled and it's a shame.  b7dcbc4 is a perfectly adequate 
solution to the original problem, and I'd like to see it merged. 
The more general issue can wait for someone else who cares about it.

Pandas was bitten by this bug: pydata/pandas@7bbc4e0.

@minrk , how about it?

I don't have time to deal with this, but I don't see anything wrong with the proposed fix.

thank you.

@ipython - would any commiter kindly merge 5f38795?
the property decorator is optional but harmless.

Github does not seem to recognize `5f38795` has a commit sha it knows about.
Could you be a little more specific like user/repo/branch or PR number ?

My bad.     0a83b1b   adds an 'encoding' property to OutStream. 
That's the one I meant.


Resolving patch:
diff --git a/IPython/zmq/iostream.py b/IPython/zmq/iostream.py
--- a/IPython/zmq/iostream.py
+++ b/IPython/zmq/iostream.py
@@ -4,7 +4,7 @@
 
 from session import extract_header, Message
 
-from IPython.utils import io, text, encoding
+from IPython.utils import io, text
 from IPython.utils import py3compat
 
 #-----------------------------------------------------------------------------
@@ -23,6 +23,7 @@ class OutStream(object):
     topic=None
 
     def __init__(self, session, pub_socket, name):
+        self.encoding = 'UTF-8'
         self.session = session
         self.pub_socket = pub_socket
         self.name = name
@@ -73,9 +74,8 @@ def write(self, string):
         else:
             # Make sure that we're handling unicode
             if not isinstance(string, unicode):
-                enc = encoding.DEFAULT_ENCODING
-                string = string.decode(enc, 'replace')
-            
+                string = string.decode(self.encoding, 'replace')
+
             self._buffer.write(string)
             current_time = time.time()
             if self._start <= 0:


Test patch: