gh-93896: allow enabling asyncio.Runner calls to asyncio.set_event_loop independantly to loop_factory by graingert · Pull Request #94058 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions Lib/asyncio/runners.py
29 changes: 26 additions & 3 deletions Lib/test/test_asyncio/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ def set_event_loop(self, loop):
self.loop = loop


class NullPolicy(asyncio.AbstractEventLoopPolicy):

def get_event_loop(self):
# shouldn't ever be called by asyncio.run()
raise RuntimeError

def new_event_loop(self):
raise RuntimeError

def set_event_loop(self, loop):
raise RuntimeError


class BaseTest(unittest.TestCase):

def new_loop(self):
Expand All @@ -57,6 +70,19 @@ async def shutdown_asyncgens():

return loop

def setUp(self):
super().setUp()

policy = NullPolicy()
asyncio.set_event_loop_policy(policy)

def tearDown(self):
asyncio.set_event_loop_policy(None)
super().tearDown()


class RunTests(BaseTest):

def setUp(self):
super().setUp()

Expand All @@ -69,12 +95,9 @@ def tearDown(self):
self.assertTrue(policy.loop.is_closed())
self.assertTrue(policy.loop.shutdown_ag_run)

asyncio.set_event_loop_policy(None)
super().tearDown()


class RunTests(BaseTest):

def test_asyncio_run_return(self):
async def main():
await asyncio.sleep(0)
Expand Down
6 changes: 5 additions & 1 deletion Lib/unittest/async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ def _callMaybeAsync(self, func, /, *args, **kwargs):

def _setupAsyncioRunner(self):
assert self._asyncioRunner is None, 'asyncio runner is already initialized'
runner = asyncio.Runner(debug=True)
runner = asyncio.Runner(
debug=True,
loop_factory=asyncio.new_event_loop,
set_policy_loop=True,
)
self._asyncioRunner = runner

def _tearDownAsyncioRunner(self):
Expand Down