|
10 | 10 | import re |
11 | 11 | import runpy |
12 | 12 | import sys |
| 13 | +import tempfile |
13 | 14 | import zipfile |
14 | 15 |
|
15 | 16 |
|
@@ -97,30 +98,28 @@ def __init__(self): |
97 | 98 |
|
98 | 99 | def find_spec(self, name, path, target=None): |
99 | 100 | """Implements abc.MetaPathFinder.""" |
100 | | - loader = self.find_module(name, path) |
101 | | - if loader is None: |
102 | | - return None |
103 | | - return spec_from_loader(name, loader) |
| 101 | + if name in self.modules: |
| 102 | + return spec_from_loader(name, self) |
104 | 103 |
|
105 | | - def find_module(self, fullname, path=None): |
106 | | - """Attempt to locate module. Returns self if found, None if not.""" |
107 | | - if fullname in self.modules: |
108 | | - return self |
109 | | - |
110 | | - def load_module(self, fullname): |
111 | | - """Actually load a module that we said we'd handle in find_module.""" |
112 | | - import tempfile |
113 | | - |
114 | | - filename = self.modules[fullname] |
| 104 | + def create_module(self, spec): |
| 105 | + """Create a module object that we're going to load.""" |
| 106 | + filename = self.modules[spec.name] |
115 | 107 | prefix, ext = self.splitext(filename) |
116 | 108 | with tempfile.NamedTemporaryFile(suffix=ext, prefix=os.path.basename(prefix)) as f: |
117 | 109 | f.write(self.zf.read(filename)) |
118 | 110 | f.flush() |
119 | | - mod = machinery.ExtensionFileLoader(fullname, f.name).load_module() |
| 111 | + spec.origin = f.name |
| 112 | + loader = machinery.ExtensionFileLoader(spec.name, f.name) |
| 113 | + spec.loader = loader |
| 114 | + mod = loader.create_module(spec) |
120 | 115 | # Make it look like module came from the original location for nicer tracebacks. |
121 | 116 | mod.__file__ = filename |
122 | 117 | return mod |
123 | 118 |
|
| 119 | + def exec_module(self, mod): |
| 120 | + """Because we set spec.loader above, the ExtensionFileLoader's exec_module is called.""" |
| 121 | + raise NotImplementedError("SoImport.exec_module isn't used") |
| 122 | + |
124 | 123 | def splitext(self, path): |
125 | 124 | """Similar to os.path.splitext, but splits our longest known suffix preferentially.""" |
126 | 125 | for suffix in self.suffixes_by_length: |
@@ -187,22 +186,18 @@ def _find_all_distributions(self, module_dir): |
187 | 186 |
|
188 | 187 | def find_spec(self, name, path, target=None): |
189 | 188 | """Implements abc.MetaPathFinder.""" |
190 | | - loader = self.find_module(name, path) |
191 | | - if loader is None: |
192 | | - return None |
193 | | - return spec_from_loader(name, loader) |
194 | | - |
195 | | - def find_module(self, fullname, path=None): |
196 | | - """Attempt to locate module. Returns self if found, None if not.""" |
197 | | - if fullname.startswith(self.prefix): |
198 | | - return self |
| 189 | + if name.startswith(self.prefix): |
| 190 | + return spec_from_loader(name, self) |
199 | 191 |
|
200 | | - def load_module(self, fullname): |
| 192 | + def create_module(self, spec): |
201 | 193 | """Actually load a module that we said we'd handle in find_module.""" |
202 | | - module = import_module(fullname[len(self.prefix):]) |
203 | | - sys.modules[fullname] = module |
| 194 | + module = import_module(spec.name[len(self.prefix):]) |
| 195 | + sys.modules[spec.name] = module |
204 | 196 | return module |
205 | 197 |
|
| 198 | + def exec_module(self, mod): |
| 199 | + """Nothing to do, create_module already did the work.""" |
| 200 | + |
206 | 201 | def find_distributions(self, context): |
207 | 202 | """Return an iterable of all Distribution instances capable of |
208 | 203 | loading the metadata for packages for the indicated ``context``. |
|
0 commit comments