_compat.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2006 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """Basic components support
  15. """
  16. import sys
  17. import types
  18. if sys.version_info[0] < 3:
  19. def _normalize_name(name):
  20. if isinstance(name, basestring):
  21. return unicode(name)
  22. raise TypeError("name must be a regular or unicode string")
  23. CLASS_TYPES = (type, types.ClassType)
  24. STRING_TYPES = (basestring,)
  25. _BUILTINS = '__builtin__'
  26. PYTHON3 = False
  27. PYTHON2 = True
  28. else:
  29. def _normalize_name(name):
  30. if isinstance(name, bytes):
  31. name = str(name, 'ascii')
  32. if isinstance(name, str):
  33. return name
  34. raise TypeError("name must be a string or ASCII-only bytes")
  35. CLASS_TYPES = (type,)
  36. STRING_TYPES = (str,)
  37. _BUILTINS = 'builtins'
  38. PYTHON3 = True
  39. PYTHON2 = False
  40. def _skip_under_py3k(test_method):
  41. import unittest
  42. return unittest.skipIf(sys.version_info[0] >= 3, "Only on Python 2")(test_method)
  43. def _skip_under_py2(test_method):
  44. import unittest
  45. return unittest.skipIf(sys.version_info[0] < 3, "Only on Python 3")(test_method)