mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
gh-111051: Check if file is modifed during debugging in pdb
(#111052)
This commit is contained in:
parent
07ef63fb6a
commit
8278fa2f56
3 changed files with 103 additions and 0 deletions
|
@ -3056,6 +3056,87 @@ def bœr():
|
|||
self.assertTrue(any("__main__.py(4)<module>()"
|
||||
in l for l in stdout.splitlines()), stdout)
|
||||
|
||||
def test_file_modified_after_execution(self):
|
||||
script = """
|
||||
print("hello")
|
||||
"""
|
||||
|
||||
commands = """
|
||||
filename = $_frame.f_code.co_filename
|
||||
f = open(filename, "w")
|
||||
f.write("print('goodbye')")
|
||||
f.close()
|
||||
ll
|
||||
"""
|
||||
|
||||
stdout, stderr = self.run_pdb_script(script, commands)
|
||||
self.assertIn("WARNING:", stdout)
|
||||
self.assertIn("was edited", 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,
|
||||
env = {**os.environ, 'PYTHONIOENCODING': 'utf-8'},
|
||||
) as proc:
|
||||
stdout, _ = proc.communicate(str.encode(commands))
|
||||
stdout = stdout and bytes.decode(stdout)
|
||||
|
||||
self.assertEqual(proc.returncode, 0)
|
||||
self.assertIn("WARNING:", stdout)
|
||||
self.assertIn("was edited", stdout)
|
||||
|
||||
def test_file_modified_after_execution_with_restart(self):
|
||||
script = """
|
||||
import random
|
||||
# Any code with a source to step into so this script is not checked
|
||||
# for changes when it's being changed
|
||||
random.randint(1, 4)
|
||||
print("hello")
|
||||
"""
|
||||
|
||||
commands = """
|
||||
ll
|
||||
n
|
||||
s
|
||||
filename = $_frame.f_back.f_code.co_filename
|
||||
def change_file(content, filename):
|
||||
with open(filename, "w") as f:
|
||||
f.write(f"print({content})")
|
||||
|
||||
change_file('world', filename)
|
||||
restart
|
||||
ll
|
||||
"""
|
||||
|
||||
stdout, stderr = self.run_pdb_script(script, commands)
|
||||
# Make sure the code is running correctly and the file is edited
|
||||
self.assertIn("hello", stdout)
|
||||
self.assertIn("world", stdout)
|
||||
# The file was edited, but restart should clear the state and consider
|
||||
# the file as up to date
|
||||
self.assertNotIn("WARNING:", stdout)
|
||||
|
||||
def test_relative_imports(self):
|
||||
self.module_name = 't_main'
|
||||
os_helper.rmtree(self.module_name)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue