diff --git a/Lib/pdb.py b/Lib/pdb.py index b7f6fd4323407e..da5d3dc74bb753 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1547,6 +1547,24 @@ def do_down(self, arg): self._select_frame(newframe) do_d = do_down + def do_frame(self, arg): + """f(rame) [frame index] + + Move the current frame levels to the new stack frame + according to the argument (from bt command). + """ + try: + new_frame_idx = int(arg) + except ValueError: + self.error("The 'frame' command requires a frame index (from bt command)") + + if new_frame_idx >= len(self.stack) or new_frame_idx < 0: + self.error('Wrong frame index, it should be in range [0, {}]'.format(len(self.stack) - 1)) + return + + self._select_frame(new_frame_idx) + do_f = do_frame + def do_until(self, arg): """unt(il) [lineno] @@ -2122,17 +2140,26 @@ def print_stack_trace(self, count=None): else: stack_to_print = self.stack[-count:] try: - for frame_lineno in stack_to_print: - self.print_stack_entry(frame_lineno) + if count is None: + # only print frame idx when we print whole frame + for frame_idx, frame_lineno in enumerate(stack_to_print, start = 0): + self.print_stack_entry(frame_lineno, frame_idx) + return + else: + for frame_lineno in stack_to_print: + self.print_stack_entry(frame_lineno) except KeyboardInterrupt: pass - def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix): + def print_stack_entry(self, frame_lineno, frame_idx = -1, prompt_prefix=line_prefix): frame, lineno = frame_lineno + prefix = "" + if frame_idx != -1: + prefix += '#' + str(frame_idx) + ' ' if frame is self.curframe: - prefix = '> ' + prefix += '> ' else: - prefix = ' ' + prefix += ' ' self.message(prefix + self.format_stack_entry(frame_lineno, prompt_prefix)) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index e5f9848319021a..49417ea600e28d 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -148,11 +148,11 @@ def test_pdb_basic_commands(): [EOF] (Pdb) bt ... - (26)() + ... (26)() -> test_function() - (3)test_function() + ... (3)test_function() -> ret = test_function_2('baz') - > (1)test_function_2() + ... > (1)test_function_2() -> def test_function_2(foo, bar='default'): (Pdb) up > (3)test_function() @@ -960,23 +960,23 @@ def test_pdb_where_command(): -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() (Pdb) w ... - (13)() + ... (13)() -> test_function() - (2)test_function() + ... (2)test_function() -> f() - (2)f() + ... (2)f() -> g() - > (2)g() + ... > (2)g() -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() (Pdb) where ... - (13)() + ... (13)() -> test_function() - (2)test_function() + ... (2)test_function() -> f() - (2)f() + ... (2)f() -> g() - > (2)g() + ... > (2)g() -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() (Pdb) w 1 > (2)g() @@ -988,13 +988,13 @@ def test_pdb_where_command(): -> g() (Pdb) w ... - (13)() + ... (13)() -> test_function() - (2)test_function() + ... (2)test_function() -> f() - > (2)f() + ... > (2)f() -> g() - (2)g() + ... (2)g() -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() (Pdb) w 0 > (2)f() @@ -1648,11 +1648,11 @@ def test_post_mortem(): -> test_function_2() (Pdb) bt ... - (11)() + ... (11)() -> test_function() - > (3)test_function() + ... > (3)test_function() -> test_function_2() - (3)test_function_2() + ... (3)test_function_2() -> 1/0 (Pdb) list 1 def test_function(): @@ -3746,10 +3746,10 @@ def test_file_modified_after_execution(self): # the time.sleep is needed for low-resolution filesystems like HFS+ commands = """ filename = $_frame.f_code.co_filename - f = open(filename, "w") - f.write("print('goodbye')") + file = open(filename, "w") + file.write("print('goodbye')") import time; time.sleep(1) - f.close() + file.close() ll """