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