@@ -938,12 +938,14 @@ def test_pdb_issue_20766():
938938 pdb 2: <built-in function default_int_handler>
939939 """
940940
941- class PdbBaseTestCase (unittest .TestCase ):
941+
942+ class PdbTestCase (unittest .TestCase ):
943+ def tearDown (self ):
944+ support .unlink (support .TESTFN )
945+
942946 def _run_pdb (self , pdb_args , commands ):
943- """Run 'script' lines with pdb and the pdb 'commands'."""
944947 self .addCleanup (support .rmtree , '__pycache__' )
945948 cmd = [sys .executable , '-m' , 'pdb' ] + pdb_args
946- stdout = stderr = None
947949 with subprocess .Popen (
948950 cmd ,
949951 stdout = subprocess .PIPE ,
@@ -955,8 +957,27 @@ def _run_pdb(self, pdb_args, commands):
955957 stderr = stderr and bytes .decode (stderr )
956958 return stdout , stderr
957959
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 )
958967
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 )
960981
961982 def _assert_find_function (self , file_content , func_name , expected ):
962983 file_content = textwrap .dedent (file_content )
@@ -969,13 +990,6 @@ def _assert_find_function(self, file_content, func_name, expected):
969990 self .assertEqual (
970991 expected , pdb .find_function (func_name , support .TESTFN ))
971992
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-
979993 def test_find_function_empty_file (self ):
980994 self ._assert_find_function ('' , 'foo' , None )
981995
@@ -1041,7 +1055,7 @@ def bar():
10411055 with open ('bar.py' , 'w' ) as f :
10421056 f .write (textwrap .dedent (bar ))
10431057 self .addCleanup (support .unlink , 'bar.py' )
1044- stdout , stderr = self .run_pdb (script , commands )
1058+ stdout , stderr = self .run_pdb_script (script , commands )
10451059 self .assertTrue (
10461060 any ('main.py(5)foo()->None' in l for l in stdout .splitlines ()),
10471061 'Fail to step into the caller after a return' )
@@ -1078,7 +1092,7 @@ def test_issue16180(self):
10781092 script = "def f: pass\n "
10791093 commands = ''
10801094 expected = "SyntaxError:"
1081- stdout , stderr = self .run_pdb (script , commands )
1095+ stdout , stderr = self .run_pdb_script (script , commands )
10821096 self .assertIn (expected , stdout ,
10831097 '\n \n Expected:\n {}\n Got:\n {}\n '
10841098 'Fail to handle a syntax error in the debuggee.'
@@ -1126,33 +1140,13 @@ def test_header(self):
11261140 pdb .set_trace (header = header )
11271141 self .assertEqual (stdout .getvalue (), header + '\n ' )
11281142
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-
11491143 def test_run_module (self ):
11501144 script = """print("SUCCESS")"""
11511145 commands = """
11521146 continue
11531147 quit
11541148 """
1155- stdout , stderr = self .run_pdb (script , commands )
1149+ stdout , stderr = self .run_pdb_module (script , commands )
11561150 self .assertTrue (any ("SUCCESS" in l for l in stdout .splitlines ()), stdout )
11571151
11581152 def test_module_is_run_as_main (self ):
@@ -1164,7 +1158,7 @@ def test_module_is_run_as_main(self):
11641158 continue
11651159 quit
11661160 """
1167- stdout , stderr = self .run_pdb (script , commands )
1161+ stdout , stderr = self .run_pdb_module (script , commands )
11681162 self .assertTrue (any ("SUCCESS" in l for l in stdout .splitlines ()), stdout )
11691163
11701164 def test_breakpoint (self ):
@@ -1174,16 +1168,16 @@ def test_breakpoint(self):
11741168 print("SUCCESS")
11751169 pass
11761170 """
1177- commands = f """
1171+ commands = """
11781172 b 3
11791173 quit
11801174 """
1181- stdout , stderr = self .run_pdb (script , commands )
1175+ stdout , stderr = self .run_pdb_module (script , commands )
11821176 self .assertTrue (any ("Breakpoint 1 at" in l for l in stdout .splitlines ()), stdout )
11831177 self .assertTrue (all ("SUCCESS" not in l for l in stdout .splitlines ()), stdout )
11841178
1185- def test_run_pdb (self ):
1186- commands = f """
1179+ def test_run_pdb_with_pdb (self ):
1180+ commands = """
11871181 c
11881182 quit
11891183 """
@@ -1208,11 +1202,11 @@ def test_blocks_at_first_code_line(self):
12081202
12091203 print("SUCCESS")
12101204 """
1211- commands = f """
1205+ commands = """
12121206 quit
12131207 """
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>()"
12161210 in l for l in stdout .splitlines ()), stdout )
12171211
12181212 def test_relative_imports (self ):
@@ -1239,7 +1233,7 @@ def test_relative_imports(self):
12391233 var = "VAR from module"
12401234 var2 = "second var"
12411235 """ ))
1242- commands = f """
1236+ commands = """
12431237 b 5
12441238 c
12451239 p top_var
@@ -1248,16 +1242,15 @@ def test_relative_imports(self):
12481242 quit
12491243 """
12501244 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 ()))
12541248
12551249
12561250def load_tests (* args ):
12571251 from test import test_pdb
12581252 suites = [
1259- unittest .makeSuite (PdbScriptTestCase ),
1260- unittest .makeSuite (PdbModuleTestCase ),
1253+ unittest .makeSuite (PdbTestCase ),
12611254 doctest .DocTestSuite (test_pdb )
12621255 ]
12631256 return unittest .TestSuite (suites )
0 commit comments