Show More
@@ -195,17 +195,26 b' class HistoryManager(Configurable):' | |||||
195 | return cur |
|
195 | return cur | |
196 |
|
196 | |||
197 |
|
197 | |||
198 | def get_hist_tail(self, n=10, raw=True, output=False): |
|
198 | def get_hist_tail(self, n=10, raw=True, output=False, include_latest=False): | |
199 |
"""Get the last n lines from the history database. |
|
199 | """Get the last n lines from the history database. | |
|
200 | ||||
|
201 | If include_latest is False (default), n+1 lines are fetched, and | |||
|
202 | the latest one is discarded. This is intended to be used where | |||
|
203 | the function is called by a user command, which it should not | |||
|
204 | return.""" | |||
200 | self.writeout_cache() |
|
205 | self.writeout_cache() | |
|
206 | if not include_latest: | |||
|
207 | n += 1 | |||
201 | cur = self._get_hist_sql("ORDER BY session DESC, line DESC LIMIT ?", |
|
208 | cur = self._get_hist_sql("ORDER BY session DESC, line DESC LIMIT ?", | |
202 | (n,), raw=raw, output=output) |
|
209 | (n,), raw=raw, output=output) | |
|
210 | if not include_latest: | |||
|
211 | return reversed(list(cur)[1:]) | |||
203 | return reversed(list(cur)) |
|
212 | return reversed(list(cur)) | |
204 |
|
213 | |||
205 | def get_hist_search(self, pattern="*", raw=True, search_raw=True, |
|
214 | def get_hist_search(self, pattern="*", raw=True, search_raw=True, | |
206 | output=False): |
|
215 | output=False): | |
207 |
"""Search the database using unix glob-style matching (wildcards |
|
216 | """Search the database using unix glob-style matching (wildcards | |
208 | ?, escape using \). |
|
217 | * and ?). | |
209 |
|
218 | |||
210 | Returns |
|
219 | Returns | |
211 | ------- |
|
220 | ------- | |
@@ -620,19 +629,20 b" def magic_rerun(self, parameter_s=''):" | |||||
620 | """ |
|
629 | """ | |
621 | opts, args = self.parse_options(parameter_s, 'l:g:', mode='string') |
|
630 | opts, args = self.parse_options(parameter_s, 'l:g:', mode='string') | |
622 | if "l" in opts: # Last n lines |
|
631 | if "l" in opts: # Last n lines | |
623 |
n = int(opts['l']) |
|
632 | n = int(opts['l']) | |
624 | hist = self.history_manager.get_hist_tail(n, raw=False) |
|
633 | hist = self.history_manager.get_hist_tail(n, raw=False) | |
625 | elif "g" in opts: # Search |
|
634 | elif "g" in opts: # Search | |
626 | p = "*"+opts['g']+"*" |
|
635 | p = "*"+opts['g']+"*" | |
627 | hist = self.history_manager.get_hist_search(p, raw=False) |
|
636 | hist = self.history_manager.get_hist_search(p, raw=False) | |
628 |
hist = list(hist) |
|
637 | hist = list(hist) | |
|
638 | if 'magic("rerun' in hist[-1][2]: | |||
|
639 | hist = hist[:-1] # We can ignore the current line | |||
|
640 | hist = hist[-1:] # Just get the last match | |||
629 | elif args: # Specify history ranges |
|
641 | elif args: # Specify history ranges | |
630 | hist = self.history_manager.get_hist_from_rangestr(args) |
|
642 | hist = self.history_manager.get_hist_from_rangestr(args) | |
631 | else: # Last line |
|
643 | else: # Last line | |
632 |
hist = self.history_manager.get_hist_tail( |
|
644 | hist = self.history_manager.get_hist_tail(1, raw=False) | |
633 | hist = [x[2] for x in hist] |
|
645 | hist = [x[2] for x in hist] | |
634 | if hist and parameter_s in hist[-1]: |
|
|||
635 | hist = hist[:-1] |
|
|||
636 | if not hist: |
|
646 | if not hist: | |
637 | print("No lines in history match specification") |
|
647 | print("No lines in history match specification") | |
638 | return |
|
648 | return | |
@@ -647,7 +657,6 b' def init_ipython(ip):' | |||||
647 | ip.define_magic("rep", magic_rep) |
|
657 | ip.define_magic("rep", magic_rep) | |
648 | ip.define_magic("recall", magic_rep) |
|
658 | ip.define_magic("recall", magic_rep) | |
649 | ip.define_magic("rerun", magic_rerun) |
|
659 | ip.define_magic("rerun", magic_rerun) | |
650 | ip.define_magic("r", magic_rerun) |
|
|||
651 | ip.define_magic("hist",magic_history) # Alternative name |
|
660 | ip.define_magic("hist",magic_history) # Alternative name | |
652 | ip.define_magic("history",magic_history) |
|
661 | ip.define_magic("history",magic_history) | |
653 |
|
662 |
@@ -1550,7 +1550,8 b' class InteractiveShell(Configurable, Magic):' | |||||
1550 | readline.set_history_length(self.history_length) |
|
1550 | readline.set_history_length(self.history_length) | |
1551 |
|
1551 | |||
1552 | # Load the last 1000 lines from history |
|
1552 | # Load the last 1000 lines from history | |
1553 |
for _, _, cell in self.history_manager.get_hist_tail(1000 |
|
1553 | for _, _, cell in self.history_manager.get_hist_tail(1000, | |
|
1554 | include_latest=True): | |||
1554 | if cell.strip(): # Ignore blank lines |
|
1555 | if cell.strip(): # Ignore blank lines | |
1555 | for line in cell.splitlines(): |
|
1556 | for line in cell.splitlines(): | |
1556 | readline.add_history(line) |
|
1557 | readline.add_history(line) |
@@ -59,13 +59,19 b' def test_history():' | |||||
59 | nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist)) |
|
59 | nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist)) | |
60 |
|
60 | |||
61 | # Check get_hist_tail |
|
61 | # Check get_hist_tail | |
62 |
gothist = ip.history_manager.get_hist_tail(4, output=True |
|
62 | gothist = ip.history_manager.get_hist_tail(4, output=True, | |
|
63 | include_latest=True) | |||
63 | expected = [(1, 3, (hist[-1], ["spam"])), |
|
64 | expected = [(1, 3, (hist[-1], ["spam"])), | |
64 | (2, 1, (newcmds[0], None)), |
|
65 | (2, 1, (newcmds[0], None)), | |
65 | (2, 2, (newcmds[1], None)), |
|
66 | (2, 2, (newcmds[1], None)), | |
66 | (2, 3, (newcmds[2], None)),] |
|
67 | (2, 3, (newcmds[2], None)),] | |
67 | nt.assert_equal(list(gothist), expected) |
|
68 | nt.assert_equal(list(gothist), expected) | |
68 |
|
69 | |||
|
70 | gothist = ip.history_manager.get_hist_tail(2) | |||
|
71 | expected = [(2, 1, newcmds[0]), | |||
|
72 | (2, 2, newcmds[1])] | |||
|
73 | nt.assert_equal(list(gothist), expected) | |||
|
74 | ||||
69 | # Check get_hist_search |
|
75 | # Check get_hist_search | |
70 | gothist = ip.history_manager.get_hist_search("*test*") |
|
76 | gothist = ip.history_manager.get_hist_search("*test*") | |
71 | nt.assert_equal(list(gothist), [(1,2,hist[1])] ) |
|
77 | nt.assert_equal(list(gothist), [(1,2,hist[1])] ) | |
@@ -92,3 +98,12 b' def test_extract_hist_ranges():' | |||||
92 | (-7, 1, 6)] |
|
98 | (-7, 1, 6)] | |
93 | actual = list(extract_hist_ranges(instr)) |
|
99 | actual = list(extract_hist_ranges(instr)) | |
94 | nt.assert_equal(actual, expected) |
|
100 | nt.assert_equal(actual, expected) | |
|
101 | ||||
|
102 | def test_magic_rerun(): | |||
|
103 | """Simple test for %rerun (no args -> rerun last line)""" | |||
|
104 | ip = get_ipython() | |||
|
105 | ip.run_cell("a = 10") | |||
|
106 | ip.run_cell("a += 1") | |||
|
107 | nt.assert_equal(ip.user_ns["a"], 11) | |||
|
108 | ip.run_cell("%rerun") | |||
|
109 | nt.assert_equal(ip.user_ns["a"], 12) |
General Comments 0
You need to be logged in to leave comments.
Login now