90_pickle.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import pickle as pkl
  2. def test(data): # type: ignore
  3. print('-'*50)
  4. b = pkl.dumps(data)
  5. print(b)
  6. o = pkl.loads(b)
  7. print(o)
  8. assert data == o
  9. return o
  10. test(None) # PKL_NONE
  11. test(...) # PKL_ELLIPSIS
  12. test(1) # PKL_INT8
  13. test(277) # PKL_INT16
  14. test(-66666) # PKL_INT32
  15. test(0xffffffffffff) # PKL_INT64
  16. test(1.0) # PKL_FLOAT32
  17. test(1.12312434234) # PKL_FLOAT64
  18. test(True) # PKL_TRUE
  19. test(False) # PKL_FALSE
  20. test("hello") # PKL_STRING
  21. test(b"hello") # PKL_BYTES
  22. from linalg import vec2, vec3, vec2i, vec3i
  23. test(vec2(2/3, 1.0)) # PKL_VEC2
  24. test(vec3(2/3, 1.0, 3.0)) # PKL_VEC3
  25. test(vec2i(1, 2)) # PKL_VEC2I
  26. test(vec3i(1, 2, 3)) # PKL_VEC3I
  27. test(vec3i) # PKL_TYPE
  28. print('-'*50)
  29. from array2d import array2d
  30. a = array2d[int].fromlist([
  31. [1, 2, 3],
  32. [4, 5, 6]
  33. ])
  34. a_encoded = pkl.dumps(a)
  35. print(a_encoded)
  36. a_decoded = pkl.loads(a_encoded)
  37. assert isinstance(a_decoded, array2d)
  38. assert a_decoded.width == 3 and a_decoded.height == 2
  39. assert (a == a_decoded).all()
  40. print(a_decoded)
  41. test([1, 2, 3]) # PKL_LIST
  42. test((1, 2, 3)) # PKL_TUPLE
  43. test({1: 2, 3: 4}) # PKL_DICT
  44. # test complex data
  45. test([1, '2', True])
  46. test([1, '2', 3.0, True])
  47. test([1, '2', True, {'key': 4}])
  48. test([1, '2', 3.0, True, {'k1': 4, 'k2': [b'xxxx']}])
  49. # test memo
  50. a = [1, 2, 3, 4, 5, 6, 745]
  51. b = [a] * 10
  52. c = test(b)
  53. assert b == c
  54. assert b is not c
  55. assert c[0] is c[1] and c[1] is c[2]
  56. s1 = 'hello'
  57. s2 = 'world'
  58. a = [s1, s2] * 10
  59. b = test(a)
  60. assert b == a
  61. assert b is not a
  62. assert b[0] is b[2]
  63. assert b[1] is b[3]
  64. exit()
  65. from pickle import dumps, loads, _wrap, _unwrap
  66. def test(x):
  67. y = dumps(x)
  68. # print(y.decode())
  69. ok = x == loads(y)
  70. if not ok:
  71. _0 = _wrap(x)
  72. _1 = _unwrap(_0)
  73. print('='*50)
  74. print(_0)
  75. print('-'*50)
  76. print(_1)
  77. print('='*50)
  78. assert False
  79. test(1)
  80. test(1.0)
  81. test("hello")
  82. test(True)
  83. test(False)
  84. test(None)
  85. test([1, 2, 3])
  86. test((1, 2, 3))
  87. test({1: 2, 3: 4})
  88. class Foo:
  89. def __init__(self, x, y):
  90. self.x = x
  91. self.y = y
  92. def __eq__(self, __value: object) -> bool:
  93. if not isinstance(__value, Foo):
  94. return False
  95. return self.x == __value.x and self.y == __value.y
  96. def __repr__(self) -> str:
  97. return f"Foo({self.x}, {self.y})"
  98. test(Foo(1, 2))
  99. test(Foo([1, True], 'c'))
  100. from linalg import vec2
  101. test(vec2(1, 2))
  102. a = {1, 2, 3, 4}
  103. test(a)
  104. a = bytes([1, 2, 3, 4])
  105. test(a)
  106. a = [1, 2]
  107. d = {'k': a, 'j': a}
  108. c = loads(dumps(d))
  109. assert c['k'] is c['j']
  110. assert c == d
  111. # test circular references
  112. from collections import deque
  113. a = deque([1, 2, 3])
  114. test(a)
  115. a = [int, float, Foo]
  116. test(a)