Show More
@@ -34,7 +34,8 b' except:' | |||||
34 | from IPython.utils import py3compat |
|
34 | from IPython.utils import py3compat | |
35 | from IPython.utils.data import flatten |
|
35 | from IPython.utils.data import flatten | |
36 | from IPython.utils.pickleutil import ( |
|
36 | from IPython.utils.pickleutil import ( | |
37 | can, uncan, can_sequence, uncan_sequence, CannedObject |
|
37 | can, uncan, can_sequence, uncan_sequence, CannedObject, | |
|
38 | istype, sequence_types, | |||
38 | ) |
|
39 | ) | |
39 |
|
40 | |||
40 | if py3compat.PY3: |
|
41 | if py3compat.PY3: | |
@@ -91,11 +92,11 b' def serialize_object(obj, buffer_threshold=MAX_BYTES, item_threshold=MAX_ITEMS):' | |||||
91 | [bufs] : list of buffers representing the serialized object. |
|
92 | [bufs] : list of buffers representing the serialized object. | |
92 | """ |
|
93 | """ | |
93 | buffers = [] |
|
94 | buffers = [] | |
94 |
if is |
|
95 | if istype(obj, sequence_types) and len(obj) < item_threshold: | |
95 | cobj = can_sequence(obj) |
|
96 | cobj = can_sequence(obj) | |
96 | for c in cobj: |
|
97 | for c in cobj: | |
97 | buffers.extend(_extract_buffers(c, buffer_threshold)) |
|
98 | buffers.extend(_extract_buffers(c, buffer_threshold)) | |
98 |
elif is |
|
99 | elif istype(obj, dict) and len(obj) < item_threshold: | |
99 | cobj = {} |
|
100 | cobj = {} | |
100 | for k in sorted(obj.iterkeys()): |
|
101 | for k in sorted(obj.iterkeys()): | |
101 | c = can(obj[k]) |
|
102 | c = can(obj[k]) | |
@@ -129,7 +130,7 b' def unserialize_object(buffers, g=None):' | |||||
129 | # a zmq message |
|
130 | # a zmq message | |
130 | pobj = bytes(pobj) |
|
131 | pobj = bytes(pobj) | |
131 | canned = pickle.loads(pobj) |
|
132 | canned = pickle.loads(pobj) | |
132 |
if is |
|
133 | if istype(canned, sequence_types) and len(canned) < MAX_ITEMS: | |
133 | for c in canned: |
|
134 | for c in canned: | |
134 | _restore_buffers(c, bufs) |
|
135 | _restore_buffers(c, bufs) | |
135 | newobj = uncan_sequence(canned, g) |
|
136 | newobj = uncan_sequence(canned, g) |
@@ -12,6 +12,7 b'' | |||||
12 | #------------------------------------------------------------------------------- |
|
12 | #------------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | import pickle |
|
14 | import pickle | |
|
15 | from collections import namedtuple | |||
15 |
|
16 | |||
16 | import nose.tools as nt |
|
17 | import nose.tools as nt | |
17 |
|
18 | |||
@@ -186,12 +187,42 b' def test_class_oldstyle():' | |||||
186 |
|
187 | |||
187 | bufs = serialize_object(dict(C=C)) |
|
188 | bufs = serialize_object(dict(C=C)) | |
188 | canned = pickle.loads(bufs[0]) |
|
189 | canned = pickle.loads(bufs[0]) | |
189 | yield nt.assert_true(canned['C'], CannedClass) |
|
190 | yield nt.assert_true(isinstance(canned['C'], CannedClass)) | |
190 | d, r = unserialize_object(bufs) |
|
191 | d, r = unserialize_object(bufs) | |
191 | C2 = d['C'] |
|
192 | C2 = d['C'] | |
192 | yield nt.assert_equal(C2.a, C.a) |
|
193 | yield nt.assert_equal(C2.a, C.a) | |
193 |
|
194 | |||
194 | @dec.parametric |
|
195 | @dec.parametric | |
|
196 | def test_tuple(): | |||
|
197 | tup = (lambda x:x, 1) | |||
|
198 | bufs = serialize_object(tup) | |||
|
199 | canned = pickle.loads(bufs[0]) | |||
|
200 | yield nt.assert_true(isinstance(canned, tuple)) | |||
|
201 | t2, r = unserialize_object(bufs) | |||
|
202 | yield nt.assert_equal(t2[0](t2[1]), tup[0](tup[1])) | |||
|
203 | ||||
|
204 | point = namedtuple('point', 'x y') | |||
|
205 | ||||
|
206 | @dec.parametric | |||
|
207 | def test_namedtuple(): | |||
|
208 | p = point(1,2) | |||
|
209 | bufs = serialize_object(p) | |||
|
210 | canned = pickle.loads(bufs[0]) | |||
|
211 | yield nt.assert_true(isinstance(canned, point)) | |||
|
212 | p2, r = unserialize_object(bufs, globals()) | |||
|
213 | yield nt.assert_equal(p2.x, p.x) | |||
|
214 | yield nt.assert_equal(p2.y, p.y) | |||
|
215 | ||||
|
216 | @dec.parametric | |||
|
217 | def test_list(): | |||
|
218 | lis = [lambda x:x, 1] | |||
|
219 | bufs = serialize_object(lis) | |||
|
220 | canned = pickle.loads(bufs[0]) | |||
|
221 | yield nt.assert_true(isinstance(canned, list)) | |||
|
222 | l2, r = unserialize_object(bufs) | |||
|
223 | yield nt.assert_equal(l2[0](l2[1]), lis[0](lis[1])) | |||
|
224 | ||||
|
225 | @dec.parametric | |||
195 | def test_class_inheritance(): |
|
226 | def test_class_inheritance(): | |
196 | @interactive |
|
227 | @interactive | |
197 | class C(object): |
|
228 | class C(object): |
@@ -19,6 +19,7 b' Authors:' | |||||
19 | import sys |
|
19 | import sys | |
20 | import platform |
|
20 | import platform | |
21 | import time |
|
21 | import time | |
|
22 | from collections import namedtuple | |||
22 | from tempfile import mktemp |
|
23 | from tempfile import mktemp | |
23 | from StringIO import StringIO |
|
24 | from StringIO import StringIO | |
24 |
|
25 | |||
@@ -43,6 +44,8 b' from .clienttest import ClusterTestCase, crash, wait, skip_without' | |||||
43 | def setup(): |
|
44 | def setup(): | |
44 | add_engines(3, total=True) |
|
45 | add_engines(3, total=True) | |
45 |
|
46 | |||
|
47 | point = namedtuple("point", "x y") | |||
|
48 | ||||
46 | class TestView(ClusterTestCase, ParametricTestCase): |
|
49 | class TestView(ClusterTestCase, ParametricTestCase): | |
47 |
|
50 | |||
48 | def setUp(self): |
|
51 | def setUp(self): | |
@@ -735,3 +738,22 b' class TestView(ClusterTestCase, ParametricTestCase):' | |||||
735 | r = view.apply_sync(lambda x: x.b, ra) |
|
738 | r = view.apply_sync(lambda x: x.b, ra) | |
736 | self.assertEqual(r, 0) |
|
739 | self.assertEqual(r, 0) | |
737 | self.assertEqual(view['a.b'], 0) |
|
740 | self.assertEqual(view['a.b'], 0) | |
|
741 | ||||
|
742 | def test_return_namedtuple(self): | |||
|
743 | def namedtuplify(x, y): | |||
|
744 | from IPython.parallel.tests.test_view import point | |||
|
745 | return point(x, y) | |||
|
746 | ||||
|
747 | view = self.client[-1] | |||
|
748 | p = view.apply_sync(namedtuplify, 1, 2) | |||
|
749 | self.assertEqual(p.x, 1) | |||
|
750 | self.assertEqual(p.y, 2) | |||
|
751 | ||||
|
752 | def test_apply_namedtuple(self): | |||
|
753 | def echoxy(p): | |||
|
754 | return p.y, p.x | |||
|
755 | ||||
|
756 | view = self.client[-1] | |||
|
757 | tup = view.apply_sync(echoxy, point(1, 2)) | |||
|
758 | self.assertEqual(tup, (2,1)) | |||
|
759 |
@@ -249,7 +249,7 b' def can_class(obj):' | |||||
249 |
|
249 | |||
250 | def can_dict(obj): |
|
250 | def can_dict(obj): | |
251 | """can the *values* of a dict""" |
|
251 | """can the *values* of a dict""" | |
252 |
if is |
|
252 | if istype(obj, dict): | |
253 | newobj = {} |
|
253 | newobj = {} | |
254 | for k, v in obj.iteritems(): |
|
254 | for k, v in obj.iteritems(): | |
255 | newobj[k] = can(v) |
|
255 | newobj[k] = can(v) | |
@@ -257,9 +257,11 b' def can_dict(obj):' | |||||
257 | else: |
|
257 | else: | |
258 | return obj |
|
258 | return obj | |
259 |
|
259 | |||
|
260 | sequence_types = (list, tuple, set) | |||
|
261 | ||||
260 | def can_sequence(obj): |
|
262 | def can_sequence(obj): | |
261 | """can the elements of a sequence""" |
|
263 | """can the elements of a sequence""" | |
262 |
if is |
|
264 | if istype(obj, sequence_types): | |
263 | t = type(obj) |
|
265 | t = type(obj) | |
264 | return t([can(i) for i in obj]) |
|
266 | return t([can(i) for i in obj]) | |
265 | else: |
|
267 | else: | |
@@ -285,7 +287,7 b' def uncan(obj, g=None):' | |||||
285 | return obj |
|
287 | return obj | |
286 |
|
288 | |||
287 | def uncan_dict(obj, g=None): |
|
289 | def uncan_dict(obj, g=None): | |
288 |
if is |
|
290 | if istype(obj, dict): | |
289 | newobj = {} |
|
291 | newobj = {} | |
290 | for k, v in obj.iteritems(): |
|
292 | for k, v in obj.iteritems(): | |
291 | newobj[k] = uncan(v,g) |
|
293 | newobj[k] = uncan(v,g) | |
@@ -294,7 +296,7 b' def uncan_dict(obj, g=None):' | |||||
294 | return obj |
|
296 | return obj | |
295 |
|
297 | |||
296 | def uncan_sequence(obj, g=None): |
|
298 | def uncan_sequence(obj, g=None): | |
297 |
if is |
|
299 | if istype(obj, sequence_types): | |
298 | t = type(obj) |
|
300 | t = type(obj) | |
299 | return t([uncan(i,g) for i in obj]) |
|
301 | return t([uncan(i,g) for i in obj]) | |
300 | else: |
|
302 | else: |
General Comments 0
You need to be logged in to leave comments.
Login now