@@ -3056,6 +3056,87 @@ def test_blocks_at_first_code_line(self):
3056
3056
self .assertTrue (any ("__main__.py(4)<module>()"
3057
3057
in l for l in stdout .splitlines ()), stdout )
3058
3058
3059
+ def test_file_modified_after_execution (self ):
3060
+ script = """
3061
+ print("hello")
3062
+ """
3063
+
3064
+ commands = """
3065
+ filename = $_frame.f_code.co_filename
3066
+ f = open(filename, "w")
3067
+ f.write("print('goodbye')")
3068
+ f.close()
3069
+ ll
3070
+ """
3071
+
3072
+ stdout , stderr = self .run_pdb_script (script , commands )
3073
+ self .assertIn ("WARNING:" , stdout )
3074
+ self .assertIn ("was edited" , stdout )
3075
+
3076
+ def test_file_modified_after_execution_with_multiple_instances (self ):
3077
+ script = """
3078
+ import pdb; pdb.Pdb().set_trace()
3079
+ with open(__file__, "w") as f:
3080
+ f.write("print('goodbye')\\ n" * 5)
3081
+ import pdb; pdb.Pdb().set_trace()
3082
+ """
3083
+
3084
+ commands = """
3085
+ continue
3086
+ continue
3087
+ """
3088
+
3089
+ filename = 'main.py'
3090
+ with open (filename , 'w' ) as f :
3091
+ f .write (textwrap .dedent (script ))
3092
+ self .addCleanup (os_helper .unlink , filename )
3093
+ self .addCleanup (os_helper .rmtree , '__pycache__' )
3094
+ cmd = [sys .executable , filename ]
3095
+ with subprocess .Popen (
3096
+ cmd ,
3097
+ stdout = subprocess .PIPE ,
3098
+ stdin = subprocess .PIPE ,
3099
+ stderr = subprocess .STDOUT ,
3100
+ env = {** os .environ , 'PYTHONIOENCODING' : 'utf-8' },
3101
+ ) as proc :
3102
+ stdout , _ = proc .communicate (str .encode (commands ))
3103
+ stdout = stdout and bytes .decode (stdout )
3104
+
3105
+ self .assertEqual (proc .returncode , 0 )
3106
+ self .assertIn ("WARNING:" , stdout )
3107
+ self .assertIn ("was edited" , stdout )
3108
+
3109
+ def test_file_modified_after_execution_with_restart (self ):
3110
+ script = """
3111
+ import random
3112
+ # Any code with a source to step into so this script is not checked
3113
+ # for changes when it's being changed
3114
+ random.randint(1, 4)
3115
+ print("hello")
3116
+ """
3117
+
3118
+ commands = """
3119
+ ll
3120
+ n
3121
+ s
3122
+ filename = $_frame.f_back.f_code.co_filename
3123
+ def change_file(content, filename):
3124
+ with open(filename, "w") as f:
3125
+ f.write(f"print({content})")
3126
+
3127
+ change_file('world', filename)
3128
+ restart
3129
+ ll
3130
+ """
3131
+
3132
+ stdout , stderr = self .run_pdb_script (script , commands )
3133
+ # Make sure the code is running correctly and the file is edited
3134
+ self .assertIn ("hello" , stdout )
3135
+ self .assertIn ("world" , stdout )
3136
+ # The file was edited, but restart should clear the state and consider
3137
+ # the file as up to date
3138
+ self .assertNotIn ("WARNING:" , stdout )
3139
+
3059
3140
def test_relative_imports (self ):
3060
3141
self .module_name = 't_main'
3061
3142
os_helper .rmtree (self .module_name )
0 commit comments