raw.py 736 B

12345678910111213141516171819202122232425262728293031323334
  1. """
  2. A sedes that does nothing. Thus, everything that can be directly encoded by RLP
  3. is serializable. This sedes can be used as a placeholder when deserializing
  4. larger structures.
  5. """
  6. from collections.abc import (
  7. Sequence,
  8. )
  9. from rlp.atomic import (
  10. Atomic,
  11. )
  12. from rlp.exceptions import (
  13. SerializationError,
  14. )
  15. def serializable(obj):
  16. if isinstance(obj, Atomic):
  17. return True
  18. elif not isinstance(obj, str) and isinstance(obj, Sequence):
  19. return all(map(serializable, obj))
  20. else:
  21. return False
  22. def serialize(obj):
  23. if not serializable(obj):
  24. raise SerializationError("Can only serialize nested lists of strings", obj)
  25. return obj
  26. def deserialize(serial):
  27. return serial