Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 1 | """A pure Python implementation of import.""" |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 2 | __all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload'] |
Brett Cannon | 9495f18 | 2009-03-12 22:01:40 +0000 | [diff] [blame] | 3 | |
Brett Cannon | 7f400e1 | 2009-01-22 22:44:04 +0000 | [diff] [blame] | 4 | # Bootstrap help ##################################################### |
Nick Coghlan | be7e49f | 2012-07-20 23:40:09 +1000 | [diff] [blame] | 5 | |
| 6 | # Until bootstrapping is complete, DO NOT import any modules that attempt |
| 7 | # to import importlib._bootstrap (directly or indirectly). Since this |
| 8 | # partially initialised package would be present in sys.modules, those |
| 9 | # modules would get an uninitialised copy of the source version, instead |
| 10 | # of a fully initialised version (either the frozen one or the one |
| 11 | # initialised below if the frozen one is not available). |
| 12 | import _imp # Just the builtin component, NOT the full Python module |
Brett Cannon | 354c26e | 2012-02-08 18:50:22 -0500 | [diff] [blame] | 13 | import sys |
Brett Cannon | 7f400e1 | 2009-01-22 22:44:04 +0000 | [diff] [blame] | 14 | |
Antoine Pitrou | 48114b9 | 2012-06-17 22:33:38 +0200 | [diff] [blame] | 15 | try: |
Brett Cannon | 53089c6 | 2012-07-04 14:03:40 -0400 | [diff] [blame] | 16 | import _frozen_importlib as _bootstrap |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 17 | except ImportError: |
Antoine Pitrou | 48114b9 | 2012-06-17 22:33:38 +0200 | [diff] [blame] | 18 | from . import _bootstrap |
Nick Coghlan | be7e49f | 2012-07-20 23:40:09 +1000 | [diff] [blame] | 19 | _bootstrap._setup(sys, _imp) |
Antoine Pitrou | 48114b9 | 2012-06-17 22:33:38 +0200 | [diff] [blame] | 20 | else: |
| 21 | # importlib._bootstrap is the built-in import, ensure we don't create |
| 22 | # a second copy of the module. |
| 23 | _bootstrap.__name__ = 'importlib._bootstrap' |
| 24 | _bootstrap.__package__ = 'importlib' |
Brett Cannon | a00c240 | 2014-03-21 10:58:33 -0400 | [diff] [blame] | 25 | try: |
| 26 | _bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py') |
| 27 | except NameError: |
| 28 | # __file__ is not guaranteed to be defined, e.g. if this code gets |
| 29 | # frozen by a tool like cx_Freeze. |
| 30 | pass |
Antoine Pitrou | 48114b9 | 2012-06-17 22:33:38 +0200 | [diff] [blame] | 31 | sys.modules['importlib._bootstrap'] = _bootstrap |
| 32 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 33 | try: |
| 34 | import _frozen_importlib_external as _bootstrap_external |
| 35 | except ImportError: |
| 36 | from . import _bootstrap_external |
| 37 | _bootstrap_external._setup(_bootstrap) |
Eric Snow | 183a941 | 2015-05-15 21:54:59 -0600 | [diff] [blame] | 38 | _bootstrap._bootstrap_external = _bootstrap_external |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 39 | else: |
| 40 | _bootstrap_external.__name__ = 'importlib._bootstrap_external' |
| 41 | _bootstrap_external.__package__ = 'importlib' |
| 42 | try: |
| 43 | _bootstrap_external.__file__ = __file__.replace('__init__.py', '_bootstrap_external.py') |
| 44 | except NameError: |
| 45 | # __file__ is not guaranteed to be defined, e.g. if this code gets |
| 46 | # frozen by a tool like cx_Freeze. |
| 47 | pass |
| 48 | sys.modules['importlib._bootstrap_external'] = _bootstrap_external |
| 49 | |
Antoine Pitrou | 48114b9 | 2012-06-17 22:33:38 +0200 | [diff] [blame] | 50 | # To simplify imports in test code |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 51 | _w_long = _bootstrap_external._w_long |
| 52 | _r_long = _bootstrap_external._r_long |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 53 | |
Nick Coghlan | be7e49f | 2012-07-20 23:40:09 +1000 | [diff] [blame] | 54 | # Fully bootstrapped at this point, import whatever you like, circular |
| 55 | # dependencies and startup overhead minimisation permitting :) |
Brett Cannon | afccd63 | 2009-01-20 02:21:27 +0000 | [diff] [blame] | 56 | |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 57 | import types |
| 58 | import warnings |
| 59 | |
| 60 | |
Brett Cannon | 7f400e1 | 2009-01-22 22:44:04 +0000 | [diff] [blame] | 61 | # Public API ######################################################### |
| 62 | |
Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 63 | from ._bootstrap import __import__ |
| 64 | |
| 65 | |
| 66 | def invalidate_caches(): |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 67 | """Call the invalidate_caches() method on all meta path finders stored in |
| 68 | sys.meta_path (where implemented).""" |
| 69 | for finder in sys.meta_path: |
Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 70 | if hasattr(finder, 'invalidate_caches'): |
| 71 | finder.invalidate_caches() |
Brett Cannon | afccd63 | 2009-01-20 02:21:27 +0000 | [diff] [blame] | 72 | |
| 73 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 74 | def find_loader(name, path=None): |
| 75 | """Return the loader for the specified module. |
| 76 | |
| 77 | This is a backward-compatible wrapper around find_spec(). |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 78 | |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 79 | This function is deprecated in favor of importlib.util.find_spec(). |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 80 | |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 81 | """ |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 82 | warnings.warn('Use importlib.util.find_spec() instead.', |
| 83 | DeprecationWarning, stacklevel=2) |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 84 | try: |
| 85 | loader = sys.modules[name].__loader__ |
| 86 | if loader is None: |
| 87 | raise ValueError('{}.__loader__ is None'.format(name)) |
| 88 | else: |
| 89 | return loader |
| 90 | except KeyError: |
| 91 | pass |
Brett Cannon | 3279923 | 2013-03-13 11:09:08 -0700 | [diff] [blame] | 92 | except AttributeError: |
Serhiy Storchaka | c446405 | 2014-11-21 20:33:57 +0200 | [diff] [blame] | 93 | raise ValueError('{}.__loader__ is not set'.format(name)) from None |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 94 | |
| 95 | spec = _bootstrap._find_spec(name, path) |
| 96 | # We won't worry about malformed specs (missing attributes). |
| 97 | if spec is None: |
| 98 | return None |
| 99 | if spec.loader is None: |
| 100 | if spec.submodule_search_locations is None: |
| 101 | raise ImportError('spec for {} missing loader'.format(name), |
| 102 | name=name) |
| 103 | raise ImportError('namespace packages do not have loaders', |
| 104 | name=name) |
| 105 | return spec.loader |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 106 | |
| 107 | |
Brett Cannon | afccd63 | 2009-01-20 02:21:27 +0000 | [diff] [blame] | 108 | def import_module(name, package=None): |
| 109 | """Import a module. |
| 110 | |
| 111 | The 'package' argument is required when performing a relative import. It |
| 112 | specifies the package to use as the anchor point from which to resolve the |
| 113 | relative import to an absolute import. |
| 114 | |
| 115 | """ |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 116 | level = 0 |
Brett Cannon | afccd63 | 2009-01-20 02:21:27 +0000 | [diff] [blame] | 117 | if name.startswith('.'): |
| 118 | if not package: |
Brett Cannon | e1f1597 | 2013-08-12 13:29:11 -0400 | [diff] [blame] | 119 | msg = ("the 'package' argument is required to perform a relative " |
| 120 | "import for {!r}") |
| 121 | raise TypeError(msg.format(name)) |
Brett Cannon | afccd63 | 2009-01-20 02:21:27 +0000 | [diff] [blame] | 122 | for character in name: |
| 123 | if character != '.': |
| 124 | break |
| 125 | level += 1 |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 126 | return _bootstrap._gcd_import(name[level:], package, level) |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 127 | |
| 128 | |
| 129 | _RELOADING = {} |
| 130 | |
| 131 | |
| 132 | def reload(module): |
| 133 | """Reload the module and return it. |
| 134 | |
| 135 | The module must have been successfully imported before. |
| 136 | |
| 137 | """ |
| 138 | if not module or not isinstance(module, types.ModuleType): |
| 139 | raise TypeError("reload() argument must be module") |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 140 | try: |
| 141 | name = module.__spec__.name |
| 142 | except AttributeError: |
| 143 | name = module.__name__ |
| 144 | |
Eric Snow | cdf6012 | 2013-10-31 22:22:15 -0600 | [diff] [blame] | 145 | if sys.modules.get(name) is not module: |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 146 | msg = "module {} not in sys.modules" |
| 147 | raise ImportError(msg.format(name), name=name) |
| 148 | if name in _RELOADING: |
| 149 | return _RELOADING[name] |
| 150 | _RELOADING[name] = module |
| 151 | try: |
| 152 | parent_name = name.rpartition('.')[0] |
Eric Snow | c1e7c74 | 2013-12-09 19:59:10 -0700 | [diff] [blame] | 153 | if parent_name: |
| 154 | try: |
| 155 | parent = sys.modules[parent_name] |
| 156 | except KeyError: |
| 157 | msg = "parent {!r} not in sys.modules" |
Serhiy Storchaka | c446405 | 2014-11-21 20:33:57 +0200 | [diff] [blame] | 158 | raise ImportError(msg.format(parent_name), |
| 159 | name=parent_name) from None |
Eric Snow | c1e7c74 | 2013-12-09 19:59:10 -0700 | [diff] [blame] | 160 | else: |
| 161 | pkgpath = parent.__path__ |
| 162 | else: |
| 163 | pkgpath = None |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 164 | target = module |
| 165 | spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target) |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 166 | _bootstrap._exec(spec, module) |
Eric Snow | 8e45540 | 2013-08-14 18:11:09 -0600 | [diff] [blame] | 167 | # The module may have replaced itself in sys.modules! |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 168 | return sys.modules[name] |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 169 | finally: |
| 170 | try: |
| 171 | del _RELOADING[name] |
| 172 | except KeyError: |
| 173 | pass |