__init__.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """A pure Python implementation of import."""
  2. __all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload']
  3. # Bootstrap help #####################################################
  4. # Until bootstrapping is complete, DO NOT import any modules that attempt
  5. # to import importlib._bootstrap (directly or indirectly). Since this
  6. # partially initialised package would be present in sys.modules, those
  7. # modules would get an uninitialised copy of the source version, instead
  8. # of a fully initialised version (either the frozen one or the one
  9. # initialised below if the frozen one is not available).
  10. import _imp # Just the builtin component, NOT the full Python module
  11. import sys
  12. try:
  13. import _frozen_importlib as _bootstrap
  14. except ImportError:
  15. from . import _bootstrap
  16. _bootstrap._setup(sys, _imp)
  17. else:
  18. # importlib._bootstrap is the built-in import, ensure we don't create
  19. # a second copy of the module.
  20. _bootstrap.__name__ = 'importlib._bootstrap'
  21. _bootstrap.__package__ = 'importlib'
  22. try:
  23. _bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py')
  24. except NameError:
  25. # __file__ is not guaranteed to be defined, e.g. if this code gets
  26. # frozen by a tool like cx_Freeze.
  27. pass
  28. sys.modules['importlib._bootstrap'] = _bootstrap
  29. try:
  30. import _frozen_importlib_external as _bootstrap_external
  31. except ImportError:
  32. from . import _bootstrap_external
  33. _bootstrap_external._set_bootstrap_module(_bootstrap)
  34. _bootstrap._bootstrap_external = _bootstrap_external
  35. else:
  36. _bootstrap_external.__name__ = 'importlib._bootstrap_external'
  37. _bootstrap_external.__package__ = 'importlib'
  38. try:
  39. _bootstrap_external.__file__ = __file__.replace('__init__.py', '_bootstrap_external.py')
  40. except NameError:
  41. # __file__ is not guaranteed to be defined, e.g. if this code gets
  42. # frozen by a tool like cx_Freeze.
  43. pass
  44. sys.modules['importlib._bootstrap_external'] = _bootstrap_external
  45. # To simplify imports in test code
  46. _pack_uint32 = _bootstrap_external._pack_uint32
  47. _unpack_uint32 = _bootstrap_external._unpack_uint32
  48. # Fully bootstrapped at this point, import whatever you like, circular
  49. # dependencies and startup overhead minimisation permitting :)
  50. import warnings
  51. # Public API #########################################################
  52. from ._bootstrap import __import__
  53. def invalidate_caches():
  54. """Call the invalidate_caches() method on all meta path finders stored in
  55. sys.meta_path (where implemented)."""
  56. for finder in sys.meta_path:
  57. if hasattr(finder, 'invalidate_caches'):
  58. finder.invalidate_caches()
  59. def import_module(name, package=None):
  60. """Import a module.
  61. The 'package' argument is required when performing a relative import. It
  62. specifies the package to use as the anchor point from which to resolve the
  63. relative import to an absolute import.
  64. """
  65. level = 0
  66. if name.startswith('.'):
  67. if not package:
  68. raise TypeError("the 'package' argument is required to perform a "
  69. f"relative import for {name!r}")
  70. for character in name:
  71. if character != '.':
  72. break
  73. level += 1
  74. return _bootstrap._gcd_import(name[level:], package, level)
  75. _RELOADING = {}
  76. def reload(module):
  77. """Reload the module and return it.
  78. The module must have been successfully imported before.
  79. """
  80. try:
  81. name = module.__spec__.name
  82. except AttributeError:
  83. try:
  84. name = module.__name__
  85. except AttributeError:
  86. raise TypeError("reload() argument must be a module")
  87. if sys.modules.get(name) is not module:
  88. raise ImportError(f"module {name} not in sys.modules", name=name)
  89. if name in _RELOADING:
  90. return _RELOADING[name]
  91. _RELOADING[name] = module
  92. try:
  93. parent_name = name.rpartition('.')[0]
  94. if parent_name:
  95. try:
  96. parent = sys.modules[parent_name]
  97. except KeyError:
  98. raise ImportError(f"parent {parent_name!r} not in sys.modules",
  99. name=parent_name) from None
  100. else:
  101. pkgpath = parent.__path__
  102. else:
  103. pkgpath = None
  104. target = module
  105. spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target)
  106. if spec is None:
  107. raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
  108. _bootstrap._exec(spec, module)
  109. # The module may have replaced itself in sys.modules!
  110. return sys.modules[name]
  111. finally:
  112. try:
  113. del _RELOADING[name]
  114. except KeyError:
  115. pass