@@ -938,12 +938,14 @@ def test_pdb_issue_20766():
938
938
pdb 2: <built-in function default_int_handler>
939
939
"""
940
940
941
- class PdbBaseTestCase (unittest .TestCase ):
941
+
942
+ class PdbTestCase (unittest .TestCase ):
943
+ def tearDown (self ):
944
+ support .unlink (support .TESTFN )
945
+
942
946
def _run_pdb (self , pdb_args , commands ):
943
- """Run 'script' lines with pdb and the pdb 'commands'."""
944
947
self .addCleanup (support .rmtree , '__pycache__' )
945
948
cmd = [sys .executable , '-m' , 'pdb' ] + pdb_args
946
- stdout = stderr = None
947
949
with subprocess .Popen (
948
950
cmd ,
949
951
stdout = subprocess .PIPE ,
@@ -955,8 +957,27 @@ def _run_pdb(self, pdb_args, commands):
955
957
stderr = stderr and bytes .decode (stderr )
956
958
return stdout , stderr
957
959
960
+ def run_pdb_script (self , script , commands ):
961
+ """Run 'script' lines with pdb and the pdb 'commands'."""
962
+ filename = 'main.py'
963
+ with open (filename , 'w' ) as f :
964
+ f .write (textwrap .dedent (script ))
965
+ self .addCleanup (support .unlink , filename )
966
+ return self ._run_pdb ([filename ], commands )
958
967
959
- class PdbScriptTestCase (PdbBaseTestCase ):
968
+ def run_pdb_module (self , script , commands ):
969
+ """Runs the script code as part of a module"""
970
+ self .module_name = 't_main'
971
+ support .rmtree (self .module_name )
972
+ main_file = self .module_name + '/__main__.py'
973
+ init_file = self .module_name + '/__init__.py'
974
+ os .mkdir (self .module_name )
975
+ with open (init_file , 'w' ) as f :
976
+ pass
977
+ with open (main_file , 'w' ) as f :
978
+ f .write (textwrap .dedent (script ))
979
+ self .addCleanup (support .rmtree , self .module_name )
980
+ return self ._run_pdb (['-m' , self .module_name ], commands )
960
981
961
982
def _assert_find_function (self , file_content , func_name , expected ):
962
983
file_content = textwrap .dedent (file_content )
@@ -969,13 +990,6 @@ def _assert_find_function(self, file_content, func_name, expected):
969
990
self .assertEqual (
970
991
expected , pdb .find_function (func_name , support .TESTFN ))
971
992
972
- def run_pdb (self , script , commands ):
973
- filename = 'main.py'
974
- with open (filename , 'w' ) as f :
975
- f .write (textwrap .dedent (script ))
976
- self .addCleanup (support .unlink , filename )
977
- return self ._run_pdb ([filename ], commands )
978
-
979
993
def test_find_function_empty_file (self ):
980
994
self ._assert_find_function ('' , 'foo' , None )
981
995
@@ -1041,7 +1055,7 @@ def bar():
1041
1055
with open ('bar.py' , 'w' ) as f :
1042
1056
f .write (textwrap .dedent (bar ))
1043
1057
self .addCleanup (support .unlink , 'bar.py' )
1044
- stdout , stderr = self .run_pdb (script , commands )
1058
+ stdout , stderr = self .run_pdb_script (script , commands )
1045
1059
self .assertTrue (
1046
1060
any ('main.py(5)foo()->None' in l for l in stdout .splitlines ()),
1047
1061
'Fail to step into the caller after a return' )
@@ -1078,7 +1092,7 @@ def test_issue16180(self):
1078
1092
script = "def f: pass\n "
1079
1093
commands = ''
1080
1094
expected = "SyntaxError:"
1081
- stdout , stderr = self .run_pdb (script , commands )
1095
+ stdout , stderr = self .run_pdb_script (script , commands )
1082
1096
self .assertIn (expected , stdout ,
1083
1097
'\n \n Expected:\n {}\n Got:\n {}\n '
1084
1098
'Fail to handle a syntax error in the debuggee.'
@@ -1126,33 +1140,13 @@ def test_header(self):
1126
1140
pdb .set_trace (header = header )
1127
1141
self .assertEqual (stdout .getvalue (), header + '\n ' )
1128
1142
1129
- def tearDown (self ):
1130
- support .unlink (support .TESTFN )
1131
-
1132
-
1133
- class PdbModuleTestCase (PdbBaseTestCase ):
1134
- """Re-runs all tests used for a script but using a module"""
1135
-
1136
- def run_pdb (self , script , commands ):
1137
- self .module_name = 't_main'
1138
- support .rmtree (self .module_name )
1139
- main_file = self .module_name + '/__main__.py'
1140
- init_file = self .module_name + '/__init__.py'
1141
- os .mkdir (self .module_name )
1142
- with open (init_file , 'w' ) as f :
1143
- pass
1144
- with open (main_file , 'w' ) as f :
1145
- f .write (textwrap .dedent (script ))
1146
- self .addCleanup (support .rmtree , self .module_name )
1147
- return self ._run_pdb (['-m' , self .module_name ], commands )
1148
-
1149
1143
def test_run_module (self ):
1150
1144
script = """print("SUCCESS")"""
1151
1145
commands = """
1152
1146
continue
1153
1147
quit
1154
1148
"""
1155
- stdout , stderr = self .run_pdb (script , commands )
1149
+ stdout , stderr = self .run_pdb_module (script , commands )
1156
1150
self .assertTrue (any ("SUCCESS" in l for l in stdout .splitlines ()), stdout )
1157
1151
1158
1152
def test_module_is_run_as_main (self ):
@@ -1164,7 +1158,7 @@ def test_module_is_run_as_main(self):
1164
1158
continue
1165
1159
quit
1166
1160
"""
1167
- stdout , stderr = self .run_pdb (script , commands )
1161
+ stdout , stderr = self .run_pdb_module (script , commands )
1168
1162
self .assertTrue (any ("SUCCESS" in l for l in stdout .splitlines ()), stdout )
1169
1163
1170
1164
def test_breakpoint (self ):
@@ -1174,16 +1168,16 @@ def test_breakpoint(self):
1174
1168
print("SUCCESS")
1175
1169
pass
1176
1170
"""
1177
- commands = f """
1171
+ commands = """
1178
1172
b 3
1179
1173
quit
1180
1174
"""
1181
- stdout , stderr = self .run_pdb (script , commands )
1175
+ stdout , stderr = self .run_pdb_module (script , commands )
1182
1176
self .assertTrue (any ("Breakpoint 1 at" in l for l in stdout .splitlines ()), stdout )
1183
1177
self .assertTrue (all ("SUCCESS" not in l for l in stdout .splitlines ()), stdout )
1184
1178
1185
- def test_run_pdb (self ):
1186
- commands = f """
1179
+ def test_run_pdb_with_pdb (self ):
1180
+ commands = """
1187
1181
c
1188
1182
quit
1189
1183
"""
@@ -1208,11 +1202,11 @@ def test_blocks_at_first_code_line(self):
1208
1202
1209
1203
print("SUCCESS")
1210
1204
"""
1211
- commands = f """
1205
+ commands = """
1212
1206
quit
1213
1207
"""
1214
- stdout , stderr = self .run_pdb (script , commands )
1215
- self .assertTrue (any (f "__main__.py(4)<module>()"
1208
+ stdout , stderr = self .run_pdb_module (script , commands )
1209
+ self .assertTrue (any ("__main__.py(4)<module>()"
1216
1210
in l for l in stdout .splitlines ()), stdout )
1217
1211
1218
1212
def test_relative_imports (self ):
@@ -1239,7 +1233,7 @@ def test_relative_imports(self):
1239
1233
var = "VAR from module"
1240
1234
var2 = "second var"
1241
1235
""" ))
1242
- commands = f """
1236
+ commands = """
1243
1237
b 5
1244
1238
c
1245
1239
p top_var
@@ -1248,16 +1242,15 @@ def test_relative_imports(self):
1248
1242
quit
1249
1243
"""
1250
1244
stdout , _ = self ._run_pdb (['-m' , self .module_name ], commands )
1251
- self .assertTrue (any (f "VAR from module" in l for l in stdout .splitlines ()))
1252
- self .assertTrue (any (f "VAR from top" in l for l in stdout .splitlines ()))
1253
- self .assertTrue (any (f "second var" in l for l in stdout .splitlines ()))
1245
+ self .assertTrue (any ("VAR from module" in l for l in stdout .splitlines ()))
1246
+ self .assertTrue (any ("VAR from top" in l for l in stdout .splitlines ()))
1247
+ self .assertTrue (any ("second var" in l for l in stdout .splitlines ()))
1254
1248
1255
1249
1256
1250
def load_tests (* args ):
1257
1251
from test import test_pdb
1258
1252
suites = [
1259
- unittest .makeSuite (PdbScriptTestCase ),
1260
- unittest .makeSuite (PdbModuleTestCase ),
1253
+ unittest .makeSuite (PdbTestCase ),
1261
1254
doctest .DocTestSuite (test_pdb )
1262
1255
]
1263
1256
return unittest .TestSuite (suites )
0 commit comments