コード例 #1
0
def test_histmanager_disabled():
    """Ensure that disabling the history manager doesn't create a database."""
    cfg = Config()
    cfg.HistoryAccessor.enabled = False

    ip = get_ipython()
    with TemporaryDirectory() as tmpdir:
        hist_manager_ori = ip.history_manager
        hist_file = Path(tmpdir) / "history.sqlite"
        cfg.HistoryManager.hist_file = hist_file
        try:
            ip.history_manager = HistoryManager(shell=ip, config=cfg)
            hist = [
                "a=1", "def f():\n    test = 1\n    return test", "b='€Æ¾÷ß'"
            ]
            for i, h in enumerate(hist, start=1):
                ip.history_manager.store_inputs(i, h)
            assert ip.history_manager.input_hist_raw == [""] + hist
            ip.history_manager.reset()
            ip.history_manager.end_session()
        finally:
            ip.history_manager = hist_manager_ori

    # hist_file should not be created
    assert hist_file.exists() is False
コード例 #2
0
 def init_history(self):
     """Sets up the command history, and starts regular autosaves."""
     self.history_manager = HistoryManager(shell=self, parent=self)
     self.dataflow_history_manager = DataflowHistoryManager(shell=self)
     self.dataflow_function_manager = \
         DataflowFunctionManager(self.dataflow_history_manager)
     self.configurables.append(self.history_manager)
コード例 #3
0
ファイル: test_history.py プロジェクト: Igorshu90/Hal1.1
def test_histmanager_disabled():
    """Ensure that disabling the history manager doesn't create a database."""
    cfg = Config()
    cfg.HistoryAccessor.enabled = False

    ip = get_ipython()
    with TemporaryDirectory() as tmpdir:
        hist_manager_ori = ip.history_manager
        hist_file = os.path.join(tmpdir, 'history.sqlite')
        cfg.HistoryManager.hist_file = hist_file
        try:
            ip.history_manager = HistoryManager(shell=ip, config=cfg)
            hist = [
                u'a=1', u'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)
            nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
            ip.history_manager.reset()
            ip.history_manager.end_session()
        finally:
            ip.history_manager = hist_manager_ori

    # hist_file should not be created
    nt.assert_false(os.path.exists(hist_file))
コード例 #4
0
def test_history():

    ip = get_ipython()
    with TemporaryDirectory() as tmpdir:
        #tmpdir = '/software/temp'
        histfile = os.path.realpath(os.path.join(tmpdir, 'history.json'))
        # 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.hist_file = histfile
            print 'test', histfile
            hist = [
                'a=1\n', 'def f():\n    test = 1\n    return test\n', 'b=2\n'
            ]
            # test save and load
            ip.history_manager.input_hist_raw[:] = []
            for h in hist:
                ip.history_manager.input_hist_raw.append(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])
        finally:
            # Restore history manager
            ip.history_manager = hist_manager_ori
コード例 #5
0
ファイル: test_history.py プロジェクト: sratcliffe/ipython
def test_hist_file_config():
    cfg = Config()
    tfile = tempfile.NamedTemporaryFile(delete=False)
    cfg.HistoryManager.hist_file = tfile.name
    try:
        hm = HistoryManager(shell=get_ipython(), config=cfg)
        nt.assert_equals(hm.hist_file, cfg.HistoryManager.hist_file)
    finally:
        os.remove(tfile.name)
コード例 #6
0
ファイル: shell.py プロジェクト: sangoma/pytest-interactive
    def init_history(self):
        """Sets up the command history, and starts regular autosaves.

        .. note::
            A separate history db is allocated for this plugin separate
            from regular shell sessions such that only relevant commands
            are retained.
        """
        self.history_manager = HistoryManager(shell=self,
                                              parent=self,
                                              hist_file=self.pytest_hist_file)
        self.configurables.append(self.history_manager)
