From e8ed3a8c55801d72a0cf8e563c7e3f2fd7649d68 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 18 Mar 2025 08:15:05 +0800 Subject: [PATCH 1/3] Ensure PDB cleans up event loop policies after using asyncio. --- Lib/test/test_pdb.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 8cd634426bd88b..cd3e2dc7ba2c27 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -13,6 +13,7 @@ import zipapp import zipfile +from asyncio.events import _set_event_loop_policy from contextlib import ExitStack, redirect_stdout from io import StringIO from test import support @@ -41,6 +42,10 @@ def __exit__(self, *exc): if self.orig_trace: sys.settrace(self.orig_trace) + # To prevent a warning "test altered the execution environment" if + # asyncio features are used. + _set_event_loop_policy(None) + def test_pdb_displayhook(): """This tests the custom displayhook for pdb. From 6c794e0a325270607480920ed6088581ae6ae92b Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 18 Mar 2025 14:52:27 +0800 Subject: [PATCH 2/3] Perform asyncio cleanup as part of Doctest teardown. --- Lib/test/test_pdb.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index cd3e2dc7ba2c27..28c3e1244eedae 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -42,10 +42,6 @@ def __exit__(self, *exc): if self.orig_trace: sys.settrace(self.orig_trace) - # To prevent a warning "test altered the execution environment" if - # asyncio features are used. - _set_event_loop_policy(None) - def test_pdb_displayhook(): """This tests the custom displayhook for pdb. @@ -4675,13 +4671,33 @@ def func(): def load_tests(loader, tests, pattern): from test import test_pdb + def setUpPdbBackend(backend): def setUp(test): import pdb pdb.set_default_backend(backend) return setUp - tests.addTest(doctest.DocTestSuite(test_pdb, setUp=setUpPdbBackend('monitoring'))) - tests.addTest(doctest.DocTestSuite(test_pdb, setUp=setUpPdbBackend('settrace'))) + + def tearDown(test): + # 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_policy(None) + + tests.addTest( + doctest.DocTestSuite( + test_pdb, + setUp=setUpPdbBackend('monitoring'), + tearDown=tearDown, + ) + ) + tests.addTest( + doctest.DocTestSuite( + test_pdb, + setUp=setUpPdbBackend('settrace'), + tearDown=tearDown, + ) + ) return tests From 584aa2b28cbd1662a8b54fa3bad0e85f32dcbe1a Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Wed, 19 Mar 2025 07:06:59 +0800 Subject: [PATCH 3/3] Modify asyncio.run usage to cleanup at time of use. --- Lib/test/test_pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 28c3e1244eedae..18fb94af479527 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -2155,7 +2155,7 @@ def test_pdb_asynctask(): ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() >>> def test_function(): - ... asyncio.run(test()) + ... asyncio.run(test(), loop_factory=asyncio.EventLoop) >>> with PdbTestInput([ # doctest: +ELLIPSIS ... '$_asynctask',