compat.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2018 the sqlparse authors and contributors
  4. # <see AUTHORS file>
  5. #
  6. # This module is part of python-sqlparse and is released under
  7. # the BSD License: https://opensource.org/licenses/BSD-3-Clause
  8. """Python 2/3 compatibility.
  9. This module only exists to avoid a dependency on six
  10. for very trivial stuff. We only need to take care of
  11. string types, buffers and metaclasses.
  12. Parts of the code is copied directly from six:
  13. https://bitbucket.org/gutworth/six
  14. """
  15. import sys
  16. from io import TextIOBase
  17. PY2 = sys.version_info[0] == 2
  18. PY3 = sys.version_info[0] == 3
  19. if PY3:
  20. def unicode_compatible(cls):
  21. return cls
  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. text_type = unicode
  32. string_types = (str, unicode,)
  33. from StringIO import StringIO
  34. file_types = (file, StringIO, TextIOBase)