Show More
@@ -1,210 +1,241 b'' | |||||
1 | """test serialization tools""" |
|
1 | """test serialization tools""" | |
2 |
|
2 | |||
3 | #------------------------------------------------------------------------------- |
|
3 | #------------------------------------------------------------------------------- | |
4 | # Copyright (C) 2011 The IPython Development Team |
|
4 | # Copyright (C) 2011 The IPython Development Team | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the BSD License. The full license is in |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
7 | # the file COPYING, distributed as part of this software. |
|
7 | # the file COPYING, distributed as part of this software. | |
8 | #------------------------------------------------------------------------------- |
|
8 | #------------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #------------------------------------------------------------------------------- |
|
10 | #------------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
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 | |||
18 | # from unittest import TestCaes |
|
19 | # from unittest import TestCaes | |
19 | from IPython.kernel.zmq.serialize import serialize_object, unserialize_object |
|
20 | from IPython.kernel.zmq.serialize import serialize_object, unserialize_object | |
20 | from IPython.testing import decorators as dec |
|
21 | from IPython.testing import decorators as dec | |
21 | from IPython.utils.pickleutil import CannedArray, CannedClass |
|
22 | from IPython.utils.pickleutil import CannedArray, CannedClass | |
22 | from IPython.parallel import interactive |
|
23 | from IPython.parallel import interactive | |
23 |
|
24 | |||
24 | #------------------------------------------------------------------------------- |
|
25 | #------------------------------------------------------------------------------- | |
25 | # Globals and Utilities |
|
26 | # Globals and Utilities | |
26 | #------------------------------------------------------------------------------- |
|
27 | #------------------------------------------------------------------------------- | |
27 |
|
28 | |||
28 | def roundtrip(obj): |
|
29 | def roundtrip(obj): | |
29 | """roundtrip an object through serialization""" |
|
30 | """roundtrip an object through serialization""" | |
30 | bufs = serialize_object(obj) |
|
31 | bufs = serialize_object(obj) | |
31 | obj2, remainder = unserialize_object(bufs) |
|
32 | obj2, remainder = unserialize_object(bufs) | |
32 | nt.assert_equals(remainder, []) |
|
33 | nt.assert_equals(remainder, []) | |
33 | return obj2 |
|
34 | return obj2 | |
34 |
|
35 | |||
35 | class C(object): |
|
36 | class C(object): | |
36 | """dummy class for """ |
|
37 | """dummy class for """ | |
37 |
|
38 | |||
38 | def __init__(self, **kwargs): |
|
39 | def __init__(self, **kwargs): | |
39 | for key,value in kwargs.iteritems(): |
|
40 | for key,value in kwargs.iteritems(): | |
40 | setattr(self, key, value) |
|
41 | setattr(self, key, value) | |
41 |
|
42 | |||
42 | SHAPES = ((100,), (1024,10), (10,8,6,5), (), (0,)) |
|
43 | SHAPES = ((100,), (1024,10), (10,8,6,5), (), (0,)) | |
43 | DTYPES = ('uint8', 'float64', 'int32', [('g', 'float32')], '|S10') |
|
44 | DTYPES = ('uint8', 'float64', 'int32', [('g', 'float32')], '|S10') | |
44 | #------------------------------------------------------------------------------- |
|
45 | #------------------------------------------------------------------------------- | |
45 | # Tests |
|
46 | # Tests | |
46 | #------------------------------------------------------------------------------- |
|
47 | #------------------------------------------------------------------------------- | |
47 |
|
48 | |||
48 | @dec.parametric |
|
49 | @dec.parametric | |
49 | def test_roundtrip_simple(): |
|
50 | def test_roundtrip_simple(): | |
50 | for obj in [ |
|
51 | for obj in [ | |
51 | 'hello', |
|
52 | 'hello', | |
52 | dict(a='b', b=10), |
|
53 | dict(a='b', b=10), | |
53 | [1,2,'hi'], |
|
54 | [1,2,'hi'], | |
54 | (b'123', 'hello'), |
|
55 | (b'123', 'hello'), | |
55 | ]: |
|
56 | ]: | |
56 | obj2 = roundtrip(obj) |
|
57 | obj2 = roundtrip(obj) | |
57 | yield nt.assert_equals(obj, obj2) |
|
58 | yield nt.assert_equals(obj, obj2) | |
58 |
|
59 | |||
59 | @dec.parametric |
|
60 | @dec.parametric | |
60 | def test_roundtrip_nested(): |
|
61 | def test_roundtrip_nested(): | |
61 | for obj in [ |
|
62 | for obj in [ | |
62 | dict(a=range(5), b={1:b'hello'}), |
|
63 | dict(a=range(5), b={1:b'hello'}), | |
63 | [range(5),[range(3),(1,[b'whoda'])]], |
|
64 | [range(5),[range(3),(1,[b'whoda'])]], | |
64 | ]: |
|
65 | ]: | |
65 | obj2 = roundtrip(obj) |
|
66 | obj2 = roundtrip(obj) | |
66 | yield nt.assert_equals(obj, obj2) |
|
67 | yield nt.assert_equals(obj, obj2) | |
67 |
|
68 | |||
68 | @dec.parametric |
|
69 | @dec.parametric | |
69 | def test_roundtrip_buffered(): |
|
70 | def test_roundtrip_buffered(): | |
70 | for obj in [ |
|
71 | for obj in [ | |
71 | dict(a=b"x"*1025), |
|
72 | dict(a=b"x"*1025), | |
72 | b"hello"*500, |
|
73 | b"hello"*500, | |
73 | [b"hello"*501, 1,2,3] |
|
74 | [b"hello"*501, 1,2,3] | |
74 | ]: |
|
75 | ]: | |
75 | bufs = serialize_object(obj) |
|
76 | bufs = serialize_object(obj) | |
76 | yield nt.assert_equals(len(bufs), 2) |
|
77 | yield nt.assert_equals(len(bufs), 2) | |
77 | obj2, remainder = unserialize_object(bufs) |
|
78 | obj2, remainder = unserialize_object(bufs) | |
78 | yield nt.assert_equals(remainder, []) |
|
79 | yield nt.assert_equals(remainder, []) | |
79 | yield nt.assert_equals(obj, obj2) |
|
80 | yield nt.assert_equals(obj, obj2) | |
80 |
|
81 | |||
81 | def _scrub_nan(A): |
|
82 | def _scrub_nan(A): | |
82 | """scrub nans out of empty arrays |
|
83 | """scrub nans out of empty arrays | |
83 |
|
84 | |||
84 | since nan != nan |
|
85 | since nan != nan | |
85 | """ |
|
86 | """ | |
86 | import numpy |
|
87 | import numpy | |
87 | if A.dtype.fields and A.shape: |
|
88 | if A.dtype.fields and A.shape: | |
88 | for field in A.dtype.fields.keys(): |
|
89 | for field in A.dtype.fields.keys(): | |
89 | try: |
|
90 | try: | |
90 | A[field][numpy.isnan(A[field])] = 0 |
|
91 | A[field][numpy.isnan(A[field])] = 0 | |
91 | except (TypeError, NotImplementedError): |
|
92 | except (TypeError, NotImplementedError): | |
92 | # e.g. str dtype |
|
93 | # e.g. str dtype | |
93 | pass |
|
94 | pass | |
94 |
|
95 | |||
95 | @dec.parametric |
|
96 | @dec.parametric | |
96 | @dec.skip_without('numpy') |
|
97 | @dec.skip_without('numpy') | |
97 | def test_numpy(): |
|
98 | def test_numpy(): | |
98 | import numpy |
|
99 | import numpy | |
99 | from numpy.testing.utils import assert_array_equal |
|
100 | from numpy.testing.utils import assert_array_equal | |
100 | for shape in SHAPES: |
|
101 | for shape in SHAPES: | |
101 | for dtype in DTYPES: |
|
102 | for dtype in DTYPES: | |
102 | A = numpy.empty(shape, dtype=dtype) |
|
103 | A = numpy.empty(shape, dtype=dtype) | |
103 | _scrub_nan(A) |
|
104 | _scrub_nan(A) | |
104 | bufs = serialize_object(A) |
|
105 | bufs = serialize_object(A) | |
105 | B, r = unserialize_object(bufs) |
|
106 | B, r = unserialize_object(bufs) | |
106 | yield nt.assert_equals(r, []) |
|
107 | yield nt.assert_equals(r, []) | |
107 | yield nt.assert_equals(A.shape, B.shape) |
|
108 | yield nt.assert_equals(A.shape, B.shape) | |
108 | yield nt.assert_equals(A.dtype, B.dtype) |
|
109 | yield nt.assert_equals(A.dtype, B.dtype) | |
109 | yield assert_array_equal(A,B) |
|
110 | yield assert_array_equal(A,B) | |
110 |
|
111 | |||
111 | @dec.parametric |
|
112 | @dec.parametric | |
112 | @dec.skip_without('numpy') |
|
113 | @dec.skip_without('numpy') | |
113 | def test_recarray(): |
|
114 | def test_recarray(): | |
114 | import numpy |
|
115 | import numpy | |
115 | from numpy.testing.utils import assert_array_equal |
|
116 | from numpy.testing.utils import assert_array_equal | |
116 | for shape in SHAPES: |
|
117 | for shape in SHAPES: | |
117 | for dtype in [ |
|
118 | for dtype in [ | |
118 | [('f', float), ('s', '|S10')], |
|
119 | [('f', float), ('s', '|S10')], | |
119 | [('n', int), ('s', '|S1'), ('u', 'uint32')], |
|
120 | [('n', int), ('s', '|S1'), ('u', 'uint32')], | |
120 | ]: |
|
121 | ]: | |
121 | A = numpy.empty(shape, dtype=dtype) |
|
122 | A = numpy.empty(shape, dtype=dtype) | |
122 | _scrub_nan(A) |
|
123 | _scrub_nan(A) | |
123 |
|
124 | |||
124 | bufs = serialize_object(A) |
|
125 | bufs = serialize_object(A) | |
125 | B, r = unserialize_object(bufs) |
|
126 | B, r = unserialize_object(bufs) | |
126 | yield nt.assert_equals(r, []) |
|
127 | yield nt.assert_equals(r, []) | |
127 | yield nt.assert_equals(A.shape, B.shape) |
|
128 | yield nt.assert_equals(A.shape, B.shape) | |
128 | yield nt.assert_equals(A.dtype, B.dtype) |
|
129 | yield nt.assert_equals(A.dtype, B.dtype) | |
129 | yield assert_array_equal(A,B) |
|
130 | yield assert_array_equal(A,B) | |
130 |
|
131 | |||
131 | @dec.parametric |
|
132 | @dec.parametric | |
132 | @dec.skip_without('numpy') |
|
133 | @dec.skip_without('numpy') | |
133 | def test_numpy_in_seq(): |
|
134 | def test_numpy_in_seq(): | |
134 | import numpy |
|
135 | import numpy | |
135 | from numpy.testing.utils import assert_array_equal |
|
136 | from numpy.testing.utils import assert_array_equal | |
136 | for shape in SHAPES: |
|
137 | for shape in SHAPES: | |
137 | for dtype in DTYPES: |
|
138 | for dtype in DTYPES: | |
138 | A = numpy.empty(shape, dtype=dtype) |
|
139 | A = numpy.empty(shape, dtype=dtype) | |
139 | _scrub_nan(A) |
|
140 | _scrub_nan(A) | |
140 | bufs = serialize_object((A,1,2,b'hello')) |
|
141 | bufs = serialize_object((A,1,2,b'hello')) | |
141 | canned = pickle.loads(bufs[0]) |
|
142 | canned = pickle.loads(bufs[0]) | |
142 | yield nt.assert_true(canned[0], CannedArray) |
|
143 | yield nt.assert_true(canned[0], CannedArray) | |
143 | tup, r = unserialize_object(bufs) |
|
144 | tup, r = unserialize_object(bufs) | |
144 | B = tup[0] |
|
145 | B = tup[0] | |
145 | yield nt.assert_equals(r, []) |
|
146 | yield nt.assert_equals(r, []) | |
146 | yield nt.assert_equals(A.shape, B.shape) |
|
147 | yield nt.assert_equals(A.shape, B.shape) | |
147 | yield nt.assert_equals(A.dtype, B.dtype) |
|
148 | yield nt.assert_equals(A.dtype, B.dtype) | |
148 | yield assert_array_equal(A,B) |
|
149 | yield assert_array_equal(A,B) | |
149 |
|
150 | |||
150 | @dec.parametric |
|
151 | @dec.parametric | |
151 | @dec.skip_without('numpy') |
|
152 | @dec.skip_without('numpy') | |
152 | def test_numpy_in_dict(): |
|
153 | def test_numpy_in_dict(): | |
153 | import numpy |
|
154 | import numpy | |
154 | from numpy.testing.utils import assert_array_equal |
|
155 | from numpy.testing.utils import assert_array_equal | |
155 | for shape in SHAPES: |
|
156 | for shape in SHAPES: | |
156 | for dtype in DTYPES: |
|
157 | for dtype in DTYPES: | |
157 | A = numpy.empty(shape, dtype=dtype) |
|
158 | A = numpy.empty(shape, dtype=dtype) | |
158 | _scrub_nan(A) |
|
159 | _scrub_nan(A) | |
159 | bufs = serialize_object(dict(a=A,b=1,c=range(20))) |
|
160 | bufs = serialize_object(dict(a=A,b=1,c=range(20))) | |
160 | canned = pickle.loads(bufs[0]) |
|
161 | canned = pickle.loads(bufs[0]) | |
161 | yield nt.assert_true(canned['a'], CannedArray) |
|
162 | yield nt.assert_true(canned['a'], CannedArray) | |
162 | d, r = unserialize_object(bufs) |
|
163 | d, r = unserialize_object(bufs) | |
163 | B = d['a'] |
|
164 | B = d['a'] | |
164 | yield nt.assert_equals(r, []) |
|
165 | yield nt.assert_equals(r, []) | |
165 | yield nt.assert_equals(A.shape, B.shape) |
|
166 | yield nt.assert_equals(A.shape, B.shape) | |
166 | yield nt.assert_equals(A.dtype, B.dtype) |
|
167 | yield nt.assert_equals(A.dtype, B.dtype) | |
167 | yield assert_array_equal(A,B) |
|
168 | yield assert_array_equal(A,B) | |
168 |
|
169 | |||
169 | @dec.parametric |
|
170 | @dec.parametric | |
170 | def test_class(): |
|
171 | def test_class(): | |
171 | @interactive |
|
172 | @interactive | |
172 | class C(object): |
|
173 | class C(object): | |
173 | a=5 |
|
174 | a=5 | |
174 | bufs = serialize_object(dict(C=C)) |
|
175 | bufs = serialize_object(dict(C=C)) | |
175 | canned = pickle.loads(bufs[0]) |
|
176 | canned = pickle.loads(bufs[0]) | |
176 | yield nt.assert_true(canned['C'], CannedClass) |
|
177 | yield nt.assert_true(canned['C'], CannedClass) | |
177 | d, r = unserialize_object(bufs) |
|
178 | d, r = unserialize_object(bufs) | |
178 | C2 = d['C'] |
|
179 | C2 = d['C'] | |
179 | yield nt.assert_equal(C2.a, C.a) |
|
180 | yield nt.assert_equal(C2.a, C.a) | |
180 |
|
181 | |||
181 | @dec.parametric |
|
182 | @dec.parametric | |
182 | def test_class_oldstyle(): |
|
183 | def test_class_oldstyle(): | |
183 | @interactive |
|
184 | @interactive | |
184 | class C: |
|
185 | class C: | |
185 | a=5 |
|
186 | a=5 | |
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): | |
198 | a=5 |
|
229 | a=5 | |
199 |
|
230 | |||
200 | @interactive |
|
231 | @interactive | |
201 | class D(C): |
|
232 | class D(C): | |
202 | b=10 |
|
233 | b=10 | |
203 |
|
234 | |||
204 | bufs = serialize_object(dict(D=D)) |
|
235 | bufs = serialize_object(dict(D=D)) | |
205 | canned = pickle.loads(bufs[0]) |
|
236 | canned = pickle.loads(bufs[0]) | |
206 | yield nt.assert_true(canned['D'], CannedClass) |
|
237 | yield nt.assert_true(canned['D'], CannedClass) | |
207 | d, r = unserialize_object(bufs) |
|
238 | d, r = unserialize_object(bufs) | |
208 | D2 = d['D'] |
|
239 | D2 = d['D'] | |
209 | yield nt.assert_equal(D2.a, D.a) |
|
240 | yield nt.assert_equal(D2.a, D.a) | |
210 | yield nt.assert_equal(D2.b, D.b) |
|
241 | yield nt.assert_equal(D2.b, D.b) |
General Comments 0
You need to be logged in to leave comments.
Login now