compat.py 492 B

123456789101112131415161718192021222324252627282930313233
  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. unicode = unicode
  17. bytes = str
  18. elif is_py3:
  19. def to_byte(char):
  20. return char
  21. def decode_hex(b):
  22. return bytes.fromhex(b)
  23. unicode = str
  24. bytes = bytes