Skip to content

Commit 54db81e

Browse files
committed
Address Tian's review
* Rename BaseTestCase to PdbTestBase. * PdbTestBase setUpClass() and tearDownClass() get an additional test positional parameter, so they can be reused for doctests.
1 parent 8155ddc commit 54db81e

1 file changed

Lines changed: 46 additions & 51 deletions

File tree

‎Lib/test/test_pdb.py‎

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,53 +3646,51 @@ def test_pdb_commands_last_breakpoint():
36463646
"""
36473647

36483648

3649-
def setUpPdb(test):
3650-
test.original_sigint_handler = signal.getsignal(signal.SIGINT)
3651-
test.previous_sigint_handler = pdb.Pdb._previous_sigint_handler
3652-
3653-
3654-
def tearDownPdb(test):
3655-
# Restore signal state even if a SIGINT doctest fails.
3656-
signal.signal(signal.SIGINT, test.original_sigint_handler)
3657-
pdb.Pdb._previous_sigint_handler = test.previous_sigint_handler
3658-
3659-
# Ensure that asyncio state has been cleared at the end of the test.
3660-
# This prevents a "test altered the execution environment" warning if
3661-
# asyncio features are used.
3662-
set_event_loop(None)
3663-
3664-
# A doctest of pdb could have residues. For example, pdb could still
3665-
# be running, or breakpoints might be left uncleared. These residues
3666-
# could potentially interfere with the following test, especially
3667-
# when we switch backends. Here we clear all the residues to restore
3668-
# to its pre-test state.
3669-
3670-
# clear all the breakpoints left
3671-
bdb.Breakpoint.clearBreakpoints()
3672-
3673-
# Stop tracing and clear the pdb instance cache
3674-
if pdb.Pdb._last_pdb_instance:
3675-
pdb.Pdb._last_pdb_instance.stop_trace()
3676-
pdb.Pdb._last_pdb_instance = None
3677-
3678-
# If garbage objects are collected right after we start tracing, we
3679-
# could stop at __del__ of the object which would fail the test.
3680-
gc.collect()
3681-
3682-
3683-
class BaseTestCase(unittest.TestCase):
3649+
class PdbTestBase(unittest.TestCase):
36843650
@classmethod
3685-
def setUpClass(cls):
3686-
setUpPdb(cls)
3651+
def setUpClass(cls, test=None):
3652+
# The positional 'test' parameter is used by DocTest
3653+
if test is None:
3654+
test = cls
3655+
test.original_sigint_handler = signal.getsignal(signal.SIGINT)
3656+
test.previous_sigint_handler = pdb.Pdb._previous_sigint_handler
36873657

36883658
@classmethod
3689-
def tearDownClass(cls):
3690-
tearDownPdb(cls)
3659+
def tearDownClass(cls, test=None):
3660+
# The positional 'test' parameter is used by DocTest
3661+
if test is None:
3662+
test = cls
3663+
# Restore signal state even if a SIGINT test fails.
3664+
signal.signal(signal.SIGINT, test.original_sigint_handler)
3665+
pdb.Pdb._previous_sigint_handler = test.previous_sigint_handler
3666+
3667+
# Ensure that asyncio state has been cleared at the end of the test.
3668+
# This prevents a "test altered the execution environment" warning if
3669+
# asyncio features are used.
3670+
set_event_loop(None)
3671+
3672+
# A doctest of pdb could have residues. For example, pdb could still
3673+
# be running, or breakpoints might be left uncleared. These residues
3674+
# could potentially interfere with the following test, especially
3675+
# when we switch backends. Here we clear all the residues to restore
3676+
# to its pre-test state.
3677+
3678+
# clear all the breakpoints left
3679+
bdb.Breakpoint.clearBreakpoints()
3680+
3681+
# Stop tracing and clear the pdb instance cache
3682+
if pdb.Pdb._last_pdb_instance:
3683+
pdb.Pdb._last_pdb_instance.stop_trace()
3684+
pdb.Pdb._last_pdb_instance = None
3685+
3686+
# If garbage objects are collected right after we start tracing, we
3687+
# could stop at __del__ of the object which would fail the test.
3688+
gc.collect()
36913689

36923690

36933691
@support.force_not_colorized_test_class
36943692
@support.requires_subprocess()
3695-
class PdbTestCase(BaseTestCase):
3693+
class PdbTestCase(PdbTestBase):
36963694
def tearDown(self):
36973695
os_helper.unlink(os_helper.TESTFN)
36983696

@@ -4995,7 +4993,7 @@ def test_pyrepl_available(self):
49954993
self.assertTrue(pdb._pyrepl_available())
49964994

49974995

4998-
class ChecklineTests(BaseTestCase):
4996+
class ChecklineTests(PdbTestBase):
49994997
def setUp(self):
50004998
linecache.clearcache() # Pdb.checkline() uses linecache.getline()
50014999

@@ -5033,7 +5031,7 @@ def test_checkline_is_not_executable(self):
50335031

50345032

50355033
@support.requires_subprocess()
5036-
class PdbTestInline(BaseTestCase):
5034+
class PdbTestInline(PdbTestBase):
50375035
@unittest.skipIf(sys.flags.safe_path,
50385036
'PYTHONSAFEPATH changes default sys.path')
50395037
def _run_script(self, script, commands,
@@ -5192,7 +5190,7 @@ def test_alternate_stdin(self):
51925190

51935191

51945192
@support.force_colorized_test_class
5195-
class PdbTestColorize(BaseTestCase):
5193+
class PdbTestColorize(PdbTestBase):
51965194
def setUp(self):
51975195
self._original_can_colorize = _colorize.can_colorize
51985196
# Force colorize to be enabled because we are sending data
@@ -5260,7 +5258,7 @@ def test_gen_colors(self):
52605258

52615259
@support.force_not_colorized_test_class
52625260
@support.requires_subprocess()
5263-
class TestREPLSession(BaseTestCase):
5261+
class TestREPLSession(PdbTestBase):
52645262
def test_return_from_inline_mode_to_REPL(self):
52655263
# GH-124703: Raise BdbQuit when exiting pdb in REPL session.
52665264
# This allows the REPL session to continue.
@@ -5283,7 +5281,7 @@ def test_return_from_inline_mode_to_REPL(self):
52835281

52845282
@support.force_not_colorized_test_class
52855283
@support.requires_subprocess()
5286-
class PdbTestReadline(BaseTestCase):
5284+
class PdbTestReadline(PdbTestBase):
52875285

52885286
@classmethod
52895287
def setUpClass(cls):
@@ -5541,24 +5539,21 @@ def setUpPdbBackend(backend):
55415539
def setUp(test):
55425540
import pdb
55435541
pdb.set_default_backend(backend)
5544-
setUpPdb(test)
5542+
PdbTestBase.setUpClass(test)
55455543
return setUp
55465544

5547-
def tearDown(test):
5548-
tearDownPdb(test)
5549-
55505545
tests.addTest(
55515546
doctest.DocTestSuite(
55525547
test_pdb,
55535548
setUp=setUpPdbBackend('monitoring'),
5554-
tearDown=tearDown,
5549+
tearDown=PdbTestBase.tearDownClass,
55555550
)
55565551
)
55575552
tests.addTest(
55585553
doctest.DocTestSuite(
55595554
test_pdb,
55605555
setUp=setUpPdbBackend('settrace'),
5561-
tearDown=tearDown,
5556+
tearDown=PdbTestBase.tearDownClass,
55625557
)
55635558
)
55645559
return tests

0 commit comments

Comments
 (0)