##// END OF EJS Templates
Separate 'Out' in user_ns from the output history logging.
Thomas Kluyver -
Show More
@@ -275,13 +275,15 b' class DisplayHook(Configurable):'
275 new_result = '_'+`self.prompt_count`
275 new_result = '_'+`self.prompt_count`
276 to_main[new_result] = result
276 to_main[new_result] = result
277 self.shell.user_ns.update(to_main)
277 self.shell.user_ns.update(to_main)
278 # This is a defaultdict of lists, so we can always append
278 self.shell.user_ns['_oh'][self.prompt_count] = result
279 self.shell.user_ns['_oh'][self.prompt_count].append(result)
280
279
281 def log_output(self, format_dict):
280 def log_output(self, format_dict):
282 """Log the output."""
281 """Log the output."""
283 if self.shell.logger.log_output:
282 if self.shell.logger.log_output:
284 self.shell.logger.log_write(format_dict['text/plain'], 'output')
283 self.shell.logger.log_write(format_dict['text/plain'], 'output')
284 # This is a defaultdict of lists, so we can always append
285 self.shell.history_manager.output_hist_reprs[self.prompt_count]\
286 .append(format_dict['text/plain'])
285
287
286 def finish_displayhook(self):
288 def finish_displayhook(self):
287 """Finish up all displayhook activities."""
289 """Finish up all displayhook activities."""
@@ -47,8 +47,13 b' class HistoryManager(Configurable):'
47 input_hist_raw = List([""])
47 input_hist_raw = List([""])
48 # A list of directories visited during session
48 # A list of directories visited during session
49 dir_hist = List()
49 dir_hist = List()
50 # A dict of output history, keyed with ints from the shell's execution count
50 # A dict of output history, keyed with ints from the shell's
51 output_hist = Instance(defaultdict)
51 # execution count. If there are several outputs from one command,
52 # only the last one is stored.
53 output_hist = Dict()
54 # Contains all outputs, in lists of reprs.
55 output_hist_reprs = Instance(defaultdict)
56
52 # String holding the path to the history file
57 # String holding the path to the history file
53 hist_file = Unicode()
58 hist_file = Unicode()
54 # The SQLite database
59 # The SQLite database
@@ -97,7 +102,7 b' class HistoryManager(Configurable):'
97 self.new_session()
102 self.new_session()
98
103
99 self._i00, self._i, self._ii, self._iii = '','','',''
104 self._i00, self._i, self._ii, self._iii = '','','',''
100 self.output_hist = defaultdict(list)
105 self.output_hist_reprs = defaultdict(list)
101
106
102 self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
107 self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
103 '%quit', '%Exit', '%exit'])
108 '%quit', '%Exit', '%exit'])
@@ -227,8 +232,7 b' class HistoryManager(Configurable):'
227
232
228 for i in range(start, stop):
233 for i in range(start, stop):
229 if output:
234 if output:
230 output_item = [repr(x) for x in self.output_hist[i]]
235 line = (input_hist[i], self.output_hist_reprs.get(i))
231 line = (input_hist[i], output_item)
232 else:
236 else:
233 line = input_hist[i]
237 line = input_hist[i]
234 yield (0, i, line)
238 yield (0, i, line)
@@ -332,9 +336,12 b' class HistoryManager(Configurable):'
332 self.shell.user_ns.update(to_main)
336 self.shell.user_ns.update(to_main)
333
337
334 def store_output(self, line_num):
338 def store_output(self, line_num):
335 if (not self.db_log_output) or not self.output_hist[line_num]:
339 """If database output logging is enabled, this saves all the
340 outputs from the indicated prompt number to the database. It's
341 called by run_cell after code has been executed."""
342 if (not self.db_log_output) or not self.output_hist_reprs[line_num]:
336 return
343 return
337 output = json.dumps([repr(x) for x in self.output_hist[line_num]])
344 output = json.dumps(self.output_hist_reprs[line_num])
338 db_row = (self.session_number, line_num, output)
345 db_row = (self.session_number, line_num, output)
339 if self.db_cache_size > 1:
346 if self.db_cache_size > 1:
340 self.db_output_cache.append(db_row)
347 self.db_output_cache.append(db_row)
@@ -38,7 +38,7 b' def test_history():'
38
38
39 ip.history_manager.db_log_output = True
39 ip.history_manager.db_log_output = True
40 # Doesn't match the input, but we'll just check it's stored.
40 # Doesn't match the input, but we'll just check it's stored.
41 ip.history_manager.output_hist[3].append("spam")
41 ip.history_manager.output_hist_reprs[3].append("spam")
42 ip.history_manager.store_output(3)
42 ip.history_manager.store_output(3)
43
43
44 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
44 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
@@ -60,7 +60,7 b' def test_history():'
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 expected = [(1, 3, (hist[-1], [repr("spam")])),
63 expected = [(1, 3, (hist[-1], ["spam"])),
64 (2, 1, (newcmds[0], None)),
64 (2, 1, (newcmds[0], None)),
65 (2, 2, (newcmds[1], None)),
65 (2, 2, (newcmds[1], None)),
66 (2, 3, (newcmds[2], None)),]
66 (2, 3, (newcmds[2], None)),]
@@ -70,7 +70,7 b' def test_history():'
70 gothist = ip.history_manager.get_hist_search("*test*")
70 gothist = ip.history_manager.get_hist_search("*test*")
71 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
71 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
72 gothist = ip.history_manager.get_hist_search("b*", output=True)
72 gothist = ip.history_manager.get_hist_search("b*", output=True)
73 nt.assert_equal(list(gothist), [(1,3,(hist[2],[repr("spam")]))] )
73 nt.assert_equal(list(gothist), [(1,3,(hist[2],["spam"]))] )
74
74
75 # Cross testing: check that magic %save can get previous session.
75 # Cross testing: check that magic %save can get previous session.
76 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
76 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
General Comments 0
You need to be logged in to leave comments. Login now