コード例 #7
0
ファイル: test_history.py プロジェクト: zyex1108/ipython
def test_hist_file_config():
    cfg = Config()
    tfile = tempfile.NamedTemporaryFile(delete=False)
    cfg.HistoryManager.hist_file = tfile.name
    try:
        hm = HistoryManager(shell=get_ipython(), config=cfg)
        nt.assert_equals(hm.hist_file, cfg.HistoryManager.hist_file)
    finally:
        try:
            os.remove(tfile.name)
        except OSError:
            # same catch as in testing.tools.TempFileMixin
            # On Windows, even though we close the file, we still can't
            # delete it.  I have no clue why
            pass
コード例 #8
0
ファイル: test_history.py プロジェクト: zyex1108/ipython
def test_history():
    ip = get_ipython()
    with TemporaryDirectory() as tmpdir:
        hist_manager_ori = ip.history_manager
        hist_file = os.path.join(tmpdir, 'history.sqlite')
        try:
            ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
            hist = [
                u'a=1', u'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)

            ip.history_manager.db_log_output = True
            # Doesn't match the input, but we'll just check it's stored.
            ip.history_manager.output_hist_reprs[3] = "spam"
            ip.history_manager.store_output(3)

            nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)

            # Detailed tests for _get_range_session
            grs = ip.history_manager._get_range_session
            nt.assert_equal(list(grs(start=2, stop=-1)),
                            zip([0], [2], hist[1:-1]))
            nt.assert_equal(list(grs(start=-2)), zip([0, 0], [2, 3],
                                                     hist[-2:]))
            nt.assert_equal(
                list(grs(output=True)),
                zip([0, 0, 0], [1, 2, 3], zip(hist, [None, None, 'spam'])))

            # Check whether specifying a range beyond the end of the current
            # session results in an error (gh-804)
            ip.magic('%hist 2-500')

            # Check that we can write non-ascii characters to a file
            ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1"))
            ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2"))
            ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3"))
            ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4"))

            # New session
            ip.history_manager.reset()
            newcmds = ["z=5", "class X(object):\n    pass", "k='p'"]
            for i, cmd in enumerate(newcmds, start=1):
                ip.history_manager.store_inputs(i, cmd)
            gothist = ip.history_manager.get_range(start=1, stop=4)
            nt.assert_equal(list(gothist), zip([0, 0, 0], [1, 2, 3], newcmds))
            # Previous session:
            gothist = ip.history_manager.get_range(-1, 1, 4)
            nt.assert_equal(list(gothist), zip([1, 1, 1], [1, 2, 3], hist))

            # Check get_hist_tail
            gothist = ip.history_manager.get_tail(4,
                                                  output=True,
                                                  include_latest=True)
            expected = [
                (1, 3, (hist[-1], "spam")),
                (2, 1, (newcmds[0], None)),
                (2, 2, (newcmds[1], None)),
                (2, 3, (newcmds[2], None)),
            ]
            nt.assert_equal(list(gothist), expected)

            gothist = ip.history_manager.get_tail(2)
            expected = [(2, 1, newcmds[0]), (2, 2, newcmds[1])]
            nt.assert_equal(list(gothist), expected)

            # Check get_hist_search
            gothist = ip.history_manager.search("*test*")
            nt.assert_equal(list(gothist), [(1, 2, hist[1])])
            gothist = ip.history_manager.search("b*", output=True)
            nt.assert_equal(list(gothist), [(1, 3, (hist[2], "spam"))])

            # 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/1-3")
            with py3compat.open(testfilename) as testfile:
                nt.assert_equal(testfile.read(),
                                u"# coding: utf-8\n" + u"\n".join(hist))

            # Duplicate line numbers - check that it doesn't crash, and
            # gets a new session
            ip.history_manager.store_inputs(1, "rogue")
            ip.history_manager.writeout_cache()
            nt.assert_equal(ip.history_manager.session_number, 3)
        finally:
            # Restore history manager
            ip.history_manager = hist_manager_ori
