Show More
@@ -3,6 +3,8 b'' | |||
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | import importlib.abc | |
|
7 | import importlib.util | |
|
6 | 8 | import sys |
|
7 | 9 | import types |
|
8 | 10 | from importlib import import_module |
@@ -13,41 +15,26 b' from .importstring import import_item' | |||
|
13 | 15 | class ShimWarning(Warning): |
|
14 | 16 | """A warning to show when a module has moved, and a shim is in its place.""" |
|
15 | 17 | |
|
16 | class ShimImporter(object): | |
|
18 | ||
|
19 | class ShimImporter(importlib.abc.MetaPathFinder): | |
|
17 | 20 | """Import hook for a shim. |
|
18 | ||
|
21 | ||
|
19 | 22 | This ensures that submodule imports return the real target module, |
|
20 | 23 | not a clone that will confuse `is` and `isinstance` checks. |
|
21 | 24 | """ |
|
22 | 25 | def __init__(self, src, mirror): |
|
23 | 26 | self.src = src |
|
24 | 27 | self.mirror = mirror |
|
25 | ||
|
28 | ||
|
26 | 29 | def _mirror_name(self, fullname): |
|
27 | 30 | """get the name of the mirrored module""" |
|
28 | ||
|
29 | return self.mirror + fullname[len(self.src):] | |
|
30 | 31 | |
|
31 | def find_module(self, fullname, path=None): | |
|
32 | """Return self if we should be used to import the module.""" | |
|
33 | if fullname.startswith(self.src + '.'): | |
|
34 | mirror_name = self._mirror_name(fullname) | |
|
35 | try: | |
|
36 | mod = import_item(mirror_name) | |
|
37 | except ImportError: | |
|
38 | return | |
|
39 | else: | |
|
40 | if not isinstance(mod, types.ModuleType): | |
|
41 | # not a module | |
|
42 | return None | |
|
43 | return self | |
|
32 | return self.mirror + fullname[len(self.src) :] | |
|
44 | 33 | |
|
45 |
def |
|
|
46 | """Import the mirrored module, and insert it into sys.modules""" | |
|
47 | mirror_name = self._mirror_name(fullname) | |
|
48 |
|
|
|
49 | sys.modules[fullname] = mod | |
|
50 | return mod | |
|
34 | def find_spec(self, fullname, path, target=None): | |
|
35 | if fullname.startswith(self.src + "."): | |
|
36 | mirror_name = self._mirror_name(fullname) | |
|
37 | return importlib.util.find_spec(mirror_name) | |
|
51 | 38 | |
|
52 | 39 | |
|
53 | 40 | class ShimModule(types.ModuleType): |
General Comments 0
You need to be logged in to leave comments.
Login now