Skip to content

Commit

Permalink
Only show stale code warning on source code display commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed Oct 16, 2024
1 parent c9826c1 commit b83fe61
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,15 @@ def _cmdloop(self):
except KeyboardInterrupt:
self.message('--KeyboardInterrupt--')

def _update_file_mtime(self):
"""update the file mtime table with the current frame's file"""
try:
filename = self.curframe.f_code.co_filename
mtime = os.path.getmtime(filename)
self._file_mtime_table.setdefault(filename, mtime)
except Exception:
return

def _validate_file_mtime(self):
"""Check if the source file of the current frame has been modified since
the last time we saw it. If so, give a warning."""
Expand All @@ -505,7 +514,7 @@ def _validate_file_mtime(self):
mtime != self._file_mtime_table[filename]):
self.message(f"*** WARNING: file '{filename}' was edited, "
"running stale code until the program is rerun")
self._file_mtime_table[filename] = mtime
self._file_mtime_table[filename] = mtime

# Called before loop, handles display expressions
# Set up convenience variable containers
Expand Down Expand Up @@ -835,7 +844,7 @@ def onecmd(self, line):
a breakpoint command list definition.
"""
if not self.commands_defining:
self._validate_file_mtime()
self._update_file_mtime()
if line.startswith('_pdbcmd'):
command, arg, line = self.parseline(line)
if hasattr(self, command):
Expand Down Expand Up @@ -979,6 +988,7 @@ def completedefault(self, text, line, begidx, endidx):

def _pdbcmd_print_frame_status(self, arg):
self.print_stack_trace(0)
self._validate_file_mtime()
self._show_display()

def _pdbcmd_silence_frame_status(self, arg):
Expand Down Expand Up @@ -1860,6 +1870,7 @@ def do_list(self, arg):
self.message('[EOF]')
except KeyboardInterrupt:
pass
self._validate_file_mtime()
do_l = do_list

def do_longlist(self, arg):
Expand All @@ -1878,6 +1889,7 @@ def do_longlist(self, arg):
self.error(err)
return
self._print_lines(lines, lineno, breaklist, self.curframe)
self._validate_file_mtime()
do_ll = do_longlist

def do_source(self, arg):
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3662,6 +3662,25 @@ def test_file_modified_after_execution(self):
self.assertIn("WARNING:", stdout)
self.assertIn("was edited", stdout)

def test_file_modified_and_immediately_restarted(self):
script = """
print("hello")
"""

# 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')")
import time; time.sleep(1)
f.close()
restart
"""

stdout, stderr = self.run_pdb_script(script, commands)
self.assertNotIn("WARNING:", stdout)
self.assertNotIn("was edited", stdout)

def test_file_modified_after_execution_with_multiple_instances(self):
# the time.sleep is needed for low-resolution filesystems like HFS+
script = """
Expand Down

0 comments on commit b83fe61

Please sign in to comment.