From 205d1241376996eb8ca5e72c7c29d4689a80fbb8 2010-09-07 00:15:42 From: Fernando Perez Date: 2010-09-07 00:15:42 Subject: [PATCH] Fix missing history in cell execution mode. Also cleaned up and commented run_cell a little better, no need to run using map() when a single run_code call suffices. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index b686bcb..2d3369c 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2093,20 +2093,28 @@ class InteractiveShell(Configurable, Magic): cell : str A single or multiline string. """ + # We need to break up the input into executable blocks that can be run + # in 'single' mode, to provide comfortable user behavior. blocks = self.input_splitter.split_blocks(cell) + if not blocks: return - + + # Single-block input should behave like an interactive prompt if len(blocks) == 1: self.runlines(blocks[0]) return + # In multi-block input, if the last block is a simple (one-two lines) + # expression, run it in single mode so it produces output. Otherwise + # just feed the whole thing to runcode. + # This seems like a reasonable usability design. last = blocks[-1] if len(last.splitlines()) < 2: - map(self.runcode, blocks[:-1]) + self.runcode('\n'.join(blocks[:-1])) self.runlines(last) else: - map(self.runcode, blocks) + self.runcode(cell) def runlines(self, lines, clean=False): """Run a string of one or more lines of source. @@ -2226,6 +2234,11 @@ class InteractiveShell(Configurable, Magic): - 1: an error occurred. """ + # It's also possible that we've been fed a plain string. In that case, + # we must store it in the input history. + if isinstance(code_obj, basestring): + self.input_hist_raw.append(code_obj) + # Set our own excepthook in case the user code tries to call it # directly, so that the IPython crash handler doesn't get triggered old_excepthook,sys.excepthook = sys.excepthook, self.excepthook