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

Method run_code

IPython/core/interactiveshell.py:3376–3437  ·  view source on GitHub ↗

Execute a code object. When an exception occurs, self.showtraceback() is called to display a traceback. Parameters ---------- code_obj : code object A compiled code object, to be executed result : ExecutionResult, optional An obje

(self, code_obj, result=None, *, async_=False)

Source from the content-addressed store, hash-verified

3374 return eval(code_obj, user_ns)
3375
3376 async def run_code(self, code_obj, result=None, *, async_=False):
3377 """Execute a code object.
3378
3379 When an exception occurs, self.showtraceback() is called to display a
3380 traceback.
3381
3382 Parameters
3383 ----------
3384 code_obj : code object
3385 A compiled code object, to be executed
3386 result : ExecutionResult, optional
3387 An object to store exceptions that occur during execution.
3388 async_ : Bool (Experimental)
3389 Attempt to run top-level asynchronous code in a default loop.
3390
3391 Returns
3392 -------
3393 False : successful execution.
3394 True : an error occurred.
3395 """
3396 # special value to say that anything above is IPython and should be
3397 # hidden.
3398 __tracebackhide__ = "__ipython_bottom__"
3399 # Set our own excepthook in case the user code tries to call it
3400 # directly, so that the IPython crash handler doesn't get triggered
3401 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
3402
3403 # we save the original sys.excepthook in the instance, in case config
3404 # code (such as magics) needs access to it.
3405 self.sys_excepthook = old_excepthook
3406 outflag = True # happens in more places, so it's easier as default
3407 try:
3408 try:
3409 self.hooks.pre_run_code_hook()
3410 if async_ and sys.version_info < (3,8):
3411 last_expr = (await self._async_exec(code_obj, self.user_ns))
3412 code = compile('last_expr', 'fake', "single")
3413 exec(code, {'last_expr': last_expr})
3414 elif async_ :
3415 await eval(code_obj, self.user_global_ns, self.user_ns)
3416 else:
3417 exec(code_obj, self.user_global_ns, self.user_ns)
3418 finally:
3419 # Reset our crash handler in place
3420 sys.excepthook = old_excepthook
3421 except SystemExit as e:
3422 if result is not None:
3423 result.error_in_exec = e
3424 self.showtraceback(exception_only=True)
3425 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
3426 except self.custom_exceptions:
3427 etype, value, tb = sys.exc_info()
3428 if result is not None:
3429 result.error_in_exec = value
3430 self.CustomTB(etype, value, tb)
3431 except:
3432 if result is not None:
3433 result.error_in_exec = sys.exc_info()[1]

Callers 1

run_ast_nodesMethod · 0.95

Calls 2

_async_execMethod · 0.95
showtracebackMethod · 0.95

Tested by

no test coverage detected