summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-01-13 12:40:29 (GMT)
committerGitHub <noreply@github.com>2023-01-13 12:40:29 (GMT)
commite5bd5ad70d9e549eeb80aadb4f3ccb0f2f23266d (patch)
tree3c13553b04873b8cd80d10a877740031aa324b31 /Lib
parent468c3bf79890ef614764b4e7543608876c792794 (diff)
downloadcpython-e5bd5ad70d9e549eeb80aadb4f3ccb0f2f23266d.zip
cpython-e5bd5ad70d9e549eeb80aadb4f3ccb0f2f23266d.tar.gz
cpython-e5bd5ad70d9e549eeb80aadb4f3ccb0f2f23266d.tar.bz2
gh-100160: Restore and deprecate implicit creation of an event loop (GH-100410)
Partially revert changes made in GH-93453. asyncio.DefaultEventLoopPolicy.get_event_loop() now emits a DeprecationWarning and creates and sets a new event loop instead of raising a RuntimeError if there is no current event loop set. Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/asyncio/events.py24
-rw-r--r--Lib/test/test_asyncio/test_events.py35
-rw-r--r--Lib/test/test_asyncio/test_unix_events.py8
3 files changed, 60 insertions, 7 deletions
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index 6cff8c5..ce44942 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -619,7 +619,7 @@ class AbstractEventLoopPolicy:
Returns an event loop object implementing the BaseEventLoop interface,
or raises an exception in case no event loop has been set for the
- current context.
+ current context and the current policy does not specify to create one.
It should never return None."""
raise NotImplementedError
@@ -672,6 +672,28 @@ class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
Returns an instance of EventLoop or raises an exception.
"""
+ if (self._local._loop is None and
+ not self._local._set_called and
+ threading.current_thread() is threading.main_thread()):
+ stacklevel = 2
+ try:
+ f = sys._getframe(1)
+ except AttributeError:
+ pass
+ else:
+ # Move up the call stack so that the warning is attached
+ # to the line outside asyncio itself.
+ while f:
+ module = f.f_globals.get('__name__')
+ if not (module == 'asyncio' or module.startswith('asyncio.')):
+ break
+ f = f.f_back
+ stacklevel += 1
+ import warnings
+ warnings.warn('There is no current event loop',
+ DeprecationWarning, stacklevel=stacklevel)
+ self.set_event_loop(self.new_event_loop())
+
if self._local._loop is None:
raise RuntimeError('There is no current event loop in thread %r.'
% threading.current_thread().name)
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
index 093f8c1..214544b 100644
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -2614,8 +2614,33 @@ class PolicyTests(unittest.TestCase):
def test_get_event_loop(self):
policy = asyncio.DefaultEventLoopPolicy()
self.assertIsNone(policy._local._loop)
- with self.assertRaisesRegex(RuntimeError, 'no current event 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)
+ self.assertIs(loop, policy.get_event_loop())
+ loop.close()
+
+ def test_get_event_loop_calls_set_event_loop(self):
+ policy = asyncio.DefaultEventLoopPolicy()
+
+ with mock.patch.object(
+ policy, "set_event_loop",
+ wraps=policy.set_event_loop) as m_set_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
+ # the child watcher correctly)
+ m_set_event_loop.assert_called_with(loop)
+
+ loop.close()
def test_get_event_loop_after_set_none(self):
policy = asyncio.DefaultEventLoopPolicy()
@@ -2801,8 +2826,10 @@ class GetEventLoopTestsMixin:
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)
- with self.assertRaisesRegex(RuntimeError, 'no current'):
- asyncio.get_event_loop()
+ with self.assertWarns(DeprecationWarning) as cm:
+ loop2 = asyncio.get_event_loop()
+ self.addCleanup(loop2.close)
+ self.assertEqual(cm.filename, __file__)
asyncio.set_event_loop(None)
with self.assertRaisesRegex(RuntimeError, 'no current'):
asyncio.get_event_loop()
diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py
index 600a590..33d0ea1 100644
--- a/Lib/test/test_asyncio/test_unix_events.py
+++ b/Lib/test/test_asyncio/test_unix_events.py
@@ -1884,7 +1884,9 @@ class TestFork(unittest.IsolatedAsyncioTestCase):
if pid == 0:
# child
try:
- loop = asyncio.get_event_loop_policy().get_event_loop()
+ with self.assertWarns(DeprecationWarning):
+ loop = asyncio.get_event_loop_policy().get_event_loop()
+ os.write(w, b'LOOP:' + str(id(loop)).encode())
except RuntimeError:
os.write(w, b'NO LOOP')
except:
@@ -1893,7 +1895,9 @@ class TestFork(unittest.IsolatedAsyncioTestCase):
os._exit(0)
else:
# parent
- self.assertEqual(os.read(r, 100), b'NO LOOP')
+ result = os.read(r, 100)
+ self.assertEqual(result[:5], b'LOOP:', result)
+ self.assertNotEqual(int(result[5:]), id(loop))
wait_process(pid, exitcode=0)
@hashlib_helper.requires_hashdigest('md5')