##// END OF EJS Templates
Merge pull request #13194 from meysam81/meysam/remove-deprecated-stuff...
Matthias Bussonnier -
r26878:796ce7f0 merge
parent child Browse files
Show More
@@ -0,0 +1,8 b''
1 Remove Deprecated Stuff
2 ================================
3
4 We no longer need to add `extensions` to the PYTHONPATH because that is being
5 handled by `load_extension`.
6
7 We are also removing Cythonmagic, sympyprinting and rmagic as they are now in
8 other packages and no longer need to be inside IPython.
@@ -1,149 +1,144 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 IPython: tools for interactive and parallel computing in Python.
3 IPython: tools for interactive and parallel computing in Python.
4
4
5 https://ipython.org
5 https://ipython.org
6 """
6 """
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Copyright (c) 2008-2011, IPython Development Team.
8 # Copyright (c) 2008-2011, IPython Development Team.
9 # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu>
9 # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu>
10 # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
10 # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
11 # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
11 # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
12 #
12 #
13 # Distributed under the terms of the Modified BSD License.
13 # Distributed under the terms of the Modified BSD License.
14 #
14 #
15 # The full license is in the file COPYING.txt, distributed with this software.
15 # The full license is in the file COPYING.txt, distributed with this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import os
22 import os
23 import sys
23 import sys
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Setup everything
26 # Setup everything
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 # Don't forget to also update setup.py when this changes!
29 # Don't forget to also update setup.py when this changes!
30 if sys.version_info < (3, 6):
30 if sys.version_info < (3, 6):
31 raise ImportError(
31 raise ImportError(
32 """
32 """
33 IPython 7.10+ supports Python 3.6 and above.
33 IPython 7.10+ supports Python 3.6 and above.
34 When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
34 When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
35 Python 3.3 and 3.4 were supported up to IPython 6.x.
35 Python 3.3 and 3.4 were supported up to IPython 6.x.
36 Python 3.5 was supported with IPython 7.0 to 7.9.
36 Python 3.5 was supported with IPython 7.0 to 7.9.
37
37
38 See IPython `README.rst` file for more information:
38 See IPython `README.rst` file for more information:
39
39
40 https://github.com/ipython/ipython/blob/master/README.rst
40 https://github.com/ipython/ipython/blob/master/README.rst
41
41
42 """)
42 """)
43
43
44 # Make it easy to import extensions - they are always directly on pythonpath.
45 # Therefore, non-IPython modules can be added to extensions directory.
46 # This should probably be in ipapp.py.
47 sys.path.append(os.path.join(os.path.dirname(__file__), "extensions"))
48
49 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
50 # Setup the top level names
45 # Setup the top level names
51 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
52
47
53 from .core.getipython import get_ipython
48 from .core.getipython import get_ipython
54 from .core import release
49 from .core import release
55 from .core.application import Application
50 from .core.application import Application
56 from .terminal.embed import embed
51 from .terminal.embed import embed
57
52
58 from .core.interactiveshell import InteractiveShell
53 from .core.interactiveshell import InteractiveShell
59 from .testing import test
54 from .testing import test
60 from .utils.sysinfo import sys_info
55 from .utils.sysinfo import sys_info
61 from .utils.frame import extract_module_locals
56 from .utils.frame import extract_module_locals
62
57
63 # Release data
58 # Release data
64 __author__ = '%s <%s>' % (release.author, release.author_email)
59 __author__ = '%s <%s>' % (release.author, release.author_email)
65 __license__ = release.license
60 __license__ = release.license
66 __version__ = release.version
61 __version__ = release.version
67 version_info = release.version_info
62 version_info = release.version_info
68
63
69 def embed_kernel(module=None, local_ns=None, **kwargs):
64 def embed_kernel(module=None, local_ns=None, **kwargs):
70 """Embed and start an IPython kernel in a given scope.
65 """Embed and start an IPython kernel in a given scope.
71
66
72 If you don't want the kernel to initialize the namespace
67 If you don't want the kernel to initialize the namespace
73 from the scope of the surrounding function,
68 from the scope of the surrounding function,
74 and/or you want to load full IPython configuration,
69 and/or you want to load full IPython configuration,
75 you probably want `IPython.start_kernel()` instead.
70 you probably want `IPython.start_kernel()` instead.
76
71
77 Parameters
72 Parameters
78 ----------
73 ----------
79 module : types.ModuleType, optional
74 module : types.ModuleType, optional
80 The module to load into IPython globals (default: caller)
75 The module to load into IPython globals (default: caller)
81 local_ns : dict, optional
76 local_ns : dict, optional
82 The namespace to load into IPython user namespace (default: caller)
77 The namespace to load into IPython user namespace (default: caller)
83 **kwargs : various, optional
78 **kwargs : various, optional
84 Further keyword args are relayed to the IPKernelApp constructor,
79 Further keyword args are relayed to the IPKernelApp constructor,
85 allowing configuration of the Kernel. Will only have an effect
80 allowing configuration of the Kernel. Will only have an effect
86 on the first embed_kernel call for a given process.
81 on the first embed_kernel call for a given process.
87 """
82 """
88
83
89 (caller_module, caller_locals) = extract_module_locals(1)
84 (caller_module, caller_locals) = extract_module_locals(1)
90 if module is None:
85 if module is None:
91 module = caller_module
86 module = caller_module
92 if local_ns is None:
87 if local_ns is None:
93 local_ns = caller_locals
88 local_ns = caller_locals
94
89
95 # Only import .zmq when we really need it
90 # Only import .zmq when we really need it
96 from ipykernel.embed import embed_kernel as real_embed_kernel
91 from ipykernel.embed import embed_kernel as real_embed_kernel
97 real_embed_kernel(module=module, local_ns=local_ns, **kwargs)
92 real_embed_kernel(module=module, local_ns=local_ns, **kwargs)
98
93
99 def start_ipython(argv=None, **kwargs):
94 def start_ipython(argv=None, **kwargs):
100 """Launch a normal IPython instance (as opposed to embedded)
95 """Launch a normal IPython instance (as opposed to embedded)
101
96
102 `IPython.embed()` puts a shell in a particular calling scope,
97 `IPython.embed()` puts a shell in a particular calling scope,
103 such as a function or method for debugging purposes,
98 such as a function or method for debugging purposes,
104 which is often not desirable.
99 which is often not desirable.
105
100
106 `start_ipython()` does full, regular IPython initialization,
101 `start_ipython()` does full, regular IPython initialization,
107 including loading startup files, configuration, etc.
102 including loading startup files, configuration, etc.
108 much of which is skipped by `embed()`.
103 much of which is skipped by `embed()`.
109
104
110 This is a public API method, and will survive implementation changes.
105 This is a public API method, and will survive implementation changes.
111
106
112 Parameters
107 Parameters
113 ----------
108 ----------
114 argv : list or None, optional
109 argv : list or None, optional
115 If unspecified or None, IPython will parse command-line options from sys.argv.
110 If unspecified or None, IPython will parse command-line options from sys.argv.
116 To prevent any command-line parsing, pass an empty list: `argv=[]`.
111 To prevent any command-line parsing, pass an empty list: `argv=[]`.
117 user_ns : dict, optional
112 user_ns : dict, optional
118 specify this dictionary to initialize the IPython user namespace with particular values.
113 specify this dictionary to initialize the IPython user namespace with particular values.
119 **kwargs : various, optional
114 **kwargs : various, optional
120 Any other kwargs will be passed to the Application constructor,
115 Any other kwargs will be passed to the Application constructor,
121 such as `config`.
116 such as `config`.
122 """
117 """
123 from IPython.terminal.ipapp import launch_new_instance
118 from IPython.terminal.ipapp import launch_new_instance
124 return launch_new_instance(argv=argv, **kwargs)
119 return launch_new_instance(argv=argv, **kwargs)
125
120
126 def start_kernel(argv=None, **kwargs):
121 def start_kernel(argv=None, **kwargs):
127 """Launch a normal IPython kernel instance (as opposed to embedded)
122 """Launch a normal IPython kernel instance (as opposed to embedded)
128
123
129 `IPython.embed_kernel()` puts a shell in a particular calling scope,
124 `IPython.embed_kernel()` puts a shell in a particular calling scope,
130 such as a function or method for debugging purposes,
125 such as a function or method for debugging purposes,
131 which is often not desirable.
126 which is often not desirable.
132
127
133 `start_kernel()` does full, regular IPython initialization,
128 `start_kernel()` does full, regular IPython initialization,
134 including loading startup files, configuration, etc.
129 including loading startup files, configuration, etc.
135 much of which is skipped by `embed()`.
130 much of which is skipped by `embed()`.
136
131
137 Parameters
132 Parameters
138 ----------
133 ----------
139 argv : list or None, optional
134 argv : list or None, optional
140 If unspecified or None, IPython will parse command-line options from sys.argv.
135 If unspecified or None, IPython will parse command-line options from sys.argv.
141 To prevent any command-line parsing, pass an empty list: `argv=[]`.
136 To prevent any command-line parsing, pass an empty list: `argv=[]`.
142 user_ns : dict, optional
137 user_ns : dict, optional
143 specify this dictionary to initialize the IPython user namespace with particular values.
138 specify this dictionary to initialize the IPython user namespace with particular values.
144 **kwargs : various, optional
139 **kwargs : various, optional
145 Any other kwargs will be passed to the Application constructor,
140 Any other kwargs will be passed to the Application constructor,
146 such as `config`.
141 such as `config`.
147 """
142 """
148 from IPython.kernel.zmq.kernelapp import launch_new_instance
143 from IPython.kernel.zmq.kernelapp import launch_new_instance
149 return launch_new_instance(argv=argv, **kwargs)
144 return launch_new_instance(argv=argv, **kwargs)
@@ -1,150 +1,167 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """A class for managing IPython extensions."""
2 """A class for managing IPython extensions."""
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 os
7 import os
8 import os.path
8 import os.path
9 import sys
9 import sys
10 from importlib import import_module, reload
10 from importlib import import_module, reload
11
11
12 from traitlets.config.configurable import Configurable
12 from traitlets.config.configurable import Configurable
13 from IPython.utils.path import ensure_dir_exists, compress_user
13 from IPython.utils.path import ensure_dir_exists, compress_user
14 from IPython.utils.decorators import undoc
14 from IPython.utils.decorators import undoc
15 from traitlets import Instance
15 from traitlets import Instance
16
16
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Main class
19 # Main class
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 BUILTINS_EXTS = {"storemagic": False, "autoreload": False}
23
24
22 class ExtensionManager(Configurable):
25 class ExtensionManager(Configurable):
23 """A class to manage IPython extensions.
26 """A class to manage IPython extensions.
24
27
25 An IPython extension is an importable Python module that has
28 An IPython extension is an importable Python module that has
26 a function with the signature::
29 a function with the signature::
27
30
28 def load_ipython_extension(ipython):
31 def load_ipython_extension(ipython):
29 # Do things with ipython
32 # Do things with ipython
30
33
31 This function is called after your extension is imported and the
34 This function is called after your extension is imported and the
32 currently active :class:`InteractiveShell` instance is passed as
35 currently active :class:`InteractiveShell` instance is passed as
33 the only argument. You can do anything you want with IPython at
36 the only argument. You can do anything you want with IPython at
34 that point, including defining new magic and aliases, adding new
37 that point, including defining new magic and aliases, adding new
35 components, etc.
38 components, etc.
36
39
37 You can also optionally define an :func:`unload_ipython_extension(ipython)`
40 You can also optionally define an :func:`unload_ipython_extension(ipython)`
38 function, which will be called if the user unloads or reloads the extension.
41 function, which will be called if the user unloads or reloads the extension.
39 The extension manager will only call :func:`load_ipython_extension` again
42 The extension manager will only call :func:`load_ipython_extension` again
40 if the extension is reloaded.
43 if the extension is reloaded.
41
44
42 You can put your extension modules anywhere you want, as long as
45 You can put your extension modules anywhere you want, as long as
43 they can be imported by Python's standard import mechanism. However,
46 they can be imported by Python's standard import mechanism. However,
44 to make it easy to write extensions, you can also put your extensions
47 to make it easy to write extensions, you can also put your extensions
45 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
48 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
46 is added to ``sys.path`` automatically.
49 is added to ``sys.path`` automatically.
47 """
50 """
48
51
49 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
52 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
50
53
51 def __init__(self, shell=None, **kwargs):
54 def __init__(self, shell=None, **kwargs):
52 super(ExtensionManager, self).__init__(shell=shell, **kwargs)
55 super(ExtensionManager, self).__init__(shell=shell, **kwargs)
53 self.shell.observe(
56 self.shell.observe(
54 self._on_ipython_dir_changed, names=('ipython_dir',)
57 self._on_ipython_dir_changed, names=('ipython_dir',)
55 )
58 )
56 self.loaded = set()
59 self.loaded = set()
57
60
58 @property
61 @property
59 def ipython_extension_dir(self):
62 def ipython_extension_dir(self):
60 return os.path.join(self.shell.ipython_dir, u'extensions')
63 return os.path.join(self.shell.ipython_dir, u'extensions')
61
64
62 def _on_ipython_dir_changed(self, change):
65 def _on_ipython_dir_changed(self, change):
63 ensure_dir_exists(self.ipython_extension_dir)
66 ensure_dir_exists(self.ipython_extension_dir)
64
67
65 def load_extension(self, module_str):
68 def load_extension(self, module_str: str):
66 """Load an IPython extension by its module name.
69 """Load an IPython extension by its module name.
67
70
68 Returns the string "already loaded" if the extension is already loaded,
71 Returns the string "already loaded" if the extension is already loaded,
69 "no load function" if the module doesn't have a load_ipython_extension
72 "no load function" if the module doesn't have a load_ipython_extension
70 function, or None if it succeeded.
73 function, or None if it succeeded.
71 """
74 """
75 try:
76 return self._load_extension(module_str)
77 except ModuleNotFoundError:
78 if module_str in BUILTINS_EXTS:
79 BUILTINS_EXTS[module_str] = True
80 return self._load_extension("IPython.extensions." + module_str)
81 raise
82
83 def _load_extension(self, module_str: str):
72 if module_str in self.loaded:
84 if module_str in self.loaded:
73 return "already loaded"
85 return "already loaded"
74
86
75 from IPython.utils.syspathcontext import prepended_to_syspath
87 from IPython.utils.syspathcontext import prepended_to_syspath
76
88
77 with self.shell.builtin_trap:
89 with self.shell.builtin_trap:
78 if module_str not in sys.modules:
90 if module_str not in sys.modules:
79 with prepended_to_syspath(self.ipython_extension_dir):
91 with prepended_to_syspath(self.ipython_extension_dir):
80 mod = import_module(module_str)
92 mod = import_module(module_str)
81 if mod.__file__.startswith(self.ipython_extension_dir):
93 if mod.__file__.startswith(self.ipython_extension_dir):
82 print(("Loading extensions from {dir} is deprecated. "
94 print(("Loading extensions from {dir} is deprecated. "
83 "We recommend managing extensions like any "
95 "We recommend managing extensions like any "
84 "other Python packages, in site-packages.").format(
96 "other Python packages, in site-packages.").format(
85 dir=compress_user(self.ipython_extension_dir)))
97 dir=compress_user(self.ipython_extension_dir)))
86 mod = sys.modules[module_str]
98 mod = sys.modules[module_str]
87 if self._call_load_ipython_extension(mod):
99 if self._call_load_ipython_extension(mod):
88 self.loaded.add(module_str)
100 self.loaded.add(module_str)
89 else:
101 else:
90 return "no load function"
102 return "no load function"
91
103
92 def unload_extension(self, module_str):
104 def unload_extension(self, module_str: str):
93 """Unload an IPython extension by its module name.
105 """Unload an IPython extension by its module name.
94
106
95 This function looks up the extension's name in ``sys.modules`` and
107 This function looks up the extension's name in ``sys.modules`` and
96 simply calls ``mod.unload_ipython_extension(self)``.
108 simply calls ``mod.unload_ipython_extension(self)``.
97
109
98 Returns the string "no unload function" if the extension doesn't define
110 Returns the string "no unload function" if the extension doesn't define
99 a function to unload itself, "not loaded" if the extension isn't loaded,
111 a function to unload itself, "not loaded" if the extension isn't loaded,
100 otherwise None.
112 otherwise None.
101 """
113 """
114 if BUILTINS_EXTS.get(module_str, False) is True:
115 module_str = "IPython.extensions." + module_str
102 if module_str not in self.loaded:
116 if module_str not in self.loaded:
103 return "not loaded"
117 return "not loaded"
104
118
105 if module_str in sys.modules:
119 if module_str in sys.modules:
106 mod = sys.modules[module_str]
120 mod = sys.modules[module_str]
107 if self._call_unload_ipython_extension(mod):
121 if self._call_unload_ipython_extension(mod):
108 self.loaded.discard(module_str)
122 self.loaded.discard(module_str)
109 else:
123 else:
110 return "no unload function"
124 return "no unload function"
111
125
112 def reload_extension(self, module_str):
126 def reload_extension(self, module_str: str):
113 """Reload an IPython extension by calling reload.
127 """Reload an IPython extension by calling reload.
114
128
115 If the module has not been loaded before,
129 If the module has not been loaded before,
116 :meth:`InteractiveShell.load_extension` is called. Otherwise
130 :meth:`InteractiveShell.load_extension` is called. Otherwise
117 :func:`reload` is called and then the :func:`load_ipython_extension`
131 :func:`reload` is called and then the :func:`load_ipython_extension`
118 function of the module, if it exists is called.
132 function of the module, if it exists is called.
119 """
133 """
120 from IPython.utils.syspathcontext import prepended_to_syspath
134 from IPython.utils.syspathcontext import prepended_to_syspath
121
135
136 if BUILTINS_EXTS.get(module_str, False) is True:
137 module_str = "IPython.extensions." + module_str
138
122 if (module_str in self.loaded) and (module_str in sys.modules):
139 if (module_str in self.loaded) and (module_str in sys.modules):
123 self.unload_extension(module_str)
140 self.unload_extension(module_str)
124 mod = sys.modules[module_str]
141 mod = sys.modules[module_str]
125 with prepended_to_syspath(self.ipython_extension_dir):
142 with prepended_to_syspath(self.ipython_extension_dir):
126 reload(mod)
143 reload(mod)
127 if self._call_load_ipython_extension(mod):
144 if self._call_load_ipython_extension(mod):
128 self.loaded.add(module_str)
145 self.loaded.add(module_str)
129 else:
146 else:
130 self.load_extension(module_str)
147 self.load_extension(module_str)
131
148
132 def _call_load_ipython_extension(self, mod):
149 def _call_load_ipython_extension(self, mod):
133 if hasattr(mod, 'load_ipython_extension'):
150 if hasattr(mod, 'load_ipython_extension'):
134 mod.load_ipython_extension(self.shell)
151 mod.load_ipython_extension(self.shell)
135 return True
152 return True
136
153
137 def _call_unload_ipython_extension(self, mod):
154 def _call_unload_ipython_extension(self, mod):
138 if hasattr(mod, 'unload_ipython_extension'):
155 if hasattr(mod, 'unload_ipython_extension'):
139 mod.unload_ipython_extension(self.shell)
156 mod.unload_ipython_extension(self.shell)
140 return True
157 return True
141
158
142 @undoc
159 @undoc
143 def install_extension(self, url, filename=None):
160 def install_extension(self, url, filename=None):
144 """
161 """
145 Deprecated.
162 Deprecated.
146 """
163 """
147 # Ensure the extension directory exists
164 # Ensure the extension directory exists
148 raise DeprecationWarning(
165 raise DeprecationWarning(
149 '`install_extension` and the `install_ext` magic have been deprecated since IPython 4.0'
166 '`install_extension` and the `install_ext` magic have been deprecated since IPython 4.0'
150 'Use pip or other package managers to manage ipython extensions.')
167 'Use pip or other package managers to manage ipython extensions.')
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now