compat.py 679 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. """
  3. hpack/compat
  4. ~~~~~~~~~~~~
  5. Normalizes the Python 2/3 API for internal use.
  6. """
  7. import sys
  8. _ver = sys.version_info
  9. is_py2 = _ver[0] == 2
  10. is_py3 = _ver[0] == 3
  11. if is_py2:
  12. def to_byte(char):
  13. return ord(char)
  14. def decode_hex(b):
  15. return b.decode('hex')
  16. def to_bytes(b):
  17. if isinstance(b, memoryview):
  18. return b.tobytes()
  19. else:
  20. return bytes(b)
  21. unicode = unicode # noqa
  22. bytes = str
  23. elif is_py3:
  24. def to_byte(char):
  25. return char
  26. def decode_hex(b):
  27. return bytes.fromhex(b)
  28. def to_bytes(b):
  29. return bytes(b)
  30. unicode = str
  31. bytes = bytes