struct_micropython.py 793 B

1234567891011121314151617181920212223242526272829303132333435
  1. # test MicroPython-specific features of struct
  2. try:
  3. import ustruct as struct
  4. except:
  5. try:
  6. import struct
  7. except ImportError:
  8. print("SKIP")
  9. raise SystemExit
  10. class A():
  11. pass
  12. # pack and unpack objects
  13. o = A()
  14. s = struct.pack("<O", o)
  15. o2 = struct.unpack("<O", s)
  16. print(o is o2[0])
  17. # pack can accept less arguments than required for the format spec
  18. print(struct.pack('<2I', 1))
  19. # pack and unpack pointer to a string
  20. # This requires uctypes to get the address of the string and instead of
  21. # putting this in a dedicated test that can be skipped we simply pass
  22. # if the import fails.
  23. try:
  24. import uctypes
  25. o = uctypes.addressof('abc')
  26. s = struct.pack("<S", o)
  27. o2 = struct.unpack("<S", s)
  28. assert o2[0] == 'abc'
  29. except ImportError:
  30. pass