コード例 #9
0
def test_history():
    ip = get_ipython()
    with TemporaryDirectory() as tmpdir:
        tmp_path = Path(tmpdir)
        hist_manager_ori = ip.history_manager
        hist_file = tmp_path / "history.sqlite"
        try:
            ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
            hist = [
                "a=1", "def f():\n    test = 1\n    return test", "b='€Æ¾÷ß'"
            ]
            for i, h in enumerate(hist, start=1):
                ip.history_manager.store_inputs(i, h)

            ip.history_manager.db_log_output = True
            # Doesn't match the input, but we'll just check it's stored.
            ip.history_manager.output_hist_reprs[3] = "spam"
            ip.history_manager.store_output(3)

            assert ip.history_manager.input_hist_raw == [""] + hist

            # Detailed tests for _get_range_session
            grs = ip.history_manager._get_range_session
            assert list(grs(start=2,
                            stop=-1)) == list(zip([0], [2], hist[1:-1]))
            assert list(grs(start=-2)) == list(zip([0, 0], [2, 3], hist[-2:]))
            assert list(grs(output=True)) == list(
                zip([0, 0, 0], [1, 2, 3], zip(hist, [None, None, "spam"])))

            # Check whether specifying a range beyond the end of the current
            # session results in an error (gh-804)
            ip.run_line_magic("hist", "2-500")

            # Check that we can write non-ascii characters to a file
            ip.run_line_magic("hist", "-f %s" % (tmp_path / "test1"))
            ip.run_line_magic("hist", "-pf %s" % (tmp_path / "test2"))
            ip.run_line_magic("hist", "-nf %s" % (tmp_path / "test3"))
            ip.run_line_magic("save", "%s 1-10" % (tmp_path / "test4"))

            # New session
            ip.history_manager.reset()
            newcmds = ["z=5", "class X(object):\n    pass", "k='p'", "z=5"]
            for i, cmd in enumerate(newcmds, start=1):
                ip.history_manager.store_inputs(i, cmd)
            gothist = ip.history_manager.get_range(start=1, stop=4)
            assert list(gothist) == list(zip([0, 0, 0], [1, 2, 3], newcmds))
            # Previous session:
            gothist = ip.history_manager.get_range(-1, 1, 4)
            assert list(gothist) == list(zip([1, 1, 1], [1, 2, 3], hist))

            newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]

            # Check get_hist_tail
            gothist = ip.history_manager.get_tail(5,
                                                  output=True,
                                                  include_latest=True)
            expected = [(1, 3, (hist[-1], "spam"))] \
                + [(s, n, (c, None)) for (s, n, c) in newhist]
            assert list(gothist) == expected

            gothist = ip.history_manager.get_tail(2)
            expected = newhist[-3:-1]
            assert list(gothist) == expected

            # Check get_hist_search

            gothist = ip.history_manager.search("*test*")
            assert list(gothist) == [(1, 2, hist[1])]

            gothist = ip.history_manager.search("*=*")
            assert list(gothist) == [
                (1, 1, hist[0]),
                (1, 2, hist[1]),
                (1, 3, hist[2]),
                newhist[0],
                newhist[2],
                newhist[3],
            ]

            gothist = ip.history_manager.search("*=*", n=4)
            assert list(gothist) == [
                (1, 3, hist[2]),
                newhist[0],
                newhist[2],
                newhist[3],
            ]

            gothist = ip.history_manager.search("*=*", unique=True)
            assert list(gothist) == [
                (1, 1, hist[0]),
                (1, 2, hist[1]),
                (1, 3, hist[2]),
                newhist[2],
                newhist[3],
            ]

            gothist = ip.history_manager.search("*=*", unique=True, n=3)
            assert list(gothist) == [(1, 3, hist[2]), newhist[2], newhist[3]]

            gothist = ip.history_manager.search("b*", output=True)
            assert list(gothist) == [(1, 3, (hist[2], "spam"))]

            # Cross testing: check that magic %save can get previous session.
            testfilename = (tmp_path / "test.py").resolve()
            ip.run_line_magic("save", str(testfilename) + " ~1/1-3")
            with io.open(testfilename, encoding="utf-8") as testfile:
                assert testfile.read(
                ) == "# coding: utf-8\n" + "\n".join(hist) + "\n"

            # Duplicate line numbers - check that it doesn't crash, and
            # gets a new session
            ip.history_manager.store_inputs(1, "rogue")
            ip.history_manager.writeout_cache()
            assert ip.history_manager.session_number == 3

            # Check that session and line values are not just max values
            sessid, lineno, entry = newhist[-1]
            assert lineno > 1
            ip.history_manager.reset()
            lineno = 1
            ip.history_manager.store_inputs(lineno, entry)
            gothist = ip.history_manager.search("*=*", unique=True)
            hist = list(gothist)[-1]
            assert sessid < hist[0]
            assert hist[1:] == (lineno, entry)
        finally:
            # Ensure saving thread is shut down before we try to clean up the files
            ip.history_manager.save_thread.stop()
            # Forcibly close database rather than relying on garbage collection
            ip.history_manager.db.close()
            # Restore history manager
            ip.history_manager = hist_manager_ori
