From d9d089759de900875c14133f9b0b8cd2318e4118 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Mon, 16 Feb 2015 21:53:26 +0000 Subject: [PATCH 1/3] Fix websocket/zmq serialization to expect memoryviews --- IPython/html/base/zmqhandlers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/IPython/html/base/zmqhandlers.py b/IPython/html/base/zmqhandlers.py index f1159e830..eedf71e80 100644 --- a/IPython/html/base/zmqhandlers.py +++ b/IPython/html/base/zmqhandlers.py @@ -43,6 +43,8 @@ def serialize_binary_message(msg): # don't modify msg or buffer list in-place msg = msg.copy() buffers = list(msg.pop('buffers')) + # for python 2, copy the buffer memoryviews to byte strings + buffers = [x.tobytes() for x in buffers] bmsg = json.dumps(msg, default=date_default).encode('utf8') buffers.insert(0, bmsg) nbufs = len(buffers) From e441242f21bc8dc737e6dd4d982ea7e1c04ca5b6 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Mon, 16 Feb 2015 22:12:29 +0000 Subject: [PATCH 2/3] Fix serialization tests --- IPython/html/tests/test_serialize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/IPython/html/tests/test_serialize.py b/IPython/html/tests/test_serialize.py index 7a88b29f4..9d45d6bc8 100644 --- a/IPython/html/tests/test_serialize.py +++ b/IPython/html/tests/test_serialize.py @@ -13,14 +13,14 @@ from ..base.zmqhandlers import ( def test_serialize_binary(): s = Session() msg = s.msg('data_pub', content={'a': 'b'}) - msg['buffers'] = [ os.urandom(3) for i in range(3) ] + msg['buffers'] = [ memoryview(os.urandom(3)) for i in range(3) ] bmsg = serialize_binary_message(msg) nt.assert_is_instance(bmsg, bytes) def test_deserialize_binary(): s = Session() msg = s.msg('data_pub', content={'a': 'b'}) - msg['buffers'] = [ os.urandom(2) for i in range(3) ] + msg['buffers'] = [ memoryview(os.urandom(2)) for i in range(3) ] bmsg = serialize_binary_message(msg) msg2 = deserialize_binary_message(bmsg) nt.assert_equal(msg2, msg) From 8b5715d738de4433b316e54f47986f47e3c97b8f Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Tue, 17 Feb 2015 16:52:28 +0000 Subject: [PATCH 3/3] Add check to skip work in versions past 3.4 --- IPython/html/base/zmqhandlers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/IPython/html/base/zmqhandlers.py b/IPython/html/base/zmqhandlers.py index eedf71e80..5ddb5d27e 100644 --- a/IPython/html/base/zmqhandlers.py +++ b/IPython/html/base/zmqhandlers.py @@ -8,6 +8,7 @@ import os import json import struct import warnings +import sys try: from urllib.parse import urlparse # Py 3 @@ -43,8 +44,8 @@ def serialize_binary_message(msg): # don't modify msg or buffer list in-place msg = msg.copy() buffers = list(msg.pop('buffers')) - # for python 2, copy the buffer memoryviews to byte strings - buffers = [x.tobytes() for x in buffers] + if sys.version_info < (3, 4): + buffers = [x.tobytes() for x in buffers] bmsg = json.dumps(msg, default=date_default).encode('utf8') buffers.insert(0, bmsg) nbufs = len(buffers)