diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index f1ce7324785ba98..9c043d7457619e0 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -78,12 +78,14 @@ def __init__(self, *, loop=None): self._waiters = None self._locked = False if loop is None: - self._loop = events.get_event_loop() + self._loop = events._get_running_loop() + if self._loop is None: + warnings.warn("The creation of asyncio objects outside of a running " + "event loop is deprecated as of Python 3.9.", + DeprecationWarning, stacklevel=2) + self._loop = events.get_event_loop() else: self._loop = loop - warnings.warn("The loop argument is deprecated since Python 3.8, " - "and scheduled for removal in Python 3.10.", - DeprecationWarning, stacklevel=2) def __repr__(self): res = super().__repr__() @@ -174,12 +176,14 @@ def __init__(self, *, loop=None): self._waiters = collections.deque() self._value = False if loop is None: - self._loop = events.get_event_loop() + self._loop = events._get_running_loop() + if self._loop is None: + warnings.warn("The creation of asyncio objects outside a running " + "event loop is deprecated as of Python 3.9.", + DeprecationWarning, stacklevel=2) + self._loop = events.get_event_loop() else: self._loop = loop - warnings.warn("The loop argument is deprecated since Python 3.8, " - "and scheduled for removal in Python 3.10.", - DeprecationWarning, stacklevel=2) def __repr__(self): res = super().__repr__() @@ -241,12 +245,14 @@ class Condition(_ContextManagerMixin): def __init__(self, lock=None, *, loop=None): if loop is None: - self._loop = events.get_event_loop() + self._loop = events._get_running_loop() + if self._loop is None: + warnings.warn("The creation of asyncio objects outside a running " + "event loop is deprecated as of Python 3.9.", + DeprecationWarning, stacklevel=2) + self._loop = events.get_event_loop() else: self._loop = loop - warnings.warn("The loop argument is deprecated since Python 3.8, " - "and scheduled for removal in Python 3.10.", - DeprecationWarning, stacklevel=2) if lock is None: lock = Lock(loop=loop) @@ -372,12 +378,14 @@ def __init__(self, value=1, *, loop=None): self._value = value self._waiters = collections.deque() if loop is None: - self._loop = events.get_event_loop() + self._loop = events._get_running_loop() + if self._loop is None: + warnings.warn("The creation of asyncio objects outside a running " + "event loop is deprecated as of Python 3.9.", + DeprecationWarning, stacklevel=2) + self._loop = events.get_event_loop() else: self._loop = loop - warnings.warn("The loop argument is deprecated since Python 3.8, " - "and scheduled for removal in Python 3.10.", - DeprecationWarning, stacklevel=2) def __repr__(self): res = super().__repr__() diff --git a/Lib/asyncio/queues.py b/Lib/asyncio/queues.py index cd3f7c6a5678915..eb8cb51bb49cdb8 100644 --- a/Lib/asyncio/queues.py +++ b/Lib/asyncio/queues.py @@ -32,12 +32,15 @@ class Queue: def __init__(self, maxsize=0, *, loop=None): if loop is None: - self._loop = events.get_event_loop() + self._loop = events._get_running_loop() + if self._loop is None: + warnings.warn("The creation of asyncio objects outside a running " + "event loop is deprecated as of Python 3.9.", + DeprecationWarning, stacklevel=2) + self._loop = events.get_event_loop() else: self._loop = loop - warnings.warn("The loop argument is deprecated since Python 3.8, " - "and scheduled for removal in Python 3.10.", - DeprecationWarning, stacklevel=2) + self._maxsize = maxsize # Futures. diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index 8c93fae2b51c6c8..dad6b52919f7aa9 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -28,12 +28,10 @@ def setUp(self): def test_ctor_loop(self): loop = mock.Mock() - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=loop) + lock = asyncio.Lock(loop=loop) self.assertIs(lock._loop, loop) - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock(loop=self.loop) self.assertIs(lock._loop, self.loop) def test_ctor_noloop(self): @@ -42,8 +40,7 @@ def test_ctor_noloop(self): self.assertIs(lock._loop, self.loop) def test_repr(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock(loop=self.loop) self.assertTrue(repr(lock).endswith('[unlocked]>')) self.assertTrue(RGX_REPR.match(repr(lock))) @@ -95,8 +92,7 @@ def test(lock): self.assertFalse(primitive.locked()) def test_acquire(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock(loop=self.loop) result = [] self.assertTrue(self.loop.run_until_complete(lock.acquire())) @@ -147,8 +143,7 @@ async def c3(result): self.assertTrue(t3.result()) def test_acquire_cancel(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock(loop=self.loop) self.assertTrue(self.loop.run_until_complete(lock.acquire())) task = self.loop.create_task(lock.acquire()) @@ -173,8 +168,7 @@ def test_cancel_race(self): # B's waiter; instead, it should move on to C's waiter. # Setup: A has the lock, b and c are waiting. - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock(loop=self.loop) async def lockit(name, blocker): await lock.acquire() @@ -210,8 +204,7 @@ def test_cancel_release_race(self): # Issue 32734 # Acquire 4 locks, cancel second, release first # and 2 locks are taken at once. - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock(loop=self.loop) lock_count = 0 call_count = 0 @@ -256,8 +249,7 @@ def trigger(): self.assertTrue(t3.cancelled()) def test_finished_waiter_cancelled(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock(loop=self.loop) ta = self.loop.create_task(lock.acquire()) test_utils.run_briefly(self.loop) @@ -279,14 +271,12 @@ def test_finished_waiter_cancelled(self): self.assertTrue(tb.cancelled()) def test_release_not_acquired(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock(loop=self.loop) self.assertRaises(RuntimeError, lock.release) def test_release_no_waiters(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock(loop=self.loop) self.loop.run_until_complete(lock.acquire()) self.assertTrue(lock.locked()) @@ -314,12 +304,10 @@ def setUp(self): def test_ctor_loop(self): loop = mock.Mock() - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=loop) + ev = asyncio.Event(loop=loop) self.assertIs(ev._loop, loop) - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=self.loop) + ev = asyncio.Event(loop=self.loop) self.assertIs(ev._loop, self.loop) def test_ctor_noloop(self): @@ -328,8 +316,7 @@ def test_ctor_noloop(self): self.assertIs(ev._loop, self.loop) def test_repr(self): - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=self.loop) + ev = asyncio.Event(loop=self.loop) self.assertTrue(repr(ev).endswith('[unset]>')) match = RGX_REPR.match(repr(ev)) self.assertEqual(match.group('extras'), 'unset') @@ -343,8 +330,7 @@ def test_repr(self): self.assertTrue(RGX_REPR.match(repr(ev))) def test_wait(self): - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=self.loop) + ev = asyncio.Event(loop=self.loop) self.assertFalse(ev.is_set()) result = [] @@ -381,16 +367,14 @@ async def c3(result): self.assertIsNone(t3.result()) def test_wait_on_set(self): - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=self.loop) + ev = asyncio.Event(loop=self.loop) ev.set() res = self.loop.run_until_complete(ev.wait()) self.assertTrue(res) def test_wait_cancel(self): - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=self.loop) + ev = asyncio.Event(loop=self.loop) wait = self.loop.create_task(ev.wait()) self.loop.call_soon(wait.cancel) @@ -400,8 +384,7 @@ def test_wait_cancel(self): self.assertFalse(ev._waiters) def test_clear(self): - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=self.loop) + ev = asyncio.Event(loop=self.loop) self.assertFalse(ev.is_set()) ev.set() @@ -411,8 +394,7 @@ def test_clear(self): self.assertFalse(ev.is_set()) def test_clear_with_waiters(self): - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=self.loop) + ev = asyncio.Event(loop=self.loop) result = [] async def c1(result): @@ -448,12 +430,11 @@ def setUp(self): def test_ctor_loop(self): loop = mock.Mock() - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=loop) - self.assertIs(cond._loop, loop) + cond = asyncio.Condition(loop=loop) + self.assertIs(cond._loop, loop) - cond = asyncio.Condition(loop=self.loop) - self.assertIs(cond._loop, self.loop) + cond = asyncio.Condition(loop=self.loop) + self.assertIs(cond._loop, self.loop) def test_ctor_noloop(self): asyncio.set_event_loop(self.loop) @@ -461,8 +442,7 @@ def test_ctor_noloop(self): self.assertIs(cond._loop, self.loop) def test_wait(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) result = [] async def c1(result): @@ -525,8 +505,7 @@ async def c3(result): self.assertTrue(t3.result()) def test_wait_cancel(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) self.loop.run_until_complete(cond.acquire()) wait = self.loop.create_task(cond.wait()) @@ -538,8 +517,7 @@ def test_wait_cancel(self): self.assertTrue(cond.locked()) def test_wait_cancel_contested(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) self.loop.run_until_complete(cond.acquire()) self.assertTrue(cond.locked()) @@ -565,8 +543,7 @@ def test_wait_cancel_contested(self): def test_wait_cancel_after_notify(self): # See bpo-32841 - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) waited = False async def wait_on_cond(): @@ -590,15 +567,13 @@ async def wait_on_cond(): self.assertTrue(waited) def test_wait_unacquired(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) self.assertRaises( RuntimeError, self.loop.run_until_complete, cond.wait()) def test_wait_for(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) presult = False def predicate(): @@ -635,8 +610,7 @@ async def c1(result): self.assertTrue(t.result()) def test_wait_for_unacquired(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) # predicate can return true immediately res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3])) @@ -648,8 +622,7 @@ def test_wait_for_unacquired(self): cond.wait_for(lambda: False)) def test_notify(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) result = [] async def c1(result): @@ -701,8 +674,7 @@ async def c3(result): self.assertTrue(t3.result()) def test_notify_all(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) result = [] @@ -738,18 +710,15 @@ async def c2(result): self.assertTrue(t2.result()) def test_notify_unacquired(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) self.assertRaises(RuntimeError, cond.notify) def test_notify_all_unacquired(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) self.assertRaises(RuntimeError, cond.notify_all) def test_repr(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition(loop=self.loop) self.assertTrue('unlocked' in repr(cond)) self.assertTrue(RGX_REPR.match(repr(cond))) @@ -775,9 +744,8 @@ async def f(): self.loop.run_until_complete(f()) def test_explicit_lock(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) - cond = asyncio.Condition(lock, loop=self.loop) + lock = asyncio.Lock(loop=self.loop) + cond = asyncio.Condition(lock, loop=self.loop) self.assertIs(cond._lock, lock) self.assertIs(cond._loop, lock._loop) @@ -785,10 +753,9 @@ def test_explicit_lock(self): def test_ambiguous_loops(self): loop = self.new_test_loop() self.addCleanup(loop.close) - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) - with self.assertRaises(ValueError): - asyncio.Condition(lock, loop=loop) + lock = asyncio.Lock(loop=self.loop) + with self.assertRaises(ValueError): + asyncio.Condition(lock, loop=loop) def test_timeout_in_block(self): loop = asyncio.new_event_loop() @@ -800,8 +767,7 @@ async def task_timeout(): with self.assertRaises(asyncio.TimeoutError): await asyncio.wait_for(condition.wait(), timeout=0.5) - with self.assertWarns(DeprecationWarning): - loop.run_until_complete(task_timeout()) + loop.run_until_complete(task_timeout()) class SemaphoreTests(test_utils.TestCase): @@ -812,12 +778,10 @@ def setUp(self): def test_ctor_loop(self): loop = mock.Mock() - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=loop) + sem = asyncio.Semaphore(loop=loop) self.assertIs(sem._loop, loop) - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=self.loop) + sem = asyncio.Semaphore(loop=self.loop) self.assertIs(sem._loop, self.loop) def test_ctor_noloop(self): @@ -826,13 +790,11 @@ def test_ctor_noloop(self): self.assertIs(sem._loop, self.loop) def test_initial_value_zero(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(0, loop=self.loop) + sem = asyncio.Semaphore(0, loop=self.loop) self.assertTrue(sem.locked()) def test_repr(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=self.loop) + sem = asyncio.Semaphore(loop=self.loop) self.assertTrue(repr(sem).endswith('[unlocked, value:1]>')) self.assertTrue(RGX_REPR.match(repr(sem))) @@ -850,9 +812,7 @@ def test_repr(self): self.assertTrue(RGX_REPR.match(repr(sem))) def test_semaphore(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=self.loop) - self.assertEqual(1, sem._value) + sem = asyncio.Semaphore(loop=self.loop) with self.assertWarns(DeprecationWarning): @asyncio.coroutine @@ -872,8 +832,7 @@ def test_semaphore_value(self): self.assertRaises(ValueError, asyncio.Semaphore, -1) def test_acquire(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(3, loop=self.loop) + sem = asyncio.Semaphore(3, loop=self.loop) result = [] self.assertTrue(self.loop.run_until_complete(sem.acquire())) @@ -934,8 +893,8 @@ async def c4(result): self.loop.run_until_complete(asyncio.gather(*race_tasks)) def test_acquire_cancel(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=self.loop) + sem = asyncio.Semaphore(loop=self.loop) + self.loop.run_until_complete(sem.acquire()) acquire = self.loop.create_task(sem.acquire()) @@ -947,8 +906,7 @@ def test_acquire_cancel(self): all(waiter.done() for waiter in sem._waiters)) def test_acquire_cancel_before_awoken(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(value=0, loop=self.loop) + sem = asyncio.Semaphore(value=0, loop=self.loop) t1 = self.loop.create_task(sem.acquire()) t2 = self.loop.create_task(sem.acquire()) @@ -970,8 +928,7 @@ def test_acquire_cancel_before_awoken(self): test_utils.run_briefly(self.loop) def test_acquire_hang(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(value=0, loop=self.loop) + sem = asyncio.Semaphore(value=0, loop=self.loop) t1 = self.loop.create_task(sem.acquire()) t2 = self.loop.create_task(sem.acquire()) @@ -991,8 +948,7 @@ def test_release_not_acquired(self): self.assertRaises(ValueError, sem.release) def test_release_no_waiters(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=self.loop) + sem = asyncio.Semaphore(loop=self.loop) self.loop.run_until_complete(sem.acquire()) self.assertTrue(sem.locked()) @@ -1000,5 +956,34 @@ def test_release_no_waiters(self): self.assertFalse(sem.locked()) +class LockLoopTests: + """Tests the deprecation of creating asyncio objects outside a + running event loop.""" + + def setUp(self): + self.loop = asyncio.new_event_loop() + asyncio.set_event_loop(self.loop) + + def tearDown(self): + asyncio.set_event_loop(None) + self.loop.close() + + def test_lock_loop_not_running(self): + with self.assertWarns(DeprecationWarning): + asyncio.Lock() + + def test_event_loop_not_running(self): + with self.assertWarns(DeprecationWarning): + asyncio.Event() + + def test_condition_loop_not_running(self): + with self.assertWarns(DeprecationWarning): + asyncio.Condition() + + def test_semaphore_loop_not_running(self): + with self.assertWarns(DeprecationWarning): + asyncio.Semaphore() + + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index 5c9aaa82c311a54..48bf21037bb1572 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -35,8 +35,7 @@ def gen(): loop = self.new_test_loop(gen) - with self.assertWarns(DeprecationWarning): - q = asyncio.Queue(loop=loop) + q = asyncio.Queue(loop=loop) self.assertTrue(fn(q).startswith('