Skip to content

Commit 77cebb1

Browse files
gh-125600: Only show stale code warning on source code display commands (#125601)
1 parent 7cf2dbc commit 77cebb1

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

Lib/pdb.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ def setup(self, f, tb):
402402
self.curframe = self.stack[self.curindex][0]
403403
self.set_convenience_variable(self.curframe, '_frame', self.curframe)
404404

405+
self._save_initial_file_mtime(self.curframe)
406+
405407
if self._chained_exceptions:
406408
self.set_convenience_variable(
407409
self.curframe,
@@ -494,9 +496,21 @@ def _cmdloop(self):
494496
except KeyboardInterrupt:
495497
self.message('--KeyboardInterrupt--')
496498

499+
def _save_initial_file_mtime(self, frame):
500+
"""save the mtime of the all the files in the frame stack in the file mtime table
501+
if they haven't been saved yet."""
502+
while frame:
503+
filename = frame.f_code.co_filename
504+
if filename not in self._file_mtime_table:
505+
try:
506+
self._file_mtime_table[filename] = os.path.getmtime(filename)
507+
except Exception:
508+
pass
509+
frame = frame.f_back
510+
497511
def _validate_file_mtime(self):
498-
"""Check if the source file of the current frame has been modified since
499-
the last time we saw it. If so, give a warning."""
512+
"""Check if the source file of the current frame has been modified.
513+
If so, give a warning and reset the modify time to current."""
500514
try:
501515
filename = self.curframe.f_code.co_filename
502516
mtime = os.path.getmtime(filename)
@@ -506,7 +520,7 @@ def _validate_file_mtime(self):
506520
mtime != self._file_mtime_table[filename]):
507521
self.message(f"*** WARNING: file '{filename}' was edited, "
508522
"running stale code until the program is rerun")
509-
self._file_mtime_table[filename] = mtime
523+
self._file_mtime_table[filename] = mtime
510524

511525
# Called before loop, handles display expressions
512526
# Set up convenience variable containers
@@ -836,7 +850,6 @@ def onecmd(self, line):
836850
a breakpoint command list definition.
837851
"""
838852
if not self.commands_defining:
839-
self._validate_file_mtime()
840853
if line.startswith('_pdbcmd'):
841854
command, arg, line = self.parseline(line)
842855
if hasattr(self, command):
@@ -980,6 +993,7 @@ def completedefault(self, text, line, begidx, endidx):
980993

981994
def _pdbcmd_print_frame_status(self, arg):
982995
self.print_stack_trace(0)
996+
self._validate_file_mtime()
983997
self._show_display()
984998

985999
def _pdbcmd_silence_frame_status(self, arg):
@@ -1861,6 +1875,7 @@ def do_list(self, arg):
18611875
self.message('[EOF]')
18621876
except KeyboardInterrupt:
18631877
pass
1878+
self._validate_file_mtime()
18641879
do_l = do_list
18651880

18661881
def do_longlist(self, arg):
@@ -1879,6 +1894,7 @@ def do_longlist(self, arg):
18791894
self.error(err)
18801895
return
18811896
self._print_lines(lines, lineno, breaklist, self.curframe)
1897+
self._validate_file_mtime()
18821898
do_ll = do_longlist
18831899

18841900
def do_source(self, arg):

Lib/test/test_pdb.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3711,6 +3711,25 @@ def test_file_modified_after_execution(self):
37113711
self.assertIn("WARNING:", stdout)
37123712
self.assertIn("was edited", stdout)
37133713

3714+
def test_file_modified_and_immediately_restarted(self):
3715+
script = """
3716+
print("hello")
3717+
"""
3718+
3719+
# the time.sleep is needed for low-resolution filesystems like HFS+
3720+
commands = """
3721+
filename = $_frame.f_code.co_filename
3722+
f = open(filename, "w")
3723+
f.write("print('goodbye')")
3724+
import time; time.sleep(1)
3725+
f.close()
3726+
restart
3727+
"""
3728+
3729+
stdout, stderr = self.run_pdb_script(script, commands)
3730+
self.assertNotIn("WARNING:", stdout)
3731+
self.assertNotIn("was edited", stdout)
3732+
37143733
def test_file_modified_after_execution_with_multiple_instances(self):
37153734
# the time.sleep is needed for low-resolution filesystems like HFS+
37163735
script = """
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Only show stale code warning in :mod:`pdb` when we display source code.

0 commit comments

Comments
 (0)