struct2.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # test ustruct with a count specified before the type
  2. try:
  3. import ustruct as struct
  4. except:
  5. try:
  6. import struct
  7. except ImportError:
  8. print("SKIP")
  9. raise SystemExit
  10. print(struct.calcsize('0s'))
  11. print(struct.unpack('0s', b''))
  12. print(struct.pack('0s', b'123'))
  13. print(struct.calcsize('2s'))
  14. print(struct.unpack('2s', b'12'))
  15. print(struct.pack('2s', b'123'))
  16. print(struct.calcsize('2H'))
  17. print(struct.unpack('<2H', b'1234'))
  18. print(struct.pack('<2H', 258, 515))
  19. print(struct.calcsize('0s1s0H2H'))
  20. print(struct.unpack('<0s1s0H2H', b'01234'))
  21. print(struct.pack('<0s1s0H2H', b'abc', b'abc', 258, 515))
  22. # check that we get an error if the buffer is too small
  23. try:
  24. struct.unpack('2H', b'\x00\x00')
  25. except:
  26. print('Exception')
  27. try:
  28. struct.pack_into('2I', bytearray(4), 0, 0)
  29. except:
  30. print('Exception')
  31. # check that unknown types raise an exception
  32. try:
  33. struct.unpack('z', b'1')
  34. except:
  35. print('Exception')
  36. try:
  37. struct.pack('z', (b'1',))
  38. except:
  39. print('Exception')
  40. try:
  41. struct.calcsize('0z')
  42. except:
  43. print('Exception')
  44. # check that a count without a type specifier raises an exception
  45. try:
  46. struct.calcsize('1')
  47. except:
  48. print('Exception')
  49. try:
  50. struct.pack('1')
  51. except:
  52. print('Exception')
  53. try:
  54. struct.pack_into('1', bytearray(4), 0, 'xx')
  55. except:
  56. print('Exception')
  57. try:
  58. struct.unpack('1', 'xx')
  59. except:
  60. print('Exception')
  61. try:
  62. struct.unpack_from('1', 'xx')
  63. except:
  64. print('Exception')