From 1d4043c22f6f873ed4d11c52698b5a26230df4d3 2011-03-22 00:05:04 From: Thomas Kluyver Date: 2011-03-22 00:05:04 Subject: [PATCH] Give .run_cell a store_history option, so that it can be used to run raw IPython code outside of the sequence of commands making the session. This also doesn't incremement the execution_count. --- diff --git a/IPython/core/history.py b/IPython/core/history.py index 90b2b67..9a33ad2 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -630,18 +630,20 @@ def magic_rerun(self, parameter_s=''): opts, args = self.parse_options(parameter_s, 'l:g:', mode='string') if "l" in opts: # Last n lines n = int(opts['l']) - hist = self.history_manager.get_hist_tail(n, raw=False) + hist = self.history_manager.get_hist_tail(n) elif "g" in opts: # Search p = "*"+opts['g']+"*" - hist = self.history_manager.get_hist_search(p, raw=False) - hist = list(hist) - if 'magic("rerun' in hist[-1][2]: - hist = hist[:-1] # We can ignore the current line - hist = hist[-1:] # Just get the last match + hist = list(self.history_manager.get_hist_search(p)) + for l in reversed(hist): + if "rerun" not in l[2]: + hist = [l] # The last match which isn't a %rerun + break + else: + hist = [] # No matches except %rerun elif args: # Specify history ranges hist = self.history_manager.get_hist_from_rangestr(args) else: # Last line - hist = self.history_manager.get_hist_tail(1, raw=False) + hist = self.history_manager.get_hist_tail(1) hist = [x[2] for x in hist] if not hist: print("No lines in history match specification") @@ -650,7 +652,7 @@ def magic_rerun(self, parameter_s=''): print("=== Executing: ===") print(histlines) print("=== Output: ===") - self.run_source("\n".join(hist), symbol="exec") + self.run_cell("\n".join(hist), store_history=False) def init_ipython(ip): diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 84d7707..818eab6 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2060,7 +2060,7 @@ class InteractiveShell(Configurable, Magic): self.showtraceback() warn('Unknown failure executing file: <%s>' % fname) - def run_cell(self, cell): + def run_cell(self, cell, store_history=True): """Run the contents of an entire multiline 'cell' of code, and store it in the history. @@ -2111,7 +2111,9 @@ class InteractiveShell(Configurable, Magic): cell = ''.join(blocks) # Store raw and processed history - self.history_manager.store_inputs(self.execution_count, cell, raw_cell) + if store_history: + self.history_manager.store_inputs(self.execution_count, + cell, raw_cell) self.logger.log(cell, raw_cell) @@ -2123,9 +2125,10 @@ class InteractiveShell(Configurable, Magic): out = self.run_source(blocks[0]) # Write output to the database. Does nothing unless # history output logging is enabled. - self.history_manager.store_output(self.execution_count) - # since we return here, we need to update the execution count - self.execution_count += 1 + if store_history: + self.history_manager.store_output(self.execution_count) + # since we return here, we need to update the execution count + self.execution_count += 1 return out # In multi-block input, if the last block is a simple (one-two @@ -2154,9 +2157,10 @@ class InteractiveShell(Configurable, Magic): # Write output to the database. Does nothing unless # history output logging is enabled. - self.history_manager.store_output(self.execution_count) - # Each cell is a *single* input, regardless of how many lines it has - self.execution_count += 1 + if store_history: + self.history_manager.store_output(self.execution_count) + # Each cell is a *single* input, regardless of how many lines it has + self.execution_count += 1 # PENDING REMOVAL: this method is slated for deletion, once our new # input logic has been 100% moved to frontends and is stable. diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 1a4862e..b1aed58 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -2369,7 +2369,8 @@ Currently the magic system has the following functions:\n""" else: print 'done. Executing edited code...' if opts_raw: - self.shell.run_cell(file_read(filename)) + self.shell.run_cell(file_read(filename), + store_history=False) else: self.shell.safe_execfile(filename,self.shell.user_ns, self.shell.user_ns)