From 38342193ecef5e9afee3bf110c5df257614814c8 2011-03-13 12:51:49 From: Thomas Kluyver Date: 2011-03-13 12:51:49 Subject: [PATCH] Tidy up store_inputs --- diff --git a/IPython/core/history.py b/IPython/core/history.py index 8b9e124..fa7da6a 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -13,6 +13,7 @@ from __future__ import print_function # Stdlib imports +import fnmatch import os import sqlite3 @@ -232,12 +233,15 @@ class HistoryManager(object): hist[i] = input_hist[i] return hist - def store_inputs(self, source, source_raw=None): + def store_inputs(self, line_num, source, source_raw=None): """Store source and raw input in history and create input cache variables _i*. Parameters ---------- + line_num : int + The prompt number of this input. + source : str Python input. @@ -254,16 +258,15 @@ class HistoryManager(object): self.input_hist_parsed.append(source.rstrip()) self.input_hist_raw.append(source_raw.rstrip()) - if self.db_cache_size: - self.db_cache.append((self.session_number, - self.shell.execution_count, source, source_raw)) + + db_row = (self.session_number, line_num, source, source_raw) + if self.db_cache_size: # Cache before writing + self.db_cache.append(db_row) if len(self.db_cache) > self.db_cache_size: self.writeout_cache() - else: # Instant write + else: # Instant write with self.db: - self.db.execute("INSERT INTO history VALUES (?, ?, ?, ?)", - (self.session_number, self.shell.execution_count, - source, source_raw)) + self.db.execute("INSERT INTO history VALUES (?, ?, ?, ?)", db_row) # update the auto _i variables self._iii = self._ii @@ -272,7 +275,7 @@ class HistoryManager(object): self._i00 = source_raw # hackish access to user namespace to create _i1,_i2... dynamically - new_i = '_i%s' % self.shell.execution_count + new_i = '_i%s' % line_num to_main = {'_i': self._i, '_ii': self._ii, '_iii': self._iii, diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index fbe79a2..fc79cc8 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2100,7 +2100,7 @@ class InteractiveShell(Configurable, Magic): ipy_cell = ''.join(blocks) # Store raw and processed history - self.history_manager.store_inputs(ipy_cell, cell) + self.history_manager.store_inputs(self.execution_count, ipy_cell, cell) self.logger.log(ipy_cell, cell) @@ -2390,8 +2390,8 @@ class InteractiveShell(Configurable, Magic): full_source = '\n'.join(self.buffer) more = self.run_source(full_source, self.filename) if not more: - self.history_manager.store_inputs('\n'.join(self.buffer_raw), - full_source) + self.history_manager.store_inputs(self.execution_count, + '\n'.join(self.buffer_raw), full_source) self.reset_buffer() self.execution_count += 1 return more