##// END OF EJS Templates
Merge pull request #8186 from minrk/shim-import-hook...
Thomas Kluyver -
r20994:01e03c55 merge
parent child Browse files
Show More
@@ -12,11 +12,7 b' working, though a warning will be printed.'
12 # Copyright (c) IPython Development Team.
12 # Copyright (c) IPython Development Team.
13 # Distributed under the terms of the Modified BSD License.
13 # Distributed under the terms of the Modified BSD License.
14
14
15 from __future__ import print_function
16
17 # Stdlib
18 import sys
15 import sys
19 import types
20 from warnings import warn
16 from warnings import warn
21
17
22 warn("The top-level `frontend` package has been deprecated. "
18 warn("The top-level `frontend` package has been deprecated. "
@@ -27,5 +23,7 b' from IPython.utils.shimmodule import ShimModule'
27 # Unconditionally insert the shim into sys.modules so that further import calls
23 # Unconditionally insert the shim into sys.modules so that further import calls
28 # trigger the custom attribute access above
24 # trigger the custom attribute access above
29
25
30 sys.modules['IPython.frontend.html.notebook'] = ShimModule('notebook', mirror='IPython.html')
26 sys.modules['IPython.frontend.html.notebook'] = ShimModule(
31 sys.modules['IPython.frontend'] = ShimModule('frontend', mirror='IPython')
27 src='IPython.frontend.html.notebook', mirror='IPython.html')
28 sys.modules['IPython.frontend'] = ShimModule(
29 src='IPython.frontend', mirror='IPython')
@@ -14,14 +14,18 b' warn("The `IPython.kernel` package has been deprecated. "'
14 from IPython.utils.shimmodule import ShimModule
14 from IPython.utils.shimmodule import ShimModule
15
15
16 # zmq subdir is gone
16 # zmq subdir is gone
17 sys.modules['IPython.kernel.zmq.session'] = ShimModule('session', mirror='jupyter_client.session')
17 sys.modules['IPython.kernel.zmq.session'] = ShimModule(
18 sys.modules['IPython.kernel.zmq'] = ShimModule('zmq', mirror='ipython_kernel')
18 src='IPython.kernel.zmq.session', mirror='jupyter_client.session')
19 sys.modules['IPython.kernel.zmq'] = ShimModule(
20 src='IPython.kernel.zmq', mirror='ipython_kernel')
19
21
20 for pkg in ('comm', 'inprocess'):
22 for pkg in ('comm', 'inprocess'):
21 sys.modules['IPython.kernel.%s' % pkg] = ShimModule(pkg, mirror='ipython_kernel.%s' % pkg)
23 src = 'IPython.kernel.%s' % pkg
24 sys.modules[src] = ShimModule(src=src, mirror='ipython_kernel.%s' % pkg)
22
25
23 for pkg in ('ioloop', 'blocking'):
26 for pkg in ('ioloop', 'blocking'):
24 sys.modules['IPython.kernel.%s' % pkg] = ShimModule(pkg, mirror='jupyter_client.%s' % pkg)
27 src = 'IPython.kernel.%s' % pkg
28 sys.modules[src] = ShimModule(src=src, mirror='jupyter_client.%s' % pkg)
25
29
26 # required for `from IPython.kernel import PKG`
30 # required for `from IPython.kernel import PKG`
27 from ipython_kernel import comm, inprocess
31 from ipython_kernel import comm, inprocess
@@ -4,8 +4,6 b' Shim to maintain backwards compatibility with old IPython.nbconvert imports.'
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
8
9 import sys
7 import sys
10 from warnings import warn
8 from warnings import warn
11
9
@@ -17,4 +15,5 b' from IPython.utils.shimmodule import ShimModule'
17 # Unconditionally insert the shim into sys.modules so that further import calls
15 # Unconditionally insert the shim into sys.modules so that further import calls
18 # trigger the custom attribute access above
16 # trigger the custom attribute access above
19
17
20 sys.modules['IPython.nbconvert'] = ShimModule('nbconvert', mirror='jupyter_nbconvert')
18 sys.modules['IPython.nbconvert'] = ShimModule(
19 src='IPython.nbconvert', mirror='jupyter_nbconvert')
@@ -4,11 +4,7 b' Shim to maintain backwards compatibility with old IPython.nbformat imports.'
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
8
9 # Stdlib
10 import sys
7 import sys
11 import types
12 from warnings import warn
8 from warnings import warn
13
9
14 warn("The `IPython.nbformat` package has been deprecated. "
10 warn("The `IPython.nbformat` package has been deprecated. "
@@ -19,4 +15,5 b' from IPython.utils.shimmodule import ShimModule'
19 # Unconditionally insert the shim into sys.modules so that further import calls
15 # Unconditionally insert the shim into sys.modules so that further import calls
20 # trigger the custom attribute access above
16 # trigger the custom attribute access above
21
17
22 sys.modules['IPython.nbformat'] = ShimModule('nbformat', mirror='jupyter_nbformat')
18 sys.modules['IPython.nbformat'] = ShimModule(
19 src='IPython.nbformat', mirror='jupyter_nbformat')
@@ -4,8 +4,6 b' Shim to maintain backwards compatibility with old IPython.parallel imports.'
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
8
9 import sys
7 import sys
10 from warnings import warn
8 from warnings import warn
11
9
@@ -17,4 +15,6 b' from IPython.utils.shimmodule import ShimModule'
17 # Unconditionally insert the shim into sys.modules so that further import calls
15 # Unconditionally insert the shim into sys.modules so that further import calls
18 # trigger the custom attribute access above
16 # trigger the custom attribute access above
19
17
20 sys.modules['IPython.parallel'] = ShimModule('parallel', mirror='ipython_parallel')
18 sys.modules['IPython.parallel'] = ShimModule(
19 src='IPython.parallel', mirror='ipython_parallel')
20
@@ -4,11 +4,7 b' Shim to maintain backwards compatibility with old IPython.qt imports.'
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
8
9 # Stdlib
10 import sys
7 import sys
11 import types
12 from warnings import warn
8 from warnings import warn
13
9
14 warn("The `IPython.qt` package has been deprecated. "
10 warn("The `IPython.qt` package has been deprecated. "
@@ -19,4 +15,4 b' from IPython.utils.shimmodule import ShimModule'
19 # Unconditionally insert the shim into sys.modules so that further import calls
15 # Unconditionally insert the shim into sys.modules so that further import calls
20 # trigger the custom attribute access above
16 # trigger the custom attribute access above
21
17
22 sys.modules['IPython.qt'] = ShimModule('qt', mirror='jupyter_qtconsole')
18 sys.modules['IPython.qt'] = ShimModule(src='IPython.qt', mirror='jupyter_qtconsole')
@@ -17,4 +17,5 b' from IPython.utils.shimmodule import ShimModule'
17 # Unconditionally insert the shim into sys.modules so that further import calls
17 # Unconditionally insert the shim into sys.modules so that further import calls
18 # trigger the custom attribute access above
18 # trigger the custom attribute access above
19
19
20 sys.modules['IPython.terminal.console'] = ShimModule('console', mirror='jupyter_console')
20 sys.modules['IPython.terminal.console'] = ShimModule(
21 src='IPython.terminal.console', mirror='jupyter_console')
@@ -3,14 +3,67 b''
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 types
7 import types
7
8
9 from .importstring import import_item
10
11
12 class ShimImporter(object):
13 """Import hook for a shim.
14
15 This ensures that submodule imports return the real target module,
16 not a clone that will confuse `is` and `isinstance` checks.
17 """
18 def __init__(self, src, mirror):
19 self.src = src
20 self.mirror = mirror
21
22 def _mirror_name(self, fullname):
23 """get the name of the mirrored module"""
24
25 return self.mirror + fullname[len(self.src):]
26
27 def find_module(self, fullname, path=None):
28 """Return self if we should be used to import the module."""
29 if fullname.startswith(self.src + '.'):
30 mirror_name = self._mirror_name(fullname)
31 try:
32 mod = import_item(mirror_name)
33 except ImportError:
34 return
35 else:
36 if not isinstance(mod, types.ModuleType):
37 # not a module
38 return None
39 return self
40
41 def load_module(self, fullname):
42 """Import the mirrored module, and insert it into sys.modules"""
43 mirror_name = self._mirror_name(fullname)
44 mod = import_item(mirror_name)
45 sys.modules[fullname] = mod
46 return mod
47
48
8 class ShimModule(types.ModuleType):
49 class ShimModule(types.ModuleType):
9
50
10 def __init__(self, *args, **kwargs):
51 def __init__(self, *args, **kwargs):
11 self._mirror = kwargs.pop("mirror")
52 self._mirror = kwargs.pop("mirror")
53 src = kwargs.pop("src", None)
54 if src:
55 kwargs['name'] = src.rsplit('.', 1)[-1]
12 super(ShimModule, self).__init__(*args, **kwargs)
56 super(ShimModule, self).__init__(*args, **kwargs)
13
57 # add import hook for descendent modules
58 if src:
59 sys.meta_path.append(
60 ShimImporter(src=src, mirror=self._mirror)
61 )
62
63 @property
64 def __path__(self):
65 return []
66
14 @property
67 @property
15 def __spec__(self):
68 def __spec__(self):
16 """Don't produce __spec__ until requested"""
69 """Don't produce __spec__ until requested"""
@@ -19,26 +72,7 b' class ShimModule(types.ModuleType):'
19 def __getattr__(self, key):
72 def __getattr__(self, key):
20 # Use the equivalent of import_item(name), see below
73 # Use the equivalent of import_item(name), see below
21 name = "%s.%s" % (self._mirror, key)
74 name = "%s.%s" % (self._mirror, key)
22
75 try:
23 # NOTE: the code below was copied *verbatim* from
76 return import_item(name)
24 # importstring.import_item. For some very strange reason that makes no
77 except ImportError:
25 # sense to me, if we call it *as a function*, it doesn't work. This
78 raise AttributeError(key)
26 # has something to do with the deep bowels of the import machinery and
27 # I couldn't find a way to make the code work as a standard function
28 # call. But at least since it's an unmodified copy of import_item,
29 # which is used extensively and has a test suite, we can be reasonably
30 # confident this is OK. If anyone finds how to call the function, all
31 # the below could be replaced simply with:
32 #
33 # from IPython.utils.importstring import import_item
34 # return import_item('MIRROR.' + key)
35
36 parts = name.rsplit('.', 1)
37 if len(parts) == 2:
38 # called with 'foo.bar....'
39 package, obj = parts
40 module = __import__(package, fromlist=[obj])
41 return getattr(module, obj)
42 else:
43 # called with un-dotted string
44 return __import__(parts[0])
General Comments 0
You need to be logged in to leave comments. Login now