コード例 #10
0
def test_history():
    ip = get_ipython()
    with TemporaryDirectory() as tmpdir:
        hist_manager_ori = ip.history_manager
        hist_file = os.path.join(tmpdir, "history.sqlite")
        try:
            ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
            hist = [u"a=1", u"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)

            ip.history_manager.db_log_output = True
            # Doesn't match the input, but we'll just check it's stored.
            ip.history_manager.output_hist_reprs[3] = "spam"
            ip.history_manager.store_output(3)

            nt.assert_equal(ip.history_manager.input_hist_raw, [""] + hist)

            # Detailed tests for _get_range_session
            grs = ip.history_manager._get_range_session
            nt.assert_equal(
                list(grs(start=2, stop=-1)), list(zip([0], [2], hist[1:-1]))
            )
            nt.assert_equal(list(grs(start=-2)), list(zip([0, 0], [2, 3], hist[-2:])))
            nt.assert_equal(
                list(grs(output=True)),
                list(zip([0, 0, 0], [1, 2, 3], zip(hist, [None, None, "spam"]))),
            )

            # Check whether specifying a range beyond the end of the current
            # session results in an error (gh-804)
            ip.magic("%hist 2-500")

            # Check that we can write non-ascii characters to a file
            ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1"))
            ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2"))
            ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3"))
            ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4"))

            # New session
            ip.history_manager.reset()
            newcmds = [u"z=5", u"class X(object):\n    pass", u"k='p'", u"z=5"]
            for i, cmd in enumerate(newcmds, start=1):
                ip.history_manager.store_inputs(i, cmd)
            gothist = ip.history_manager.get_range(start=1, stop=4)
            nt.assert_equal(list(gothist), list(zip([0, 0, 0], [1, 2, 3], newcmds)))
            # Previous session:
            gothist = ip.history_manager.get_range(-1, 1, 4)
            nt.assert_equal(list(gothist), list(zip([1, 1, 1], [1, 2, 3], hist)))

            newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]

            # Check get_hist_tail
            gothist = ip.history_manager.get_tail(5, output=True, include_latest=True)
            expected = [(1, 3, (hist[-1], "spam"))] + [
                (s, n, (c, None)) for (s, n, c) in newhist
            ]
            nt.assert_equal(list(gothist), expected)

            gothist = ip.history_manager.get_tail(2)
            expected = newhist[-3:-1]
            nt.assert_equal(list(gothist), expected)

            # Check get_hist_search

            gothist = ip.history_manager.search("*test*")
            nt.assert_equal(list(gothist), [(1, 2, hist[1])])

            gothist = ip.history_manager.search("*=*")
            nt.assert_equal(
                list(gothist),
                [
                    (1, 1, hist[0]),
                    (1, 2, hist[1]),
                    (1, 3, hist[2]),
                    newhist[0],
                    newhist[2],
                    newhist[3],
                ],
            )

            gothist = ip.history_manager.search("*=*", n=4)
            nt.assert_equal(
                list(gothist), [(1, 3, hist[2]), newhist[0], newhist[2], newhist[3]]
            )

            gothist = ip.history_manager.search("*=*", unique=True)
            nt.assert_equal(
                list(gothist),
                [
                    (1, 1, hist[0]),
                    (1, 2, hist[1]),
                    (1, 3, hist[2]),
                    newhist[2],
                    newhist[3],
                ],
            )

            gothist = ip.history_manager.search("*=*", unique=True, n=3)
            nt.assert_equal(list(gothist), [(1, 3, hist[2]), newhist[2], newhist[3]])

            gothist = ip.history_manager.search("b*", output=True)
            nt.assert_equal(list(gothist), [(1, 3, (hist[2], "spam"))])

            # 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/1-3")
            with io.open(testfilename, encoding="utf-8") as testfile:
                nt.assert_equal(
                    testfile.read(), u"# coding: utf-8\n" + u"\n".join(hist) + u"\n"
                )

            # Duplicate line numbers - check that it doesn't crash, and
            # gets a new session
            ip.history_manager.store_inputs(1, "rogue")
            ip.history_manager.writeout_cache()
            nt.assert_equal(ip.history_manager.session_number, 3)
        finally:
            # Ensure saving thread is shut down before we try to clean up the files
            ip.history_manager.save_thread.stop()
            # Forcibly close database rather than relying on garbage collection
            ip.history_manager.db.close()
            # Restore history manager
            ip.history_manager = hist_manager_ori
