Show More
@@ -8,6 +8,7 b' import os' | |||
|
8 | 8 | import json |
|
9 | 9 | import struct |
|
10 | 10 | import warnings |
|
11 | import sys | |
|
11 | 12 | |
|
12 | 13 | try: |
|
13 | 14 | from urllib.parse import urlparse # Py 3 |
@@ -43,6 +44,8 b' def serialize_binary_message(msg):' | |||
|
43 | 44 | # don't modify msg or buffer list in-place |
|
44 | 45 | msg = msg.copy() |
|
45 | 46 | buffers = list(msg.pop('buffers')) |
|
47 | if sys.version_info < (3, 4): | |
|
48 | buffers = [x.tobytes() for x in buffers] | |
|
46 | 49 | bmsg = json.dumps(msg, default=date_default).encode('utf8') |
|
47 | 50 | buffers.insert(0, bmsg) |
|
48 | 51 | nbufs = len(buffers) |
@@ -13,14 +13,14 b' from ..base.zmqhandlers import (' | |||
|
13 | 13 | def test_serialize_binary(): |
|
14 | 14 | s = Session() |
|
15 | 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 | 17 | bmsg = serialize_binary_message(msg) |
|
18 | 18 | nt.assert_is_instance(bmsg, bytes) |
|
19 | 19 | |
|
20 | 20 | def test_deserialize_binary(): |
|
21 | 21 | s = Session() |
|
22 | 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 | 24 | bmsg = serialize_binary_message(msg) |
|
25 | 25 | msg2 = deserialize_binary_message(bmsg) |
|
26 | 26 | nt.assert_equal(msg2, msg) |
@@ -810,18 +810,19 b' class Session(Configurable):' | |||
|
810 | 810 | Whether to unpack the content dict (True), or leave it packed |
|
811 | 811 | (False). |
|
812 | 812 | copy : bool (True) |
|
813 |
Whether |
|
|
814 | object in each place (False). | |
|
813 | Whether msg_list contains bytes (True) or the non-copying Message | |
|
814 | objects in each place (False). | |
|
815 | 815 | |
|
816 | 816 | Returns |
|
817 | 817 | ------- |
|
818 | 818 | msg : dict |
|
819 | 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 | 822 | minlen = 5 |
|
823 | 823 | message = {} |
|
824 | 824 | if not copy: |
|
825 | # pyzmq didn't copy the first parts of the message, so we'll do it | |
|
825 | 826 | for i in range(minlen): |
|
826 | 827 | msg_list[i] = msg_list[i].bytes |
|
827 | 828 | if self.auth is not None: |
@@ -846,8 +847,11 b' class Session(Configurable):' | |||
|
846 | 847 | message['content'] = self.unpack(msg_list[4]) |
|
847 | 848 | else: |
|
848 | 849 | message['content'] = msg_list[4] |
|
849 | ||
|
850 | message['buffers'] = msg_list[5:] | |
|
850 | buffers = [memoryview(b) for b in 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 | 855 | # adapt to the current version |
|
852 | 856 | return adapt(message) |
|
853 | 857 |
General Comments 0
You need to be logged in to leave comments.
Login now