##// END OF EJS Templates
add ShimWarning for shimmed imports...
Min RK -
Show More
@@ -0,0 +1,15 b''
1 import sys
2 import warnings
3
4 import nose.tools as nt
5
6 from IPython.utils.capture import capture_output
7 from IPython.utils.shimmodule import ShimWarning
8
9 def test_shim_warning():
10 sys.modules.pop('IPython.config', None)
11 with warnings.catch_warnings(record=True) as w:
12 warnings.simplefilter("always")
13 import IPython.config
14 assert len(w) == 1
15 assert issubclass(w[-1].category, ShimWarning)
@@ -1,18 +1,19 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.config imports.
2 Shim to maintain backwards compatibility with old IPython.config imports.
3 """
3 """
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 import sys
7 import sys
8 from warnings import warn
8 from warnings import warn
9
9
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
11
10 warn("The `IPython.config` package has been deprecated. "
12 warn("The `IPython.config` package has been deprecated. "
11 "You should import from traitlets.config instead.")
13 "You should import from traitlets.config instead.", ShimWarning)
12
14
13 from IPython.utils.shimmodule import ShimModule
14
15
15 # Unconditionally insert the shim into sys.modules so that further import calls
16 # Unconditionally insert the shim into sys.modules so that further import calls
16 # trigger the custom attribute access above
17 # trigger the custom attribute access above
17
18
18 sys.modules['IPython.config'] = ShimModule(src='IPython.config', mirror='traitlets.config')
19 sys.modules['IPython.config'] = ShimModule(src='IPython.config', mirror='traitlets.config')
@@ -1,29 +1,29 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old frontend imports.
2 Shim to maintain backwards compatibility with old frontend imports.
3
3
4 We have moved all contents of the old `frontend` subpackage into top-level
4 We have moved all contents of the old `frontend` subpackage into top-level
5 subpackages (`html`, `qt` and `terminal`), and flattened the notebook into
5 subpackages (`html`, `qt` and `terminal`), and flattened the notebook into
6 just `IPython.html`, formerly `IPython.frontend.html.notebook`.
6 just `IPython.html`, formerly `IPython.frontend.html.notebook`.
7
7
8 This will let code that was making `from IPython.frontend...` calls continue
8 This will let code that was making `from IPython.frontend...` calls continue
9 working, though a warning will be printed.
9 working, though a warning will be printed.
10 """
10 """
11
11
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 import sys
15 import sys
16 from warnings import warn
16 from warnings import warn
17
17
18 warn("The top-level `frontend` package has been deprecated. "
18 from IPython.utils.shimmodule import ShimModule, ShimWarning
19 "All its subpackages have been moved to the top `IPython` level.")
20
19
21 from IPython.utils.shimmodule import ShimModule
20 warn("The top-level `frontend` package has been deprecated. "
21 "All its subpackages have been moved to the top `IPython` level.", ShimWarning)
22
22
23 # 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
24 # trigger the custom attribute access above
24 # trigger the custom attribute access above
25
25
26 sys.modules['IPython.frontend.html.notebook'] = ShimModule(
26 sys.modules['IPython.frontend.html.notebook'] = ShimModule(
27 src='IPython.frontend.html.notebook', mirror='IPython.html')
27 src='IPython.frontend.html.notebook', mirror='IPython.html')
28 sys.modules['IPython.frontend'] = ShimModule(
28 sys.modules['IPython.frontend'] = ShimModule(
29 src='IPython.frontend', mirror='IPython')
29 src='IPython.frontend', mirror='IPython')
@@ -1,28 +1,28 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.html imports.
2 Shim to maintain backwards compatibility with old IPython.html imports.
3 """
3 """
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 import sys
7 import sys
8 from warnings import warn
8 from warnings import warn
9
9
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
11
10 warn("The `IPython.html` package has been deprecated. "
12 warn("The `IPython.html` package has been deprecated. "
11 "You should import from `notebook` instead. "
13 "You should import from `notebook` instead. "
12 "`IPython.html.widgets` has moved to `ipywidgets`.")
14 "`IPython.html.widgets` has moved to `ipywidgets`.", ShimWarning)
13
14 from IPython.utils.shimmodule import ShimModule
15
15
16 _widgets = sys.modules['IPython.html.widgets'] = ShimModule(
16 _widgets = sys.modules['IPython.html.widgets'] = ShimModule(
17 src='IPython.html.widgets', mirror='ipywidgets')
17 src='IPython.html.widgets', mirror='ipywidgets')
18
18
19 _html = ShimModule(
19 _html = ShimModule(
20 src='IPython.html', mirror='notebook')
20 src='IPython.html', mirror='notebook')
21
21
22 # hook up widgets
22 # hook up widgets
23 _html.widgets = _widgets
23 _html.widgets = _widgets
24 sys.modules['IPython.html'] = _html
24 sys.modules['IPython.html'] = _html
25
25
26 if __name__ == '__main__':
26 if __name__ == '__main__':
27 from notebook import notebookapp as app
27 from notebook import notebookapp as app
28 app.launch_new_instance()
28 app.launch_new_instance()
@@ -1,35 +1,35 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.kernel imports.
2 Shim to maintain backwards compatibility with old IPython.kernel imports.
3 """
3 """
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 import sys
7 import sys
8 from warnings import warn
8 from warnings import warn
9
9
10 warn("The `IPython.kernel` package has been deprecated. "
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
11 "You should import from ipykernel or jupyter_client instead.")
12
11
12 warn("The `IPython.kernel` package has been deprecated. "
13 "You should import from ipykernel or jupyter_client instead.", ShimWarning)
13
14
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(
17 sys.modules['IPython.kernel.zmq.session'] = ShimModule(
18 src='IPython.kernel.zmq.session', mirror='jupyter_client.session')
18 src='IPython.kernel.zmq.session', mirror='jupyter_client.session')
19 sys.modules['IPython.kernel.zmq'] = ShimModule(
19 sys.modules['IPython.kernel.zmq'] = ShimModule(
20 src='IPython.kernel.zmq', mirror='ipykernel')
20 src='IPython.kernel.zmq', mirror='ipykernel')
21
21
22 for pkg in ('comm', 'inprocess'):
22 for pkg in ('comm', 'inprocess'):
23 src = 'IPython.kernel.%s' % pkg
23 src = 'IPython.kernel.%s' % pkg
24 sys.modules[src] = ShimModule(src=src, mirror='ipykernel.%s' % pkg)
24 sys.modules[src] = ShimModule(src=src, mirror='ipykernel.%s' % pkg)
25
25
26 for pkg in ('ioloop', 'blocking'):
26 for pkg in ('ioloop', 'blocking'):
27 src = 'IPython.kernel.%s' % pkg
27 src = 'IPython.kernel.%s' % pkg
28 sys.modules[src] = ShimModule(src=src, mirror='jupyter_client.%s' % pkg)
28 sys.modules[src] = ShimModule(src=src, mirror='jupyter_client.%s' % pkg)
29
29
30 # required for `from IPython.kernel import PKG`
30 # required for `from IPython.kernel import PKG`
31 from ipykernel import comm, inprocess
31 from ipykernel import comm, inprocess
32 from jupyter_client import ioloop, blocking
32 from jupyter_client import ioloop, blocking
33 # public API
33 # public API
34 from ipykernel.connect import *
34 from ipykernel.connect import *
35 from jupyter_client import *
35 from jupyter_client import *
@@ -1,19 +1,19 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.nbconvert imports.
2 Shim to maintain backwards compatibility with old IPython.nbconvert imports.
3 """
3 """
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 import sys
7 import sys
8 from warnings import warn
8 from warnings import warn
9
9
10 warn("The `IPython.nbconvert` package has been deprecated. "
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
11 "You should import from ipython_nbconvert instead.")
12
11
13 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.nbconvert` package has been deprecated. "
13 "You should import from ipython_nbconvert instead.", ShimWarning)
14
14
15 # 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
16 # trigger the custom attribute access above
16 # trigger the custom attribute access above
17
17
18 sys.modules['IPython.nbconvert'] = ShimModule(
18 sys.modules['IPython.nbconvert'] = ShimModule(
19 src='IPython.nbconvert', mirror='nbconvert')
19 src='IPython.nbconvert', mirror='nbconvert')
@@ -1,19 +1,19 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.nbformat imports.
2 Shim to maintain backwards compatibility with old IPython.nbformat imports.
3 """
3 """
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 import sys
7 import sys
8 from warnings import warn
8 from warnings import warn
9
9
10 warn("The `IPython.nbformat` package has been deprecated. "
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
11 "You should import from nbformat instead.")
12
11
13 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.nbformat` package has been deprecated. "
13 "You should import from nbformat instead.", ShimWarning)
14
14
15 # 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
16 # trigger the custom attribute access above
16 # trigger the custom attribute access above
17
17
18 sys.modules['IPython.nbformat'] = ShimModule(
18 sys.modules['IPython.nbformat'] = ShimModule(
19 src='IPython.nbformat', mirror='nbformat')
19 src='IPython.nbformat', mirror='nbformat')
@@ -1,20 +1,20 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.parallel imports.
2 Shim to maintain backwards compatibility with old IPython.parallel imports.
3 """
3 """
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 import sys
7 import sys
8 from warnings import warn
8 from warnings import warn
9
9
10 warn("The `IPython.parallel` package has been deprecated. "
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
11 "You should import from ipyparallel instead.")
12
11
13 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.parallel` package has been deprecated. "
13 "You should import from ipyparallel instead.", ShimWarning)
14
14
15 # 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
16 # trigger the custom attribute access above
16 # trigger the custom attribute access above
17
17
18 sys.modules['IPython.parallel'] = ShimModule(
18 sys.modules['IPython.parallel'] = ShimModule(
19 src='IPython.parallel', mirror='ipyparallel')
19 src='IPython.parallel', mirror='ipyparallel')
20
20
@@ -1,24 +1,24 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.qt imports.
2 Shim to maintain backwards compatibility with old IPython.qt imports.
3 """
3 """
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 import sys
7 import sys
8 from warnings import warn
8 from warnings import warn
9
9
10 warn("The `IPython.qt` package has been deprecated. "
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
11 "You should import from qtconsole instead.")
12
11
13 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.qt` package has been deprecated. "
13 "You should import from qtconsole instead.", ShimWarning)
14
14
15 # 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
16 # trigger the custom attribute access above
16 # trigger the custom attribute access above
17
17
18 _console = sys.modules['IPython.qt.console'] = ShimModule(
18 _console = sys.modules['IPython.qt.console'] = ShimModule(
19 src='IPython.qt.console', mirror='qtconsole')
19 src='IPython.qt.console', mirror='qtconsole')
20
20
21 _qt = ShimModule(src='IPython.qt', mirror='qtconsole')
21 _qt = ShimModule(src='IPython.qt', mirror='qtconsole')
22
22
23 _qt.console = _console
23 _qt.console = _console
24 sys.modules['IPython.qt'] = _qt
24 sys.modules['IPython.qt'] = _qt
@@ -1,21 +1,19 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.terminal.console imports.
2 Shim to maintain backwards compatibility with old IPython.terminal.console imports.
3 """
3 """
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
12 warn("The `IPython.terminal.console` package has been deprecated. "
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
13 "You should import from jupyter_console instead.")
14
11
15 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.terminal.console` package has been deprecated. "
13 "You should import from jupyter_console instead.", ShimWarning)
16
14
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.terminal.console'] = ShimModule(
18 sys.modules['IPython.terminal.console'] = ShimModule(
21 src='IPython.terminal.console', mirror='jupyter_console')
19 src='IPython.terminal.console', mirror='jupyter_console')
@@ -1,90 +1,92 b''
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 class ShimWarning(Warning):
12 """A warning to show when a module has moved, and a shim is in its place."""
11
13
12 class ShimImporter(object):
14 class ShimImporter(object):
13 """Import hook for a shim.
15 """Import hook for a shim.
14
16
15 This ensures that submodule imports return the real target module,
17 This ensures that submodule imports return the real target module,
16 not a clone that will confuse `is` and `isinstance` checks.
18 not a clone that will confuse `is` and `isinstance` checks.
17 """
19 """
18 def __init__(self, src, mirror):
20 def __init__(self, src, mirror):
19 self.src = src
21 self.src = src
20 self.mirror = mirror
22 self.mirror = mirror
21
23
22 def _mirror_name(self, fullname):
24 def _mirror_name(self, fullname):
23 """get the name of the mirrored module"""
25 """get the name of the mirrored module"""
24
26
25 return self.mirror + fullname[len(self.src):]
27 return self.mirror + fullname[len(self.src):]
26
28
27 def find_module(self, fullname, path=None):
29 def find_module(self, fullname, path=None):
28 """Return self if we should be used to import the module."""
30 """Return self if we should be used to import the module."""
29 if fullname.startswith(self.src + '.'):
31 if fullname.startswith(self.src + '.'):
30 mirror_name = self._mirror_name(fullname)
32 mirror_name = self._mirror_name(fullname)
31 try:
33 try:
32 mod = import_item(mirror_name)
34 mod = import_item(mirror_name)
33 except ImportError:
35 except ImportError:
34 return
36 return
35 else:
37 else:
36 if not isinstance(mod, types.ModuleType):
38 if not isinstance(mod, types.ModuleType):
37 # not a module
39 # not a module
38 return None
40 return None
39 return self
41 return self
40
42
41 def load_module(self, fullname):
43 def load_module(self, fullname):
42 """Import the mirrored module, and insert it into sys.modules"""
44 """Import the mirrored module, and insert it into sys.modules"""
43 mirror_name = self._mirror_name(fullname)
45 mirror_name = self._mirror_name(fullname)
44 mod = import_item(mirror_name)
46 mod = import_item(mirror_name)
45 sys.modules[fullname] = mod
47 sys.modules[fullname] = mod
46 return mod
48 return mod
47
49
48
50
49 class ShimModule(types.ModuleType):
51 class ShimModule(types.ModuleType):
50
52
51 def __init__(self, *args, **kwargs):
53 def __init__(self, *args, **kwargs):
52 self._mirror = kwargs.pop("mirror")
54 self._mirror = kwargs.pop("mirror")
53 src = kwargs.pop("src", None)
55 src = kwargs.pop("src", None)
54 if src:
56 if src:
55 kwargs['name'] = src.rsplit('.', 1)[-1]
57 kwargs['name'] = src.rsplit('.', 1)[-1]
56 super(ShimModule, self).__init__(*args, **kwargs)
58 super(ShimModule, self).__init__(*args, **kwargs)
57 # add import hook for descendent modules
59 # add import hook for descendent modules
58 if src:
60 if src:
59 sys.meta_path.append(
61 sys.meta_path.append(
60 ShimImporter(src=src, mirror=self._mirror)
62 ShimImporter(src=src, mirror=self._mirror)
61 )
63 )
62
64
63 @property
65 @property
64 def __path__(self):
66 def __path__(self):
65 return []
67 return []
66
68
67 @property
69 @property
68 def __spec__(self):
70 def __spec__(self):
69 """Don't produce __spec__ until requested"""
71 """Don't produce __spec__ until requested"""
70 return __import__(self._mirror).__spec__
72 return __import__(self._mirror).__spec__
71
73
72 def __dir__(self):
74 def __dir__(self):
73 return dir(__import__(self._mirror))
75 return dir(__import__(self._mirror))
74
76
75 @property
77 @property
76 def __all__(self):
78 def __all__(self):
77 """Ensure __all__ is always defined"""
79 """Ensure __all__ is always defined"""
78 mod = __import__(self._mirror)
80 mod = __import__(self._mirror)
79 try:
81 try:
80 return mod.__all__
82 return mod.__all__
81 except AttributeError:
83 except AttributeError:
82 return [name for name in dir(mod) if not name.startswith('_')]
84 return [name for name in dir(mod) if not name.startswith('_')]
83
85
84 def __getattr__(self, key):
86 def __getattr__(self, key):
85 # Use the equivalent of import_item(name), see below
87 # Use the equivalent of import_item(name), see below
86 name = "%s.%s" % (self._mirror, key)
88 name = "%s.%s" % (self._mirror, key)
87 try:
89 try:
88 return import_item(name)
90 return import_item(name)
89 except ImportError:
91 except ImportError:
90 raise AttributeError(key)
92 raise AttributeError(key)
General Comments 0
You need to be logged in to leave comments. Login now