##// END OF EJS Templates
Add context manager to reload readline history when leaving a block.
Thomas Kluyver -
Show More
@@ -131,6 +131,23 b' class SeparateStr(Str):'
131 class MultipleInstanceError(Exception):
131 class MultipleInstanceError(Exception):
132 pass
132 pass
133
133
134 class ReadlineNoRecord(object):
135 """Context manager to execute some code, then reload readline history
136 so that interactive input to the code doesn't appear when pressing up."""
137 def __init__(self, shell):
138 self.shell = shell
139 self._nested_level = 0
140
141 def __enter__(self):
142 self._nested_level += 1
143
144 def __exit__(self, type, value, traceback):
145 self._nested_level -= 1
146 if self._nested_level == 0:
147 self.shell.refill_readline_hist()
148 # Returning False will cause exceptions to propagate
149 return False
150
134
151
135 #-----------------------------------------------------------------------------
152 #-----------------------------------------------------------------------------
136 # Main IPython class
153 # Main IPython class
@@ -737,6 +754,8 b' class InteractiveShell(Configurable, Magic):'
737 else:
754 else:
738 # fallback to our internal debugger
755 # fallback to our internal debugger
739 pm = lambda : self.InteractiveTB.debugger(force=True)
756 pm = lambda : self.InteractiveTB.debugger(force=True)
757
758 with self.readline_no_record:
740 pm()
759 pm()
741
760
742 #-------------------------------------------------------------------------
761 #-------------------------------------------------------------------------
@@ -1530,17 +1549,21 b' class InteractiveShell(Configurable, Magic):'
1530 # otherwise we end up with a monster history after a while:
1549 # otherwise we end up with a monster history after a while:
1531 readline.set_history_length(self.history_length)
1550 readline.set_history_length(self.history_length)
1532
1551
1533 stdin_encoding = sys.stdin.encoding or "utf-8"
1552 self.refill_readline_hist()
1553 self.readline_no_record = ReadlineNoRecord(self)
1534
1554
1555 # Configure auto-indent for all platforms
1556 self.set_autoindent(self.autoindent)
1557
1558 def refill_readline_hist(self):
1535 # Load the last 1000 lines from history
1559 # Load the last 1000 lines from history
1560 self.readline.clear_history()
1561 stdin_encoding = sys.stdin.encoding or "utf-8"
1536 for _, _, cell in self.history_manager.get_tail(1000,
1562 for _, _, cell in self.history_manager.get_tail(1000,
1537 include_latest=True):
1563 include_latest=True):
1538 if cell.strip(): # Ignore blank lines
1564 if cell.strip(): # Ignore blank lines
1539 for line in cell.splitlines():
1565 for line in cell.splitlines():
1540 readline.add_history(line.encode(stdin_encoding))
1566 self.readline.add_history(line.encode(stdin_encoding))
1541
1542 # Configure auto-indent for all platforms
1543 self.set_autoindent(self.autoindent)
1544
1567
1545 def set_next_input(self, s):
1568 def set_next_input(self, s):
1546 """ Sets the 'default' input string for the next command line.
1569 """ Sets the 'default' input string for the next command line.
@@ -1600,8 +1600,9 b' Currently the magic system has the following functions:\\n"""'
1600 # every single object ever created.
1600 # every single object ever created.
1601 sys.modules[main_mod_name] = main_mod
1601 sys.modules[main_mod_name] = main_mod
1602
1602
1603 stats = None
1604 try:
1603 try:
1604 stats = None
1605 with self.readline_no_record:
1605 if opts.has_key('p'):
1606 if opts.has_key('p'):
1606 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1607 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1607 else:
1608 else:
General Comments 0
You need to be logged in to leave comments. Login now