From b83fe614024a617665e1ab700692e526b26a19fc Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Wed, 16 Oct 2024 11:53:40 -0400 Subject: [PATCH 1/8] Only show stale code warning on source code display commands --- Lib/pdb.py | 16 ++++++++++++++-- Lib/test/test_pdb.py | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 2b36b1e3fa7cbe..f8b9ac398e107a 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -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.""" @@ -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 @@ -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): @@ -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): @@ -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): @@ -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): diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 474d31f1ae03d9..059cb767556c77 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -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 = """ From 121900372579d9fed460d9cfc6d02c8ba6a6321e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 15:55:52 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-10-16-15-55-50.gh-issue-125600.yMsJx0.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-10-16-15-55-50.gh-issue-125600.yMsJx0.rst 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. From 3fbac42f67a852254cb9a216d1bb40e72b630c10 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 17 Oct 2024 11:49:58 -0400 Subject: [PATCH 3/8] Only read the disk when it's needed --- Lib/pdb.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index f8b9ac398e107a..ed7f4b70d6f574 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -494,11 +494,12 @@ def _cmdloop(self): self.message('--KeyboardInterrupt--') def _update_file_mtime(self): - """update the file mtime table with the current frame's file""" + """update the file mtime table with the current frame's file if it + hasn't been seen yet.""" try: filename = self.curframe.f_code.co_filename - mtime = os.path.getmtime(filename) - self._file_mtime_table.setdefault(filename, mtime) + if filename not in self._file_mtime_table: + self._file_mtime_table[filename] = os.path.getmtime(filename) except Exception: return From 9e2949c58d554e1de8ac0a1b8045980dc5fda406 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 17 Oct 2024 12:30:34 -0400 Subject: [PATCH 4/8] Move try block into if --- Lib/pdb.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index ed7f4b70d6f574..c10eb0c3d5db7c 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -496,12 +496,12 @@ def _cmdloop(self): def _update_file_mtime(self): """update the file mtime table with the current frame's file if it hasn't been seen yet.""" - try: - filename = self.curframe.f_code.co_filename - if filename not in self._file_mtime_table: + filename = self.curframe.f_code.co_filename + if filename not in self._file_mtime_table: + try: self._file_mtime_table[filename] = os.path.getmtime(filename) - except Exception: - return + except Exception: + pass def _validate_file_mtime(self): """Check if the source file of the current frame has been modified since From fc46da5ee30b5e9c7c35ab1205dbb5d96e7dbaff Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 17 Oct 2024 15:33:08 -0400 Subject: [PATCH 5/8] Rename function and update docstring --- Lib/pdb.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index c10eb0c3d5db7c..fcf97abb5f5e48 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -493,8 +493,8 @@ def _cmdloop(self): except KeyboardInterrupt: self.message('--KeyboardInterrupt--') - def _update_file_mtime(self): - """update the file mtime table with the current frame's file if it + def _init_file_mtime(self): + """initialize the file mtime table with the current frame's file if it hasn't been seen yet.""" filename = self.curframe.f_code.co_filename if filename not in self._file_mtime_table: @@ -504,8 +504,8 @@ def _update_file_mtime(self): pass 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) @@ -845,7 +845,7 @@ def onecmd(self, line): a breakpoint command list definition. """ if not self.commands_defining: - self._update_file_mtime() + self._init_file_mtime() if line.startswith('_pdbcmd'): command, arg, line = self.parseline(line) if hasattr(self, command): From 546ab72a909dbc21fc23eb60e36bc3f3c96dc592 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 17 Oct 2024 17:39:34 -0400 Subject: [PATCH 6/8] Move save_init_file_mtime to setup() --- Lib/pdb.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index fcf97abb5f5e48..4b8a72717bb33a 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -401,6 +401,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, @@ -493,15 +495,17 @@ def _cmdloop(self): except KeyboardInterrupt: self.message('--KeyboardInterrupt--') - def _init_file_mtime(self): - """initialize the file mtime table with the current frame's file if it - hasn't been seen yet.""" - filename = self.curframe.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 + def _save_initial_file_mtime(self, frame): + """save the mtile of the all the files in the frame stack in the file mtime table + if it hasn'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. @@ -845,7 +849,6 @@ def onecmd(self, line): a breakpoint command list definition. """ if not self.commands_defining: - self._init_file_mtime() if line.startswith('_pdbcmd'): command, arg, line = self.parseline(line) if hasattr(self, command): From 91bbe2a55edce2717a42d4e2b862bfe1eb0d3de0 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 17 Oct 2024 17:52:39 -0400 Subject: [PATCH 7/8] Update Lib/pdb.py Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 4b8a72717bb33a..c4418990cebdec 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -496,7 +496,7 @@ def _cmdloop(self): self.message('--KeyboardInterrupt--') def _save_initial_file_mtime(self, frame): - """save the mtile of the all the files in the frame stack in the file mtime table + """save the mtime of the all the files in the frame stack in the file mtime table if it hasn't been saved yet.""" while frame: filename = frame.f_code.co_filename From df9e11e13d28857b6306795df38b98f36c385395 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 17 Oct 2024 18:46:18 -0400 Subject: [PATCH 8/8] Update Lib/pdb.py Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index c4418990cebdec..7dcfbcc215d459 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -497,7 +497,7 @@ def _cmdloop(self): 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 it hasn't been saved yet.""" + if they haven't been saved yet.""" while frame: filename = frame.f_code.co_filename if filename not in self._file_mtime_table: