|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import importlib |
| 6 | +import importlib.util |
6 | 7 | import sys |
7 | 8 | import warnings |
8 | 9 |
|
|
16 | 17 |
|
17 | 18 |
|
18 | 19 | class TornadoImporter: |
19 | | - def find_module(self, module_name, package_path=None): |
20 | | - if USE_VENDORED_TORNADO: |
21 | | - if module_name.startswith("tornado"): |
22 | | - return self |
23 | | - else: |
24 | | - if module_name.startswith("salt.ext.tornado"): |
25 | | - return self |
26 | | - return None |
27 | | - |
28 | | - def load_module(self, name): |
29 | | - if USE_VENDORED_TORNADO: |
30 | | - mod = importlib.import_module("salt.ext.{}".format(name)) |
31 | | - else: |
32 | | - # Remove 'salt.ext.' from the module |
33 | | - mod = importlib.import_module(name[9:]) |
34 | | - sys.modules[name] = mod |
35 | | - return mod |
| 20 | + """Implementation of importlib.abc.MetaPathFinder that intercepts tornado imports. |
36 | 21 |
|
37 | | - def create_module(self, spec): |
38 | | - return self.load_module(spec.name) |
| 22 | + Normally, a Finder is only responsible for finding a source. However, we not |
| 23 | + only need to redirect the import, we also need to add it to sys.modules. |
| 24 | + That's outside of a finder's scope, but it is easiest to do it in one place. |
| 25 | + """ |
39 | 26 |
|
40 | | - def exec_module(self, module): |
| 27 | + def find_spec(self, fullname, path=None, target=None): |
| 28 | + if USE_VENDORED_TORNADO and fullname.startswith("tornado"): |
| 29 | + vendored_name = fullname.replace("tornado", "salt.ext.tornado", 1) |
| 30 | + spec = importlib.util.find_spec(vendored_name) |
| 31 | + if spec is not None: |
| 32 | + sys.modules[fullname] = importlib.import_module(vendored_name) |
| 33 | + return spec |
41 | 34 | return None |
42 | 35 |
|
43 | 36 |
|
44 | 37 | # Try our importer first |
45 | | -sys.meta_path = [TornadoImporter()] + sys.meta_path |
| 38 | +sys.meta_path.insert(0, TornadoImporter()) |
46 | 39 |
|
47 | 40 |
|
48 | 41 | # All salt related deprecation warnings should be shown once each! |
|
0 commit comments