##// END OF EJS Templates
Add context manager to reload readline history when leaving a block.
Thomas Kluyver -
Show More
@@ -130,6 +130,23 b' class SeparateStr(Str):'
130
130
131 class MultipleInstanceError(Exception):
131 class MultipleInstanceError(Exception):
132 pass
132 pass
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
133
150
134
151
135 #-----------------------------------------------------------------------------
152 #-----------------------------------------------------------------------------
@@ -737,7 +754,9 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)
740 pm()
757
758 with self.readline_no_record:
759 pm()
741
760
742 #-------------------------------------------------------------------------
761 #-------------------------------------------------------------------------
743 # Things related to IPython's various namespaces
762 # Things related to IPython's various namespaces
@@ -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()
1534
1553 self.readline_no_record = ReadlineNoRecord(self)
1535 # Load the last 1000 lines from history
1536 for _, _, cell in self.history_manager.get_tail(1000,
1537 include_latest=True):
1538 if cell.strip(): # Ignore blank lines
1539 for line in cell.splitlines():
1540 readline.add_history(line.encode(stdin_encoding))
1541
1554
1542 # Configure auto-indent for all platforms
1555 # Configure auto-indent for all platforms
1543 self.set_autoindent(self.autoindent)
1556 self.set_autoindent(self.autoindent)
1557
1558 def refill_readline_hist(self):
1559 # Load the last 1000 lines from history
1560 self.readline.clear_history()
1561 stdin_encoding = sys.stdin.encoding or "utf-8"
1562 for _, _, cell in self.history_manager.get_tail(1000,
1563 include_latest=True):
1564 if cell.strip(): # Ignore blank lines
1565 for line in cell.splitlines():
1566 self.readline.add_history(line.encode(stdin_encoding))
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,88 +1600,89 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:
1605 if opts.has_key('p'):
1604 stats = None
1606 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1605 with self.readline_no_record:
1607 else:
1606 if opts.has_key('p'):
1608 if opts.has_key('d'):
1607 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1609 deb = debugger.Pdb(self.shell.colors)
1610 # reset Breakpoint state, which is moronically kept
1611 # in a class
1612 bdb.Breakpoint.next = 1
1613 bdb.Breakpoint.bplist = {}
1614 bdb.Breakpoint.bpbynumber = [None]
1615 # Set an initial breakpoint to stop execution
1616 maxtries = 10
1617 bp = int(opts.get('b',[1])[0])
1618 checkline = deb.checkline(filename,bp)
1619 if not checkline:
1620 for bp in range(bp+1,bp+maxtries+1):
1621 if deb.checkline(filename,bp):
1622 break
1623 else:
1624 msg = ("\nI failed to find a valid line to set "
1625 "a breakpoint\n"
1626 "after trying up to line: %s.\n"
1627 "Please set a valid breakpoint manually "
1628 "with the -b option." % bp)
1629 error(msg)
1630 return
1631 # if we find a good linenumber, set the breakpoint
1632 deb.do_break('%s:%s' % (filename,bp))
1633 # Start file run
1634 print "NOTE: Enter 'c' at the",
1635 print "%s prompt to start your script." % deb.prompt
1636 try:
1637 deb.run('execfile("%s")' % filename,prog_ns)
1638
1639 except:
1640 etype, value, tb = sys.exc_info()
1641 # Skip three frames in the traceback: the %run one,
1642 # one inside bdb.py, and the command-line typed by the
1643 # user (run by exec in pdb itself).
1644 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1645 else:
1608 else:
1646 if runner is None:
1609 if opts.has_key('d'):
1647 runner = self.shell.safe_execfile
1610 deb = debugger.Pdb(self.shell.colors)
1648 if opts.has_key('t'):
1611 # reset Breakpoint state, which is moronically kept
1649 # timed execution
1612 # in a class
1650 try:
1613 bdb.Breakpoint.next = 1
1651 nruns = int(opts['N'][0])
1614 bdb.Breakpoint.bplist = {}
1652 if nruns < 1:
1615 bdb.Breakpoint.bpbynumber = [None]
1653 error('Number of runs must be >=1')
1616 # Set an initial breakpoint to stop execution
1617 maxtries = 10
1618 bp = int(opts.get('b',[1])[0])
1619 checkline = deb.checkline(filename,bp)
1620 if not checkline:
1621 for bp in range(bp+1,bp+maxtries+1):
1622 if deb.checkline(filename,bp):
1623 break
1624 else:
1625 msg = ("\nI failed to find a valid line to set "
1626 "a breakpoint\n"
1627 "after trying up to line: %s.\n"
1628 "Please set a valid breakpoint manually "
1629 "with the -b option." % bp)
1630 error(msg)
1654 return
1631 return
1655 except (KeyError):
1632 # if we find a good linenumber, set the breakpoint
1656 nruns = 1
1633 deb.do_break('%s:%s' % (filename,bp))
1657 if nruns == 1:
1634 # Start file run
1658 t0 = clock2()
1635 print "NOTE: Enter 'c' at the",
1659 runner(filename,prog_ns,prog_ns,
1636 print "%s prompt to start your script." % deb.prompt
1660 exit_ignore=exit_ignore)
1637 try:
1661 t1 = clock2()
1638 deb.run('execfile("%s")' % filename,prog_ns)
1662 t_usr = t1[0]-t0[0]
1663 t_sys = t1[1]-t0[1]
1664 print "\nIPython CPU timings (estimated):"
1665 print " User : %10s s." % t_usr
1666 print " System: %10s s." % t_sys
1667 else:
1668 runs = range(nruns)
1669 t0 = clock2()
1670 for nr in runs:
1671 runner(filename,prog_ns,prog_ns,
1672 exit_ignore=exit_ignore)
1673 t1 = clock2()
1674 t_usr = t1[0]-t0[0]
1675 t_sys = t1[1]-t0[1]
1676 print "\nIPython CPU timings (estimated):"
1677 print "Total runs performed:",nruns
1678 print " Times : %10s %10s" % ('Total','Per run')
1679 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1680 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1681
1639
1640 except:
1641 etype, value, tb = sys.exc_info()
1642 # Skip three frames in the traceback: the %run one,
1643 # one inside bdb.py, and the command-line typed by the
1644 # user (run by exec in pdb itself).
1645 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1682 else:
1646 else:
1683 # regular execution
1647 if runner is None:
1684 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1648 runner = self.shell.safe_execfile
1649 if opts.has_key('t'):
1650 # timed execution
1651 try:
1652 nruns = int(opts['N'][0])
1653 if nruns < 1:
1654 error('Number of runs must be >=1')
1655 return
1656 except (KeyError):
1657 nruns = 1
1658 if nruns == 1:
1659 t0 = clock2()
1660 runner(filename,prog_ns,prog_ns,
1661 exit_ignore=exit_ignore)
1662 t1 = clock2()
1663 t_usr = t1[0]-t0[0]
1664 t_sys = t1[1]-t0[1]
1665 print "\nIPython CPU timings (estimated):"
1666 print " User : %10s s." % t_usr
1667 print " System: %10s s." % t_sys
1668 else:
1669 runs = range(nruns)
1670 t0 = clock2()
1671 for nr in runs:
1672 runner(filename,prog_ns,prog_ns,
1673 exit_ignore=exit_ignore)
1674 t1 = clock2()
1675 t_usr = t1[0]-t0[0]
1676 t_sys = t1[1]-t0[1]
1677 print "\nIPython CPU timings (estimated):"
1678 print "Total runs performed:",nruns
1679 print " Times : %10s %10s" % ('Total','Per run')
1680 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1681 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1682
1683 else:
1684 # regular execution
1685 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1685
1686
1686 if opts.has_key('i'):
1687 if opts.has_key('i'):
1687 self.shell.user_ns['__name__'] = __name__save
1688 self.shell.user_ns['__name__'] = __name__save
General Comments 0
You need to be logged in to leave comments. Login now