_compat.py 865 B

12345678910111213141516171819202122232425262728293031
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. import sys
  6. PY2 = sys.version_info[0] == 2
  7. PY3 = sys.version_info[0] == 3
  8. # flake8: noqa
  9. if PY3:
  10. string_types = (str,)
  11. else:
  12. string_types = (basestring,)
  13. def with_metaclass(meta, *bases):
  14. """
  15. Create a base class with a metaclass.
  16. """
  17. # This requires a bit of explanation: the basic idea is to make a dummy
  18. # metaclass for one level of class instantiation that replaces itself with
  19. # the actual metaclass.
  20. class metaclass(meta):
  21. def __new__(cls, name, this_bases, d):
  22. return meta(name, bases, d)
  23. return type.__new__(metaclass, "temporary_class", (), {})