Show More
@@ -1,115 +1,168 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 | |
22 |
|
22 | |||
|
23 | #------------------------------------------------------------------------------- | |||
|
24 | # Globals and Utilities | |||
|
25 | #------------------------------------------------------------------------------- | |||
|
26 | ||||
23 | def roundtrip(obj): |
|
27 | def roundtrip(obj): | |
24 | """roundtrip an object through serialization""" |
|
28 | """roundtrip an object through serialization""" | |
25 | bufs = serialize_object(obj) |
|
29 | bufs = serialize_object(obj) | |
26 | obj2, remainder = unserialize_object(bufs) |
|
30 | obj2, remainder = unserialize_object(bufs) | |
27 | nt.assert_equals(remainder, []) |
|
31 | nt.assert_equals(remainder, []) | |
28 | return obj2 |
|
32 | return obj2 | |
29 |
|
33 | |||
30 | class C(object): |
|
34 | class C(object): | |
31 | """dummy class for """ |
|
35 | """dummy class for """ | |
32 |
|
36 | |||
33 | def __init__(self, **kwargs): |
|
37 | def __init__(self, **kwargs): | |
34 | for key,value in kwargs.iteritems(): |
|
38 | for key,value in kwargs.iteritems(): | |
35 | setattr(self, key, value) |
|
39 | setattr(self, key, value) | |
36 |
|
40 | |||
|
41 | SHAPES = ((100,), (1024,10), (10,8,6,5), (), (0,)) | |||
|
42 | DTYPES = ('uint8', 'float64', 'int32', [('g', 'float32')], '|S10') | |||
|
43 | #------------------------------------------------------------------------------- | |||
|
44 | # Tests | |||
|
45 | #------------------------------------------------------------------------------- | |||
|
46 | ||||
37 | @dec.parametric |
|
47 | @dec.parametric | |
38 | def test_roundtrip_simple(): |
|
48 | def test_roundtrip_simple(): | |
39 | for obj in [ |
|
49 | for obj in [ | |
40 | 'hello', |
|
50 | 'hello', | |
41 | dict(a='b', b=10), |
|
51 | dict(a='b', b=10), | |
42 | [1,2,'hi'], |
|
52 | [1,2,'hi'], | |
43 | (b'123', 'hello'), |
|
53 | (b'123', 'hello'), | |
44 | ]: |
|
54 | ]: | |
45 | obj2 = roundtrip(obj) |
|
55 | obj2 = roundtrip(obj) | |
46 | yield nt.assert_equals(obj, obj2) |
|
56 | yield nt.assert_equals(obj, obj2) | |
47 |
|
57 | |||
48 | @dec.parametric |
|
58 | @dec.parametric | |
49 | def test_roundtrip_nested(): |
|
59 | def test_roundtrip_nested(): | |
50 | for obj in [ |
|
60 | for obj in [ | |
51 | dict(a=range(5), b={1:b'hello'}), |
|
61 | dict(a=range(5), b={1:b'hello'}), | |
52 | [range(5),[range(3),(1,[b'whoda'])]], |
|
62 | [range(5),[range(3),(1,[b'whoda'])]], | |
53 | ]: |
|
63 | ]: | |
54 | obj2 = roundtrip(obj) |
|
64 | obj2 = roundtrip(obj) | |
55 | yield nt.assert_equals(obj, obj2) |
|
65 | yield nt.assert_equals(obj, obj2) | |
56 |
|
66 | |||
57 | @dec.parametric |
|
67 | @dec.parametric | |
58 | def test_roundtrip_buffered(): |
|
68 | def test_roundtrip_buffered(): | |
59 | for obj in [ |
|
69 | for obj in [ | |
60 | dict(a=b"x"*1025), |
|
70 | dict(a=b"x"*1025), | |
61 | b"hello"*500, |
|
71 | b"hello"*500, | |
62 | [b"hello"*501, 1,2,3] |
|
72 | [b"hello"*501, 1,2,3] | |
63 | ]: |
|
73 | ]: | |
64 | bufs = serialize_object(obj) |
|
74 | bufs = serialize_object(obj) | |
65 | yield nt.assert_equals(len(bufs), 2) |
|
75 | yield nt.assert_equals(len(bufs), 2) | |
66 | obj2, remainder = unserialize_object(bufs) |
|
76 | obj2, remainder = unserialize_object(bufs) | |
67 | yield nt.assert_equals(remainder, []) |
|
77 | yield nt.assert_equals(remainder, []) | |
68 | yield nt.assert_equals(obj, obj2) |
|
78 | yield nt.assert_equals(obj, obj2) | |
69 |
|
79 | |||
|
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: | |||
|
91 | # e.g. str dtype | |||
|
92 | pass | |||
|
93 | ||||
70 | @dec.parametric |
|
94 | @dec.parametric | |
71 | @dec.skip_without('numpy') |
|
95 | @dec.skip_without('numpy') | |
72 | def test_numpy(): |
|
96 | def test_numpy(): | |
73 | import numpy |
|
97 | import numpy | |
74 | from numpy.testing.utils import assert_array_equal |
|
98 | from numpy.testing.utils import assert_array_equal | |
75 | for shape in ((), (0,), (100,), (1024,10), (10,8,6,5)): |
|
99 | for shape in SHAPES: | |
76 | for dtype in ('uint8', 'float64', 'int32', [('int16', 'float32')]): |
|
100 | for dtype in DTYPES: | |
|
101 | A = numpy.empty(shape, dtype=dtype) | |||
|
102 | _scrub_nan(A) | |||
|
103 | bufs = serialize_object(A) | |||
|
104 | B, r = unserialize_object(bufs) | |||
|
105 | yield nt.assert_equals(r, []) | |||
|
106 | yield nt.assert_equals(A.shape, B.shape) | |||
|
107 | yield nt.assert_equals(A.dtype, B.dtype) | |||
|
108 | yield assert_array_equal(A,B) | |||
|
109 | ||||
|
110 | @dec.parametric | |||
|
111 | @dec.skip_without('numpy') | |||
|
112 | def test_recarray(): | |||
|
113 | import numpy | |||
|
114 | from numpy.testing.utils import assert_array_equal | |||
|
115 | for shape in SHAPES: | |||
|
116 | for dtype in [ | |||
|
117 | [('f', float), ('s', '|S10')], | |||
|
118 | [('n', int), ('s', '|S1'), ('u', 'uint32')], | |||
|
119 | ]: | |||
77 | A = numpy.empty(shape, dtype=dtype) |
|
120 | A = numpy.empty(shape, dtype=dtype) | |
|
121 | _scrub_nan(A) | |||
|
122 | ||||
78 | bufs = serialize_object(A) |
|
123 | bufs = serialize_object(A) | |
79 | B, r = unserialize_object(bufs) |
|
124 | B, r = unserialize_object(bufs) | |
80 | yield nt.assert_equals(r, []) |
|
125 | yield nt.assert_equals(r, []) | |
|
126 | yield nt.assert_equals(A.shape, B.shape) | |||
|
127 | yield nt.assert_equals(A.dtype, B.dtype) | |||
81 | yield assert_array_equal(A,B) |
|
128 | yield assert_array_equal(A,B) | |
82 |
|
129 | |||
83 | @dec.parametric |
|
130 | @dec.parametric | |
84 | @dec.skip_without('numpy') |
|
131 | @dec.skip_without('numpy') | |
85 | def test_numpy_in_seq(): |
|
132 | def test_numpy_in_seq(): | |
86 | import numpy |
|
133 | import numpy | |
87 | from numpy.testing.utils import assert_array_equal |
|
134 | from numpy.testing.utils import assert_array_equal | |
88 | for shape in ((), (0,), (100,), (1024,10), (10,8,6,5)): |
|
135 | for shape in SHAPES: | |
89 | for dtype in ('uint8', 'float64', 'int32', [('int16', 'float32')]): |
|
136 | for dtype in DTYPES: | |
90 | A = numpy.empty(shape, dtype=dtype) |
|
137 | A = numpy.empty(shape, dtype=dtype) | |
|
138 | _scrub_nan(A) | |||
91 | bufs = serialize_object((A,1,2,b'hello')) |
|
139 | bufs = serialize_object((A,1,2,b'hello')) | |
92 | canned = pickle.loads(bufs[0]) |
|
140 | canned = pickle.loads(bufs[0]) | |
93 | yield nt.assert_true(canned[0], CannedArray) |
|
141 | yield nt.assert_true(canned[0], CannedArray) | |
94 | tup, r = unserialize_object(bufs) |
|
142 | tup, r = unserialize_object(bufs) | |
95 | B = tup[0] |
|
143 | B = tup[0] | |
96 | yield nt.assert_equals(r, []) |
|
144 | yield nt.assert_equals(r, []) | |
|
145 | yield nt.assert_equals(A.shape, B.shape) | |||
|
146 | yield nt.assert_equals(A.dtype, B.dtype) | |||
97 | yield assert_array_equal(A,B) |
|
147 | yield assert_array_equal(A,B) | |
98 |
|
148 | |||
99 | @dec.parametric |
|
149 | @dec.parametric | |
100 | @dec.skip_without('numpy') |
|
150 | @dec.skip_without('numpy') | |
101 | def test_numpy_in_dict(): |
|
151 | def test_numpy_in_dict(): | |
102 | import numpy |
|
152 | import numpy | |
103 | from numpy.testing.utils import assert_array_equal |
|
153 | from numpy.testing.utils import assert_array_equal | |
104 | for shape in ((), (0,), (100,), (1024,10), (10,8,6,5)): |
|
154 | for shape in SHAPES: | |
105 | for dtype in ('uint8', 'float64', 'int32', [('int16', 'float32')]): |
|
155 | for dtype in DTYPES: | |
106 | A = numpy.empty(shape, dtype=dtype) |
|
156 | A = numpy.empty(shape, dtype=dtype) | |
|
157 | _scrub_nan(A) | |||
107 | bufs = serialize_object(dict(a=A,b=1,c=range(20))) |
|
158 | bufs = serialize_object(dict(a=A,b=1,c=range(20))) | |
108 | canned = pickle.loads(bufs[0]) |
|
159 | canned = pickle.loads(bufs[0]) | |
109 | yield nt.assert_true(canned['a'], CannedArray) |
|
160 | yield nt.assert_true(canned['a'], CannedArray) | |
110 | d, r = unserialize_object(bufs) |
|
161 | d, r = unserialize_object(bufs) | |
111 | B = d['a'] |
|
162 | B = d['a'] | |
112 | yield nt.assert_equals(r, []) |
|
163 | yield nt.assert_equals(r, []) | |
|
164 | yield nt.assert_equals(A.shape, B.shape) | |||
|
165 | yield nt.assert_equals(A.dtype, B.dtype) | |||
113 | yield assert_array_equal(A,B) |
|
166 | yield assert_array_equal(A,B) | |
114 |
|
167 | |||
115 |
|
168 |
General Comments 0
You need to be logged in to leave comments.
Login now