No Description

test.py 668B

1234567891011121314151617181920212223
  1. # Define a custom class, encoder and decoder
  2. class CustomClass:
  3. def __eq__(self, other):
  4. return isinstance(other, CustomClass)
  5. def enc_CustomClass(obj):
  6. if isinstance(obj, CustomClass):
  7. return msgpack.ExtType(10, b'')
  8. raise TypeError("Unknown type: %r" % (obj,))
  9. def dec_CustomClass(code, data):
  10. if code == 10:
  11. return CustomClass()
  12. return msgpack.ExtType(code, data)
  13. # Define a structure
  14. G = EcGroup()
  15. custom_obj = CustomClass()
  16. test_data = [G, G.generator(), G.order(), custom_obj]
  17. # Encode and decode custom structure
  18. packed = encode(test_data, enc_CustomClass)
  19. x = decode(packed, dec_CustomClass)
  20. assert x == test_data