Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added squash_unicode to prevent unicode pollution of json

  • Loading branch information...
commit 44caf8425ee29227f82d1e66aa7faf8d360f2ee9 1 parent 48fdd06
@minrk minrk authored
Showing with 55 additions and 4 deletions.
  1. +55 −4 IPython/zmq/parallel/streamsession.py
View
59 IPython/zmq/parallel/streamsession.py
@@ -35,9 +35,22 @@
else:
use_json = False
+def squash_unicode(obj):
+ if isinstance(obj,dict):
+ for key in obj.keys():
+ obj[key] = squash_unicode(obj[key])
+ if isinstance(key, unicode):
+ obj[squash_unicode(key)] = obj.pop(key)
+ elif isinstance(obj, list):
+ for i,v in enumerate(obj):
+ obj[i] = squash_unicode(v)
+ elif isinstance(obj, unicode):
+ obj = obj.encode('utf8')
+ return obj
+
if use_json:
default_packer = jsonapi.dumps
- default_unpacker = jsonapi.loads
+ default_unpacker = lambda s: squash_unicode(jsonapi.loads(s))
else:
default_packer = lambda o: pickle.dumps(o,-1)
default_unpacker = pickle.loads
@@ -300,8 +313,30 @@ def msg(self, msg_type, content=None, parent=None, subheader=None):
return msg
def send(self, stream, msg_type, content=None, buffers=None, parent=None, subheader=None, ident=None):
- """send a message via stream"""
- msg = self.msg(msg_type, content, parent, subheader)
+ """Build and send a message via stream or socket.
+
+ Parameters
+ ----------
+
+ msg_type : str or Message/dict
+ Normally, msg_type will be
+
+
+
+ Returns
+ -------
+ (msg,sent) : tuple
+ msg : Message
+ the nice wrapped dict-like object containing the headers
+
+ """
+ if isinstance(msg_type, (Message, dict)):
+ # we got a Message, not a msg_type
+ # don't build a new Message
+ msg = msg_type
+ content = msg['content']
+ else:
+ msg = self.msg(msg_type, content, parent, subheader)
buffers = [] if buffers is None else buffers
to_send = []
if isinstance(ident, list):
@@ -339,8 +374,24 @@ def send(self, stream, msg_type, content=None, buffers=None, parent=None, subhea
pprint.pprint(omsg)
pprint.pprint(to_send)
pprint.pprint(buffers)
+ # return both the msg object and the buffers
return omsg
-
+
+ def send_raw(self, stream, msg, flags=0, copy=True, idents=None):
+ """send a raw message via idents.
+
+ Parameters
+ ----------
+ msg : list of sendable buffers"""
+ to_send = []
+ if isinstance(ident, str):
+ ident = [ident]
+ if ident is not None:
+ to_send.extend(ident)
+ to_send.append(DELIM)
+ to_send.extend(msg)
+ stream.send_multipart(msg, flags, copy=copy)
+
def recv(self, socket, mode=zmq.NOBLOCK, content=True, copy=True):
"""receives and unpacks a message
returns [idents], msg"""
Please sign in to comment.
Something went wrong with that request. Please try again.