MCPcopy Create free account
hub / github.com/ipython/ipython / mainloop

Method mainloop

IPython/terminal/embed.py:240–334  ·  view source on GitHub ↗

Embeds IPython into a running python program. Parameters ---------- local_ns, module Working local namespace (a dict) and module (a module or similar object). If given as None, they are automatically taken from the scope where the shell was cal

(self, local_ns=None, module=None, stack_depth=0,
                 display_banner=None, global_ns=None, compile_flags=None)

Source from the content-addressed store, hash-verified

238
239
240 def mainloop(self, local_ns=None, module=None, stack_depth=0,
241 display_banner=None, global_ns=None, compile_flags=None):
242 """Embeds IPython into a running python program.
243
244 Parameters
245 ----------
246
247 local_ns, module
248 Working local namespace (a dict) and module (a module or similar
249 object). If given as None, they are automatically taken from the scope
250 where the shell was called, so that program variables become visible.
251
252 stack_depth : int
253 How many levels in the stack to go to looking for namespaces (when
254 local_ns or module is None). This allows an intermediate caller to
255 make sure that this function gets the namespace from the intended
256 level in the stack. By default (0) it will get its locals and globals
257 from the immediate caller.
258
259 compile_flags
260 A bit field identifying the __future__ features
261 that are enabled, as passed to the builtin :func:`compile` function.
262 If given as None, they are automatically taken from the scope where
263 the shell was called.
264
265 """
266
267 if (global_ns is not None) and (module is None):
268 raise DeprecationWarning("'global_ns' keyword argument is deprecated, and has been removed in IPython 5.0 use `module` keyword argument instead.")
269
270 if (display_banner is not None):
271 warnings.warn("The display_banner parameter is deprecated since IPython 4.0", DeprecationWarning)
272
273 # Get locals and globals from caller
274 if ((local_ns is None or module is None or compile_flags is None)
275 and self.default_user_namespaces):
276 call_frame = sys._getframe(stack_depth).f_back
277
278 if local_ns is None:
279 local_ns = call_frame.f_locals
280 if module is None:
281 global_ns = call_frame.f_globals
282 try:
283 module = sys.modules[global_ns['__name__']]
284 except KeyError:
285 warnings.warn("Failed to get module %s" % \
286 global_ns.get('__name__', 'unknown module')
287 )
288 module = DummyMod()
289 module.__dict__ = global_ns
290 if compile_flags is None:
291 compile_flags = (call_frame.f_code.co_flags &
292 compilerop.PyCF_MASK)
293
294 # Save original namespace and module so we can restore them after
295 # embedding; otherwise the shell doesn't shut down correctly.
296 orig_user_module = self.user_module
297 orig_user_ns = self.user_ns

Callers 3

__call__Method · 0.95
startMethod · 0.45
gui-tk.pyFile · 0.45

Calls 6

DummyModClass · 0.90
warnMethod · 0.80
init_user_nsMethod · 0.80
set_completer_frameMethod · 0.80
interactMethod · 0.80
updateMethod · 0.45

Tested by

no test coverage detected