bytes_construct.py 443 B

123456789101112131415161718192021222324
  1. # test construction of bytes from different objects
  2. # tuple, list, bytearray
  3. print(bytes((1, 2)))
  4. print(bytes([1, 2]))
  5. print(bytes(bytearray(4)))
  6. # constructor value out of range
  7. try:
  8. bytes([-1])
  9. except ValueError:
  10. print('ValueError')
  11. # constructor value out of range
  12. try:
  13. bytes([256])
  14. except ValueError:
  15. print('ValueError')
  16. # error in construction
  17. try:
  18. a = bytes([1, 2, 3], 1)
  19. except TypeError:
  20. print('TypeError')