10000 gh-111051: Check if file is modifed during debugging in `pdb` by gaogaotiantian · Pull Request #111052 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111051: Check if file is modifed during debugging in pdb #111052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 25, 2024
Merged
Prev Previous commit
Next Next commit
Make _file_mtime_table class member
  • Loading branch information
gaogaotiantian committed Dec 11, 2023
commit dbf669de0ed0ea070bfea916fccefea24aef4570
4 changes: 2 additions & 2 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ class Pdb(bdb.Bdb, cmd.Cmd):
# but in case there are recursions, we stop at 999.
MAX_CHAINED_EXCEPTION_DEPTH = 999

_file_mtime_table = {}

def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
nosigint=False, readrc=True):
bdb.Bdb.__init__(self, skip=skip)
Expand Down Expand Up @@ -275,8 +277,6 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
self._chained_exceptions = tuple()
self._chained_exception_index = 0

self._file_mtime_table = {}

def sigint_handler(self, signum, frame):
if self.allow_kbdint:
raise KeyboardInterrupt
Expand Down
32 changes: 32 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2884,6 +2884,38 @@ def test_file_modified_after_execution(self):
self.assertIn("WARNING:", stdout)
self.assertIn("changed after pdb started", stdout)

def test_file_modified_after_execution_with_multiple_instances(self):
script = """
import pdb; pdb.Pdb().set_trace()
with open(__file__, "w") as f:
f.write("print('goodbye')\\n" * 5)
import pdb; pdb.Pdb().set_trace()
"""

commands = """
continue
continue
"""

filename = 'main.py'
with open(filename, 'w') as f:
f.write(textwrap.dedent(script))
self.addCleanup(os_helper.unlink, filename)
self.addCleanup(os_helper.rmtree, '__pycache__')
cmd = [sys.executable, filename]
with subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
) as proc:
stdout, stderr = proc.communicate(str.encode(commands))
stdout = stdout and bytes.decode(stdout)

self.assertEqual(proc.returncode, 0)
self.assertIn("WARNING:", stdout)
self.assertIn("changed after pdb started", stdout)

def test_relative_imports(self):
self.module_name = 't_main'
os_helper.rmtree(self.module_name)
Expand Down
0