##// END OF EJS Templates
Reexpose a KillEmbeded alias for backward compat
Matthias Bussonnier -
Show More
@@ -1,392 +1,396 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 An embedded IPython shell.
3 An embedded IPython shell.
4 """
4 """
5 # Copyright (c) IPython Development Team.
5 # Copyright (c) IPython Development Team.
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7
7
8
8
9 import sys
9 import sys
10 import warnings
10 import warnings
11
11
12 from IPython.core import ultratb, compilerop
12 from IPython.core import ultratb, compilerop
13 from IPython.core import magic_arguments
13 from IPython.core import magic_arguments
14 from IPython.core.magic import Magics, magics_class, line_magic
14 from IPython.core.magic import Magics, magics_class, line_magic
15 from IPython.core.interactiveshell import DummyMod, InteractiveShell
15 from IPython.core.interactiveshell import DummyMod, InteractiveShell
16 from IPython.terminal.interactiveshell import TerminalInteractiveShell
16 from IPython.terminal.interactiveshell import TerminalInteractiveShell
17 from IPython.terminal.ipapp import load_default_config
17 from IPython.terminal.ipapp import load_default_config
18
18
19 from traitlets import Bool, CBool, Unicode
19 from traitlets import Bool, CBool, Unicode
20 from IPython.utils.io import ask_yes_no
20 from IPython.utils.io import ask_yes_no
21
21
22 class KillEmbedded(Exception):pass
22 class KillEmbedded(Exception):pass
23
23
24 # kept for backward compatibility as IPython 6 was released with
25 # the typo. See https://github.com/ipython/ipython/pull/10706
26 KillEmbeded = KillEmbedded
27
24 # This is an additional magic that is exposed in embedded shells.
28 # This is an additional magic that is exposed in embedded shells.
25 @magics_class
29 @magics_class
26 class EmbeddedMagics(Magics):
30 class EmbeddedMagics(Magics):
27
31
28 @line_magic
32 @line_magic
29 @magic_arguments.magic_arguments()
33 @magic_arguments.magic_arguments()
30 @magic_arguments.argument('-i', '--instance', action='store_true',
34 @magic_arguments.argument('-i', '--instance', action='store_true',
31 help='Kill instance instead of call location')
35 help='Kill instance instead of call location')
32 @magic_arguments.argument('-x', '--exit', action='store_true',
36 @magic_arguments.argument('-x', '--exit', action='store_true',
33 help='Also exit the current session')
37 help='Also exit the current session')
34 @magic_arguments.argument('-y', '--yes', action='store_true',
38 @magic_arguments.argument('-y', '--yes', action='store_true',
35 help='Do not ask confirmation')
39 help='Do not ask confirmation')
36 def kill_embedded(self, parameter_s=''):
40 def kill_embedded(self, parameter_s=''):
37 """%kill_embedded : deactivate for good the current embedded IPython
41 """%kill_embedded : deactivate for good the current embedded IPython
38
42
39 This function (after asking for confirmation) sets an internal flag so
43 This function (after asking for confirmation) sets an internal flag so
40 that an embedded IPython will never activate again for the given call
44 that an embedded IPython will never activate again for the given call
41 location. This is useful to permanently disable a shell that is being
45 location. This is useful to permanently disable a shell that is being
42 called inside a loop: once you've figured out what you needed from it,
46 called inside a loop: once you've figured out what you needed from it,
43 you may then kill it and the program will then continue to run without
47 you may then kill it and the program will then continue to run without
44 the interactive shell interfering again.
48 the interactive shell interfering again.
45
49
46
50
47 Kill Instance Option:
51 Kill Instance Option:
48
52
49 If for some reasons you need to kill the location where the instance
53 If for some reasons you need to kill the location where the instance
50 is created and not called, for example if you create a single
54 is created and not called, for example if you create a single
51 instance in one place and debug in many locations, you can use the
55 instance in one place and debug in many locations, you can use the
52 ``--instance`` option to kill this specific instance. Like for the
56 ``--instance`` option to kill this specific instance. Like for the
53 ``call location`` killing an "instance" should work even if it is
57 ``call location`` killing an "instance" should work even if it is
54 recreated within a loop.
58 recreated within a loop.
55
59
56 .. note::
60 .. note::
57
61
58 This was the default behavior before IPython 5.2
62 This was the default behavior before IPython 5.2
59
63
60 """
64 """
61
65
62 args = magic_arguments.parse_argstring(self.kill_embedded, parameter_s)
66 args = magic_arguments.parse_argstring(self.kill_embedded, parameter_s)
63 print(args)
67 print(args)
64 if args.instance:
68 if args.instance:
65 # let no ask
69 # let no ask
66 if not args.yes:
70 if not args.yes:
67 kill = ask_yes_no(
71 kill = ask_yes_no(
68 "Are you sure you want to kill this embedded instance? [y/N] ", 'n')
72 "Are you sure you want to kill this embedded instance? [y/N] ", 'n')
69 else:
73 else:
70 kill = True
74 kill = True
71 if kill:
75 if kill:
72 self.shell._disable_init_location()
76 self.shell._disable_init_location()
73 print("This embedded IPython instance will not reactivate anymore "
77 print("This embedded IPython instance will not reactivate anymore "
74 "once you exit.")
78 "once you exit.")
75 else:
79 else:
76 if not args.yes:
80 if not args.yes:
77 kill = ask_yes_no(
81 kill = ask_yes_no(
78 "Are you sure you want to kill this embedded call_location? [y/N] ", 'n')
82 "Are you sure you want to kill this embedded call_location? [y/N] ", 'n')
79 else:
83 else:
80 kill = True
84 kill = True
81 if kill:
85 if kill:
82 self.shell.embedded_active = False
86 self.shell.embedded_active = False
83 print("This embedded IPython call location will not reactivate anymore "
87 print("This embedded IPython call location will not reactivate anymore "
84 "once you exit.")
88 "once you exit.")
85
89
86 if args.exit:
90 if args.exit:
87 # Ask-exit does not really ask, it just set internals flags to exit
91 # Ask-exit does not really ask, it just set internals flags to exit
88 # on next loop.
92 # on next loop.
89 self.shell.ask_exit()
93 self.shell.ask_exit()
90
94
91
95
92 @line_magic
96 @line_magic
93 def exit_raise(self, parameter_s=''):
97 def exit_raise(self, parameter_s=''):
94 """%exit_raise Make the current embedded kernel exit and raise and exception.
98 """%exit_raise Make the current embedded kernel exit and raise and exception.
95
99
96 This function sets an internal flag so that an embedded IPython will
100 This function sets an internal flag so that an embedded IPython will
97 raise a `IPython.terminal.embed.KillEmbedded` Exception on exit, and then exit the current I. This is
101 raise a `IPython.terminal.embed.KillEmbedded` Exception on exit, and then exit the current I. This is
98 useful to permanently exit a loop that create IPython embed instance.
102 useful to permanently exit a loop that create IPython embed instance.
99 """
103 """
100
104
101 self.shell.should_raise = True
105 self.shell.should_raise = True
102 self.shell.ask_exit()
106 self.shell.ask_exit()
103
107
104
108
105
109
106 class InteractiveShellEmbed(TerminalInteractiveShell):
110 class InteractiveShellEmbed(TerminalInteractiveShell):
107
111
108 dummy_mode = Bool(False)
112 dummy_mode = Bool(False)
109 exit_msg = Unicode('')
113 exit_msg = Unicode('')
110 embedded = CBool(True)
114 embedded = CBool(True)
111 should_raise = CBool(False)
115 should_raise = CBool(False)
112 # Like the base class display_banner is not configurable, but here it
116 # Like the base class display_banner is not configurable, but here it
113 # is True by default.
117 # is True by default.
114 display_banner = CBool(True)
118 display_banner = CBool(True)
115 exit_msg = Unicode()
119 exit_msg = Unicode()
116
120
117 # When embedding, by default we don't change the terminal title
121 # When embedding, by default we don't change the terminal title
118 term_title = Bool(False,
122 term_title = Bool(False,
119 help="Automatically set the terminal title"
123 help="Automatically set the terminal title"
120 ).tag(config=True)
124 ).tag(config=True)
121
125
122 _inactive_locations = set()
126 _inactive_locations = set()
123
127
124 @property
128 @property
125 def embedded_active(self):
129 def embedded_active(self):
126 return (self._call_location_id not in InteractiveShellEmbed._inactive_locations)\
130 return (self._call_location_id not in InteractiveShellEmbed._inactive_locations)\
127 and (self._init_location_id not in InteractiveShellEmbed._inactive_locations)
131 and (self._init_location_id not in InteractiveShellEmbed._inactive_locations)
128
132
129 def _disable_init_location(self):
133 def _disable_init_location(self):
130 """Disable the current Instance creation location"""
134 """Disable the current Instance creation location"""
131 InteractiveShellEmbed._inactive_locations.add(self._init_location_id)
135 InteractiveShellEmbed._inactive_locations.add(self._init_location_id)
132
136
133 @embedded_active.setter
137 @embedded_active.setter
134 def embedded_active(self, value):
138 def embedded_active(self, value):
135 if value:
139 if value:
136 InteractiveShellEmbed._inactive_locations.discard(
140 InteractiveShellEmbed._inactive_locations.discard(
137 self._call_location_id)
141 self._call_location_id)
138 InteractiveShellEmbed._inactive_locations.discard(
142 InteractiveShellEmbed._inactive_locations.discard(
139 self._init_location_id)
143 self._init_location_id)
140 else:
144 else:
141 InteractiveShellEmbed._inactive_locations.add(
145 InteractiveShellEmbed._inactive_locations.add(
142 self._call_location_id)
146 self._call_location_id)
143
147
144 def __init__(self, **kw):
148 def __init__(self, **kw):
145 if kw.get('user_global_ns', None) is not None:
149 if kw.get('user_global_ns', None) is not None:
146 raise DeprecationWarning(
150 raise DeprecationWarning(
147 "Key word argument `user_global_ns` has been replaced by `user_module` since IPython 4.0.")
151 "Key word argument `user_global_ns` has been replaced by `user_module` since IPython 4.0.")
148
152
149 clid = kw.pop('_init_location_id', None)
153 clid = kw.pop('_init_location_id', None)
150 if not clid:
154 if not clid:
151 frame = sys._getframe(1)
155 frame = sys._getframe(1)
152 clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
156 clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
153 self._init_location_id = clid
157 self._init_location_id = clid
154
158
155 super(InteractiveShellEmbed,self).__init__(**kw)
159 super(InteractiveShellEmbed,self).__init__(**kw)
156
160
157 # don't use the ipython crash handler so that user exceptions aren't
161 # don't use the ipython crash handler so that user exceptions aren't
158 # trapped
162 # trapped
159 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
163 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
160 mode=self.xmode,
164 mode=self.xmode,
161 call_pdb=self.pdb)
165 call_pdb=self.pdb)
162
166
163 def init_sys_modules(self):
167 def init_sys_modules(self):
164 """
168 """
165 Explicitly overwrite :mod:`IPython.core.interactiveshell` to do nothing.
169 Explicitly overwrite :mod:`IPython.core.interactiveshell` to do nothing.
166 """
170 """
167 pass
171 pass
168
172
169 def init_magics(self):
173 def init_magics(self):
170 super(InteractiveShellEmbed, self).init_magics()
174 super(InteractiveShellEmbed, self).init_magics()
171 self.register_magics(EmbeddedMagics)
175 self.register_magics(EmbeddedMagics)
172
176
173 def __call__(self, header='', local_ns=None, module=None, dummy=None,
177 def __call__(self, header='', local_ns=None, module=None, dummy=None,
174 stack_depth=1, global_ns=None, compile_flags=None, **kw):
178 stack_depth=1, global_ns=None, compile_flags=None, **kw):
175 """Activate the interactive interpreter.
179 """Activate the interactive interpreter.
176
180
177 __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
181 __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
178 the interpreter shell with the given local and global namespaces, and
182 the interpreter shell with the given local and global namespaces, and
179 optionally print a header string at startup.
183 optionally print a header string at startup.
180
184
181 The shell can be globally activated/deactivated using the
185 The shell can be globally activated/deactivated using the
182 dummy_mode attribute. This allows you to turn off a shell used
186 dummy_mode attribute. This allows you to turn off a shell used
183 for debugging globally.
187 for debugging globally.
184
188
185 However, *each* time you call the shell you can override the current
189 However, *each* time you call the shell you can override the current
186 state of dummy_mode with the optional keyword parameter 'dummy'. For
190 state of dummy_mode with the optional keyword parameter 'dummy'. For
187 example, if you set dummy mode on with IPShell.dummy_mode = True, you
191 example, if you set dummy mode on with IPShell.dummy_mode = True, you
188 can still have a specific call work by making it as IPShell(dummy=False).
192 can still have a specific call work by making it as IPShell(dummy=False).
189 """
193 """
190
194
191 # we are called, set the underlying interactiveshell not to exit.
195 # we are called, set the underlying interactiveshell not to exit.
192 self.keep_running = True
196 self.keep_running = True
193
197
194 # If the user has turned it off, go away
198 # If the user has turned it off, go away
195 clid = kw.pop('_call_location_id', None)
199 clid = kw.pop('_call_location_id', None)
196 if not clid:
200 if not clid:
197 frame = sys._getframe(1)
201 frame = sys._getframe(1)
198 clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
202 clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
199 self._call_location_id = clid
203 self._call_location_id = clid
200
204
201 if not self.embedded_active:
205 if not self.embedded_active:
202 return
206 return
203
207
204 # Normal exits from interactive mode set this flag, so the shell can't
208 # Normal exits from interactive mode set this flag, so the shell can't
205 # re-enter (it checks this variable at the start of interactive mode).
209 # re-enter (it checks this variable at the start of interactive mode).
206 self.exit_now = False
210 self.exit_now = False
207
211
208 # Allow the dummy parameter to override the global __dummy_mode
212 # Allow the dummy parameter to override the global __dummy_mode
209 if dummy or (dummy != 0 and self.dummy_mode):
213 if dummy or (dummy != 0 and self.dummy_mode):
210 return
214 return
211
215
212 # self.banner is auto computed
216 # self.banner is auto computed
213 if header:
217 if header:
214 self.old_banner2 = self.banner2
218 self.old_banner2 = self.banner2
215 self.banner2 = self.banner2 + '\n' + header + '\n'
219 self.banner2 = self.banner2 + '\n' + header + '\n'
216 else:
220 else:
217 self.old_banner2 = ''
221 self.old_banner2 = ''
218
222
219 if self.display_banner:
223 if self.display_banner:
220 self.show_banner()
224 self.show_banner()
221
225
222 # Call the embedding code with a stack depth of 1 so it can skip over
226 # Call the embedding code with a stack depth of 1 so it can skip over
223 # our call and get the original caller's namespaces.
227 # our call and get the original caller's namespaces.
224 self.mainloop(local_ns, module, stack_depth=stack_depth,
228 self.mainloop(local_ns, module, stack_depth=stack_depth,
225 global_ns=global_ns, compile_flags=compile_flags)
229 global_ns=global_ns, compile_flags=compile_flags)
226
230
227 self.banner2 = self.old_banner2
231 self.banner2 = self.old_banner2
228
232
229 if self.exit_msg is not None:
233 if self.exit_msg is not None:
230 print(self.exit_msg)
234 print(self.exit_msg)
231
235
232 if self.should_raise:
236 if self.should_raise:
233 raise KillEmbedded('Embedded IPython raising error, as user requested.')
237 raise KillEmbedded('Embedded IPython raising error, as user requested.')
234
238
235
239
236 def mainloop(self, local_ns=None, module=None, stack_depth=0,
240 def mainloop(self, local_ns=None, module=None, stack_depth=0,
237 display_banner=None, global_ns=None, compile_flags=None):
241 display_banner=None, global_ns=None, compile_flags=None):
238 """Embeds IPython into a running python program.
242 """Embeds IPython into a running python program.
239
243
240 Parameters
244 Parameters
241 ----------
245 ----------
242
246
243 local_ns, module
247 local_ns, module
244 Working local namespace (a dict) and module (a module or similar
248 Working local namespace (a dict) and module (a module or similar
245 object). If given as None, they are automatically taken from the scope
249 object). If given as None, they are automatically taken from the scope
246 where the shell was called, so that program variables become visible.
250 where the shell was called, so that program variables become visible.
247
251
248 stack_depth : int
252 stack_depth : int
249 How many levels in the stack to go to looking for namespaces (when
253 How many levels in the stack to go to looking for namespaces (when
250 local_ns or module is None). This allows an intermediate caller to
254 local_ns or module is None). This allows an intermediate caller to
251 make sure that this function gets the namespace from the intended
255 make sure that this function gets the namespace from the intended
252 level in the stack. By default (0) it will get its locals and globals
256 level in the stack. By default (0) it will get its locals and globals
253 from the immediate caller.
257 from the immediate caller.
254
258
255 compile_flags
259 compile_flags
256 A bit field identifying the __future__ features
260 A bit field identifying the __future__ features
257 that are enabled, as passed to the builtin :func:`compile` function.
261 that are enabled, as passed to the builtin :func:`compile` function.
258 If given as None, they are automatically taken from the scope where
262 If given as None, they are automatically taken from the scope where
259 the shell was called.
263 the shell was called.
260
264
261 """
265 """
262
266
263 if (global_ns is not None) and (module is None):
267 if (global_ns is not None) and (module is None):
264 raise DeprecationWarning("'global_ns' keyword argument is deprecated, and has been removed in IPython 5.0 use `module` keyword argument instead.")
268 raise DeprecationWarning("'global_ns' keyword argument is deprecated, and has been removed in IPython 5.0 use `module` keyword argument instead.")
265
269
266 if (display_banner is not None):
270 if (display_banner is not None):
267 warnings.warn("The display_banner parameter is deprecated since IPython 4.0", DeprecationWarning)
271 warnings.warn("The display_banner parameter is deprecated since IPython 4.0", DeprecationWarning)
268
272
269 # Get locals and globals from caller
273 # Get locals and globals from caller
270 if ((local_ns is None or module is None or compile_flags is None)
274 if ((local_ns is None or module is None or compile_flags is None)
271 and self.default_user_namespaces):
275 and self.default_user_namespaces):
272 call_frame = sys._getframe(stack_depth).f_back
276 call_frame = sys._getframe(stack_depth).f_back
273
277
274 if local_ns is None:
278 if local_ns is None:
275 local_ns = call_frame.f_locals
279 local_ns = call_frame.f_locals
276 if module is None:
280 if module is None:
277 global_ns = call_frame.f_globals
281 global_ns = call_frame.f_globals
278 try:
282 try:
279 module = sys.modules[global_ns['__name__']]
283 module = sys.modules[global_ns['__name__']]
280 except KeyError:
284 except KeyError:
281 warnings.warn("Failed to get module %s" % \
285 warnings.warn("Failed to get module %s" % \
282 global_ns.get('__name__', 'unknown module')
286 global_ns.get('__name__', 'unknown module')
283 )
287 )
284 module = DummyMod()
288 module = DummyMod()
285 module.__dict__ = global_ns
289 module.__dict__ = global_ns
286 if compile_flags is None:
290 if compile_flags is None:
287 compile_flags = (call_frame.f_code.co_flags &
291 compile_flags = (call_frame.f_code.co_flags &
288 compilerop.PyCF_MASK)
292 compilerop.PyCF_MASK)
289
293
290 # Save original namespace and module so we can restore them after
294 # Save original namespace and module so we can restore them after
291 # embedding; otherwise the shell doesn't shut down correctly.
295 # embedding; otherwise the shell doesn't shut down correctly.
292 orig_user_module = self.user_module
296 orig_user_module = self.user_module
293 orig_user_ns = self.user_ns
297 orig_user_ns = self.user_ns
294 orig_compile_flags = self.compile.flags
298 orig_compile_flags = self.compile.flags
295
299
296 # Update namespaces and fire up interpreter
300 # Update namespaces and fire up interpreter
297
301
298 # The global one is easy, we can just throw it in
302 # The global one is easy, we can just throw it in
299 if module is not None:
303 if module is not None:
300 self.user_module = module
304 self.user_module = module
301
305
302 # But the user/local one is tricky: ipython needs it to store internal
306 # But the user/local one is tricky: ipython needs it to store internal
303 # data, but we also need the locals. We'll throw our hidden variables
307 # data, but we also need the locals. We'll throw our hidden variables
304 # like _ih and get_ipython() into the local namespace, but delete them
308 # like _ih and get_ipython() into the local namespace, but delete them
305 # later.
309 # later.
306 if local_ns is not None:
310 if local_ns is not None:
307 reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()}
311 reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()}
308 self.user_ns = reentrant_local_ns
312 self.user_ns = reentrant_local_ns
309 self.init_user_ns()
313 self.init_user_ns()
310
314
311 # Compiler flags
315 # Compiler flags
312 if compile_flags is not None:
316 if compile_flags is not None:
313 self.compile.flags = compile_flags
317 self.compile.flags = compile_flags
314
318
315 # make sure the tab-completer has the correct frame information, so it
319 # make sure the tab-completer has the correct frame information, so it
316 # actually completes using the frame's locals/globals
320 # actually completes using the frame's locals/globals
317 self.set_completer_frame()
321 self.set_completer_frame()
318
322
319 with self.builtin_trap, self.display_trap:
323 with self.builtin_trap, self.display_trap:
320 self.interact()
324 self.interact()
321
325
322 # now, purge out the local namespace of IPython's hidden variables.
326 # now, purge out the local namespace of IPython's hidden variables.
323 if local_ns is not None:
327 if local_ns is not None:
324 local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()})
328 local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()})
325
329
326
330
327 # Restore original namespace so shell can shut down when we exit.
331 # Restore original namespace so shell can shut down when we exit.
328 self.user_module = orig_user_module
332 self.user_module = orig_user_module
329 self.user_ns = orig_user_ns
333 self.user_ns = orig_user_ns
330 self.compile.flags = orig_compile_flags
334 self.compile.flags = orig_compile_flags
331
335
332
336
333 def embed(**kwargs):
337 def embed(**kwargs):
334 """Call this to embed IPython at the current point in your program.
338 """Call this to embed IPython at the current point in your program.
335
339
336 The first invocation of this will create an :class:`InteractiveShellEmbed`
340 The first invocation of this will create an :class:`InteractiveShellEmbed`
337 instance and then call it. Consecutive calls just call the already
341 instance and then call it. Consecutive calls just call the already
338 created instance.
342 created instance.
339
343
340 If you don't want the kernel to initialize the namespace
344 If you don't want the kernel to initialize the namespace
341 from the scope of the surrounding function,
345 from the scope of the surrounding function,
342 and/or you want to load full IPython configuration,
346 and/or you want to load full IPython configuration,
343 you probably want `IPython.start_ipython()` instead.
347 you probably want `IPython.start_ipython()` instead.
344
348
345 Here is a simple example::
349 Here is a simple example::
346
350
347 from IPython import embed
351 from IPython import embed
348 a = 10
352 a = 10
349 b = 20
353 b = 20
350 embed(header='First time')
354 embed(header='First time')
351 c = 30
355 c = 30
352 d = 40
356 d = 40
353 embed()
357 embed()
354
358
355 Full customization can be done by passing a :class:`Config` in as the
359 Full customization can be done by passing a :class:`Config` in as the
356 config argument.
360 config argument.
357 """
361 """
358 config = kwargs.get('config')
362 config = kwargs.get('config')
359 header = kwargs.pop('header', u'')
363 header = kwargs.pop('header', u'')
360 compile_flags = kwargs.pop('compile_flags', None)
364 compile_flags = kwargs.pop('compile_flags', None)
361 if config is None:
365 if config is None:
362 config = load_default_config()
366 config = load_default_config()
363 config.InteractiveShellEmbed = config.TerminalInteractiveShell
367 config.InteractiveShellEmbed = config.TerminalInteractiveShell
364 kwargs['config'] = config
368 kwargs['config'] = config
365 #save ps1/ps2 if defined
369 #save ps1/ps2 if defined
366 ps1 = None
370 ps1 = None
367 ps2 = None
371 ps2 = None
368 try:
372 try:
369 ps1 = sys.ps1
373 ps1 = sys.ps1
370 ps2 = sys.ps2
374 ps2 = sys.ps2
371 except AttributeError:
375 except AttributeError:
372 pass
376 pass
373 #save previous instance
377 #save previous instance
374 saved_shell_instance = InteractiveShell._instance
378 saved_shell_instance = InteractiveShell._instance
375 if saved_shell_instance is not None:
379 if saved_shell_instance is not None:
376 cls = type(saved_shell_instance)
380 cls = type(saved_shell_instance)
377 cls.clear_instance()
381 cls.clear_instance()
378 frame = sys._getframe(1)
382 frame = sys._getframe(1)
379 shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % (
383 shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % (
380 frame.f_code.co_filename, frame.f_lineno), **kwargs)
384 frame.f_code.co_filename, frame.f_lineno), **kwargs)
381 shell(header=header, stack_depth=2, compile_flags=compile_flags,
385 shell(header=header, stack_depth=2, compile_flags=compile_flags,
382 _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno))
386 _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno))
383 InteractiveShellEmbed.clear_instance()
387 InteractiveShellEmbed.clear_instance()
384 #restore previous instance
388 #restore previous instance
385 if saved_shell_instance is not None:
389 if saved_shell_instance is not None:
386 cls = type(saved_shell_instance)
390 cls = type(saved_shell_instance)
387 cls.clear_instance()
391 cls.clear_instance()
388 for subclass in cls._walk_mro():
392 for subclass in cls._walk_mro():
389 subclass._instance = saved_shell_instance
393 subclass._instance = saved_shell_instance
390 if ps1 is not None:
394 if ps1 is not None:
391 sys.ps1 = ps1
395 sys.ps1 = ps1
392 sys.ps2 = ps2
396 sys.ps2 = ps2
General Comments 0
You need to be logged in to leave comments. Login now