From ef15883b20a74a89423c54db4cc6318bd0b1944a 2011-03-13 12:51:49 From: Thomas Kluyver Date: 2011-03-13 12:51:49 Subject: [PATCH] Passing IPython.core tests. --- diff --git a/IPython/core/history.py b/IPython/core/history.py index 59c10c7..e5d1922 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -97,6 +97,7 @@ class HistoryManager(Configurable): '%quit', '%Exit', '%exit']) def init_db(self): + """Connect to the database and get new session number.""" self.db = sqlite3.connect(self.hist_file) self.db.execute("""CREATE TABLE IF NOT EXISTS history (session integer, line integer, source text, source_raw text, @@ -173,7 +174,8 @@ class HistoryManager(Configurable): start : int First line to retrieve. stop : int - Last line to retrieve. If None, retrieve to the end of the session. + End of line range (excluded from output itself). If None, retrieve + to the end of the session. raw : bool If True, return untranslated input output : bool @@ -294,13 +296,17 @@ class HistoryManager(Configurable): self.input_hist_parsed[:lp-lr] = [] def reset(self): - """Clear all histories managed by this object.""" - self.input_hist_parsed[:] = [] - self.input_hist_raw[:] = [] + """Clear all histories managed by this object, and start a new + session.""" + self.input_hist_parsed[:] = [""] + self.input_hist_raw[:] = [""] self.output_hist.clear() # The directory history can't be completely empty self.dir_hist[:] = [os.getcwd()] + self.writeout_cache() + self.init_db() # New session + # To match, e.g. ~5/8-~2/3 range_re = re.compile(r""" ((?P~?\d+)/)? @@ -483,7 +489,7 @@ def magic_history(self, parameter_s = ''): line_sep = '\n' if multiline else '' if print_nums: print('%s:%s' % (_format_lineno(session, lineno).ljust(width), - line_sep[multiline]), file=outfile, end='') + line_sep), file=outfile, end='') if pyprompts: print(">>> ", end="", file=outfile) if multiline: diff --git a/IPython/core/magic.py b/IPython/core/magic.py index cbf74a9..0613ef5 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -1603,7 +1603,7 @@ Currently the magic system has the following functions:\n""" stats = None try: - self.shell.save_history() + #self.shell.save_history() if opts.has_key('p'): stats = self.magic_prun('',0,opts,arg_lst,prog_ns) @@ -1722,7 +1722,7 @@ Currently the magic system has the following functions:\n""" # contained therein. del sys.modules[main_mod_name] - self.shell.reload_history() + #self.shell.reload_history() return stats diff --git a/IPython/core/tests/test_history.py b/IPython/core/tests/test_history.py index 650549f..d471981 100644 --- a/IPython/core/tests/test_history.py +++ b/IPython/core/tests/test_history.py @@ -14,52 +14,61 @@ import nose.tools as nt # our own packages from IPython.utils.tempdir import TemporaryDirectory -from IPython.core.history import HistoryManager +from IPython.core.history import HistoryManager, extract_hist_ranges def test_history(): ip = get_ipython() with TemporaryDirectory() as tmpdir: #tmpdir = '/software/temp' - histfile = os.path.realpath(os.path.join(tmpdir, 'history.json')) + 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. hist_manager_ori = ip.history_manager try: - ip.history_manager = HistoryManager(ip) + ip.history_manager = HistoryManager(shell=ip) ip.history_manager.hist_file = histfile + ip.history_manager.init_db() # Has to be called after changing file print 'test',histfile hist = ['a=1', 'def f():\n test = 1\n return test', 'b=2'] - # test save and load - ip.history_manager.input_hist_raw[:] = [] - for h in hist: - ip.history_manager.store_inputs(h) - ip.save_history() - ip.history_manager.input_hist_raw[:] = [] - ip.reload_history() - print type(ip.history_manager.input_hist_raw) - print ip.history_manager.input_hist_raw - nt.assert_equal(len(ip.history_manager.input_hist_raw), len(hist)) - for i,h in enumerate(hist): - nt.assert_equal(hist[i], ip.history_manager.input_hist_raw[i]) + for i, h in enumerate(hist, start=1): + ip.history_manager.store_inputs(i, h) + + nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist) + + # Check lines were written to DB + c = ip.history_manager.db.execute("SELECT source_raw FROM history") + nt.assert_equal([x for x, in c], hist) - # Test that session offset works. - ip.history_manager.session_offset = \ - len(ip.history_manager.input_hist_raw) -1 + # New session + ip.history_manager.reset() newcmds = ["z=5","class X(object):\n pass", "k='p'"] - for cmd in newcmds: - ip.history_manager.store_inputs(cmd) - gothist = ip.history_manager.get_history((1,4), - raw=True, output=False) - nt.assert_equal(gothist, dict(zip([1,2,3], newcmds))) + for i, cmd in enumerate(newcmds): + ip.history_manager.store_inputs(i, cmd) + gothist = ip.history_manager.get_history(start=1, stop=4) + nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds)) + # Previous session: + gothist = ip.history_manager.get_history(-1, 1, 4) + nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist)) - # Cross testing: check that magic %save picks up on the session - # offset. + # Cross testing: check that magic %save can get previous session. testfilename = os.path.realpath(os.path.join(tmpdir, "test.py")) - ip.magic_save(testfilename + " 1-3") + ip.magic_save(testfilename + " ~1/1-3") testfile = open(testfilename, "r") - nt.assert_equal(testfile.read(), "\n".join(newcmds)) + nt.assert_equal(testfile.read(), "\n".join(hist)) finally: # Restore history manager ip.history_manager = hist_manager_ori + +def test_extract_hist_ranges(): + instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5" + expected = [(0, 1, 2), # 0 == current session + (2, 3, 4), + (-4, 5, 7), + (-4, 7, 10), + (-9, 2, None), # None == to end + (-8, 1, None), + (-7, 1, 6)] + actual = list(extract_hist_ranges(instr)) + nt.assert_equal(actual, expected) diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 07185c5..72f6f66 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -62,7 +62,7 @@ def doctest_hist_f(): In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') - In [11]: %hist -n -f $tfile 3 + In [11]: %hist -nl -f $tfile 3 In [13]: import os; os.unlink(tfile) """ @@ -80,7 +80,7 @@ def doctest_hist_r(): In [2]: x=1 - In [3]: %hist -r 2 + In [3]: %hist -rl 2 x=1 # random %hist -r 2 """ @@ -150,36 +150,14 @@ def doctest_hist_op(): <...s instance at ...> >>> """ - -def test_shist(): - # Simple tests of ShadowHist class - test generator. - import os, shutil, tempfile - - from IPython.utils import pickleshare - from IPython.core.history import ShadowHist - - tfile = tempfile.mktemp('','tmp-ipython-') - - db = pickleshare.PickleShareDB(tfile) - s = ShadowHist(db, get_ipython()) - s.add('hello') - s.add('world') - s.add('hello') - s.add('hello') - s.add('karhu') - - yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')] - - yield nt.assert_equal,s.get(2),'world' - - shutil.rmtree(tfile) - + def test_macro(): ip = get_ipython() ip.history_manager.reset() # Clear any existing history. cmds = ["a=1", "def b():\n return a**2", "print(a,b())"] - for cmd in cmds: - ip.history_manager.store_inputs(cmd) + for i, cmd in enumerate(cmds, start=1): + ip.history_manager.store_inputs(i, cmd) + print i, cmd ip.magic("macro test 1-3") nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")