https://github.com/python/cpython/commit/77cebb1ce9baac9e01a45d34113c3bea74940d90
commit: 77cebb1ce9baac9e01a45d34113c3bea74940d90
branch: main
author: Tian Gao <[email protected]>
committer: gaogaotiantian <[email protected]>
date: 2024-10-17T20:29:11-04:00
summary:

gh-125600: Only show stale code warning on source code display commands 
(#125601)

files:
A Misc/NEWS.d/next/Library/2024-10-16-15-55-50.gh-issue-125600.yMsJx0.rst
M Lib/pdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/pdb.py b/Lib/pdb.py
index 3e5e6088fdcc7e..cd7a7042fa6987 100644
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -402,6 +402,8 @@ def setup(self, f, tb):
         self.curframe = self.stack[self.curindex][0]
         self.set_convenience_variable(self.curframe, '_frame', self.curframe)
 
+        self._save_initial_file_mtime(self.curframe)
+
         if self._chained_exceptions:
             self.set_convenience_variable(
                 self.curframe,
@@ -494,9 +496,21 @@ def _cmdloop(self):
             except KeyboardInterrupt:
                 self.message('--KeyboardInterrupt--')
 
+    def _save_initial_file_mtime(self, frame):
+        """save the mtime of the all the files in the frame stack in the file 
mtime table
+        if they haven't been saved yet."""
+        while frame:
+            filename = frame.f_code.co_filename
+            if filename not in self._file_mtime_table:
+                try:
+                    self._file_mtime_table[filename] = 
os.path.getmtime(filename)
+                except Exception:
+                    pass
+            frame = frame.f_back
+
     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."""
+        """Check if the source file of the current frame has been modified.
+        If so, give a warning and reset the modify time to current."""
         try:
             filename = self.curframe.f_code.co_filename
             mtime = os.path.getmtime(filename)
@@ -506,7 +520,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
@@ -836,7 +850,6 @@ def onecmd(self, line):
         a breakpoint command list definition.
         """
         if not self.commands_defining:
-            self._validate_file_mtime()
             if line.startswith('_pdbcmd'):
                 command, arg, line = self.parseline(line)
                 if hasattr(self, command):
@@ -980,6 +993,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):
@@ -1861,6 +1875,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):
@@ -1879,6 +1894,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):
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 8136c591a33001..7e6f276d355a14 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -3711,6 +3711,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 = """
diff --git 
a/Misc/NEWS.d/next/Library/2024-10-16-15-55-50.gh-issue-125600.yMsJx0.rst 
b/Misc/NEWS.d/next/Library/2024-10-16-15-55-50.gh-issue-125600.yMsJx0.rst
new file mode 100644
index 00000000000000..19bf4fbefb601b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-10-16-15-55-50.gh-issue-125600.yMsJx0.rst
@@ -0,0 +1 @@
+Only show stale code warning in :mod:`pdb` when we display source code.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to