Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Separate 'Out' in user_ns from the output history logging.

  • Loading branch information...
commit 0c7eb96f95bbe19b30171d9a51262ec4a01656f0 1 parent 5fd0b6b
Thomas Kluyver authored
6 IPython/core/displayhook.py
@@ -275,13 +275,15 @@ def update_user_ns(self, result):
275 275 new_result = '_'+`self.prompt_count`
276 276 to_main[new_result] = result
277 277 self.shell.user_ns.update(to_main)
278   - # This is a defaultdict of lists, so we can always append
279   - self.shell.user_ns['_oh'][self.prompt_count].append(result)
  278 + self.shell.user_ns['_oh'][self.prompt_count] = result
280 279
281 280 def log_output(self, format_dict):
282 281 """Log the output."""
283 282 if self.shell.logger.log_output:
284 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 288 def finish_displayhook(self):
287 289 """Finish up all displayhook activities."""
21 IPython/core/history.py
@@ -47,8 +47,13 @@ class HistoryManager(Configurable):
47 47 input_hist_raw = List([""])
48 48 # A list of directories visited during session
49 49 dir_hist = List()
50   - # A dict of output history, keyed with ints from the shell's execution count
51   - output_hist = Instance(defaultdict)
  50 + # A dict of output history, keyed with ints from the shell's
  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 57 # String holding the path to the history file
53 58 hist_file = Unicode()
54 59 # The SQLite database
@@ -97,7 +102,7 @@ def __init__(self, shell, config=None):
97 102 self.new_session()
98 103
99 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 107 self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
103 108 '%quit', '%Exit', '%exit'])
@@ -227,8 +232,7 @@ def _get_hist_session(self, start=1, stop=None, raw=True, output=False):
227 232
228 233 for i in range(start, stop):
229 234 if output:
230   - output_item = [repr(x) for x in self.output_hist[i]]
231   - line = (input_hist[i], output_item)
  235 + line = (input_hist[i], self.output_hist_reprs.get(i))
232 236 else:
233 237 line = input_hist[i]
234 238 yield (0, i, line)
@@ -332,9 +336,12 @@ def store_inputs(self, line_num, source, source_raw=None):
332 336 self.shell.user_ns.update(to_main)
333 337
334 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 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 345 db_row = (self.session_number, line_num, output)
339 346 if self.db_cache_size > 1:
340 347 self.db_output_cache.append(db_row)
6 IPython/core/tests/test_history.py
@@ -38,7 +38,7 @@ def test_history():
38 38
39 39 ip.history_manager.db_log_output = True
40 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 42 ip.history_manager.store_output(3)
43 43
44 44 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
@@ -60,7 +60,7 @@ def test_history():
60 60
61 61 # Check get_hist_tail
62 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 64 (2, 1, (newcmds[0], None)),
65 65 (2, 2, (newcmds[1], None)),
66 66 (2, 3, (newcmds[2], None)),]
@@ -70,7 +70,7 @@ def test_history():
70 70 gothist = ip.history_manager.get_hist_search("*test*")
71 71 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
72 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 75 # Cross testing: check that magic %save can get previous session.
76 76 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))

0 comments on commit 0c7eb96

Please sign in to comment.
Something went wrong with that request. Please try again.