Show More
@@ -1,156 +1,157 b'' | |||
|
1 | 1 | # PYTHON_ARGCOMPLETE_OK |
|
2 | 2 | """ |
|
3 | 3 | IPython: tools for interactive and parallel computing in Python. |
|
4 | 4 | |
|
5 | 5 | https://ipython.org |
|
6 | 6 | """ |
|
7 | 7 | #----------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (c) 2008-2011, IPython Development Team. |
|
9 | 9 | # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu> |
|
10 | 10 | # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> |
|
11 | 11 | # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu> |
|
12 | 12 | # |
|
13 | 13 | # Distributed under the terms of the Modified BSD License. |
|
14 | 14 | # |
|
15 | 15 | # The full license is in the file COPYING.txt, distributed with this software. |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # Imports |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | import sys |
|
23 | 23 | |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | # Setup everything |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 | 28 | # Don't forget to also update setup.py when this changes! |
|
29 | 29 | if sys.version_info < (3, 8): |
|
30 | 30 | raise ImportError( |
|
31 | 31 | """ |
|
32 | 32 | IPython 8+ supports Python 3.8 and above, following NEP 29. |
|
33 | 33 | When using Python 2.7, please install IPython 5.x LTS Long Term Support version. |
|
34 | 34 | Python 3.3 and 3.4 were supported up to IPython 6.x. |
|
35 | 35 | Python 3.5 was supported with IPython 7.0 to 7.9. |
|
36 | 36 | Python 3.6 was supported with IPython up to 7.16. |
|
37 | 37 | Python 3.7 was still supported with the 7.x branch. |
|
38 | 38 | |
|
39 | 39 | See IPython `README.rst` file for more information: |
|
40 | 40 | |
|
41 | 41 | https://github.com/ipython/ipython/blob/main/README.rst |
|
42 | 42 | |
|
43 | 43 | """ |
|
44 | 44 | ) |
|
45 | 45 | |
|
46 | 46 | #----------------------------------------------------------------------------- |
|
47 | 47 | # Setup the top level names |
|
48 | 48 | #----------------------------------------------------------------------------- |
|
49 | 49 | |
|
50 | 50 | from .core.getipython import get_ipython |
|
51 | 51 | from .core import release |
|
52 | 52 | from .core.application import Application |
|
53 | 53 | from .terminal.embed import embed |
|
54 | 54 | |
|
55 | 55 | from .core.interactiveshell import InteractiveShell |
|
56 | 56 | from .utils.sysinfo import sys_info |
|
57 | 57 | from .utils.frame import extract_module_locals |
|
58 | 58 | |
|
59 | 59 | # Release data |
|
60 | 60 | __author__ = '%s <%s>' % (release.author, release.author_email) |
|
61 | 61 | __license__ = release.license |
|
62 | 62 | __version__ = release.version |
|
63 | 63 | version_info = release.version_info |
|
64 | 64 | # list of CVEs that should have been patched in this release. |
|
65 | 65 | # this is informational and should not be relied upon. |
|
66 | 66 | __patched_cves__ = {"CVE-2022-21699", "CVE-2023-24816"} |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def embed_kernel(module=None, local_ns=None, **kwargs): |
|
70 | 70 | """Embed and start an IPython kernel in a given scope. |
|
71 | 71 | |
|
72 | 72 | If you don't want the kernel to initialize the namespace |
|
73 | 73 | from the scope of the surrounding function, |
|
74 | 74 | and/or you want to load full IPython configuration, |
|
75 | 75 | you probably want `IPython.start_kernel()` instead. |
|
76 | 76 | |
|
77 | 77 | Parameters |
|
78 | 78 | ---------- |
|
79 | 79 | module : types.ModuleType, optional |
|
80 | 80 | The module to load into IPython globals (default: caller) |
|
81 | 81 | local_ns : dict, optional |
|
82 | 82 | The namespace to load into IPython user namespace (default: caller) |
|
83 | 83 | **kwargs : various, optional |
|
84 | 84 | Further keyword args are relayed to the IPKernelApp constructor, |
|
85 | such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`), | |
|
85 | 86 | allowing configuration of the Kernel. Will only have an effect |
|
86 | 87 | on the first embed_kernel call for a given process. |
|
87 | 88 | """ |
|
88 | 89 | |
|
89 | 90 | (caller_module, caller_locals) = extract_module_locals(1) |
|
90 | 91 | if module is None: |
|
91 | 92 | module = caller_module |
|
92 | 93 | if local_ns is None: |
|
93 | 94 | local_ns = caller_locals |
|
94 | 95 | |
|
95 | 96 | # Only import .zmq when we really need it |
|
96 | 97 | from ipykernel.embed import embed_kernel as real_embed_kernel |
|
97 | 98 | real_embed_kernel(module=module, local_ns=local_ns, **kwargs) |
|
98 | 99 | |
|
99 | 100 | def start_ipython(argv=None, **kwargs): |
|
100 | 101 | """Launch a normal IPython instance (as opposed to embedded) |
|
101 | 102 | |
|
102 | 103 | `IPython.embed()` puts a shell in a particular calling scope, |
|
103 | 104 | such as a function or method for debugging purposes, |
|
104 | 105 | which is often not desirable. |
|
105 | 106 | |
|
106 | 107 | `start_ipython()` does full, regular IPython initialization, |
|
107 | 108 | including loading startup files, configuration, etc. |
|
108 | 109 | much of which is skipped by `embed()`. |
|
109 | 110 | |
|
110 | 111 | This is a public API method, and will survive implementation changes. |
|
111 | 112 | |
|
112 | 113 | Parameters |
|
113 | 114 | ---------- |
|
114 | 115 | argv : list or None, optional |
|
115 | 116 | If unspecified or None, IPython will parse command-line options from sys.argv. |
|
116 | 117 | To prevent any command-line parsing, pass an empty list: `argv=[]`. |
|
117 | 118 | user_ns : dict, optional |
|
118 | 119 | specify this dictionary to initialize the IPython user namespace with particular values. |
|
119 | 120 | **kwargs : various, optional |
|
120 | 121 | Any other kwargs will be passed to the Application constructor, |
|
121 | such as `config`. | |
|
122 | such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`). | |
|
122 | 123 | """ |
|
123 | 124 | from IPython.terminal.ipapp import launch_new_instance |
|
124 | 125 | return launch_new_instance(argv=argv, **kwargs) |
|
125 | 126 | |
|
126 | 127 | def start_kernel(argv=None, **kwargs): |
|
127 | 128 | """Launch a normal IPython kernel instance (as opposed to embedded) |
|
128 | 129 | |
|
129 | 130 | `IPython.embed_kernel()` puts a shell in a particular calling scope, |
|
130 | 131 | such as a function or method for debugging purposes, |
|
131 | 132 | which is often not desirable. |
|
132 | 133 | |
|
133 | 134 | `start_kernel()` does full, regular IPython initialization, |
|
134 | 135 | including loading startup files, configuration, etc. |
|
135 | 136 | much of which is skipped by `embed_kernel()`. |
|
136 | 137 | |
|
137 | 138 | Parameters |
|
138 | 139 | ---------- |
|
139 | 140 | argv : list or None, optional |
|
140 | 141 | If unspecified or None, IPython will parse command-line options from sys.argv. |
|
141 | 142 | To prevent any command-line parsing, pass an empty list: `argv=[]`. |
|
142 | 143 | user_ns : dict, optional |
|
143 | 144 | specify this dictionary to initialize the IPython user namespace with particular values. |
|
144 | 145 | **kwargs : various, optional |
|
145 | 146 | Any other kwargs will be passed to the Application constructor, |
|
146 | such as `config`. | |
|
147 | such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`). | |
|
147 | 148 | """ |
|
148 | 149 | import warnings |
|
149 | 150 | |
|
150 | 151 | warnings.warn( |
|
151 | 152 | "start_kernel is deprecated since IPython 8.0, use from `ipykernel.kernelapp.launch_new_instance`", |
|
152 | 153 | DeprecationWarning, |
|
153 | 154 | stacklevel=2, |
|
154 | 155 | ) |
|
155 | 156 | from ipykernel.kernelapp import launch_new_instance |
|
156 | 157 | return launch_new_instance(argv=argv, **kwargs) |
@@ -1,410 +1,410 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | An embedded IPython shell. |
|
4 | 4 | """ |
|
5 | 5 | # Copyright (c) IPython Development Team. |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | import sys |
|
10 | 10 | import warnings |
|
11 | 11 | |
|
12 | 12 | from IPython.core import ultratb, compilerop |
|
13 | 13 | from IPython.core import magic_arguments |
|
14 | 14 | from IPython.core.magic import Magics, magics_class, line_magic |
|
15 | 15 | from IPython.core.interactiveshell import DummyMod, InteractiveShell |
|
16 | 16 | from IPython.terminal.interactiveshell import TerminalInteractiveShell |
|
17 | 17 | from IPython.terminal.ipapp import load_default_config |
|
18 | 18 | |
|
19 | 19 | from traitlets import Bool, CBool, Unicode |
|
20 | 20 | from IPython.utils.io import ask_yes_no |
|
21 | 21 | |
|
22 | 22 | from typing import Set |
|
23 | 23 | |
|
24 | 24 | class KillEmbedded(Exception):pass |
|
25 | 25 | |
|
26 | 26 | # kept for backward compatibility as IPython 6 was released with |
|
27 | 27 | # the typo. See https://github.com/ipython/ipython/pull/10706 |
|
28 | 28 | KillEmbeded = KillEmbedded |
|
29 | 29 | |
|
30 | 30 | # This is an additional magic that is exposed in embedded shells. |
|
31 | 31 | @magics_class |
|
32 | 32 | class EmbeddedMagics(Magics): |
|
33 | 33 | |
|
34 | 34 | @line_magic |
|
35 | 35 | @magic_arguments.magic_arguments() |
|
36 | 36 | @magic_arguments.argument('-i', '--instance', action='store_true', |
|
37 | 37 | help='Kill instance instead of call location') |
|
38 | 38 | @magic_arguments.argument('-x', '--exit', action='store_true', |
|
39 | 39 | help='Also exit the current session') |
|
40 | 40 | @magic_arguments.argument('-y', '--yes', action='store_true', |
|
41 | 41 | help='Do not ask confirmation') |
|
42 | 42 | def kill_embedded(self, parameter_s=''): |
|
43 | 43 | """%kill_embedded : deactivate for good the current embedded IPython |
|
44 | 44 | |
|
45 | 45 | This function (after asking for confirmation) sets an internal flag so |
|
46 | 46 | that an embedded IPython will never activate again for the given call |
|
47 | 47 | location. This is useful to permanently disable a shell that is being |
|
48 | 48 | called inside a loop: once you've figured out what you needed from it, |
|
49 | 49 | you may then kill it and the program will then continue to run without |
|
50 | 50 | the interactive shell interfering again. |
|
51 | 51 | |
|
52 | 52 | Kill Instance Option: |
|
53 | 53 | |
|
54 | 54 | If for some reasons you need to kill the location where the instance |
|
55 | 55 | is created and not called, for example if you create a single |
|
56 | 56 | instance in one place and debug in many locations, you can use the |
|
57 | 57 | ``--instance`` option to kill this specific instance. Like for the |
|
58 | 58 | ``call location`` killing an "instance" should work even if it is |
|
59 | 59 | recreated within a loop. |
|
60 | 60 | |
|
61 | 61 | .. note:: |
|
62 | 62 | |
|
63 | 63 | This was the default behavior before IPython 5.2 |
|
64 | 64 | |
|
65 | 65 | """ |
|
66 | 66 | |
|
67 | 67 | args = magic_arguments.parse_argstring(self.kill_embedded, parameter_s) |
|
68 | 68 | print(args) |
|
69 | 69 | if args.instance: |
|
70 | 70 | # let no ask |
|
71 | 71 | if not args.yes: |
|
72 | 72 | kill = ask_yes_no( |
|
73 | 73 | "Are you sure you want to kill this embedded instance? [y/N] ", 'n') |
|
74 | 74 | else: |
|
75 | 75 | kill = True |
|
76 | 76 | if kill: |
|
77 | 77 | self.shell._disable_init_location() |
|
78 | 78 | print("This embedded IPython instance will not reactivate anymore " |
|
79 | 79 | "once you exit.") |
|
80 | 80 | else: |
|
81 | 81 | if not args.yes: |
|
82 | 82 | kill = ask_yes_no( |
|
83 | 83 | "Are you sure you want to kill this embedded call_location? [y/N] ", 'n') |
|
84 | 84 | else: |
|
85 | 85 | kill = True |
|
86 | 86 | if kill: |
|
87 | 87 | self.shell.embedded_active = False |
|
88 | 88 | print("This embedded IPython call location will not reactivate anymore " |
|
89 | 89 | "once you exit.") |
|
90 | 90 | |
|
91 | 91 | if args.exit: |
|
92 | 92 | # Ask-exit does not really ask, it just set internals flags to exit |
|
93 | 93 | # on next loop. |
|
94 | 94 | self.shell.ask_exit() |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | @line_magic |
|
98 | 98 | def exit_raise(self, parameter_s=''): |
|
99 | 99 | """%exit_raise Make the current embedded kernel exit and raise and exception. |
|
100 | 100 | |
|
101 | 101 | This function sets an internal flag so that an embedded IPython will |
|
102 | 102 | raise a `IPython.terminal.embed.KillEmbedded` Exception on exit, and then exit the current I. This is |
|
103 | 103 | useful to permanently exit a loop that create IPython embed instance. |
|
104 | 104 | """ |
|
105 | 105 | |
|
106 | 106 | self.shell.should_raise = True |
|
107 | 107 | self.shell.ask_exit() |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | class _Sentinel: |
|
111 | 111 | def __init__(self, repr): |
|
112 | 112 | assert isinstance(repr, str) |
|
113 | 113 | self.repr = repr |
|
114 | 114 | |
|
115 | 115 | def __repr__(self): |
|
116 | 116 | return repr |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | class InteractiveShellEmbed(TerminalInteractiveShell): |
|
120 | 120 | |
|
121 | 121 | dummy_mode = Bool(False) |
|
122 | 122 | exit_msg = Unicode('') |
|
123 | 123 | embedded = CBool(True) |
|
124 | 124 | should_raise = CBool(False) |
|
125 | 125 | # Like the base class display_banner is not configurable, but here it |
|
126 | 126 | # is True by default. |
|
127 | 127 | display_banner = CBool(True) |
|
128 | 128 | exit_msg = Unicode() |
|
129 | 129 | |
|
130 | 130 | # When embedding, by default we don't change the terminal title |
|
131 | 131 | term_title = Bool(False, |
|
132 | 132 | help="Automatically set the terminal title" |
|
133 | 133 | ).tag(config=True) |
|
134 | 134 | |
|
135 | 135 | _inactive_locations: Set[str] = set() |
|
136 | 136 | |
|
137 | 137 | def _disable_init_location(self): |
|
138 | 138 | """Disable the current Instance creation location""" |
|
139 | 139 | InteractiveShellEmbed._inactive_locations.add(self._init_location_id) |
|
140 | 140 | |
|
141 | 141 | @property |
|
142 | 142 | def embedded_active(self): |
|
143 | 143 | return (self._call_location_id not in InteractiveShellEmbed._inactive_locations)\ |
|
144 | 144 | and (self._init_location_id not in InteractiveShellEmbed._inactive_locations) |
|
145 | 145 | |
|
146 | 146 | @embedded_active.setter |
|
147 | 147 | def embedded_active(self, value): |
|
148 | 148 | if value: |
|
149 | 149 | InteractiveShellEmbed._inactive_locations.discard( |
|
150 | 150 | self._call_location_id) |
|
151 | 151 | InteractiveShellEmbed._inactive_locations.discard( |
|
152 | 152 | self._init_location_id) |
|
153 | 153 | else: |
|
154 | 154 | InteractiveShellEmbed._inactive_locations.add( |
|
155 | 155 | self._call_location_id) |
|
156 | 156 | |
|
157 | 157 | def __init__(self, **kw): |
|
158 | 158 | assert ( |
|
159 | 159 | "user_global_ns" not in kw |
|
160 | 160 | ), "Key word argument `user_global_ns` has been replaced by `user_module` since IPython 4.0." |
|
161 | 161 | |
|
162 | 162 | clid = kw.pop('_init_location_id', None) |
|
163 | 163 | if not clid: |
|
164 | 164 | frame = sys._getframe(1) |
|
165 | 165 | clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno) |
|
166 | 166 | self._init_location_id = clid |
|
167 | 167 | |
|
168 | 168 | super(InteractiveShellEmbed,self).__init__(**kw) |
|
169 | 169 | |
|
170 | 170 | # don't use the ipython crash handler so that user exceptions aren't |
|
171 | 171 | # trapped |
|
172 | 172 | sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors, |
|
173 | 173 | mode=self.xmode, |
|
174 | 174 | call_pdb=self.pdb) |
|
175 | 175 | |
|
176 | 176 | def init_sys_modules(self): |
|
177 | 177 | """ |
|
178 | 178 | Explicitly overwrite :mod:`IPython.core.interactiveshell` to do nothing. |
|
179 | 179 | """ |
|
180 | 180 | pass |
|
181 | 181 | |
|
182 | 182 | def init_magics(self): |
|
183 | 183 | super(InteractiveShellEmbed, self).init_magics() |
|
184 | 184 | self.register_magics(EmbeddedMagics) |
|
185 | 185 | |
|
186 | 186 | def __call__( |
|
187 | 187 | self, |
|
188 | 188 | header="", |
|
189 | 189 | local_ns=None, |
|
190 | 190 | module=None, |
|
191 | 191 | dummy=None, |
|
192 | 192 | stack_depth=1, |
|
193 | 193 | compile_flags=None, |
|
194 | 194 | **kw |
|
195 | 195 | ): |
|
196 | 196 | """Activate the interactive interpreter. |
|
197 | 197 | |
|
198 | 198 | __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start |
|
199 | 199 | the interpreter shell with the given local and global namespaces, and |
|
200 | 200 | optionally print a header string at startup. |
|
201 | 201 | |
|
202 | 202 | The shell can be globally activated/deactivated using the |
|
203 | 203 | dummy_mode attribute. This allows you to turn off a shell used |
|
204 | 204 | for debugging globally. |
|
205 | 205 | |
|
206 | 206 | However, *each* time you call the shell you can override the current |
|
207 | 207 | state of dummy_mode with the optional keyword parameter 'dummy'. For |
|
208 | 208 | example, if you set dummy mode on with IPShell.dummy_mode = True, you |
|
209 | 209 | can still have a specific call work by making it as IPShell(dummy=False). |
|
210 | 210 | """ |
|
211 | 211 | |
|
212 | 212 | # we are called, set the underlying interactiveshell not to exit. |
|
213 | 213 | self.keep_running = True |
|
214 | 214 | |
|
215 | 215 | # If the user has turned it off, go away |
|
216 | 216 | clid = kw.pop('_call_location_id', None) |
|
217 | 217 | if not clid: |
|
218 | 218 | frame = sys._getframe(1) |
|
219 | 219 | clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno) |
|
220 | 220 | self._call_location_id = clid |
|
221 | 221 | |
|
222 | 222 | if not self.embedded_active: |
|
223 | 223 | return |
|
224 | 224 | |
|
225 | 225 | # Normal exits from interactive mode set this flag, so the shell can't |
|
226 | 226 | # re-enter (it checks this variable at the start of interactive mode). |
|
227 | 227 | self.exit_now = False |
|
228 | 228 | |
|
229 | 229 | # Allow the dummy parameter to override the global __dummy_mode |
|
230 | 230 | if dummy or (dummy != 0 and self.dummy_mode): |
|
231 | 231 | return |
|
232 | 232 | |
|
233 | 233 | # self.banner is auto computed |
|
234 | 234 | if header: |
|
235 | 235 | self.old_banner2 = self.banner2 |
|
236 | 236 | self.banner2 = self.banner2 + '\n' + header + '\n' |
|
237 | 237 | else: |
|
238 | 238 | self.old_banner2 = '' |
|
239 | 239 | |
|
240 | 240 | if self.display_banner: |
|
241 | 241 | self.show_banner() |
|
242 | 242 | |
|
243 | 243 | # Call the embedding code with a stack depth of 1 so it can skip over |
|
244 | 244 | # our call and get the original caller's namespaces. |
|
245 | 245 | self.mainloop( |
|
246 | 246 | local_ns, module, stack_depth=stack_depth, compile_flags=compile_flags |
|
247 | 247 | ) |
|
248 | 248 | |
|
249 | 249 | self.banner2 = self.old_banner2 |
|
250 | 250 | |
|
251 | 251 | if self.exit_msg is not None: |
|
252 | 252 | print(self.exit_msg) |
|
253 | 253 | |
|
254 | 254 | if self.should_raise: |
|
255 | 255 | raise KillEmbedded('Embedded IPython raising error, as user requested.') |
|
256 | 256 | |
|
257 | 257 | def mainloop( |
|
258 | 258 | self, |
|
259 | 259 | local_ns=None, |
|
260 | 260 | module=None, |
|
261 | 261 | stack_depth=0, |
|
262 | 262 | compile_flags=None, |
|
263 | 263 | ): |
|
264 | 264 | """Embeds IPython into a running python program. |
|
265 | 265 | |
|
266 | 266 | Parameters |
|
267 | 267 | ---------- |
|
268 | 268 | local_ns, module |
|
269 | 269 | Working local namespace (a dict) and module (a module or similar |
|
270 | 270 | object). If given as None, they are automatically taken from the scope |
|
271 | 271 | where the shell was called, so that program variables become visible. |
|
272 | 272 | stack_depth : int |
|
273 | 273 | How many levels in the stack to go to looking for namespaces (when |
|
274 | 274 | local_ns or module is None). This allows an intermediate caller to |
|
275 | 275 | make sure that this function gets the namespace from the intended |
|
276 | 276 | level in the stack. By default (0) it will get its locals and globals |
|
277 | 277 | from the immediate caller. |
|
278 | 278 | compile_flags |
|
279 | 279 | A bit field identifying the __future__ features |
|
280 | 280 | that are enabled, as passed to the builtin :func:`compile` function. |
|
281 | 281 | If given as None, they are automatically taken from the scope where |
|
282 | 282 | the shell was called. |
|
283 | 283 | |
|
284 | 284 | """ |
|
285 | 285 | |
|
286 | 286 | # Get locals and globals from caller |
|
287 | 287 | if ((local_ns is None or module is None or compile_flags is None) |
|
288 | 288 | and self.default_user_namespaces): |
|
289 | 289 | call_frame = sys._getframe(stack_depth).f_back |
|
290 | 290 | |
|
291 | 291 | if local_ns is None: |
|
292 | 292 | local_ns = call_frame.f_locals |
|
293 | 293 | if module is None: |
|
294 | 294 | global_ns = call_frame.f_globals |
|
295 | 295 | try: |
|
296 | 296 | module = sys.modules[global_ns['__name__']] |
|
297 | 297 | except KeyError: |
|
298 | 298 | warnings.warn("Failed to get module %s" % \ |
|
299 | 299 | global_ns.get('__name__', 'unknown module') |
|
300 | 300 | ) |
|
301 | 301 | module = DummyMod() |
|
302 | 302 | module.__dict__ = global_ns |
|
303 | 303 | if compile_flags is None: |
|
304 | 304 | compile_flags = (call_frame.f_code.co_flags & |
|
305 | 305 | compilerop.PyCF_MASK) |
|
306 | 306 | |
|
307 | 307 | # Save original namespace and module so we can restore them after |
|
308 | 308 | # embedding; otherwise the shell doesn't shut down correctly. |
|
309 | 309 | orig_user_module = self.user_module |
|
310 | 310 | orig_user_ns = self.user_ns |
|
311 | 311 | orig_compile_flags = self.compile.flags |
|
312 | 312 | |
|
313 | 313 | # Update namespaces and fire up interpreter |
|
314 | 314 | |
|
315 | 315 | # The global one is easy, we can just throw it in |
|
316 | 316 | if module is not None: |
|
317 | 317 | self.user_module = module |
|
318 | 318 | |
|
319 | 319 | # But the user/local one is tricky: ipython needs it to store internal |
|
320 | 320 | # data, but we also need the locals. We'll throw our hidden variables |
|
321 | 321 | # like _ih and get_ipython() into the local namespace, but delete them |
|
322 | 322 | # later. |
|
323 | 323 | if local_ns is not None: |
|
324 | 324 | reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()} |
|
325 | 325 | self.user_ns = reentrant_local_ns |
|
326 | 326 | self.init_user_ns() |
|
327 | 327 | |
|
328 | 328 | # Compiler flags |
|
329 | 329 | if compile_flags is not None: |
|
330 | 330 | self.compile.flags = compile_flags |
|
331 | 331 | |
|
332 | 332 | # make sure the tab-completer has the correct frame information, so it |
|
333 | 333 | # actually completes using the frame's locals/globals |
|
334 | 334 | self.set_completer_frame() |
|
335 | 335 | |
|
336 | 336 | with self.builtin_trap, self.display_trap: |
|
337 | 337 | self.interact() |
|
338 | 338 | |
|
339 | 339 | # now, purge out the local namespace of IPython's hidden variables. |
|
340 | 340 | if local_ns is not None: |
|
341 | 341 | local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()}) |
|
342 | 342 | |
|
343 | 343 | |
|
344 | 344 | # Restore original namespace so shell can shut down when we exit. |
|
345 | 345 | self.user_module = orig_user_module |
|
346 | 346 | self.user_ns = orig_user_ns |
|
347 | 347 | self.compile.flags = orig_compile_flags |
|
348 | 348 | |
|
349 | 349 | |
|
350 | 350 | def embed(*, header="", compile_flags=None, **kwargs): |
|
351 | 351 | """Call this to embed IPython at the current point in your program. |
|
352 | 352 | |
|
353 | 353 | The first invocation of this will create an :class:`InteractiveShellEmbed` |
|
354 | 354 | instance and then call it. Consecutive calls just call the already |
|
355 | 355 | created instance. |
|
356 | 356 | |
|
357 | 357 | If you don't want the kernel to initialize the namespace |
|
358 | 358 | from the scope of the surrounding function, |
|
359 | 359 | and/or you want to load full IPython configuration, |
|
360 | 360 | you probably want `IPython.start_ipython()` instead. |
|
361 | 361 | |
|
362 | 362 | Here is a simple example:: |
|
363 | 363 | |
|
364 | 364 | from IPython import embed |
|
365 | 365 | a = 10 |
|
366 | 366 | b = 20 |
|
367 | 367 | embed(header='First time') |
|
368 | 368 | c = 30 |
|
369 | 369 | d = 40 |
|
370 | 370 | embed() |
|
371 | 371 | |
|
372 | Full customization can be done by passing a :class:`Config` in as the | |
|
373 | config argument. | |
|
372 | Full customization can be done by passing a traitlets :class:`Config` in as the | |
|
373 | `config` argument (see :ref:`configure_start_ipython`). | |
|
374 | 374 | """ |
|
375 | 375 | config = kwargs.get('config') |
|
376 | 376 | if config is None: |
|
377 | 377 | config = load_default_config() |
|
378 | 378 | config.InteractiveShellEmbed = config.TerminalInteractiveShell |
|
379 | 379 | kwargs['config'] = config |
|
380 | 380 | using = kwargs.get('using', 'sync') |
|
381 | 381 | if using : |
|
382 | 382 | kwargs['config'].update({'TerminalInteractiveShell':{'loop_runner':using, 'colors':'NoColor', 'autoawait': using!='sync'}}) |
|
383 | 383 | #save ps1/ps2 if defined |
|
384 | 384 | ps1 = None |
|
385 | 385 | ps2 = None |
|
386 | 386 | try: |
|
387 | 387 | ps1 = sys.ps1 |
|
388 | 388 | ps2 = sys.ps2 |
|
389 | 389 | except AttributeError: |
|
390 | 390 | pass |
|
391 | 391 | #save previous instance |
|
392 | 392 | saved_shell_instance = InteractiveShell._instance |
|
393 | 393 | if saved_shell_instance is not None: |
|
394 | 394 | cls = type(saved_shell_instance) |
|
395 | 395 | cls.clear_instance() |
|
396 | 396 | frame = sys._getframe(1) |
|
397 | 397 | shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % ( |
|
398 | 398 | frame.f_code.co_filename, frame.f_lineno), **kwargs) |
|
399 | 399 | shell(header=header, stack_depth=2, compile_flags=compile_flags, |
|
400 | 400 | _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno)) |
|
401 | 401 | InteractiveShellEmbed.clear_instance() |
|
402 | 402 | #restore previous instance |
|
403 | 403 | if saved_shell_instance is not None: |
|
404 | 404 | cls = type(saved_shell_instance) |
|
405 | 405 | cls.clear_instance() |
|
406 | 406 | for subclass in cls._walk_mro(): |
|
407 | 407 | subclass._instance = saved_shell_instance |
|
408 | 408 | if ps1 is not None: |
|
409 | 409 | sys.ps1 = ps1 |
|
410 | 410 | sys.ps2 = ps2 |
@@ -1,235 +1,235 b'' | |||
|
1 | 1 | ===================================== |
|
2 | 2 | Introduction to IPython configuration |
|
3 | 3 | ===================================== |
|
4 | 4 | |
|
5 | 5 | .. _setting_config: |
|
6 | 6 | |
|
7 | 7 | Setting configurable options |
|
8 | 8 | ============================ |
|
9 | 9 | |
|
10 | 10 | Many of IPython's classes have configurable attributes (see |
|
11 | 11 | :doc:`options/index` for the list). These can be |
|
12 | 12 | configured in several ways. |
|
13 | 13 | |
|
14 | 14 | Python configuration files |
|
15 | 15 | -------------------------- |
|
16 | 16 | |
|
17 | 17 | To create the blank configuration files, run:: |
|
18 | 18 | |
|
19 | 19 | ipython profile create [profilename] |
|
20 | 20 | |
|
21 | 21 | If you leave out the profile name, the files will be created for the |
|
22 | 22 | ``default`` profile (see :ref:`profiles`). These will typically be located in |
|
23 | 23 | :file:`~/.ipython/profile_default/`, and will be named |
|
24 | 24 | :file:`ipython_config.py`, for historical reasons you may also find files |
|
25 | 25 | named with IPython prefix instead of Jupyter: |
|
26 | 26 | :file:`ipython_notebook_config.py`, etc. The settings in |
|
27 | 27 | :file:`ipython_config.py` apply to all IPython commands. |
|
28 | 28 | |
|
29 | 29 | By default, configuration files are fully featured Python scripts that can |
|
30 | 30 | execute arbitrary code, the main usage is to set value on the configuration |
|
31 | 31 | object ``c`` which exist in your configuration file. |
|
32 | 32 | |
|
33 | 33 | You can then configure class attributes like this:: |
|
34 | 34 | |
|
35 | 35 | c.InteractiveShell.automagic = False |
|
36 | 36 | |
|
37 | 37 | Be careful with spelling--incorrect names will simply be ignored, with |
|
38 | 38 | no error. |
|
39 | 39 | |
|
40 | 40 | To add to a collection which may have already been defined elsewhere or have |
|
41 | 41 | default values, you can use methods like those found on lists, dicts and |
|
42 | 42 | sets: append, extend, :meth:`~traitlets.config.LazyConfigValue.prepend` (like |
|
43 | 43 | extend, but at the front), add and update (which works both for dicts and |
|
44 | 44 | sets):: |
|
45 | 45 | |
|
46 | 46 | c.InteractiveShellApp.extensions.append('Cython') |
|
47 | 47 | |
|
48 | 48 | .. versionadded:: 2.0 |
|
49 | 49 | list, dict and set methods for config values |
|
50 | 50 | |
|
51 | 51 | Example configuration file |
|
52 | 52 | `````````````````````````` |
|
53 | 53 | |
|
54 | 54 | :: |
|
55 | 55 | |
|
56 | 56 | # sample ipython_config.py |
|
57 | 57 | |
|
58 | 58 | c.TerminalIPythonApp.display_banner = True |
|
59 | 59 | c.InteractiveShellApp.log_level = 20 |
|
60 | 60 | c.InteractiveShellApp.extensions = [ |
|
61 | 61 | 'myextension' |
|
62 | 62 | ] |
|
63 | 63 | c.InteractiveShellApp.exec_lines = [ |
|
64 | 64 | 'import numpy', |
|
65 | 65 | 'import scipy' |
|
66 | 66 | ] |
|
67 | 67 | c.InteractiveShellApp.exec_files = [ |
|
68 | 68 | 'mycode.py', |
|
69 | 69 | 'fancy.ipy' |
|
70 | 70 | ] |
|
71 | 71 | c.InteractiveShell.colors = 'LightBG' |
|
72 | 72 | c.InteractiveShell.xmode = 'Context' |
|
73 | 73 | c.TerminalInteractiveShell.confirm_exit = False |
|
74 | 74 | c.TerminalInteractiveShell.editor = 'nano' |
|
75 | 75 | |
|
76 | 76 | c.PrefilterManager.multi_line_specials = True |
|
77 | 77 | |
|
78 | 78 | c.AliasManager.user_aliases = [ |
|
79 | 79 | ('la', 'ls -al') |
|
80 | 80 | ] |
|
81 | 81 | |
|
82 | 82 | JSON Configuration files |
|
83 | 83 | ------------------------ |
|
84 | 84 | |
|
85 | 85 | In case where executability of configuration can be problematic, or |
|
86 | 86 | configurations need to be modified programmatically, IPython also support a |
|
87 | 87 | limited set of functionalities via ``.json`` configuration files. |
|
88 | 88 | |
|
89 | 89 | You can defined most of the configuration options via a json object which |
|
90 | 90 | hierarchy represent the value you would normally set on the ``c`` object of |
|
91 | 91 | ``.py`` configuration files. The following ``ipython_config.json`` file:: |
|
92 | 92 | |
|
93 | 93 | { |
|
94 | 94 | "InteractiveShell": { |
|
95 | 95 | "colors": "LightBG", |
|
96 | 96 | }, |
|
97 | 97 | "InteractiveShellApp": { |
|
98 | 98 | "extensions": [ |
|
99 | 99 | "myextension" |
|
100 | 100 | ] |
|
101 | 101 | }, |
|
102 | 102 | "TerminalInteractiveShell": { |
|
103 | 103 | "editor": "nano" |
|
104 | 104 | } |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | Is equivalent to the following ``ipython_config.py``:: |
|
108 | 108 | |
|
109 | 109 | c.InteractiveShellApp.extensions = [ |
|
110 | 110 | 'myextension' |
|
111 | 111 | ] |
|
112 | 112 | |
|
113 | 113 | c.InteractiveShell.colors = 'LightBG' |
|
114 | 114 | c.TerminalInteractiveShell.editor = 'nano' |
|
115 | 115 | |
|
116 | 116 | |
|
117 | 117 | Command line arguments |
|
118 | 118 | ---------------------- |
|
119 | 119 | |
|
120 | 120 | Every configurable value can be set from the command line, using this |
|
121 | 121 | syntax:: |
|
122 | 122 | |
|
123 | 123 | ipython --ClassName.attribute=value |
|
124 | 124 | |
|
125 | 125 | Many frequently used options have short aliases and flags, such as |
|
126 | 126 | ``--matplotlib`` (to integrate with a matplotlib GUI event loop) or |
|
127 | 127 | ``--pdb`` (automatic post-mortem debugging of exceptions). |
|
128 | 128 | |
|
129 | 129 | To see all of these abbreviated options, run:: |
|
130 | 130 | |
|
131 | 131 | ipython --help |
|
132 | 132 | jupyter notebook --help |
|
133 | 133 | # etc. |
|
134 | 134 | |
|
135 | 135 | Options specified at the command line, in either format, override |
|
136 | 136 | options set in a configuration file. |
|
137 | 137 | |
|
138 | 138 | The config magic |
|
139 | 139 | ---------------- |
|
140 | 140 | |
|
141 | 141 | You can also modify config from inside IPython, using a magic command:: |
|
142 | 142 | |
|
143 | 143 | %config IPCompleter.greedy = True |
|
144 | 144 | |
|
145 | 145 | At present, this only affects the current session - changes you make to |
|
146 | 146 | config are not saved anywhere. Also, some options are only read when |
|
147 | 147 | IPython starts, so they can't be changed like this. |
|
148 | 148 | |
|
149 | 149 | .. _configure_start_ipython: |
|
150 | 150 | |
|
151 | 151 | Running IPython from Python |
|
152 | 152 | ---------------------------- |
|
153 | 153 | |
|
154 | 154 | If you are using :ref:`embedding` to start IPython from a normal |
|
155 | 155 | python file, you can set configuration options the same way as in a |
|
156 | config file by creating a traitlets config object and passing it to | |
|
156 | config file by creating a traitlets :class:`Config` object and passing it to | |
|
157 | 157 | start_ipython like in the example below. |
|
158 | 158 | |
|
159 | 159 | .. literalinclude:: ../../../examples/Embedding/start_ipython_config.py |
|
160 | 160 | :language: python |
|
161 | 161 | |
|
162 | 162 | .. _profiles: |
|
163 | 163 | |
|
164 | 164 | Profiles |
|
165 | 165 | ======== |
|
166 | 166 | |
|
167 | 167 | IPython can use multiple profiles, with separate configuration and |
|
168 | 168 | history. By default, if you don't specify a profile, IPython always runs |
|
169 | 169 | in the ``default`` profile. To use a new profile:: |
|
170 | 170 | |
|
171 | 171 | ipython profile create foo # create the profile foo |
|
172 | 172 | ipython --profile=foo # start IPython using the new profile |
|
173 | 173 | |
|
174 | 174 | Profiles are typically stored in :ref:`ipythondir`, but you can also keep |
|
175 | 175 | a profile in the current working directory, for example to distribute it |
|
176 | 176 | with a project. To find a profile directory on the filesystem:: |
|
177 | 177 | |
|
178 | 178 | ipython locate profile foo |
|
179 | 179 | |
|
180 | 180 | .. _ipythondir: |
|
181 | 181 | |
|
182 | 182 | The IPython directory |
|
183 | 183 | ===================== |
|
184 | 184 | |
|
185 | 185 | IPython stores its files---config, command history and extensions---in |
|
186 | 186 | the directory :file:`~/.ipython/` by default. |
|
187 | 187 | |
|
188 | 188 | .. envvar:: IPYTHONDIR |
|
189 | 189 | |
|
190 | 190 | If set, this environment variable should be the path to a directory, |
|
191 | 191 | which IPython will use for user data. IPython will create it if it |
|
192 | 192 | does not exist. |
|
193 | 193 | |
|
194 | 194 | .. option:: --ipython-dir=<path> |
|
195 | 195 | |
|
196 | 196 | This command line option can also be used to override the default |
|
197 | 197 | IPython directory. |
|
198 | 198 | |
|
199 | 199 | To see where IPython is looking for the IPython directory, use the command |
|
200 | 200 | ``ipython locate``, or the Python function :func:`IPython.paths.get_ipython_dir`. |
|
201 | 201 | |
|
202 | 202 | |
|
203 | 203 | Systemwide configuration |
|
204 | 204 | ======================== |
|
205 | 205 | |
|
206 | 206 | It can be useful to deploy systemwide ipython or ipykernel configuration |
|
207 | 207 | when managing environment for many users. At startup time IPython and |
|
208 | 208 | IPykernel will search for configuration file in multiple systemwide |
|
209 | 209 | locations, mainly: |
|
210 | 210 | |
|
211 | 211 | - ``/etc/ipython/`` |
|
212 | 212 | - ``/usr/local/etc/ipython/`` |
|
213 | 213 | |
|
214 | 214 | When the global install is a standalone python distribution it may also |
|
215 | 215 | search in distribution specific location, for example: |
|
216 | 216 | |
|
217 | 217 | - ``$ANACONDA_LOCATION/etc/ipython/`` |
|
218 | 218 | |
|
219 | 219 | In those locations, Terminal IPython will look for a file called |
|
220 | 220 | ``ipython_config.py`` and ``ipython_config.json``, ipykernel will look for |
|
221 | 221 | ``ipython_kernel_config.py`` and ``ipython_kernel.json``. |
|
222 | 222 | |
|
223 | 223 | Configuration files are loaded in order and merged with configuration on |
|
224 | 224 | later location taking precedence on earlier locations (that is to say a user |
|
225 | 225 | can overwrite a systemwide configuration option). |
|
226 | 226 | |
|
227 | 227 | You can see all locations in which IPython is looking for configuration files |
|
228 | 228 | by starting ipython in debug mode:: |
|
229 | 229 | |
|
230 | 230 | $ ipython --debug -c 'exit()' |
|
231 | 231 | |
|
232 | 232 | Identically with ipykernel though the command is currently blocking until |
|
233 | 233 | this process is killed with ``Ctrl-\``:: |
|
234 | 234 | |
|
235 | 235 | $ python -m ipykernel --debug |
@@ -1,12 +1,12 b'' | |||
|
1 | 1 | =============== |
|
2 | 2 | IPython options |
|
3 | 3 | =============== |
|
4 | 4 | |
|
5 | 5 | Any of the options listed here can be set in config files, at the |
|
6 |
command line, |
|
|
7 | details. | |
|
6 | command line, from inside IPython, or using a traitlets :class:`Config` object. | |
|
7 | See :ref:`setting_config` for details. | |
|
8 | 8 | |
|
9 | 9 | .. toctree:: |
|
10 | 10 | |
|
11 | 11 | terminal |
|
12 | 12 | kernel |
General Comments 0
You need to be logged in to leave comments.
Login now