コード例 #11
0
ファイル: test_history.py プロジェクト: ilustreous/ipython
def test_history():
    ip = get_ipython()
    with TemporaryDirectory() as tmpdir:
        # Make a new :memory: DB.
        hist_manager_ori = ip.history_manager
        try:
            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)

            ip.history_manager.db_log_output = True
            # Doesn't match the input, but we'll just check it's stored.
            ip.history_manager.output_hist_reprs[3].append("spam")
            ip.history_manager.store_output(3)

            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)

            # New session
            ip.history_manager.reset()
            newcmds = ["z=5", "class X(object):\n    pass", "k='p'"]
            for i, cmd in enumerate(newcmds, start=1):
                ip.history_manager.store_inputs(i, cmd)
            gothist = ip.history_manager.get_range(start=1, stop=4)
            nt.assert_equal(list(gothist), zip([0, 0, 0], [1, 2, 3], newcmds))
            # Previous session:
            gothist = ip.history_manager.get_range(-1, 1, 4)
            nt.assert_equal(list(gothist), zip([1, 1, 1], [1, 2, 3], hist))

            # Check get_hist_tail
            gothist = ip.history_manager.get_tail(4,
                                                  output=True,
                                                  include_latest=True)
            expected = [
                (1, 3, (hist[-1], ["spam"])),
                (2, 1, (newcmds[0], None)),
                (2, 2, (newcmds[1], None)),
                (2, 3, (newcmds[2], None)),
            ]
            nt.assert_equal(list(gothist), expected)

            gothist = ip.history_manager.get_tail(2)
            expected = [(2, 1, newcmds[0]), (2, 2, newcmds[1])]
            nt.assert_equal(list(gothist), expected)

            # Check get_hist_search
            gothist = ip.history_manager.search("*test*")
            nt.assert_equal(list(gothist), [(1, 2, hist[1])])
            gothist = ip.history_manager.search("b*", output=True)
            nt.assert_equal(list(gothist), [(1, 3, (hist[2], ["spam"]))])

            # 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/1-3")
            testfile = open(testfilename, "r")
            nt.assert_equal(testfile.read().decode("utf-8"),
                            "# coding: utf-8\n" + "\n".join(hist))

            # Duplicate line numbers - check that it doesn't crash, and
            # gets a new session
            ip.history_manager.store_inputs(1, "rogue")
            nt.assert_equal(ip.history_manager.session_number, 3)
        finally:
            # Restore history manager
            ip.history_manager = hist_manager_ori