From 79ea1ea815243a3d20d9b359b4fadf81cf06e964 2012-05-27 21:17:25 From: MinRK Date: 2012-05-27 21:17:25 Subject: [PATCH] displaypub b64 encoding is non-destructive `_encode_binary` helper fund encoded in-place, which is destructive if the dict is ever re-used. also prevents double-encoding, by only encoding bytes objects, since unicode objects are necessarily already encoded. --- diff --git a/IPython/zmq/displayhook.py b/IPython/zmq/displayhook.py index a3f76ab..a3d9178 100644 --- a/IPython/zmq/displayhook.py +++ b/IPython/zmq/displayhook.py @@ -31,12 +31,15 @@ class ZMQDisplayHook(object): def _encode_binary(format_dict): + encoded = format_dict.copy() pngdata = format_dict.get('image/png') - if pngdata is not None: - format_dict['image/png'] = encodestring(pngdata).decode('ascii') + if isinstance(pngdata, bytes): + encoded['image/png'] = encodestring(pngdata).decode('ascii') jpegdata = format_dict.get('image/jpeg') - if jpegdata is not None: - format_dict['image/jpeg'] = encodestring(jpegdata).decode('ascii') + if isinstance(jpegdata, bytes): + encoded['image/jpeg'] = encodestring(jpegdata).decode('ascii') + + return encoded class ZMQShellDisplayHook(DisplayHook): @@ -61,8 +64,7 @@ class ZMQShellDisplayHook(DisplayHook): self.msg['content']['execution_count'] = self.prompt_count def write_format_data(self, format_dict): - _encode_binary(format_dict) - self.msg['content']['data'] = format_dict + self.msg['content']['data'] = _encode_binary(format_dict) def finish_displayhook(self): """Finish up all displayhook activities.""" diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py index 7d9a7b7..0a0709f 100644 --- a/IPython/zmq/zmqshell.py +++ b/IPython/zmq/zmqshell.py @@ -77,8 +77,7 @@ class ZMQDisplayPublisher(DisplayPublisher): self._validate_data(source, data, metadata) content = {} content['source'] = source - _encode_binary(data) - content['data'] = data + content['data'] = _encode_binary(data) content['metadata'] = metadata self.session.send( self.pub_socket, u'display_data', json_clean(content),