1
0

py27compat.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. Compatibility Support for Python 2.7 and earlier
  3. """
  4. import sys
  5. import platform
  6. from setuptools.extern import six
  7. def get_all_headers(message, key):
  8. """
  9. Given an HTTPMessage, return all headers matching a given key.
  10. """
  11. return message.get_all(key)
  12. if six.PY2:
  13. def get_all_headers(message, key): # noqa
  14. return message.getheaders(key)
  15. linux_py2_ascii = (
  16. platform.system() == 'Linux' and
  17. six.PY2
  18. )
  19. rmtree_safe = str if linux_py2_ascii else lambda x: x
  20. """Workaround for http://bugs.python.org/issue24672"""
  21. try:
  22. from ._imp import find_module, PY_COMPILED, PY_FROZEN, PY_SOURCE
  23. from ._imp import get_frozen_object, get_module
  24. except ImportError:
  25. import imp
  26. from imp import PY_COMPILED, PY_FROZEN, PY_SOURCE # noqa
  27. def find_module(module, paths=None):
  28. """Just like 'imp.find_module()', but with package support"""
  29. parts = module.split('.')
  30. while parts:
  31. part = parts.pop(0)
  32. f, path, (suffix, mode, kind) = info = imp.find_module(part, paths)
  33. if kind == imp.PKG_DIRECTORY:
  34. parts = parts or ['__init__']
  35. paths = [path]
  36. elif parts:
  37. raise ImportError("Can't find %r in %s" % (parts, module))
  38. return info
  39. def get_frozen_object(module, paths):
  40. return imp.get_frozen_object(module)
  41. def get_module(module, paths, info):
  42. imp.load_module(module, *info)
  43. return sys.modules[module]