##// END OF EJS Templates
Merge pull request #6071 from minrk/serialize-nan...
Thomas Kluyver -
r17149:639fc3ac merge
parent child Browse files
Show More
@@ -1,229 +1,208 b''
1 """test serialization tools"""
1 """test serialization tools"""
2
2
3 #-------------------------------------------------------------------------------
3 # Copyright (c) IPython Development Team.
4 # Copyright (C) 2011 The IPython Development Team
4 # Distributed under the terms of the Modified BSD License.
5 #
6 # Distributed under the terms of the BSD License. The full license is in
7 # the file COPYING, distributed as part of this software.
8 #-------------------------------------------------------------------------------
9
10 #-------------------------------------------------------------------------------
11 # Imports
12 #-------------------------------------------------------------------------------
13
5
14 import pickle
6 import pickle
15 from collections import namedtuple
7 from collections import namedtuple
16
8
17 import nose.tools as nt
9 import nose.tools as nt
18
10
19 # from unittest import TestCaes
11 # from unittest import TestCaes
20 from IPython.kernel.zmq.serialize import serialize_object, unserialize_object
12 from IPython.kernel.zmq.serialize import serialize_object, unserialize_object
21 from IPython.testing import decorators as dec
13 from IPython.testing import decorators as dec
22 from IPython.utils.pickleutil import CannedArray, CannedClass
14 from IPython.utils.pickleutil import CannedArray, CannedClass
23 from IPython.utils.py3compat import iteritems
15 from IPython.utils.py3compat import iteritems
24 from IPython.parallel import interactive
16 from IPython.parallel import interactive
25
17
26 #-------------------------------------------------------------------------------
18 #-------------------------------------------------------------------------------
27 # Globals and Utilities
19 # Globals and Utilities
28 #-------------------------------------------------------------------------------
20 #-------------------------------------------------------------------------------
29
21
30 def roundtrip(obj):
22 def roundtrip(obj):
31 """roundtrip an object through serialization"""
23 """roundtrip an object through serialization"""
32 bufs = serialize_object(obj)
24 bufs = serialize_object(obj)
33 obj2, remainder = unserialize_object(bufs)
25 obj2, remainder = unserialize_object(bufs)
34 nt.assert_equals(remainder, [])
26 nt.assert_equals(remainder, [])
35 return obj2
27 return obj2
36
28
37 class C(object):
29 class C(object):
38 """dummy class for """
30 """dummy class for """
39
31
40 def __init__(self, **kwargs):
32 def __init__(self, **kwargs):
41 for key,value in iteritems(kwargs):
33 for key,value in iteritems(kwargs):
42 setattr(self, key, value)
34 setattr(self, key, value)
43
35
44 SHAPES = ((100,), (1024,10), (10,8,6,5), (), (0,))
36 SHAPES = ((100,), (1024,10), (10,8,6,5), (), (0,))
45 DTYPES = ('uint8', 'float64', 'int32', [('g', 'float32')], '|S10')
37 DTYPES = ('uint8', 'float64', 'int32', [('g', 'float32')], '|S10')
38
46 #-------------------------------------------------------------------------------
39 #-------------------------------------------------------------------------------
47 # Tests
40 # Tests
48 #-------------------------------------------------------------------------------
41 #-------------------------------------------------------------------------------
49
42
43 def new_array(shape, dtype):
44 import numpy
45 return numpy.random.random(shape).astype(dtype)
46
50 def test_roundtrip_simple():
47 def test_roundtrip_simple():
51 for obj in [
48 for obj in [
52 'hello',
49 'hello',
53 dict(a='b', b=10),
50 dict(a='b', b=10),
54 [1,2,'hi'],
51 [1,2,'hi'],
55 (b'123', 'hello'),
52 (b'123', 'hello'),
56 ]:
53 ]:
57 obj2 = roundtrip(obj)
54 obj2 = roundtrip(obj)
58 nt.assert_equal(obj, obj2)
55 nt.assert_equal(obj, obj2)
59
56
60 def test_roundtrip_nested():
57 def test_roundtrip_nested():
61 for obj in [
58 for obj in [
62 dict(a=range(5), b={1:b'hello'}),
59 dict(a=range(5), b={1:b'hello'}),
63 [range(5),[range(3),(1,[b'whoda'])]],
60 [range(5),[range(3),(1,[b'whoda'])]],
64 ]:
61 ]:
65 obj2 = roundtrip(obj)
62 obj2 = roundtrip(obj)
66 nt.assert_equal(obj, obj2)
63 nt.assert_equal(obj, obj2)
67
64
68 def test_roundtrip_buffered():
65 def test_roundtrip_buffered():
69 for obj in [
66 for obj in [
70 dict(a=b"x"*1025),
67 dict(a=b"x"*1025),
71 b"hello"*500,
68 b"hello"*500,
72 [b"hello"*501, 1,2,3]
69 [b"hello"*501, 1,2,3]
73 ]:
70 ]:
74 bufs = serialize_object(obj)
71 bufs = serialize_object(obj)
75 nt.assert_equal(len(bufs), 2)
72 nt.assert_equal(len(bufs), 2)
76 obj2, remainder = unserialize_object(bufs)
73 obj2, remainder = unserialize_object(bufs)
77 nt.assert_equal(remainder, [])
74 nt.assert_equal(remainder, [])
78 nt.assert_equal(obj, obj2)
75 nt.assert_equal(obj, obj2)
79
76
80 def _scrub_nan(A):
81 """scrub nans out of empty arrays
82
83 since nan != nan
84 """
85 import numpy
86 if A.dtype.fields and A.shape:
87 for field in A.dtype.fields.keys():
88 try:
89 A[field][numpy.isnan(A[field])] = 0
90 except (TypeError, NotImplementedError):
91 # e.g. str dtype
92 pass
93
94 @dec.skip_without('numpy')
77 @dec.skip_without('numpy')
95 def test_numpy():
78 def test_numpy():
96 import numpy
79 import numpy
97 from numpy.testing.utils import assert_array_equal
80 from numpy.testing.utils import assert_array_equal
98 for shape in SHAPES:
81 for shape in SHAPES:
99 for dtype in DTYPES:
82 for dtype in DTYPES:
100 A = numpy.empty(shape, dtype=dtype)
83 A = new_array(shape, dtype=dtype)
101 _scrub_nan(A)
102 bufs = serialize_object(A)
84 bufs = serialize_object(A)
103 B, r = unserialize_object(bufs)
85 B, r = unserialize_object(bufs)
104 nt.assert_equal(r, [])
86 nt.assert_equal(r, [])
105 nt.assert_equal(A.shape, B.shape)
87 nt.assert_equal(A.shape, B.shape)
106 nt.assert_equal(A.dtype, B.dtype)
88 nt.assert_equal(A.dtype, B.dtype)
107 assert_array_equal(A,B)
89 assert_array_equal(A,B)
108
90
109 @dec.skip_without('numpy')
91 @dec.skip_without('numpy')
110 def test_recarray():
92 def test_recarray():
111 import numpy
93 import numpy
112 from numpy.testing.utils import assert_array_equal
94 from numpy.testing.utils import assert_array_equal
113 for shape in SHAPES:
95 for shape in SHAPES:
114 for dtype in [
96 for dtype in [
115 [('f', float), ('s', '|S10')],
97 [('f', float), ('s', '|S10')],
116 [('n', int), ('s', '|S1'), ('u', 'uint32')],
98 [('n', int), ('s', '|S1'), ('u', 'uint32')],
117 ]:
99 ]:
118 A = numpy.empty(shape, dtype=dtype)
100 A = new_array(shape, dtype=dtype)
119 _scrub_nan(A)
120
101
121 bufs = serialize_object(A)
102 bufs = serialize_object(A)
122 B, r = unserialize_object(bufs)
103 B, r = unserialize_object(bufs)
123 nt.assert_equal(r, [])
104 nt.assert_equal(r, [])
124 nt.assert_equal(A.shape, B.shape)
105 nt.assert_equal(A.shape, B.shape)
125 nt.assert_equal(A.dtype, B.dtype)
106 nt.assert_equal(A.dtype, B.dtype)
126 assert_array_equal(A,B)
107 assert_array_equal(A,B)
127
108
128 @dec.skip_without('numpy')
109 @dec.skip_without('numpy')
129 def test_numpy_in_seq():
110 def test_numpy_in_seq():
130 import numpy
111 import numpy
131 from numpy.testing.utils import assert_array_equal
112 from numpy.testing.utils import assert_array_equal
132 for shape in SHAPES:
113 for shape in SHAPES:
133 for dtype in DTYPES:
114 for dtype in DTYPES:
134 A = numpy.empty(shape, dtype=dtype)
115 A = new_array(shape, dtype=dtype)
135 _scrub_nan(A)
136 bufs = serialize_object((A,1,2,b'hello'))
116 bufs = serialize_object((A,1,2,b'hello'))
137 canned = pickle.loads(bufs[0])
117 canned = pickle.loads(bufs[0])
138 nt.assert_is_instance(canned[0], CannedArray)
118 nt.assert_is_instance(canned[0], CannedArray)
139 tup, r = unserialize_object(bufs)
119 tup, r = unserialize_object(bufs)
140 B = tup[0]
120 B = tup[0]
141 nt.assert_equal(r, [])
121 nt.assert_equal(r, [])
142 nt.assert_equal(A.shape, B.shape)
122 nt.assert_equal(A.shape, B.shape)
143 nt.assert_equal(A.dtype, B.dtype)
123 nt.assert_equal(A.dtype, B.dtype)
144 assert_array_equal(A,B)
124 assert_array_equal(A,B)
145
125
146 @dec.skip_without('numpy')
126 @dec.skip_without('numpy')
147 def test_numpy_in_dict():
127 def test_numpy_in_dict():
148 import numpy
128 import numpy
149 from numpy.testing.utils import assert_array_equal
129 from numpy.testing.utils import assert_array_equal
150 for shape in SHAPES:
130 for shape in SHAPES:
151 for dtype in DTYPES:
131 for dtype in DTYPES:
152 A = numpy.empty(shape, dtype=dtype)
132 A = new_array(shape, dtype=dtype)
153 _scrub_nan(A)
154 bufs = serialize_object(dict(a=A,b=1,c=range(20)))
133 bufs = serialize_object(dict(a=A,b=1,c=range(20)))
155 canned = pickle.loads(bufs[0])
134 canned = pickle.loads(bufs[0])
156 nt.assert_is_instance(canned['a'], CannedArray)
135 nt.assert_is_instance(canned['a'], CannedArray)
157 d, r = unserialize_object(bufs)
136 d, r = unserialize_object(bufs)
158 B = d['a']
137 B = d['a']
159 nt.assert_equal(r, [])
138 nt.assert_equal(r, [])
160 nt.assert_equal(A.shape, B.shape)
139 nt.assert_equal(A.shape, B.shape)
161 nt.assert_equal(A.dtype, B.dtype)
140 nt.assert_equal(A.dtype, B.dtype)
162 assert_array_equal(A,B)
141 assert_array_equal(A,B)
163
142
164 def test_class():
143 def test_class():
165 @interactive
144 @interactive
166 class C(object):
145 class C(object):
167 a=5
146 a=5
168 bufs = serialize_object(dict(C=C))
147 bufs = serialize_object(dict(C=C))
169 canned = pickle.loads(bufs[0])
148 canned = pickle.loads(bufs[0])
170 nt.assert_is_instance(canned['C'], CannedClass)
149 nt.assert_is_instance(canned['C'], CannedClass)
171 d, r = unserialize_object(bufs)
150 d, r = unserialize_object(bufs)
172 C2 = d['C']
151 C2 = d['C']
173 nt.assert_equal(C2.a, C.a)
152 nt.assert_equal(C2.a, C.a)
174
153
175 def test_class_oldstyle():
154 def test_class_oldstyle():
176 @interactive
155 @interactive
177 class C:
156 class C:
178 a=5
157 a=5
179
158
180 bufs = serialize_object(dict(C=C))
159 bufs = serialize_object(dict(C=C))
181 canned = pickle.loads(bufs[0])
160 canned = pickle.loads(bufs[0])
182 nt.assert_is_instance(canned['C'], CannedClass)
161 nt.assert_is_instance(canned['C'], CannedClass)
183 d, r = unserialize_object(bufs)
162 d, r = unserialize_object(bufs)
184 C2 = d['C']
163 C2 = d['C']
185 nt.assert_equal(C2.a, C.a)
164 nt.assert_equal(C2.a, C.a)
186
165
187 def test_tuple():
166 def test_tuple():
188 tup = (lambda x:x, 1)
167 tup = (lambda x:x, 1)
189 bufs = serialize_object(tup)
168 bufs = serialize_object(tup)
190 canned = pickle.loads(bufs[0])
169 canned = pickle.loads(bufs[0])
191 nt.assert_is_instance(canned, tuple)
170 nt.assert_is_instance(canned, tuple)
192 t2, r = unserialize_object(bufs)
171 t2, r = unserialize_object(bufs)
193 nt.assert_equal(t2[0](t2[1]), tup[0](tup[1]))
172 nt.assert_equal(t2[0](t2[1]), tup[0](tup[1]))
194
173
195 point = namedtuple('point', 'x y')
174 point = namedtuple('point', 'x y')
196
175
197 def test_namedtuple():
176 def test_namedtuple():
198 p = point(1,2)
177 p = point(1,2)
199 bufs = serialize_object(p)
178 bufs = serialize_object(p)
200 canned = pickle.loads(bufs[0])
179 canned = pickle.loads(bufs[0])
201 nt.assert_is_instance(canned, point)
180 nt.assert_is_instance(canned, point)
202 p2, r = unserialize_object(bufs, globals())
181 p2, r = unserialize_object(bufs, globals())
203 nt.assert_equal(p2.x, p.x)
182 nt.assert_equal(p2.x, p.x)
204 nt.assert_equal(p2.y, p.y)
183 nt.assert_equal(p2.y, p.y)
205
184
206 def test_list():
185 def test_list():
207 lis = [lambda x:x, 1]
186 lis = [lambda x:x, 1]
208 bufs = serialize_object(lis)
187 bufs = serialize_object(lis)
209 canned = pickle.loads(bufs[0])
188 canned = pickle.loads(bufs[0])
210 nt.assert_is_instance(canned, list)
189 nt.assert_is_instance(canned, list)
211 l2, r = unserialize_object(bufs)
190 l2, r = unserialize_object(bufs)
212 nt.assert_equal(l2[0](l2[1]), lis[0](lis[1]))
191 nt.assert_equal(l2[0](l2[1]), lis[0](lis[1]))
213
192
214 def test_class_inheritance():
193 def test_class_inheritance():
215 @interactive
194 @interactive
216 class C(object):
195 class C(object):
217 a=5
196 a=5
218
197
219 @interactive
198 @interactive
220 class D(C):
199 class D(C):
221 b=10
200 b=10
222
201
223 bufs = serialize_object(dict(D=D))
202 bufs = serialize_object(dict(D=D))
224 canned = pickle.loads(bufs[0])
203 canned = pickle.loads(bufs[0])
225 nt.assert_is_instance(canned['D'], CannedClass)
204 nt.assert_is_instance(canned['D'], CannedClass)
226 d, r = unserialize_object(bufs)
205 d, r = unserialize_object(bufs)
227 D2 = d['D']
206 D2 = d['D']
228 nt.assert_equal(D2.a, D.a)
207 nt.assert_equal(D2.a, D.a)
229 nt.assert_equal(D2.b, D.b)
208 nt.assert_equal(D2.b, D.b)
General Comments 0
You need to be logged in to leave comments. Login now