From 2941d1ea790db2eb343f3ad0561a1b7715d4163e Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Sun, 26 Jan 2020 20:43:49 -0300 Subject: [PATCH 01/12] Deprecate creation of asyncio object when the loop is not running Add Deprecation Warning when Queue and Locks are created and the loop is not running. --- Lib/asyncio/locks.py | 6 ++++++ Lib/asyncio/queues.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index d94daeb5a173f5..a547af9c886a7f 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -168,6 +168,12 @@ def __init__(self, *, loop=None): "and scheduled for removal in Python 3.10.", DeprecationWarning, stacklevel=2) + if not self._loop.is_running(): + warnings.warn("The asyncio objects created without running " + "event loop is deprecated since Python 3.9 and " + "scheduled for removal in Python 3.10.", + DeprecationWarning, stacklevel=2) + def __repr__(self): res = super().__repr__() extra = 'locked' if self._locked else 'unlocked' diff --git a/Lib/asyncio/queues.py b/Lib/asyncio/queues.py index cd3f7c6a567891..c8ead15b7b91b0 100644 --- a/Lib/asyncio/queues.py +++ b/Lib/asyncio/queues.py @@ -40,6 +40,12 @@ def __init__(self, maxsize=0, *, loop=None): DeprecationWarning, stacklevel=2) self._maxsize = maxsize + if not self._loop.is_running(): + warnings.warn("The asyncio objects created without running " + "event loop is deprecated since Python 3.9 and " + "scheduled for removal in Python 3.10.", + DeprecationWarning, stacklevel=2) + # Futures. self._getters = collections.deque() # Futures. From 7958de92ede93d6023430b62eeff9ff22fdae9f4 Mon Sep 17 00:00:00 2001 From: eamanu Date: Sun, 26 Jan 2020 20:55:49 -0300 Subject: [PATCH 02/12] Add NEWS --- .../next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst diff --git a/Misc/NEWS.d/next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst b/Misc/NEWS.d/next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst new file mode 100644 index 00000000000000..e61430a85a8524 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst @@ -0,0 +1,2 @@ +Add Deprecation Warning message when an Queue and Locks objects are created +and the loop is not running. From 976a808874b7bceb759bb2fd9c7164a62b738c3e Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Wed, 5 Feb 2020 19:16:38 -0300 Subject: [PATCH 03/12] Fix Deprecation Warning message. Add @aeros comments --- Lib/asyncio/locks.py | 25 ++++++++++++++++++++++--- Lib/asyncio/queues.py | 5 ++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index a547af9c886a7f..294230e895314c 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -169,9 +169,8 @@ def __init__(self, *, loop=None): DeprecationWarning, stacklevel=2) if not self._loop.is_running(): - warnings.warn("The asyncio objects created without running " - "event loop is deprecated since Python 3.9 and " - "scheduled for removal in Python 3.10.", + warnings.warn("The creation of asyncio objects without a running " + "event loop is deprecated as of Python 3.9.", DeprecationWarning, stacklevel=2) def __repr__(self): @@ -270,6 +269,11 @@ def __init__(self, *, loop=None): "and scheduled for removal in Python 3.10.", DeprecationWarning, stacklevel=2) + if not self._loop.is_running(): + warnings.warn("The creation of asyncio objects without a running " + "event loop is deprecated as of Python 3.9.", + DeprecationWarning, stacklevel=2) + def __repr__(self): res = super().__repr__() extra = 'set' if self._value else 'unset' @@ -337,6 +341,11 @@ def __init__(self, lock=None, *, loop=None): "and scheduled for removal in Python 3.10.", DeprecationWarning, stacklevel=2) + if not self._loop.is_running(): + warnings.warn("The creation of asyncio objects without a running " + "event loop is deprecated as of Python 3.9.", + DeprecationWarning, stacklevel=2) + if lock is None: lock = Lock(loop=loop) elif lock._loop is not self._loop: @@ -468,6 +477,11 @@ def __init__(self, value=1, *, loop=None): "and scheduled for removal in Python 3.10.", DeprecationWarning, stacklevel=2) + if not self._loop.is_running(): + warnings.warn("The creation of asyncio objects without a running " + "event loop is deprecated as of Python 3.9.", + DeprecationWarning, stacklevel=2) + def __repr__(self): res = super().__repr__() extra = 'locked' if self.locked() else f'unlocked, value:{self._value}' @@ -531,6 +545,11 @@ def __init__(self, value=1, *, loop=None): "and scheduled for removal in Python 3.10.", DeprecationWarning, stacklevel=2) + if not self._loop.is_running(): + warnings.warn("The creation of asyncio objects without a running " + "event loop is deprecated as of Python 3.9.", + DeprecationWarning, stacklevel=2) + self._bound_value = value super().__init__(value, loop=loop) diff --git a/Lib/asyncio/queues.py b/Lib/asyncio/queues.py index c8ead15b7b91b0..62f6b978d01b9a 100644 --- a/Lib/asyncio/queues.py +++ b/Lib/asyncio/queues.py @@ -41,9 +41,8 @@ def __init__(self, maxsize=0, *, loop=None): self._maxsize = maxsize if not self._loop.is_running(): - warnings.warn("The asyncio objects created without running " - "event loop is deprecated since Python 3.9 and " - "scheduled for removal in Python 3.10.", + warnings.warn("The creation of asyncio objects without a running " + "event loop is deprecated as of Python 3.9.", DeprecationWarning, stacklevel=2) # Futures. From 44fa5f66bc6033e5eb366ee49b8dbe9bae28b0e0 Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Sun, 9 Feb 2020 22:51:27 -0300 Subject: [PATCH 04/12] Remove change on BoundedSemaphore --- Lib/asyncio/locks.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index 294230e895314c..ff5e8f0b117faa 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -545,11 +545,6 @@ def __init__(self, value=1, *, loop=None): "and scheduled for removal in Python 3.10.", DeprecationWarning, stacklevel=2) - if not self._loop.is_running(): - warnings.warn("The creation of asyncio objects without a running " - "event loop is deprecated as of Python 3.9.", - DeprecationWarning, stacklevel=2) - self._bound_value = value super().__init__(value, loop=loop) From e486fe478e4df855b5f52638bc9c16363d974b1f Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Mon, 10 Feb 2020 11:34:10 -0300 Subject: [PATCH 05/12] add test --- Lib/test/test_asyncio/test_locks.py | 24 ++++++++++++++++++++++++ Lib/test/test_asyncio/test_queues.py | 6 ++++++ 2 files changed, 30 insertions(+) diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index 9468e740b3c1de..52506dc0cf51c5 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -349,6 +349,12 @@ def test_context_manager_no_yield(self): self.assertFalse(lock.locked()) + def test_lock_loop_not_running(self): + loop = asyncio.new_event_loop() + loop.stop() + with self.assertWarns(DeprecationWarning): + q = asyncio.Lock(loop=loop) + class EventTests(test_utils.TestCase): @@ -483,6 +489,12 @@ async def c1(result): self.assertTrue(t.done()) self.assertTrue(t.result()) + def test_event_loop_not_running(self): + loop = asyncio.new_event_loop() + loop.stop() + with self.assertWarns(DeprecationWarning): + q = asyncio.Event(loop=loop) + class ConditionTests(test_utils.TestCase): @@ -866,6 +878,12 @@ async def task_timeout(): with self.assertWarns(DeprecationWarning): loop.run_until_complete(task_timeout()) + def test_condition_loop_not_running(self): + loop = asyncio.new_event_loop() + loop.stop() + with self.assertWarns(DeprecationWarning): + q = asyncio.Condition(loop=loop) + class SemaphoreTests(test_utils.TestCase): @@ -1096,6 +1114,12 @@ def test_context_manager_no_yield(self): self.assertEqual(2, sem._value) + def test_semaphore_loop_not_running(self): + loop = asyncio.new_event_loop() + loop.stop() + with self.assertWarns(DeprecationWarning): + q = asyncio.Semaphore(loop=loop) + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index 5c9aaa82c311a5..694756e3c28b6d 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -162,6 +162,12 @@ async def test(): loop.run_until_complete(test()) self.assertAlmostEqual(0.02, loop.time()) + def test_queue_loop_not_running(self): + loop = asyncio.new_event_loop() + loop.stop() + with self.assertWarns(DeprecationWarning): + q = asyncio.Queue(loop=loop) + class QueueGetTests(_QueueTestBase): From 18c4ddf640113503004841de1e86c68ec5bde42a Mon Sep 17 00:00:00 2001 From: eamanu Date: Sun, 16 Feb 2020 18:48:09 -0300 Subject: [PATCH 06/12] Fix tests --- Lib/test/test_asyncio/test_locks.py | 44 +++++++++++++++------------- Lib/test/test_asyncio/test_queues.py | 20 +++++++++---- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index 485ca5df18e99c..37d248fb8a9796 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -305,12 +305,6 @@ async def f(): self.loop.run_until_complete(f()) - def test_lock_loop_not_running(self): - loop = asyncio.new_event_loop() - loop.stop() - with self.assertWarns(DeprecationWarning): - q = asyncio.Lock(loop=loop) - class EventTests(test_utils.TestCase): @@ -445,12 +439,6 @@ async def c1(result): self.assertTrue(t.done()) self.assertTrue(t.result()) - def test_event_loop_not_running(self): - loop = asyncio.new_event_loop() - loop.stop() - with self.assertWarns(DeprecationWarning): - q = asyncio.Event(loop=loop) - class ConditionTests(test_utils.TestCase): @@ -815,12 +803,6 @@ async def task_timeout(): with self.assertWarns(DeprecationWarning): loop.run_until_complete(task_timeout()) - def test_condition_loop_not_running(self): - loop = asyncio.new_event_loop() - loop.stop() - with self.assertWarns(DeprecationWarning): - q = asyncio.Condition(loop=loop) - class SemaphoreTests(test_utils.TestCase): @@ -1017,11 +999,31 @@ def test_release_no_waiters(self): sem.release() self.assertFalse(sem.locked()) + +class LockLoopTests: + 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): - loop = asyncio.new_event_loop() - loop.stop() with self.assertWarns(DeprecationWarning): - q = asyncio.Semaphore(loop=loop) + asyncio.Semaphore() if __name__ == '__main__': diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index 694756e3c28b6d..a8bf2eb6402e78 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -162,12 +162,6 @@ async def test(): loop.run_until_complete(test()) self.assertAlmostEqual(0.02, loop.time()) - def test_queue_loop_not_running(self): - loop = asyncio.new_event_loop() - loop.stop() - with self.assertWarns(DeprecationWarning): - q = asyncio.Queue(loop=loop) - class QueueGetTests(_QueueTestBase): @@ -720,5 +714,19 @@ class PriorityQueueJoinTests(_QueueJoinTestMixin, _QueueTestBase): q_class = asyncio.PriorityQueue +class QueueLoopTests: + 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_queue_loop_not_running(self): + with self.assertWarns(DeprecationWarning): + asyncio.Queue() + + if __name__ == '__main__': unittest.main() From dc78f3a54df383953720df04bdcab496d6035a49 Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Tue, 25 Feb 2020 21:04:40 -0300 Subject: [PATCH 07/12] Fix PR according to @aeros comments --- Lib/test/test_asyncio/test_locks.py | 3 +++ Lib/test/test_asyncio/test_queues.py | 3 +++ .../next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst | 6 ++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index 37d248fb8a9796..54b280a29b31f8 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -1001,6 +1001,9 @@ def test_release_no_waiters(self): class LockLoopTests: + """Tests the deprecation of creating asyncio objects outside of a + running event loop.""" + def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index a8bf2eb6402e78..ae30a66cbe0c62 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -715,6 +715,9 @@ class PriorityQueueJoinTests(_QueueJoinTestMixin, _QueueTestBase): class QueueLoopTests: + """Tests the deprecation of creating asyncio objects outside of a + running event loop.""" + def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) diff --git a/Misc/NEWS.d/next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst b/Misc/NEWS.d/next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst index e61430a85a8524..6636fbbe3a10f9 100644 --- a/Misc/NEWS.d/next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst +++ b/Misc/NEWS.d/next/Library/2020-01-26-20-54-04.bpo-38599.0PObOD.rst @@ -1,2 +1,4 @@ -Add Deprecation Warning message when an Queue and Locks objects are created -and the loop is not running. +The creation of :class:`asyncio.Queue`, :class:`asyncio.Lock`, +:class:`asyncio.Event`, :class:`asyncio.Condition`, +:class:`asyncio.Semaphore`, and :class:`asyncio.BoundedSemaphore` +objects outside of a running event loop has been deprecated. From 1bb46a873b763901a9b8d76be9efc622d693e78b Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Fri, 13 Mar 2020 08:32:20 -0300 Subject: [PATCH 08/12] Fix according to review comments --- Lib/asyncio/locks.py | 60 +++++++++++++++++-------------------------- Lib/asyncio/queues.py | 16 +++++------- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index 24ccfb43577973..709c47975cac31 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -78,17 +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 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 not self._loop.is_running(): - warnings.warn("The creation of asyncio objects without a running " - "event loop is deprecated as of Python 3.9.", - DeprecationWarning, stacklevel=2) def __repr__(self): res = super().__repr__() @@ -179,17 +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) - - if not self._loop.is_running(): - warnings.warn("The creation of asyncio objects without a running " - "event loop is deprecated as of Python 3.9.", - DeprecationWarning, stacklevel=2) def __repr__(self): res = super().__repr__() @@ -251,17 +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 not self._loop.is_running(): - warnings.warn("The creation of asyncio objects without a running " - "event loop is deprecated as of Python 3.9.", - DeprecationWarning, stacklevel=2) if lock is None: lock = Lock(loop=loop) @@ -387,17 +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) - - if not self._loop.is_running(): - warnings.warn("The creation of asyncio objects without a running " - "event loop is deprecated as of Python 3.9.", - DeprecationWarning, stacklevel=2) def __repr__(self): res = super().__repr__() diff --git a/Lib/asyncio/queues.py b/Lib/asyncio/queues.py index 62f6b978d01b9a..eb8cb51bb49cdb 100644 --- a/Lib/asyncio/queues.py +++ b/Lib/asyncio/queues.py @@ -32,18 +32,16 @@ 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 - if not self._loop.is_running(): - warnings.warn("The creation of asyncio objects without a running " - "event loop is deprecated as of Python 3.9.", - DeprecationWarning, stacklevel=2) + self._maxsize = maxsize # Futures. self._getters = collections.deque() From 0ca9f6174dfcb31fe7848ffe6e26775100b5ee0d Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Sun, 15 Mar 2020 23:15:18 -0300 Subject: [PATCH 09/12] remove assertWarns on where is not applied --- Lib/test/test_asyncio/test_locks.py | 146 ++++++++++------------------ 1 file changed, 51 insertions(+), 95 deletions(-) diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index 54b280a29b31f8..7fd72e62d6a312 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()) From c4afc8f78a8cb15b4c44413c7d7c2c2765d7c1ab Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Sun, 15 Mar 2020 23:16:18 -0300 Subject: [PATCH 10/12] Update Lib/asyncio/locks.py Co-Authored-By: Kyle Stanley --- Lib/asyncio/locks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index 709c47975cac31..9c043d7457619e 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -80,7 +80,7 @@ def __init__(self, *, loop=None): if loop is None: self._loop = events._get_running_loop() if self._loop is None: - warnings.warn("The creation of asyncio objects outside a running " + 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() From ec3a6aac8ed7c24e09c7dcf489bf20b6725d7901 Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Sun, 15 Mar 2020 23:21:23 -0300 Subject: [PATCH 11/12] fix grammar error --- Lib/test/test_asyncio/test_locks.py | 2 +- Lib/test/test_asyncio/test_queues.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index 7fd72e62d6a312..dad6b52919f7aa 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -957,7 +957,7 @@ def test_release_no_waiters(self): class LockLoopTests: - """Tests the deprecation of creating asyncio objects outside of a + """Tests the deprecation of creating asyncio objects outside a running event loop.""" def setUp(self): diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index ae30a66cbe0c62..12f8bd5b99dfa5 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -715,7 +715,7 @@ class PriorityQueueJoinTests(_QueueJoinTestMixin, _QueueTestBase): class QueueLoopTests: - """Tests the deprecation of creating asyncio objects outside of a + """Tests the deprecation of creating asyncio objects outside a running event loop.""" def setUp(self): From dd6dec31d366a97bd662064d89a695f76fcfebae Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Sun, 15 Mar 2020 23:37:36 -0300 Subject: [PATCH 12/12] Remove assertWarns not applied on queues test --- Lib/test/test_asyncio/test_queues.py | 130 +++++++++------------------ 1 file changed, 44 insertions(+), 86 deletions(-) diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index 12f8bd5b99dfa5..48bf21037bb157 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('