Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1430)

Side by Side Diff: Lib/importlib/__init__.py

Issue 18864: Implementation for PEP 451 (importlib.machinery.ModuleSpec)
Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """A pure Python implementation of import.""" 1 """A pure Python implementation of import."""
2 __all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload'] 2 __all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload']
3 3
4 # Bootstrap help ##################################################### 4 # Bootstrap help #####################################################
5 5
6 # Until bootstrapping is complete, DO NOT import any modules that attempt 6 # Until bootstrapping is complete, DO NOT import any modules that attempt
7 # to import importlib._bootstrap (directly or indirectly). Since this 7 # to import importlib._bootstrap (directly or indirectly). Since this
8 # partially initialised package would be present in sys.modules, those 8 # partially initialised package would be present in sys.modules, those
9 # modules would get an uninitialised copy of the source version, instead 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 10 # of a fully initialised version (either the frozen one or the one
(...skipping 28 matching lines...) Expand all
39 39
40 40
41 def invalidate_caches(): 41 def invalidate_caches():
42 """Call the invalidate_caches() method on all meta path finders stored in 42 """Call the invalidate_caches() method on all meta path finders stored in
43 sys.meta_path (where implemented).""" 43 sys.meta_path (where implemented)."""
44 for finder in sys.meta_path: 44 for finder in sys.meta_path:
45 if hasattr(finder, 'invalidate_caches'): 45 if hasattr(finder, 'invalidate_caches'):
46 finder.invalidate_caches() 46 finder.invalidate_caches()
47 47
48 48
49 def find_spec(name, path=None):
50 """Docstring here..."""
Jim.J.Jewett 2013/10/30 03:32:01 Not yet, it isn't... Perhaps """Return the module
51 if name not in sys.modules:
52 return _bootstrap._find_spec(name, path)
53 else:
54 module = sys.modules[name]
55 if module is None:
56 return None
57 try:
58 spec = module.__spec__
59 except AttributeError:
60 raise ValueError('{}.__spec__ is not set'.format(name))
61 else:
62 if spec is None:
63 raise ValueError('{}.__spec__ is None'.format(name))
64 return spec
65
66
67 # XXX Deprecate...
49 def find_loader(name, path=None): 68 def find_loader(name, path=None):
50 """Find the loader for the specified module. 69 """Find the loader for the specified module.
51 70
52 First, sys.modules is checked to see if the module was already imported. If 71 First, sys.modules is checked to see if the module was already imported. If
53 so, then sys.modules[name].__loader__ is returned. If that happens to be 72 so, then sys.modules[name].__loader__ is returned. If that happens to be
Jim.J.Jewett 2013/10/30 03:32:01 Below, it doesn't look like sys.modules[name].__lo
eric.snow 2013/11/01 01:07:38 Yeah, I need to update that docstring. I also nee
54 set to None, then ValueError is raised. If the module is not in 73 set to None, then ValueError is raised. If the module is not in
55 sys.modules, then sys.meta_path is searched for a suitable loader with the 74 sys.modules, then sys.meta_path is searched for a suitable loader with the
56 value of 'path' given to the finders. None is returned if no loader could 75 value of 'path' given to the finders. None is returned if no loader could
57 be found. 76 be found.
58 77
59 Dotted names do not have their parent packages implicitly imported. You will 78 Dotted names do not have their parent packages implicitly imported. You will
60 most likely need to explicitly import all parent packages in the proper 79 most likely need to explicitly import all parent packages in the proper
61 order for a submodule to get the correct loader. 80 order for a submodule to get the correct loader.
62 81
63 """ 82 """
83 spec = find_spec(name, path)
84 if spec is None:
85 return None
64 try: 86 try:
65 loader = sys.modules[name].__loader__ 87 loader = spec.loader
88 except AttributeError:
89 raise ValueError('{}.__spec__.loader is not set'.format(name))
90 else:
66 if loader is None: 91 if loader is None:
67 raise ValueError('{}.__loader__ is None'.format(name)) 92 raise ValueError('{}.__spec__.loader is not set'.format(name))
68 else: 93 return loader
69 return loader
70 except KeyError:
71 pass
72 except AttributeError:
73 raise ValueError('{}.__loader__ is not set'.format(name))
74 return _bootstrap._find_module(name, path)
75 94
76 95
77 def import_module(name, package=None): 96 def import_module(name, package=None):
78 """Import a module. 97 """Import a module.
79 98
80 The 'package' argument is required when performing a relative import. It 99 The 'package' argument is required when performing a relative import. It
81 specifies the package to use as the anchor point from which to resolve the 100 specifies the package to use as the anchor point from which to resolve the
82 relative import to an absolute import. 101 relative import to an absolute import.
83 102
84 """ 103 """
(...skipping 12 matching lines...) Expand all
97 116
98 _RELOADING = {} 117 _RELOADING = {}
99 118
100 119
101 def reload(module): 120 def reload(module):
102 """Reload the module and return it. 121 """Reload the module and return it.
103 122
104 The module must have been successfully imported before. 123 The module must have been successfully imported before.
105 124
106 """ 125 """
126 # XXX Is this check wrong?
Jim.J.Jewett 2013/10/30 03:32:01 I would say that it is wrong; it requires alternat
eric.snow 2013/11/01 01:07:38 I agree. This is actually more or less how it sta
107 if not module or not isinstance(module, types.ModuleType): 127 if not module or not isinstance(module, types.ModuleType):
108 raise TypeError("reload() argument must be module") 128 raise TypeError("reload() argument must be module")
109 name = module.__name__ 129 try:
110 if name not in sys.modules: 130 name = module.__spec__.name
131 except AttributeError:
132 name = module.__name__
133 spec = module.__spec__ = find_spec(name)
Jim.J.Jewett 2013/10/30 03:32:01 (Combined with earlier in this patch ...) I think
eric.snow 2013/11/01 01:07:38 This bit relates to fixing reload to work like it
134
135 if sys.modules.get(name) is not module:
111 msg = "module {} not in sys.modules" 136 msg = "module {} not in sys.modules"
112 raise ImportError(msg.format(name), name=name) 137 raise ImportError(msg.format(name), name=name)
113 if name in _RELOADING: 138 if name in _RELOADING:
114 return _RELOADING[name] 139 return _RELOADING[name]
115 _RELOADING[name] = module 140 _RELOADING[name] = module
116 try: 141 try:
117 parent_name = name.rpartition('.')[0] 142 if spec.parent and spec.parent not in sys.modules:
118 if parent_name and parent_name not in sys.modules:
119 msg = "parent {!r} not in sys.modules" 143 msg = "parent {!r} not in sys.modules"
120 raise ImportError(msg.format(parent_name), name=parent_name) 144 raise ImportError(msg.format(spec.parent), name=spec.parent)
121 module.__loader__.load_module(name) 145 methods = _bootstrap._SpecMethods(spec)
146 methods.exec(module)
122 # The module may have replaced itself in sys.modules! 147 # The module may have replaced itself in sys.modules!
123 return sys.modules[module.__name__] 148 return sys.modules[name]
124 finally: 149 finally:
125 try: 150 try:
126 del _RELOADING[name] 151 del _RELOADING[name]
127 except KeyError: 152 except KeyError:
128 pass 153 pass
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+