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