_compat.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright 2014 Donald Stufft
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import, division, print_function
  15. import sys
  16. PY2 = sys.version_info[0] == 2
  17. PY3 = sys.version_info[0] == 3
  18. # flake8: noqa
  19. if PY3:
  20. string_types = str,
  21. else:
  22. string_types = basestring,
  23. def with_metaclass(meta, *bases):
  24. """
  25. Create a base class with a metaclass.
  26. """
  27. # This requires a bit of explanation: the basic idea is to make a dummy
  28. # metaclass for one level of class instantiation that replaces itself with
  29. # the actual metaclass.
  30. class metaclass(meta):
  31. def __new__(cls, name, this_bases, d):
  32. return meta(name, bases, d)
  33. return type.__new__(metaclass, 'temporary_class', (), {})