diff --git a/Lib/test/libregrtest/save_env.py b/Lib/test/libregrtest/save_env.py index 4149210fa17310..2ab1d76b945ceb 100644 --- a/Lib/test/libregrtest/save_env.py +++ b/Lib/test/libregrtest/save_env.py @@ -1,6 +1,7 @@ import builtins import locale import os +import signal import sys import threading @@ -72,7 +73,7 @@ def __init__(self, test_name, verbose, quiet, *, pgo): 'shutil_archive_formats', 'shutil_unpack_formats', 'asyncio.events._local._loop', 'urllib.requests._url_tempfiles', 'urllib.requests._opener', - 'stty_echo', + 'stty_echo', 'signal_handlers', ) def get_module(self, name): @@ -318,6 +319,14 @@ def restore_stty_echo(self, echo): attrs[3] &= ~termios.ECHO termios.tcsetattr(fd, termios.TCSADRAIN, attrs) + def get_signal_handlers(self): + # The two signals which are the most likely to be overridden + signums = [signal.SIGINT, signal.SIGTERM] + return {signum: signal.getsignal(signum) for signum in signums} + def restore_signal_handlers(self, handlers): + for signum, handler in handlers.items(): + signal.signal(signum, handler) + def resource_info(self): for name in self.resources: method_suffix = name.replace('.', '_') diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index bc9591d05de6b3..bcd02411f16152 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1,20 +1,21 @@ # A test suite for pdb; not very comprehensive at the moment. import _colorize +import bdb +import codecs import doctest import gc import io +import linecache import os import pdb import re import signal +import subprocess import sys +import textwrap import types -import codecs import unittest -import subprocess -import textwrap -import linecache import zipapp import zipfile @@ -3645,9 +3646,53 @@ def test_pdb_commands_last_breakpoint(): """ +def setUpPdb(test): + test.original_sigint_handler = signal.getsignal(signal.SIGINT) + test.previous_sigint_handler = pdb.Pdb._previous_sigint_handler + + +def tearDownPdb(test): + # Restore signal state even if a SIGINT doctest fails. + signal.signal(signal.SIGINT, test.original_sigint_handler) + pdb.Pdb._previous_sigint_handler = test.previous_sigint_handler + + # Ensure that asyncio state has been cleared at the end of the test. + # This prevents a "test altered the execution environment" warning if + # asyncio features are used. + set_event_loop(None) + + # A doctest of pdb could have residues. For example, pdb could still + # be running, or breakpoints might be left uncleared. These residues + # could potentially interfere with the following test, especially + # when we switch backends. Here we clear all the residues to restore + # to its pre-test state. + + # clear all the breakpoints left + bdb.Breakpoint.clearBreakpoints() + + # Stop tracing and clear the pdb instance cache + if pdb.Pdb._last_pdb_instance: + pdb.Pdb._last_pdb_instance.stop_trace() + pdb.Pdb._last_pdb_instance = None + + # If garbage objects are collected right after we start tracing, we + # could stop at __del__ of the object which would fail the test. + gc.collect() + + +class BaseTestCase(unittest.TestCase): + @classmethod + def setUpClass(cls): + setUpPdb(cls) + + @classmethod + def tearDownClass(cls): + tearDownPdb(cls) + + @support.force_not_colorized_test_class @support.requires_subprocess() -class PdbTestCase(unittest.TestCase): +class PdbTestCase(BaseTestCase): def tearDown(self): os_helper.unlink(os_helper.TESTFN) @@ -4950,7 +4995,7 @@ def test_pyrepl_available(self): self.assertTrue(pdb._pyrepl_available()) -class ChecklineTests(unittest.TestCase): +class ChecklineTests(BaseTestCase): def setUp(self): linecache.clearcache() # Pdb.checkline() uses linecache.getline() @@ -4988,7 +5033,7 @@ def test_checkline_is_not_executable(self): @support.requires_subprocess() -class PdbTestInline(unittest.TestCase): +class PdbTestInline(BaseTestCase): @unittest.skipIf(sys.flags.safe_path, 'PYTHONSAFEPATH changes default sys.path') def _run_script(self, script, commands, @@ -5147,7 +5192,7 @@ def test_alternate_stdin(self): @support.force_colorized_test_class -class PdbTestColorize(unittest.TestCase): +class PdbTestColorize(BaseTestCase): def setUp(self): self._original_can_colorize = _colorize.can_colorize # Force colorize to be enabled because we are sending data @@ -5215,7 +5260,7 @@ def test_gen_colors(self): @support.force_not_colorized_test_class @support.requires_subprocess() -class TestREPLSession(unittest.TestCase): +class TestREPLSession(BaseTestCase): def test_return_from_inline_mode_to_REPL(self): # GH-124703: Raise BdbQuit when exiting pdb in REPL session. # This allows the REPL session to continue. @@ -5238,10 +5283,11 @@ def test_return_from_inline_mode_to_REPL(self): @support.force_not_colorized_test_class @support.requires_subprocess() -class PdbTestReadline(unittest.TestCase): +class PdbTestReadline(BaseTestCase): @classmethod def setUpClass(cls): + super().setUpClass() # Ensure that the readline module is loaded # If this fails, the test is skipped because SkipTest will be raised readline = import_module('readline') @@ -5495,38 +5541,11 @@ def setUpPdbBackend(backend): def setUp(test): import pdb pdb.set_default_backend(backend) - test.original_sigint_handler = signal.getsignal(signal.SIGINT) - test.previous_sigint_handler = pdb.Pdb._previous_sigint_handler + setUpPdb(test) return setUp def tearDown(test): - # Restore signal state even if a SIGINT doctest fails. - signal.signal(signal.SIGINT, test.original_sigint_handler) - pdb.Pdb._previous_sigint_handler = test.previous_sigint_handler - - # Ensure that asyncio state has been cleared at the end of the test. - # This prevents a "test altered the execution environment" warning if - # asyncio features are used. - set_event_loop(None) - - # A doctest of pdb could have residues. For example, pdb could still - # be running, or breakpoints might be left uncleared. These residues - # could potentially interfere with the following test, especially - # when we switch backends. Here we clear all the residues to restore - # to its pre-test state. - - # clear all the breakpoints left - import bdb - bdb.Breakpoint.clearBreakpoints() - - # Stop tracing and clear the pdb instance cache - if pdb.Pdb._last_pdb_instance: - pdb.Pdb._last_pdb_instance.stop_trace() - pdb.Pdb._last_pdb_instance = None - - # If garbage objects are collected right after we start tracing, we - # could stop at __del__ of the object which would fail the test. - gc.collect() + tearDownPdb(test) tests.addTest( doctest.DocTestSuite( diff --git a/Misc/NEWS.d/next/Tests/2026-11-20-01-04-44.gh-issue-162300.8xBrs0.rst b/Misc/NEWS.d/next/Tests/2026-11-20-01-04-44.gh-issue-162300.8xBrs0.rst new file mode 100644 index 00000000000000..c993991dcbc992 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-11-20-01-04-44.gh-issue-162300.8xBrs0.rst @@ -0,0 +1,3 @@ +test_pdb now saves/restores :const:`~signal.SIGINT` signal handler. +Previously, test_signal failed when run after test_pdb in the same process. +Patch by Victor Stinner.