##// END OF EJS Templates
Handle unicode properly in IPython.zmq.iostream
Thomas Kluyver -
Show More
@@ -1,7 +1,7 b''
1 import logging
1 import logging
2 import sys
2 import sys
3 import time
3 import time
4 from cStringIO import StringIO
4 from io import StringIO
5
5
6 from session import extract_header, Message
6 from session import extract_header, Message
7
7
@@ -45,10 +45,6 b' class OutStream(object):'
45 else:
45 else:
46 data = self._buffer.getvalue()
46 data = self._buffer.getvalue()
47 if data:
47 if data:
48 # Make sure that we're handling unicode
49 if not isinstance(data, unicode):
50 enc = sys.stdin.encoding or sys.getdefaultencoding()
51 data = data.decode(enc, 'replace')
52 content = {u'name':self.name, u'data':data}
48 content = {u'name':self.name, u'data':data}
53 msg = self.session.send(self.pub_socket, u'stream', content=content,
49 msg = self.session.send(self.pub_socket, u'stream', content=content,
54 parent=self.parent_header, ident=self.topic)
50 parent=self.parent_header, ident=self.topic)
@@ -73,11 +69,11 b' class OutStream(object):'
73 if self.pub_socket is None:
69 if self.pub_socket is None:
74 raise ValueError('I/O operation on closed file')
70 raise ValueError('I/O operation on closed file')
75 else:
71 else:
76 # We can only send raw bytes, not unicode objects, so we encode
72 # Make sure that we're handling unicode
77 # into utf-8 for all frontends if we get unicode inputs.
73 if not isinstance(string, unicode):
78 if type(string) == unicode:
74 enc = sys.stdin.encoding or sys.getdefaultencoding()
79 string = string.encode('utf-8')
75 string = string.decode(enc, 'replace')
80
76
81 self._buffer.write(string)
77 self._buffer.write(string)
82 current_time = time.time()
78 current_time = time.time()
83 if self._start <= 0:
79 if self._start <= 0:
General Comments 0
You need to be logged in to leave comments. Login now