Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_imp.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. Re-implementation of find_module and get_frozen_object
  3. from the deprecated imp module.
  4. """
  5. import os
  6. import importlib.util
  7. import importlib.machinery
  8. from .py34compat import module_from_spec
  9. PY_SOURCE = 1
  10. PY_COMPILED = 2
  11. C_EXTENSION = 3
  12. C_BUILTIN = 6
  13. PY_FROZEN = 7
  14. def find_spec(module, paths):
  15. finder = (
  16. importlib.machinery.PathFinder().find_spec
  17. if isinstance(paths, list) else
  18. importlib.util.find_spec
  19. )
  20. return finder(module, paths)
  21. def find_module(module, paths=None):
  22. """Just like 'imp.find_module()', but with package support"""
  23. spec = find_spec(module, paths)
  24. if spec is None:
  25. raise ImportError("Can't find %s" % module)
  26. if not spec.has_location and hasattr(spec, 'submodule_search_locations'):
  27. spec = importlib.util.spec_from_loader('__init__.py', spec.loader)
  28. kind = -1
  29. file = None
  30. static = isinstance(spec.loader, type)
  31. if spec.origin == 'frozen' or static and issubclass(
  32. spec.loader, importlib.machinery.FrozenImporter):
  33. kind = PY_FROZEN
  34. path = None # imp compabilty
  35. suffix = mode = '' # imp compatibility
  36. elif spec.origin == 'built-in' or static and issubclass(
  37. spec.loader, importlib.machinery.BuiltinImporter):
  38. kind = C_BUILTIN
  39. path = None # imp compabilty
  40. suffix = mode = '' # imp compatibility
  41. elif spec.has_location:
  42. path = spec.origin
  43. suffix = os.path.splitext(path)[1]
  44. mode = 'r' if suffix in importlib.machinery.SOURCE_SUFFIXES else 'rb'
  45. if suffix in importlib.machinery.SOURCE_SUFFIXES:
  46. kind = PY_SOURCE
  47. elif suffix in importlib.machinery.BYTECODE_SUFFIXES:
  48. kind = PY_COMPILED
  49. elif suffix in importlib.machinery.EXTENSION_SUFFIXES:
  50. kind = C_EXTENSION
  51. if kind in {PY_SOURCE, PY_COMPILED}:
  52. file = open(path, mode)
  53. else:
  54. path = None
  55. suffix = mode = ''
  56. return file, path, (suffix, mode, kind)
  57. def get_frozen_object(module, paths=None):
  58. spec = find_spec(module, paths)
  59. if not spec:
  60. raise ImportError("Can't find %s" % module)
  61. return spec.loader.get_code(module)
  62. def get_module(module, paths, info):
  63. spec = find_spec(module, paths)
  64. if not spec:
  65. raise ImportError("Can't find %s" % module)
  66. return module_from_spec(spec)