##// END OF EJS Templates
Don't find importable non-modules in shim
Min RK -
Show More
@@ -1,75 +1,78
1 """A shim module for deprecated imports
1 """A shim module for deprecated imports
2 """
2 """
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 import sys
6 import sys
7 import types
7 import types
8
8
9 from .importstring import import_item
9 from .importstring import import_item
10
10
11
11
12 class ShimImporter(object):
12 class ShimImporter(object):
13 """Import hook for a shim.
13 """Import hook for a shim.
14
14
15 This ensures that submodule imports return the real target module,
15 This ensures that submodule imports return the real target module,
16 not a clone that will confuse `is` and `isinstance` checks.
16 not a clone that will confuse `is` and `isinstance` checks.
17 """
17 """
18 def __init__(self, src, mirror):
18 def __init__(self, src, mirror):
19 self.src = src
19 self.src = src
20 self.mirror = mirror
20 self.mirror = mirror
21
21
22 def _mirror_name(self, fullname):
22 def _mirror_name(self, fullname):
23 """get the name of the mirrored module"""
23 """get the name of the mirrored module"""
24
24
25 return self.mirror + fullname[len(self.src):]
25 return self.mirror + fullname[len(self.src):]
26
26
27 def find_module(self, fullname, path=None):
27 def find_module(self, fullname, path=None):
28 """Return self if we should be used to import the module."""
28 """Return self if we should be used to import the module."""
29 if fullname.startswith(self.src + '.'):
29 if fullname.startswith(self.src + '.'):
30 mirror_name = self._mirror_name(fullname)
30 mirror_name = self._mirror_name(fullname)
31 try:
31 try:
32 __import__(mirror_name)
32 mod = import_item(mirror_name)
33 except ImportError:
33 except ImportError:
34 return
34 return
35 else:
35 else:
36 if not isinstance(mod, types.ModuleType):
37 # not a module
38 return None
36 return self
39 return self
37
40
38 def load_module(self, fullname):
41 def load_module(self, fullname):
39 """Import the mirrored module, and insert it into sys.modules"""
42 """Import the mirrored module, and insert it into sys.modules"""
40 mirror_name = self._mirror_name(fullname)
43 mirror_name = self._mirror_name(fullname)
41 mod = import_item(mirror_name)
44 mod = import_item(mirror_name)
42 sys.modules[fullname] = mod
45 sys.modules[fullname] = mod
43 return mod
46 return mod
44
47
45
48
46 class ShimModule(types.ModuleType):
49 class ShimModule(types.ModuleType):
47
50
48 def __init__(self, *args, **kwargs):
51 def __init__(self, *args, **kwargs):
49 self._mirror = kwargs.pop("mirror")
52 self._mirror = kwargs.pop("mirror")
50 src = kwargs.pop("src", None)
53 src = kwargs.pop("src", None)
51 if src:
54 if src:
52 kwargs['name'] = src.rsplit('.', 1)[-1]
55 kwargs['name'] = src.rsplit('.', 1)[-1]
53 super(ShimModule, self).__init__(*args, **kwargs)
56 super(ShimModule, self).__init__(*args, **kwargs)
54 # add import hook for descendent modules
57 # add import hook for descendent modules
55 if src:
58 if src:
56 sys.meta_path.append(
59 sys.meta_path.append(
57 ShimImporter(src=src, mirror=self._mirror)
60 ShimImporter(src=src, mirror=self._mirror)
58 )
61 )
59
62
60 @property
63 @property
61 def __path__(self):
64 def __path__(self):
62 return []
65 return []
63
66
64 @property
67 @property
65 def __spec__(self):
68 def __spec__(self):
66 """Don't produce __spec__ until requested"""
69 """Don't produce __spec__ until requested"""
67 return __import__(self._mirror).__spec__
70 return __import__(self._mirror).__spec__
68
71
69 def __getattr__(self, key):
72 def __getattr__(self, key):
70 # Use the equivalent of import_item(name), see below
73 # Use the equivalent of import_item(name), see below
71 name = "%s.%s" % (self._mirror, key)
74 name = "%s.%s" % (self._mirror, key)
72 try:
75 try:
73 return import_item(name)
76 return import_item(name)
74 except ImportError:
77 except ImportError:
75 raise AttributeError(key)
78 raise AttributeError(key)
General Comments 0
You need to be logged in to leave comments. Login now