_compat.py 821 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import sys
  2. PY2 = sys.version_info[0] == 2
  3. if PY2:
  4. def iteritems(d):
  5. return d.iteritems()
  6. def itervalues(d):
  7. return d.itervalues()
  8. xrange = xrange
  9. string_types = (unicode, bytes)
  10. def to_str(x, charset='utf8', errors='strict'):
  11. if x is None or isinstance(x, str):
  12. return x
  13. if isinstance(x, unicode):
  14. return x.encode(charset, errors)
  15. return str(x)
  16. else:
  17. def iteritems(d):
  18. return iter(d.items())
  19. def itervalues(d):
  20. return iter(d.values())
  21. xrange = range
  22. string_types = (str,)
  23. def to_str(x, charset='utf8', errors='strict'):
  24. if x is None or isinstance(x, str):
  25. return x
  26. if isinstance(x, bytes):
  27. return x.decode(charset, errors)
  28. return str(x)