##// END OF EJS Templates
Merge pull request #7798 from jasongrout/buffer-memoryview...
Min RK -
r20764:84817ef0 merge
parent child Browse files
Show More
@@ -8,6 +8,7 b' import os'
8 import json
8 import json
9 import struct
9 import struct
10 import warnings
10 import warnings
11 import sys
11
12
12 try:
13 try:
13 from urllib.parse import urlparse # Py 3
14 from urllib.parse import urlparse # Py 3
@@ -43,6 +44,8 b' def serialize_binary_message(msg):'
43 # don't modify msg or buffer list in-place
44 # don't modify msg or buffer list in-place
44 msg = msg.copy()
45 msg = msg.copy()
45 buffers = list(msg.pop('buffers'))
46 buffers = list(msg.pop('buffers'))
47 if sys.version_info < (3, 4):
48 buffers = [x.tobytes() for x in buffers]
46 bmsg = json.dumps(msg, default=date_default).encode('utf8')
49 bmsg = json.dumps(msg, default=date_default).encode('utf8')
47 buffers.insert(0, bmsg)
50 buffers.insert(0, bmsg)
48 nbufs = len(buffers)
51 nbufs = len(buffers)
@@ -13,14 +13,14 b' from ..base.zmqhandlers import ('
13 def test_serialize_binary():
13 def test_serialize_binary():
14 s = Session()
14 s = Session()
15 msg = s.msg('data_pub', content={'a': 'b'})
15 msg = s.msg('data_pub', content={'a': 'b'})
16 msg['buffers'] = [ os.urandom(3) for i in range(3) ]
16 msg['buffers'] = [ memoryview(os.urandom(3)) for i in range(3) ]
17 bmsg = serialize_binary_message(msg)
17 bmsg = serialize_binary_message(msg)
18 nt.assert_is_instance(bmsg, bytes)
18 nt.assert_is_instance(bmsg, bytes)
19
19
20 def test_deserialize_binary():
20 def test_deserialize_binary():
21 s = Session()
21 s = Session()
22 msg = s.msg('data_pub', content={'a': 'b'})
22 msg = s.msg('data_pub', content={'a': 'b'})
23 msg['buffers'] = [ os.urandom(2) for i in range(3) ]
23 msg['buffers'] = [ memoryview(os.urandom(2)) for i in range(3) ]
24 bmsg = serialize_binary_message(msg)
24 bmsg = serialize_binary_message(msg)
25 msg2 = deserialize_binary_message(bmsg)
25 msg2 = deserialize_binary_message(bmsg)
26 nt.assert_equal(msg2, msg)
26 nt.assert_equal(msg2, msg)
@@ -810,18 +810,19 b' class Session(Configurable):'
810 Whether to unpack the content dict (True), or leave it packed
810 Whether to unpack the content dict (True), or leave it packed
811 (False).
811 (False).
812 copy : bool (True)
812 copy : bool (True)
813 Whether to return the bytes (True), or the non-copying Message
813 Whether msg_list contains bytes (True) or the non-copying Message
814 object in each place (False).
814 objects in each place (False).
815
815
816 Returns
816 Returns
817 -------
817 -------
818 msg : dict
818 msg : dict
819 The nested message dict with top-level keys [header, parent_header,
819 The nested message dict with top-level keys [header, parent_header,
820 content, buffers].
820 content, buffers]. The buffers are returned as memoryviews.
821 """
821 """
822 minlen = 5
822 minlen = 5
823 message = {}
823 message = {}
824 if not copy:
824 if not copy:
825 # pyzmq didn't copy the first parts of the message, so we'll do it
825 for i in range(minlen):
826 for i in range(minlen):
826 msg_list[i] = msg_list[i].bytes
827 msg_list[i] = msg_list[i].bytes
827 if self.auth is not None:
828 if self.auth is not None:
@@ -846,8 +847,11 b' class Session(Configurable):'
846 message['content'] = self.unpack(msg_list[4])
847 message['content'] = self.unpack(msg_list[4])
847 else:
848 else:
848 message['content'] = msg_list[4]
849 message['content'] = msg_list[4]
849
850 buffers = [memoryview(b) for b in msg_list[5:]]
850 message['buffers'] = msg_list[5:]
851 if buffers and buffers[0].shape is None:
852 # force copy to workaround pyzmq #646
853 buffers = [memoryview(b.bytes) for b in msg_list[5:]]
854 message['buffers'] = buffers
851 # adapt to the current version
855 # adapt to the current version
852 return adapt(message)
856 return adapt(message)
853
857
General Comments 0
You need to be logged in to leave comments. Login now