test_serialize.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. """test serialization tools"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import pickle
  5. from collections import namedtuple
  6. from ipykernel.serialize import serialize_object, deserialize_object
  7. from IPython.testing import decorators as dec
  8. from ipykernel.pickleutil import CannedArray, CannedClass, interactive
  9. def roundtrip(obj):
  10. """roundtrip an object through serialization"""
  11. bufs = serialize_object(obj)
  12. obj2, remainder = deserialize_object(bufs)
  13. assert remainder == []
  14. return obj2
  15. SHAPES = ((100,), (1024,10), (10,8,6,5), (), (0,))
  16. DTYPES = ('uint8', 'float64', 'int32', [('g', 'float32')], '|S10')
  17. def new_array(shape, dtype):
  18. import numpy
  19. return numpy.random.random(shape).astype(dtype)
  20. def test_roundtrip_simple():
  21. for obj in [
  22. 'hello',
  23. dict(a='b', b=10),
  24. [1,2,'hi'],
  25. (b'123', 'hello'),
  26. ]:
  27. obj2 = roundtrip(obj)
  28. assert obj == obj2
  29. def test_roundtrip_nested():
  30. for obj in [
  31. dict(a=range(5), b={1:b'hello'}),
  32. [range(5),[range(3),(1,[b'whoda'])]],
  33. ]:
  34. obj2 = roundtrip(obj)
  35. assert obj == obj2
  36. def test_roundtrip_buffered():
  37. for obj in [
  38. dict(a=b"x"*1025),
  39. b"hello"*500,
  40. [b"hello"*501, 1,2,3]
  41. ]:
  42. bufs = serialize_object(obj)
  43. assert len(bufs) == 2
  44. obj2, remainder = deserialize_object(bufs)
  45. assert remainder == []
  46. assert obj == obj2
  47. def test_roundtrip_memoryview():
  48. b = b'asdf' * 1025
  49. view = memoryview(b)
  50. bufs = serialize_object(view)
  51. assert len(bufs) == 2
  52. v2, remainder = deserialize_object(bufs)
  53. assert remainder == []
  54. assert v2.tobytes() == b
  55. @dec.skip_without('numpy')
  56. def test_numpy():
  57. import numpy
  58. from numpy.testing.utils import assert_array_equal
  59. for shape in SHAPES:
  60. for dtype in DTYPES:
  61. A = new_array(shape, dtype=dtype)
  62. bufs = serialize_object(A)
  63. bufs = [memoryview(b) for b in bufs]
  64. B, r = deserialize_object(bufs)
  65. assert r == []
  66. assert A.shape == B.shape
  67. assert A.dtype == B.dtype
  68. assert_array_equal(A,B)
  69. @dec.skip_without('numpy')
  70. def test_recarray():
  71. import numpy
  72. from numpy.testing.utils import assert_array_equal
  73. for shape in SHAPES:
  74. for dtype in [
  75. [('f', float), ('s', '|S10')],
  76. [('n', int), ('s', '|S1'), ('u', 'uint32')],
  77. ]:
  78. A = new_array(shape, dtype=dtype)
  79. bufs = serialize_object(A)
  80. B, r = deserialize_object(bufs)
  81. assert r == []
  82. assert A.shape == B.shape
  83. assert A.dtype == B.dtype
  84. assert_array_equal(A,B)
  85. @dec.skip_without('numpy')
  86. def test_numpy_in_seq():
  87. import numpy
  88. from numpy.testing.utils import assert_array_equal
  89. for shape in SHAPES:
  90. for dtype in DTYPES:
  91. A = new_array(shape, dtype=dtype)
  92. bufs = serialize_object((A,1,2,b'hello'))
  93. canned = pickle.loads(bufs[0])
  94. assert isinstance(canned[0], CannedArray)
  95. tup, r = deserialize_object(bufs)
  96. B = tup[0]
  97. assert r == []
  98. assert A.shape == B.shape
  99. assert A.dtype == B.dtype
  100. assert_array_equal(A,B)
  101. @dec.skip_without('numpy')
  102. def test_numpy_in_dict():
  103. import numpy
  104. from numpy.testing.utils import assert_array_equal
  105. for shape in SHAPES:
  106. for dtype in DTYPES:
  107. A = new_array(shape, dtype=dtype)
  108. bufs = serialize_object(dict(a=A,b=1,c=range(20)))
  109. canned = pickle.loads(bufs[0])
  110. assert isinstance(canned['a'], CannedArray)
  111. d, r = deserialize_object(bufs)
  112. B = d['a']
  113. assert r == []
  114. assert A.shape == B.shape
  115. assert A.dtype == B.dtype
  116. assert_array_equal(A,B)
  117. def test_class():
  118. @interactive
  119. class C(object):
  120. a=5
  121. bufs = serialize_object(dict(C=C))
  122. canned = pickle.loads(bufs[0])
  123. assert isinstance(canned['C'], CannedClass)
  124. d, r = deserialize_object(bufs)
  125. C2 = d['C']
  126. assert C2.a == C.a
  127. def test_class_oldstyle():
  128. @interactive
  129. class C:
  130. a=5
  131. bufs = serialize_object(dict(C=C))
  132. canned = pickle.loads(bufs[0])
  133. assert isinstance(canned['C'], CannedClass)
  134. d, r = deserialize_object(bufs)
  135. C2 = d['C']
  136. assert C2.a == C.a
  137. def test_tuple():
  138. tup = (lambda x:x, 1)
  139. bufs = serialize_object(tup)
  140. canned = pickle.loads(bufs[0])
  141. assert isinstance(canned, tuple)
  142. t2, r = deserialize_object(bufs)
  143. assert t2[0](t2[1]) == tup[0](tup[1])
  144. point = namedtuple('point', 'x y')
  145. def test_namedtuple():
  146. p = point(1,2)
  147. bufs = serialize_object(p)
  148. canned = pickle.loads(bufs[0])
  149. assert isinstance(canned, point)
  150. p2, r = deserialize_object(bufs, globals())
  151. assert p2.x == p.x
  152. assert p2.y == p.y
  153. def test_list():
  154. lis = [lambda x:x, 1]
  155. bufs = serialize_object(lis)
  156. canned = pickle.loads(bufs[0])
  157. assert isinstance(canned, list)
  158. l2, r = deserialize_object(bufs)
  159. assert l2[0](l2[1]) == lis[0](lis[1])
  160. def test_class_inheritance():
  161. @interactive
  162. class C(object):
  163. a=5
  164. @interactive
  165. class D(C):
  166. b=10
  167. bufs = serialize_object(dict(D=D))
  168. canned = pickle.loads(bufs[0])
  169. assert isinstance(canned['D'], CannedClass)
  170. d, r = deserialize_object(bufs)
  171. D2 = d['D']
  172. assert D2.a == D.a
  173. assert D2.b == D.b