From a55f1f68ee2fac03b6402a244af88b839c6c4644 2016-06-03 16:30:12 From: Matthias Bussonnier Date: 2016-06-03 16:30:12 Subject: [PATCH] Keep a reference to last execution result on the shell. In particular to know if the last command has failed. Note: Cannot keep a handle on the result object, or it does not get garbage collected until end of next run, which break some assumption about when __del__ is called. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 7c20eed..3015048 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -166,6 +166,10 @@ class ExecutionResult(object): if self.error_in_exec is not None: raise self.error_in_exec + def __repr__(self): + return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\ + (self.__class__.__qualname__, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result)) + class InteractiveShell(SingletonConfigurable): """An enhanced, interactive shell for Python.""" @@ -434,6 +438,8 @@ class InteractiveShell(SingletonConfigurable): # Tracks any GUI loop loaded for pylab pylab_gui_select = None + last_execution_succeeded = Bool(True, help='Did last executed command succeeded') + def __init__(self, ipython_dir=None, profile_dir=None, user_module=None, user_ns=None, custom_exceptions=((), None), **kwargs): @@ -2607,6 +2613,7 @@ class InteractiveShell(SingletonConfigurable): result = ExecutionResult() if (not raw_cell) or raw_cell.isspace(): + self.last_execution_succeeded = True return result if silent: @@ -2617,6 +2624,7 @@ class InteractiveShell(SingletonConfigurable): def error_before_exec(value): result.error_before_exec = value + self.last_execution_succeeded = False return result self.events.trigger('pre_execute') @@ -2703,8 +2711,10 @@ class InteractiveShell(SingletonConfigurable): # Execute the user code interactivity = "none" if silent else self.ast_node_interactivity - self.run_ast_nodes(code_ast.body, cell_name, + has_raised = self.run_ast_nodes(code_ast.body, cell_name, interactivity=interactivity, compiler=compiler, result=result) + + self.last_execution_succeeded = not has_raised # Reset this so later displayed values do not modify the # ExecutionResult