diff --git a/IPython/core/history.py b/IPython/core/history.py index ddd5dd4..0c77a1c 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -47,15 +47,22 @@ class HistoryManager(Configurable): input_hist_raw = List([""]) # A list of directories visited during session dir_hist = List() + def _dir_hist_default(self): + try: + return [os.getcwd()] + except OSError: + return [] + # A dict of output history, keyed with ints from the shell's # execution count. If there are several outputs from one command, # only the last one is stored. output_hist = Dict() # Contains all outputs, in lists of reprs. - output_hist_reprs = Instance(defaultdict) - + output_hist_reprs = Instance(defaultdict, args=(list,)) + # String holding the path to the history file - hist_file = Unicode() + hist_file = Unicode(config=True) + # The SQLite database db = Instance(sqlite3.Connection) # The number of the current session in the history database @@ -73,47 +80,48 @@ class HistoryManager(Configurable): # Variables used to store the three last inputs from the user. On each new # history update, we populate the user's namespace with these, shifted as # necessary. - _i00, _i, _ii, _iii = '','','','' + _i00 = Unicode(u'') + _i = Unicode(u'') + _ii = Unicode(u'') + _iii = Unicode(u'') # A set with all forms of the exit command, so that we don't store them in # the history (it's annoying to rewind the first entry and land on an exit # call). - _exit_commands = None - - def __init__(self, shell, config=None): + _exit_commands = Instance(set, args=(['Quit', 'quit', 'Exit', 'exit', + '%Quit', '%quit', '%Exit', '%exit'],)) + + def __init__(self, shell, config=None, **traits): """Create a new history manager associated with a shell instance. """ # We need a pointer back to the shell for various tasks. - super(HistoryManager, self).__init__(shell=shell, config=config) + super(HistoryManager, self).__init__(shell=shell, config=config, + **traits) - # list of visited directories - try: - self.dir_hist = [os.getcwd()] - except OSError: - self.dir_hist = [] + if self.hist_file == u'': + # No one has set the hist_file, yet. + if shell.profile: + histfname = 'history-%s' % shell.profile + else: + histfname = 'history' + self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite') - # Now the history file - if shell.profile: - histfname = 'history-%s' % shell.profile - else: - histfname = 'history' - self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite') try: self.init_db() except sqlite3.DatabaseError: - newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite") - os.rename(self.hist_file, newpath) - print("ERROR! History file wasn't a valid SQLite database.", - "It was moved to %s" % newpath, "and a new file created.") - self.init_db() - + if os.path.isfile(self.hist_file): + # Try to move the file out of the way. + newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite") + os.rename(self.hist_file, newpath) + print("ERROR! History file wasn't a valid SQLite database.", + "It was moved to %s" % newpath, "and a new file created.") + self.init_db() + else: + # The hist_file is probably :memory: or something else. + raise + self.new_session() - - self._i00, self._i, self._ii, self._iii = '','','','' - self.output_hist_reprs = defaultdict(list) - self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit', - '%quit', '%Exit', '%exit']) def init_db(self): """Connect to the database, and create tables if necessary.""" diff --git a/IPython/core/tests/test_history.py b/IPython/core/tests/test_history.py index a2d3353..cdbc525 100644 --- a/IPython/core/tests/test_history.py +++ b/IPython/core/tests/test_history.py @@ -23,18 +23,10 @@ def setUp(): def test_history(): ip = get_ipython() with TemporaryDirectory() as tmpdir: - #tmpdir = '/software/temp' - histfile = os.path.realpath(os.path.join(tmpdir, 'history.sqlite')) - # Ensure that we restore the history management that we mess with in - # this test doesn't affect the IPython instance used by the test suite - # beyond this test. + # Make a new :memory: DB. hist_manager_ori = ip.history_manager try: - ip.history_manager = HistoryManager(shell=ip) - ip.history_manager.hist_file = histfile - ip.history_manager.init_db() # Has to be called after changing file - ip.history_manager.reset() - print 'test',histfile + ip.history_manager = HistoryManager(shell=ip, hist_file=':memory:') hist = ['a=1', 'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"] for i, h in enumerate(hist, start=1): ip.history_manager.store_inputs(i, h) diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py index 2477cbb..02cbe0d 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -167,6 +167,7 @@ def default_config(): config.TerminalInteractiveShell.colors = 'NoColor' config.TerminalTerminalInteractiveShell.term_title = False, config.TerminalInteractiveShell.autocall = 0 + config.HistoryManager.hist_file = u':memory:' return config