Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move hlen_before_cell into _replace_rlhist_multiline

make hlen_before_cell a class member and move all multiline
history functionality into _replace_rlhist_multiline.
Closes gh-911
  • Loading branch information...
commit e68660659803dfc67b3d05e89f8b3013f6a11cae 1 parent 2e01c88
@juliantaylor authored
Showing with 6 additions and 7 deletions.
  1. +6 −7 IPython/frontend/terminal/interactiveshell.py
View
13 IPython/frontend/terminal/interactiveshell.py
@@ -229,15 +229,16 @@ def mainloop(self, display_banner=None):
# handling seems rather unpredictable...
self.write("\nKeyboardInterrupt in interact()\n")
- def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
+ def _replace_rlhist_multiline(self, source_raw):
"""Store multiple lines as a single entry in history"""
if self.multiline_history and self.has_readline:
hlen = self.readline.get_current_history_length()
- for i in range(hlen - hlen_before_cell):
+ for i in range(hlen - self.hlen_before_cell):
self.readline.remove_history_item(hlen - i - 1)
stdin_encoding = sys.stdin.encoding or "utf-8"
self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
stdin_encoding))
+ self.hlen_before_cell = self.readline.get_current_history_length()

Note that this would store the history length before we run the cell. If the user does raw_input() inside the cell, that will add an extra entry in readline history, which will make a mess of joining that cell back up.

@juliantaylor Owner

good point, although it will only delete the history of raw_input in some situations, there is no reason to save that at all.
Moving _replace_rlhist after run_cell should do the trick

I don't think it will, because then when you're doing the stuff to pull out history from the last cell and put it back, you'll add on any lines from raw_input commands in the current cell.

This needs code in two places: storing the hlen_before_cell must occur after the previous cell has executed and before the user starts entering code for the next cell. Modifying the readline history must occur after the user finishes entering code for the current cell, but before that code is run. After the code has executed, we could also remove anything added to readline history by the cell (we have a readline_no_record context manager already), but this is less pressing, and there are probably situations when the user wants those things to be available in readline history.

@juliantaylor Owner

the line from raw_input will not be part of source_raw which is readded to the history, so only the code cell is stored in history.
Maybe I misunderstood? Can you give an example input that produces problems?

Ah, no, you're right. I'd forgotten we were restoring the history based on source_raw, rather than on the lines that we've pulled out from readline. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
def interact(self, display_banner=None):
"""Closely emulate the interactive Python console."""
@@ -255,13 +256,13 @@ def interact(self, display_banner=None):
self.show_banner()
more = False
- hlen_before_cell = self.readline.get_current_history_length()
# Mark activity in the builtins
__builtin__.__dict__['__IPYTHON__active'] += 1
if self.has_readline:
self.readline_startup_hook(self.pre_readline)
+ self.hlen_before_cell = self.readline.get_current_history_length()
# exit_now is set by a call to %Exit or %Quit, through the
# ask_exit callback.
@@ -293,8 +294,7 @@ def interact(self, display_banner=None):
try:
self.write('\nKeyboardInterrupt\n')
source_raw = self.input_splitter.source_raw_reset()[1]
- self._replace_rlhist_multiline(source_raw, hlen_before_cell)
- hlen_before_cell = self.readline.get_current_history_length()
+ self._replace_rlhist_multiline(source_raw)
more = False
except KeyboardInterrupt:
pass
@@ -322,8 +322,7 @@ def interact(self, display_banner=None):
self.edit_syntax_error()
if not more:
source_raw = self.input_splitter.source_raw_reset()[1]
- self._replace_rlhist_multiline(source_raw, hlen_before_cell)
- hlen_before_cell = self.readline.get_current_history_length()
+ self._replace_rlhist_multiline(source_raw)
self.run_cell(source_raw, store_history=True)
# We are off again...
Please sign in to comment.
Something went wrong with that request. Please try again.