[3.10] gh-93453: Only emit deprecation warning in asyncio.get_event_loop when a new event loop is created by ambv · Pull Request #100059 · python/cpython
loop = policy.get_event_loop() with self.assertWarns(DeprecationWarning) as cm: loop = policy.get_event_loop() self.assertEqual(cm.filename, __file__) self.assertIsInstance(loop, asyncio.AbstractEventLoop)
self.assertIs(policy._local._loop, loop)
loop = policy.get_event_loop() with self.assertWarns(DeprecationWarning) as cm: loop = policy.get_event_loop() self.addCleanup(loop.close) self.assertEqual(cm.filename, __file__)
# policy._local._loop must be set through .set_event_loop() # (the unix DefaultEventLoopPolicy needs this call to attach
def test_set_event_loop(self): policy = asyncio.DefaultEventLoopPolicy() old_loop = policy.get_event_loop() old_loop = policy.new_event_loop() policy.set_event_loop(old_loop)
self.assertRaises(AssertionError, policy.set_event_loop, object())
with self.assertWarns(DeprecationWarning) as cm: with self.assertRaises(TestError): asyncio.get_event_loop() self.assertEqual(cm.warnings[0].filename, __file__) with self.assertRaises(TestError): asyncio.get_event_loop() asyncio.set_event_loop(None) with self.assertWarns(DeprecationWarning) as cm: with self.assertRaises(TestError): asyncio.get_event_loop() self.assertEqual(cm.warnings[0].filename, __file__) with self.assertRaises(TestError): asyncio.get_event_loop()
with self.assertRaisesRegex(RuntimeError, 'no running'): asyncio.get_running_loop()
asyncio.set_event_loop(loop) with self.assertWarns(DeprecationWarning) as cm: with self.assertRaises(TestError): asyncio.get_event_loop() self.assertEqual(cm.warnings[0].filename, __file__)
with self.assertRaises(TestError): asyncio.get_event_loop() asyncio.set_event_loop(None) with self.assertWarns(DeprecationWarning) as cm: with self.assertRaises(TestError): asyncio.get_event_loop() self.assertEqual(cm.warnings[0].filename, __file__) with self.assertRaises(TestError): asyncio.get_event_loop()
finally: asyncio.set_event_loop_policy(old_policy)
with self.assertRaisesRegex(RuntimeError, 'no running'): asyncio.get_running_loop()
asyncio.set_event_loop(loop) with self.assertWarns(DeprecationWarning) as cm: self.assertIs(asyncio.get_event_loop(), loop) self.assertEqual(cm.warnings[0].filename, __file__) self.assertIs(asyncio.get_event_loop(), loop)
asyncio.set_event_loop(None) with self.assertWarns(DeprecationWarning) as cm: with self.assertRaisesRegex(RuntimeError, 'no current'): asyncio.get_event_loop() self.assertEqual(cm.warnings[0].filename, __file__) with self.assertRaisesRegex(RuntimeError, 'no current'): asyncio.get_event_loop()
finally: asyncio.set_event_loop_policy(old_policy)