Skip to content

Commit b754aee

Browse files
authored
gh-121468: Ensure PDB cleans up event loop policies after using asyncio. (#131388)
Adds teardown logic, plus a change to asyncio.run usage, to avoid warnings when running the test suite single process.
1 parent d783d7b commit b754aee

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Lib/test/test_pdb.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import zipapp
1414
import zipfile
1515

16+
from asyncio.events import _set_event_loop_policy
1617
from contextlib import ExitStack, redirect_stdout
1718
from io import StringIO
1819
from test import support
@@ -2154,7 +2155,7 @@ def test_pdb_asynctask():
21542155
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
21552156
21562157
>>> def test_function():
2157-
... asyncio.run(test())
2158+
... asyncio.run(test(), loop_factory=asyncio.EventLoop)
21582159
21592160
>>> with PdbTestInput([ # doctest: +ELLIPSIS
21602161
... '$_asynctask',
@@ -4670,13 +4671,33 @@ def func():
46704671

46714672
def load_tests(loader, tests, pattern):
46724673
from test import test_pdb
4674+
46734675
def setUpPdbBackend(backend):
46744676
def setUp(test):
46754677
import pdb
46764678
pdb.set_default_backend(backend)
46774679
return setUp
4678-
tests.addTest(doctest.DocTestSuite(test_pdb, setUp=setUpPdbBackend('monitoring')))
4679-
tests.addTest(doctest.DocTestSuite(test_pdb, setUp=setUpPdbBackend('settrace')))
4680+
4681+
def tearDown(test):
4682+
# Ensure that asyncio state has been cleared at the end of the test.
4683+
# This prevents a "test altered the execution environment" warning if
4684+
# asyncio features are used.
4685+
_set_event_loop_policy(None)
4686+
4687+
tests.addTest(
4688+
doctest.DocTestSuite(
4689+
test_pdb,
4690+
setUp=setUpPdbBackend('monitoring'),
4691+
tearDown=tearDown,
4692+
)
4693+
)
4694+
tests.addTest(
4695+
doctest.DocTestSuite(
4696+
test_pdb,
4697+
setUp=setUpPdbBackend('settrace'),
4698+
tearDown=tearDown,
4699+
)
4700+
)
46804701
return tests
46814702

46824703

0 commit comments

Comments
 (0)