compat.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
  4. #
  5. # This module is part of python-sqlparse and is released under
  6. # the BSD License: https://opensource.org/licenses/BSD-3-Clause
  7. """Python 2/3 compatibility.
  8. This module only exists to avoid a dependency on six
  9. for very trivial stuff. We only need to take care of
  10. string types, buffers and metaclasses.
  11. Parts of the code is copied directly from six:
  12. https://bitbucket.org/gutworth/six
  13. """
  14. import sys
  15. from io import TextIOBase
  16. PY2 = sys.version_info[0] == 2
  17. PY3 = sys.version_info[0] == 3
  18. if PY3:
  19. def unicode_compatible(cls):
  20. return cls
  21. bytes_type = bytes
  22. text_type = str
  23. string_types = (str,)
  24. from io import StringIO
  25. file_types = (StringIO, TextIOBase)
  26. elif PY2:
  27. def unicode_compatible(cls):
  28. cls.__unicode__ = cls.__str__
  29. cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
  30. return cls
  31. bytes_type = str
  32. text_type = unicode
  33. string_types = (str, unicode,)
  34. from StringIO import StringIO
  35. file_types = (file, StringIO, TextIOBase)
  36. from StringIO import StringIO