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 wa
(
self,
local_ns=None,
module=None,
stack_depth=0,
compile_flags=None,
)
| 263 | raise KillEmbedded('Embedded IPython raising error, as user requested.') |
| 264 | |
| 265 | def mainloop( |
| 266 | self, |
| 267 | local_ns=None, |
| 268 | module=None, |
| 269 | stack_depth=0, |
| 270 | compile_flags=None, |
| 271 | ): |
| 272 | """Embeds IPython into a running python program. |
| 273 | |
| 274 | Parameters |
| 275 | ---------- |
| 276 | local_ns, module |
| 277 | Working local namespace (a dict) and module (a module or similar |
| 278 | object). If given as None, they are automatically taken from the scope |
| 279 | where the shell was called, so that program variables become visible. |
| 280 | stack_depth : int |
| 281 | How many levels in the stack to go to looking for namespaces (when |
| 282 | local_ns or module is None). This allows an intermediate caller to |
| 283 | make sure that this function gets the namespace from the intended |
| 284 | level in the stack. By default (0) it will get its locals and globals |
| 285 | from the immediate caller. |
| 286 | compile_flags |
| 287 | A bit field identifying the __future__ features |
| 288 | that are enabled, as passed to the builtin :func:`compile` function. |
| 289 | If given as None, they are automatically taken from the scope where |
| 290 | the shell was called. |
| 291 | |
| 292 | """ |
| 293 | |
| 294 | # Get locals and globals from caller |
| 295 | if ((local_ns is None or module is None or compile_flags is None) |
| 296 | and self.default_user_namespaces): |
| 297 | call_frame = sys._getframe(stack_depth).f_back |
| 298 | |
| 299 | if local_ns is None: |
| 300 | local_ns = call_frame.f_locals |
| 301 | if module is None: |
| 302 | global_ns = call_frame.f_globals |
| 303 | try: |
| 304 | module = sys.modules[global_ns['__name__']] |
| 305 | except KeyError: |
| 306 | warnings.warn("Failed to get module %s" % \ |
| 307 | global_ns.get('__name__', 'unknown module') |
| 308 | ) |
| 309 | module = make_main_module_type(global_ns)() |
| 310 | if compile_flags is None: |
| 311 | compile_flags = (call_frame.f_code.co_flags & |
| 312 | compilerop.PyCF_MASK) |
| 313 | |
| 314 | # Save original namespace and module so we can restore them after |
| 315 | # embedding; otherwise the shell doesn't shut down correctly. |
| 316 | orig_user_module = self.user_module |
| 317 | orig_user_ns = self.user_ns |
| 318 | orig_compile_flags = self.compile.flags |
| 319 | |
| 320 | # Update namespaces and fire up interpreter |
| 321 | |
| 322 | # The global one is easy, we can just throw it in |
no test coverage detected