1
0

compat.py 603 B

12345678910111213141516171819202122232425262728293031
  1. import sys
  2. __all__ = ['decode', 'find_spec']
  3. def decode(s):
  4. '''Decode Python 3 bytes as str
  5. '''
  6. try:
  7. return s.decode()
  8. except Exception:
  9. return str(s)
  10. if sys.version_info[0] == 2:
  11. from collections import namedtuple
  12. import imp
  13. ModuleSpec = namedtuple('ModuleSpec', ('name', 'origin'))
  14. def find_spec(name):
  15. return ModuleSpec(name, imp.find_module(name)[1])
  16. else:
  17. import importlib
  18. try:
  19. find_spec = importlib.util.find_spec
  20. except AttributeError:
  21. import importlib.util
  22. find_spec = importlib.util.find_spec