| 
									
										
										
										
											2012-02-27 18:15:42 -05:00
										 |  |  | """A pure Python implementation of import.""" | 
					
						
							| 
									
										
										
										
											2013-06-14 15:04:26 -04:00
										 |  |  | __all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload'] | 
					
						
							| 
									
										
										
										
											2009-03-12 22:01:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-17 22:33:38 +02:00
										 |  |  | # Bootstrap help ##################################################### | 
					
						
							| 
									
										
										
										
											2012-07-20 23:40:09 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Until bootstrapping is complete, DO NOT import any modules that attempt | 
					
						
							|  |  |  | # to import importlib._bootstrap (directly or indirectly). Since this | 
					
						
							|  |  |  | # partially initialised package would be present in sys.modules, those | 
					
						
							|  |  |  | # modules would get an uninitialised copy of the source version, instead | 
					
						
							|  |  |  | # of a fully initialised version (either the frozen one or the one | 
					
						
							|  |  |  | # initialised below if the frozen one is not available). | 
					
						
							|  |  |  | import _imp  # Just the builtin component, NOT the full Python module | 
					
						
							| 
									
										
										
										
											2012-06-17 22:33:38 +02:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-17 22:33:38 +02:00
										 |  |  | try: | 
					
						
							| 
									
										
										
										
											2012-07-04 14:03:40 -04:00
										 |  |  |     import _frozen_importlib as _bootstrap | 
					
						
							| 
									
										
										
										
											2013-07-04 17:43:24 -04:00
										 |  |  | except ImportError: | 
					
						
							| 
									
										
										
										
											2012-06-17 22:33:38 +02:00
										 |  |  |     from . import _bootstrap | 
					
						
							| 
									
										
										
										
											2012-07-20 23:40:09 +10:00
										 |  |  |     _bootstrap._setup(sys, _imp) | 
					
						
							| 
									
										
										
										
											2012-06-17 22:33:38 +02:00
										 |  |  | else: | 
					
						
							|  |  |  |     # importlib._bootstrap is the built-in import, ensure we don't create | 
					
						
							|  |  |  |     # a second copy of the module. | 
					
						
							|  |  |  |     _bootstrap.__name__ = 'importlib._bootstrap' | 
					
						
							|  |  |  |     _bootstrap.__package__ = 'importlib' | 
					
						
							| 
									
										
										
										
											2014-03-21 10:58:33 -04:00
										 |  |  |     try: | 
					
						
							|  |  |  |         _bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py') | 
					
						
							|  |  |  |     except NameError: | 
					
						
							|  |  |  |         # __file__ is not guaranteed to be defined, e.g. if this code gets | 
					
						
							|  |  |  |         # frozen by a tool like cx_Freeze. | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2012-06-17 22:33:38 +02:00
										 |  |  |     sys.modules['importlib._bootstrap'] = _bootstrap | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-02 19:15:18 -06:00
										 |  |  | try: | 
					
						
							|  |  |  |     import _frozen_importlib_external as _bootstrap_external | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     from . import _bootstrap_external | 
					
						
							|  |  |  |     _bootstrap_external._setup(_bootstrap) | 
					
						
							| 
									
										
										
										
											2015-05-15 21:54:59 -06:00
										 |  |  |     _bootstrap._bootstrap_external = _bootstrap_external | 
					
						
							| 
									
										
										
										
											2015-05-02 19:15:18 -06:00
										 |  |  | else: | 
					
						
							|  |  |  |     _bootstrap_external.__name__ = 'importlib._bootstrap_external' | 
					
						
							|  |  |  |     _bootstrap_external.__package__ = 'importlib' | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         _bootstrap_external.__file__ = __file__.replace('__init__.py', '_bootstrap_external.py') | 
					
						
							|  |  |  |     except NameError: | 
					
						
							|  |  |  |         # __file__ is not guaranteed to be defined, e.g. if this code gets | 
					
						
							|  |  |  |         # frozen by a tool like cx_Freeze. | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  |     sys.modules['importlib._bootstrap_external'] = _bootstrap_external | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-25 18:58:03 -05:00
										 |  |  | # To simplify imports in test code | 
					
						
							| 
									
										
										
										
											2015-05-02 19:15:18 -06:00
										 |  |  | _w_long = _bootstrap_external._w_long | 
					
						
							|  |  |  | _r_long = _bootstrap_external._r_long | 
					
						
							| 
									
										
										
										
											2012-01-25 18:58:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-20 23:40:09 +10:00
										 |  |  | # Fully bootstrapped at this point, import whatever you like, circular | 
					
						
							|  |  |  | # dependencies and startup overhead minimisation permitting :) | 
					
						
							| 
									
										
										
										
											2012-01-25 18:58:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:46 -07:00
										 |  |  | import types | 
					
						
							|  |  |  | import warnings | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 22:44:04 +00:00
										 |  |  | # Public API ######################################################### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 18:15:42 -05:00
										 |  |  | from ._bootstrap import __import__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def invalidate_caches(): | 
					
						
							| 
									
										
										
										
											2012-08-10 12:21:12 -04:00
										 |  |  |     """Call the invalidate_caches() method on all meta path finders stored in
 | 
					
						
							|  |  |  |     sys.meta_path (where implemented)."""
 | 
					
						
							|  |  |  |     for finder in sys.meta_path: | 
					
						
							| 
									
										
										
										
											2012-02-27 18:15:42 -05:00
										 |  |  |         if hasattr(finder, 'invalidate_caches'): | 
					
						
							|  |  |  |             finder.invalidate_caches() | 
					
						
							| 
									
										
										
										
											2009-01-20 02:21:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  | def find_loader(name, path=None): | 
					
						
							|  |  |  |     """Return the loader for the specified module.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     This is a backward-compatible wrapper around find_spec(). | 
					
						
							| 
									
										
										
										
											2012-05-12 17:43:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:46 -07:00
										 |  |  |     This function is deprecated in favor of importlib.util.find_spec(). | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-12 17:43:17 -04:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:46 -07:00
										 |  |  |     warnings.warn('Use importlib.util.find_spec() instead.', | 
					
						
							|  |  |  |                   DeprecationWarning, stacklevel=2) | 
					
						
							| 
									
										
										
										
											2012-05-12 17:43:17 -04:00
										 |  |  |     try: | 
					
						
							|  |  |  |         loader = sys.modules[name].__loader__ | 
					
						
							|  |  |  |         if loader is None: | 
					
						
							|  |  |  |             raise ValueError('{}.__loader__ is None'.format(name)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return loader | 
					
						
							|  |  |  |     except KeyError: | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2013-03-13 11:09:08 -07:00
										 |  |  |     except AttributeError: | 
					
						
							| 
									
										
										
										
											2014-11-21 20:33:57 +02:00
										 |  |  |         raise ValueError('{}.__loader__ is not set'.format(name)) from None | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     spec = _bootstrap._find_spec(name, path) | 
					
						
							|  |  |  |     # We won't worry about malformed specs (missing attributes). | 
					
						
							|  |  |  |     if spec is None: | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  |     if spec.loader is None: | 
					
						
							|  |  |  |         if spec.submodule_search_locations is None: | 
					
						
							|  |  |  |             raise ImportError('spec for {} missing loader'.format(name), | 
					
						
							|  |  |  |                               name=name) | 
					
						
							|  |  |  |         raise ImportError('namespace packages do not have loaders', | 
					
						
							|  |  |  |                           name=name) | 
					
						
							|  |  |  |     return spec.loader | 
					
						
							| 
									
										
										
										
											2012-05-12 17:43:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 02:21:27 +00:00
										 |  |  | def import_module(name, package=None): | 
					
						
							|  |  |  |     """Import a module.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The 'package' argument is required when performing a relative import. It | 
					
						
							|  |  |  |     specifies the package to use as the anchor point from which to resolve the | 
					
						
							|  |  |  |     relative import to an absolute import. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2009-02-07 01:15:27 +00:00
										 |  |  |     level = 0 | 
					
						
							| 
									
										
										
										
											2009-01-20 02:21:27 +00:00
										 |  |  |     if name.startswith('.'): | 
					
						
							|  |  |  |         if not package: | 
					
						
							| 
									
										
										
										
											2013-08-12 13:29:11 -04:00
										 |  |  |             msg = ("the 'package' argument is required to perform a relative " | 
					
						
							|  |  |  |                    "import for {!r}") | 
					
						
							|  |  |  |             raise TypeError(msg.format(name)) | 
					
						
							| 
									
										
										
										
											2009-01-20 02:21:27 +00:00
										 |  |  |         for character in name: | 
					
						
							|  |  |  |             if character != '.': | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             level += 1 | 
					
						
							| 
									
										
										
										
											2009-02-07 01:15:27 +00:00
										 |  |  |     return _bootstrap._gcd_import(name[level:], package, level) | 
					
						
							| 
									
										
										
										
											2013-06-14 15:04:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _RELOADING = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def reload(module): | 
					
						
							|  |  |  |     """Reload the module and return it.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The module must have been successfully imported before. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if not module or not isinstance(module, types.ModuleType): | 
					
						
							|  |  |  |         raise TypeError("reload() argument must be module") | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     try: | 
					
						
							|  |  |  |         name = module.__spec__.name | 
					
						
							|  |  |  |     except AttributeError: | 
					
						
							|  |  |  |         name = module.__name__ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-31 22:22:15 -06:00
										 |  |  |     if sys.modules.get(name) is not module: | 
					
						
							| 
									
										
										
										
											2013-06-14 15:04:26 -04:00
										 |  |  |         msg = "module {} not in sys.modules" | 
					
						
							|  |  |  |         raise ImportError(msg.format(name), name=name) | 
					
						
							|  |  |  |     if name in _RELOADING: | 
					
						
							|  |  |  |         return _RELOADING[name] | 
					
						
							|  |  |  |     _RELOADING[name] = module | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         parent_name = name.rpartition('.')[0] | 
					
						
							| 
									
										
										
										
											2013-12-09 19:59:10 -07:00
										 |  |  |         if parent_name: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 parent = sys.modules[parent_name] | 
					
						
							|  |  |  |             except KeyError: | 
					
						
							|  |  |  |                 msg = "parent {!r} not in sys.modules" | 
					
						
							| 
									
										
										
										
											2014-11-21 20:33:57 +02:00
										 |  |  |                 raise ImportError(msg.format(parent_name), | 
					
						
							|  |  |  |                                   name=parent_name) from None | 
					
						
							| 
									
										
										
										
											2013-12-09 19:59:10 -07:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 pkgpath = parent.__path__ | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             pkgpath = None | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:46 -07:00
										 |  |  |         target = module | 
					
						
							|  |  |  |         spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target) | 
					
						
							| 
									
										
										
										
											2014-05-30 14:55:29 -04:00
										 |  |  |         _bootstrap._exec(spec, module) | 
					
						
							| 
									
										
										
										
											2013-08-14 18:11:09 -06:00
										 |  |  |         # The module may have replaced itself in sys.modules! | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |         return sys.modules[name] | 
					
						
							| 
									
										
										
										
											2013-06-14 15:04:26 -04:00
										 |  |  |     finally: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             del _RELOADING[name] | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             pass |