@@ -419,10 +419,18 @@ def interaction(self, frame, traceback):
419419 else :
420420 Pdb ._previous_sigint_handler = None
421421 self .setup (frame , traceback )
422- # if we have more commands to process, do not show the stack entry
423- if not self .cmdqueue :
422+ # We should print the stack entry if and only if the user input
423+ # is expected, and we should print it right before the user input.
424+ # If self.cmdqueue is not empty, we append a "w 0" command to the
425+ # queue, which is equivalent to print_stack_entry
426+ if self .cmdqueue :
427+ self .cmdqueue .append ('w 0' )
428+ else :
424429 self .print_stack_entry (self .stack [self .curindex ])
425430 self ._cmdloop ()
431+ # If "w 0" is not used, pop it out
432+ if self .cmdqueue and self .cmdqueue [- 1 ] == 'w 0' :
433+ self .cmdqueue .pop ()
426434 self .forget ()
427435
428436 def displayhook (self , obj ):
@@ -1045,13 +1053,24 @@ def do_clear(self, arg):
10451053 complete_cl = _complete_location
10461054
10471055 def do_where (self , arg ):
1048- """w(here)
1056+ """w(here) [count]
10491057
1050- Print a stack trace, with the most recent frame at the bottom.
1058+ Print a stack trace. If count is not specified, print the full stack.
1059+ If count is 0, print the current frame entry. If count is positive,
1060+ print count entries from the most recent frame. If count is negative,
1061+ print -count entries from the least recent frame.
10511062 An arrow indicates the "current frame", which determines the
10521063 context of most commands. 'bt' is an alias for this command.
10531064 """
1054- self .print_stack_trace ()
1065+ if not arg :
1066+ count = None
1067+ else :
1068+ try :
1069+ count = int (arg )
1070+ except ValueError :
1071+ self .error ('Invalid count (%s)' % arg )
1072+ return
1073+ self .print_stack_trace (count )
10551074 do_w = do_where
10561075 do_bt = do_where
10571076
@@ -1623,10 +1642,22 @@ def complete_unalias(self, text, line, begidx, endidx):
16231642 # It is also consistent with the up/down commands (which are
16241643 # compatible with dbx and gdb: up moves towards 'main()'
16251644 # and down moves towards the most recent stack frame).
1626-
1627- def print_stack_trace (self ):
1645+ # * if count is None, prints the full stack
1646+ # * if count = 0, prints the current frame entry
1647+ # * if count < 0, prints -count least recent frame entries
1648+ # * if count > 0, prints count most recent frame entries
1649+
1650+ def print_stack_trace (self , count = None ):
1651+ if count is None :
1652+ stack_to_print = self .stack
1653+ elif count == 0 :
1654+ stack_to_print = [self .stack [self .curindex ]]
1655+ elif count < 0 :
1656+ stack_to_print = self .stack [:- count ]
1657+ else :
1658+ stack_to_print = self .stack [- count :]
16281659 try :
1629- for frame_lineno in self . stack :
1660+ for frame_lineno in stack_to_print :
16301661 self .print_stack_entry (frame_lineno )
16311662 except KeyboardInterrupt :
16321663 pass
0 commit comments