From 537877d85d1c27d2c2f5189e39da64a7a0c413d3 Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Tue, 10 Sep 2019 07:55:07 -0300 Subject: bpo-36373: Deprecate explicit loop parameter in all public asyncio APIs [locks] (GH-13920) This PR deprecate explicit loop parameters in all public asyncio APIs This issues is split to be easier to review. Third step: locks.py https://bugs.python.org/issue36373 --- Doc/library/asyncio-sync.rst | 18 +++ Lib/asyncio/locks.py | 41 ++++-- Lib/test/test_asyncio/test_events.py | 241 +++++++++++++++++++---------------- Lib/test/test_asyncio/test_locks.py | 198 +++++++++++++++++----------- Lib/test/test_asyncio/test_pep492.py | 26 ++-- Lib/test/test_asyncio/test_queues.py | 135 +++++++++++++------- Lib/test/test_asyncio/test_tasks.py | 25 ++-- 7 files changed, 419 insertions(+), 265 deletions(-) diff --git a/Doc/library/asyncio-sync.rst b/Doc/library/asyncio-sync.rst index 79f6b02..cc8c29d 100644 --- a/Doc/library/asyncio-sync.rst +++ b/Doc/library/asyncio-sync.rst @@ -59,6 +59,9 @@ Lock finally: lock.release() + .. deprecated-removed:: 3.8 3.10 + The *loop* parameter. + .. coroutinemethod:: acquire() Acquire the lock. @@ -101,6 +104,10 @@ Event :meth:`clear` method. The :meth:`wait` method blocks until the flag is set to *true*. The flag is set to *false* initially. + + .. deprecated-removed:: 3.8 3.10 + The *loop* parameter. + .. _asyncio_example_sync_event: Example:: @@ -173,6 +180,10 @@ Condition ``None``. In the latter case a new Lock object is created automatically. + + .. deprecated-removed:: 3.8 3.10 + The *loop* parameter. + The preferred way to use a Condition is an :keyword:`async with` statement:: @@ -269,6 +280,10 @@ Semaphore internal counter (``1`` by default). If the given value is less than ``0`` a :exc:`ValueError` is raised. + + .. deprecated-removed:: 3.8 3.10 + The *loop* parameter. + The preferred way to use a Semaphore is an :keyword:`async with` statement:: @@ -322,6 +337,9 @@ BoundedSemaphore increases the internal counter above the initial *value*. + .. deprecated-removed:: 3.8 3.10 + The *loop* parameter. + --------- diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index 1324eef..f63d4ce 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -160,10 +160,13 @@ class Lock(_ContextManagerMixin): def __init__(self, *, loop=None): self._waiters = None self._locked = False - if loop is not None: - self._loop = loop - else: + if loop is None: 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__() @@ -253,10 +256,13 @@ class Event: def __init__(self, *, loop=None): self._waiters = collections.deque() self._value = False - if loop is not None: - self._loop = loop - else: + if loop is None: 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__() @@ -317,10 +323,13 @@ class Condition(_ContextManagerMixin): """ def __init__(self, lock=None, *, loop=None): - if loop is not None: - self._loop = loop - else: + if loop is None: 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=self._loop) @@ -445,10 +454,13 @@ class Semaphore(_ContextManagerMixin): raise ValueError("Semaphore initial value must be >= 0") self._value = value self._waiters = collections.deque() - if loop is not None: - self._loop = loop - else: + if loop is None: 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__() @@ -508,6 +520,11 @@ class BoundedSemaphore(Semaphore): """ def __init__(self, value=1, *, loop=None): + if loop: + warnings.warn("The loop argument is deprecated since Python 3.8, " + "and scheduled for removal in Python 3.10.", + DeprecationWarning, stacklevel=2) + self._bound_value = value super().__init__(value, loop=loop) diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 5bc1bc2..bdc8613 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -1719,19 +1719,20 @@ class SubprocessTestsMixin: connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) - self.assertEqual('CONNECTED', proto.state) + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) + self.assertEqual('CONNECTED', proto.state) - stdin = transp.get_pipe_transport(0) - stdin.write(b'Python The Winner') - self.loop.run_until_complete(proto.got_data[1].wait()) - with test_utils.disable_logger(): - transp.close() - self.loop.run_until_complete(proto.completed) - self.check_killed(proto.returncode) - self.assertEqual(b'Python The Winner', proto.data[1]) + stdin = transp.get_pipe_transport(0) + stdin.write(b'Python The Winner') + self.loop.run_until_complete(proto.got_data[1].wait()) + with test_utils.disable_logger(): + transp.close() + self.loop.run_until_complete(proto.completed) + self.check_killed(proto.returncode) + self.assertEqual(b'Python The Winner', proto.data[1]) def test_subprocess_interactive(self): prog = os.path.join(os.path.dirname(__file__), 'echo.py') @@ -1739,47 +1740,52 @@ class SubprocessTestsMixin: connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) - self.assertEqual('CONNECTED', proto.state) - stdin = transp.get_pipe_transport(0) - stdin.write(b'Python ') - self.loop.run_until_complete(proto.got_data[1].wait()) - proto.got_data[1].clear() - self.assertEqual(b'Python ', proto.data[1]) + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) + self.assertEqual('CONNECTED', proto.state) - stdin.write(b'The Winner') - self.loop.run_until_complete(proto.got_data[1].wait()) - self.assertEqual(b'Python The Winner', proto.data[1]) + stdin = transp.get_pipe_transport(0) + stdin.write(b'Python ') + self.loop.run_until_complete(proto.got_data[1].wait()) + proto.got_data[1].clear() + self.assertEqual(b'Python ', proto.data[1]) - with test_utils.disable_logger(): - transp.close() - self.loop.run_until_complete(proto.completed) - self.check_killed(proto.returncode) + stdin.write(b'The Winner') + self.loop.run_until_complete(proto.got_data[1].wait()) + self.assertEqual(b'Python The Winner', proto.data[1]) + + with test_utils.disable_logger(): + transp.close() + self.loop.run_until_complete(proto.completed) + self.check_killed(proto.returncode) def test_subprocess_shell(self): - connect = self.loop.subprocess_shell( - functools.partial(MySubprocessProtocol, self.loop), - 'echo Python') - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) + with self.assertWarns(DeprecationWarning): + connect = self.loop.subprocess_shell( + functools.partial(MySubprocessProtocol, self.loop), + 'echo Python') + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) - transp.get_pipe_transport(0).close() - self.loop.run_until_complete(proto.completed) - self.assertEqual(0, proto.returncode) - self.assertTrue(all(f.done() for f in proto.disconnects.values())) - self.assertEqual(proto.data[1].rstrip(b'\r\n'), b'Python') - self.assertEqual(proto.data[2], b'') - transp.close() + transp.get_pipe_transport(0).close() + self.loop.run_until_complete(proto.completed) + self.assertEqual(0, proto.returncode) + self.assertTrue(all(f.done() for f in proto.disconnects.values())) + self.assertEqual(proto.data[1].rstrip(b'\r\n'), b'Python') + self.assertEqual(proto.data[2], b'') + transp.close() def test_subprocess_exitcode(self): connect = self.loop.subprocess_shell( functools.partial(MySubprocessProtocol, self.loop), 'exit 7', stdin=None, stdout=None, stderr=None) - transp, proto = self.loop.run_until_complete(connect) + + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) self.assertIsInstance(proto, MySubprocessProtocol) self.loop.run_until_complete(proto.completed) self.assertEqual(7, proto.returncode) @@ -1789,7 +1795,8 @@ class SubprocessTestsMixin: connect = self.loop.subprocess_shell( functools.partial(MySubprocessProtocol, self.loop), 'exit 7', stdin=None, stdout=None, stderr=None) - transp, proto = self.loop.run_until_complete(connect) + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsNone(transp.get_pipe_transport(0)) self.assertIsNone(transp.get_pipe_transport(1)) @@ -1804,14 +1811,16 @@ class SubprocessTestsMixin: connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) - transp.kill() - self.loop.run_until_complete(proto.completed) - self.check_killed(proto.returncode) - transp.close() + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) + + transp.kill() + self.loop.run_until_complete(proto.completed) + self.check_killed(proto.returncode) + transp.close() def test_subprocess_terminate(self): prog = os.path.join(os.path.dirname(__file__), 'echo.py') @@ -1819,14 +1828,16 @@ class SubprocessTestsMixin: connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) - transp.terminate() - self.loop.run_until_complete(proto.completed) - self.check_terminated(proto.returncode) - transp.close() + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) + + transp.terminate() + self.loop.run_until_complete(proto.completed) + self.check_terminated(proto.returncode) + transp.close() @unittest.skipIf(sys.platform == 'win32', "Don't have SIGHUP") def test_subprocess_send_signal(self): @@ -1840,14 +1851,16 @@ class SubprocessTestsMixin: connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) - transp.send_signal(signal.SIGHUP) - self.loop.run_until_complete(proto.completed) - self.assertEqual(-signal.SIGHUP, proto.returncode) - transp.close() + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) + + transp.send_signal(signal.SIGHUP) + self.loop.run_until_complete(proto.completed) + self.assertEqual(-signal.SIGHUP, proto.returncode) + transp.close() finally: signal.signal(signal.SIGHUP, old_handler) @@ -1857,19 +1870,21 @@ class SubprocessTestsMixin: connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) - stdin = transp.get_pipe_transport(0) - stdin.write(b'test') + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) - self.loop.run_until_complete(proto.completed) + stdin = transp.get_pipe_transport(0) + stdin.write(b'test') - transp.close() - self.assertEqual(b'OUT:test', proto.data[1]) - self.assertTrue(proto.data[2].startswith(b'ERR:test'), proto.data[2]) - self.assertEqual(0, proto.returncode) + self.loop.run_until_complete(proto.completed) + + transp.close() + self.assertEqual(b'OUT:test', proto.data[1]) + self.assertTrue(proto.data[2].startswith(b'ERR:test'), proto.data[2]) + self.assertEqual(0, proto.returncode) def test_subprocess_stderr_redirect_to_stdout(self): prog = os.path.join(os.path.dirname(__file__), 'echo2.py') @@ -1877,22 +1892,24 @@ class SubprocessTestsMixin: connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog, stderr=subprocess.STDOUT) - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) - stdin = transp.get_pipe_transport(0) - self.assertIsNotNone(transp.get_pipe_transport(1)) - self.assertIsNone(transp.get_pipe_transport(2)) + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) - stdin.write(b'test') - self.loop.run_until_complete(proto.completed) - self.assertTrue(proto.data[1].startswith(b'OUT:testERR:test'), - proto.data[1]) - self.assertEqual(b'', proto.data[2]) + stdin = transp.get_pipe_transport(0) + self.assertIsNotNone(transp.get_pipe_transport(1)) + self.assertIsNone(transp.get_pipe_transport(2)) - transp.close() - self.assertEqual(0, proto.returncode) + stdin.write(b'test') + self.loop.run_until_complete(proto.completed) + self.assertTrue(proto.data[1].startswith(b'OUT:testERR:test'), + proto.data[1]) + self.assertEqual(b'', proto.data[2]) + + transp.close() + self.assertEqual(0, proto.returncode) def test_subprocess_close_client_stream(self): prog = os.path.join(os.path.dirname(__file__), 'echo3.py') @@ -1900,32 +1917,33 @@ class SubprocessTestsMixin: connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) + with self.assertWarns(DeprecationWarning): + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) - stdin = transp.get_pipe_transport(0) - stdout = transp.get_pipe_transport(1) - stdin.write(b'test') - self.loop.run_until_complete(proto.got_data[1].wait()) - self.assertEqual(b'OUT:test', proto.data[1]) + stdin = transp.get_pipe_transport(0) + stdout = transp.get_pipe_transport(1) + stdin.write(b'test') + self.loop.run_until_complete(proto.got_data[1].wait()) + self.assertEqual(b'OUT:test', proto.data[1]) - stdout.close() - self.loop.run_until_complete(proto.disconnects[1]) - stdin.write(b'xxx') - self.loop.run_until_complete(proto.got_data[2].wait()) - if sys.platform != 'win32': - self.assertEqual(b'ERR:BrokenPipeError', proto.data[2]) - else: - # After closing the read-end of a pipe, writing to the - # write-end using os.write() fails with errno==EINVAL and - # GetLastError()==ERROR_INVALID_NAME on Windows!?! (Using - # WriteFile() we get ERROR_BROKEN_PIPE as expected.) - self.assertEqual(b'ERR:OSError', proto.data[2]) - with test_utils.disable_logger(): - transp.close() - self.loop.run_until_complete(proto.completed) - self.check_killed(proto.returncode) + stdout.close() + self.loop.run_until_complete(proto.disconnects[1]) + stdin.write(b'xxx') + self.loop.run_until_complete(proto.got_data[2].wait()) + if sys.platform != 'win32': + self.assertEqual(b'ERR:BrokenPipeError', proto.data[2]) + else: + # After closing the read-end of a pipe, writing to the + # write-end using os.write() fails with errno==EINVAL and + # GetLastError()==ERROR_INVALID_NAME on Windows!?! (Using + # WriteFile() we get ERROR_BROKEN_PIPE as expected.) + self.assertEqual(b'ERR:OSError', proto.data[2]) + with test_utils.disable_logger(): + transp.close() + self.loop.run_until_complete(proto.completed) + self.check_killed(proto.returncode) def test_subprocess_wait_no_same_group(self): # start the new process in a new session @@ -1939,7 +1957,6 @@ class SubprocessTestsMixin: self.assertEqual(7, proto.returncode) def test_subprocess_exec_invalid_args(self): - async def connect(**kwds): await self.loop.subprocess_exec( asyncio.SubprocessProtocol, diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index 5063a1d..d69b56d 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -28,10 +28,12 @@ class LockTests(test_utils.TestCase): def test_ctor_loop(self): loop = mock.Mock() - lock = asyncio.Lock(loop=loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=loop) self.assertIs(lock._loop, loop) - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) self.assertIs(lock._loop, self.loop) def test_ctor_noloop(self): @@ -40,7 +42,8 @@ class LockTests(test_utils.TestCase): self.assertIs(lock._loop, self.loop) def test_repr(self): - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) self.assertTrue(repr(lock).endswith('[unlocked]>')) self.assertTrue(RGX_REPR.match(repr(lock))) @@ -55,9 +58,10 @@ class LockTests(test_utils.TestCase): self.assertTrue(RGX_REPR.match(repr(lock))) def test_lock(self): - lock = asyncio.Lock(loop=self.loop) - with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) + + @asyncio.coroutine def acquire_lock(): with self.assertWarns(DeprecationWarning): @@ -74,14 +78,14 @@ class LockTests(test_utils.TestCase): def test_lock_by_with_statement(self): loop = asyncio.new_event_loop() # don't use TestLoop quirks self.set_event_loop(loop) - primitives = [ - asyncio.Lock(loop=loop), - asyncio.Condition(loop=loop), - asyncio.Semaphore(loop=loop), - asyncio.BoundedSemaphore(loop=loop), - ] - with self.assertWarns(DeprecationWarning): + primitives = [ + asyncio.Lock(loop=loop), + asyncio.Condition(loop=loop), + asyncio.Semaphore(loop=loop), + asyncio.BoundedSemaphore(loop=loop), + ] + @asyncio.coroutine def test(lock): yield from asyncio.sleep(0.01) @@ -99,7 +103,8 @@ class LockTests(test_utils.TestCase): self.assertFalse(primitive.locked()) def test_acquire(self): - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) result = [] self.assertTrue(self.loop.run_until_complete(lock.acquire())) @@ -150,7 +155,8 @@ class LockTests(test_utils.TestCase): self.assertTrue(t3.result()) def test_acquire_cancel(self): - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) self.assertTrue(self.loop.run_until_complete(lock.acquire())) task = asyncio.Task(lock.acquire(), loop=self.loop) @@ -175,7 +181,8 @@ class LockTests(test_utils.TestCase): # B's waiter; instead, it should move on to C's waiter. # Setup: A has the lock, b and c are waiting. - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) async def lockit(name, blocker): await lock.acquire() @@ -211,7 +218,8 @@ class LockTests(test_utils.TestCase): # Issue 32734 # Acquire 4 locks, cancel second, release first # and 2 locks are taken at once. - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) lock_count = 0 call_count = 0 @@ -256,7 +264,8 @@ class LockTests(test_utils.TestCase): self.assertTrue(t3.cancelled()) def test_finished_waiter_cancelled(self): - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) ta = asyncio.Task(lock.acquire(), loop=self.loop) test_utils.run_briefly(self.loop) @@ -278,12 +287,14 @@ class LockTests(test_utils.TestCase): self.assertTrue(tb.cancelled()) def test_release_not_acquired(self): - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) self.assertRaises(RuntimeError, lock.release) def test_release_no_waiters(self): - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) self.loop.run_until_complete(lock.acquire()) self.assertTrue(lock.locked()) @@ -291,9 +302,9 @@ class LockTests(test_utils.TestCase): self.assertFalse(lock.locked()) def test_context_manager(self): - lock = asyncio.Lock(loop=self.loop) - with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) + @asyncio.coroutine def acquire_lock(): with self.assertWarns(DeprecationWarning): @@ -305,9 +316,9 @@ class LockTests(test_utils.TestCase): self.assertFalse(lock.locked()) def test_context_manager_cant_reuse(self): - lock = asyncio.Lock(loop=self.loop) - with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) + @asyncio.coroutine def acquire_lock(): with self.assertWarns(DeprecationWarning): @@ -325,7 +336,8 @@ class LockTests(test_utils.TestCase): pass def test_context_manager_no_yield(self): - lock = asyncio.Lock(loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) try: with lock: @@ -346,10 +358,12 @@ class EventTests(test_utils.TestCase): def test_ctor_loop(self): loop = mock.Mock() - ev = asyncio.Event(loop=loop) + with self.assertWarns(DeprecationWarning): + ev = asyncio.Event(loop=loop) self.assertIs(ev._loop, loop) - ev = asyncio.Event(loop=self.loop) + with self.assertWarns(DeprecationWarning): + ev = asyncio.Event(loop=self.loop) self.assertIs(ev._loop, self.loop) def test_ctor_noloop(self): @@ -358,7 +372,8 @@ class EventTests(test_utils.TestCase): self.assertIs(ev._loop, self.loop) def test_repr(self): - ev = asyncio.Event(loop=self.loop) + with self.assertWarns(DeprecationWarning): + ev = asyncio.Event(loop=self.loop) self.assertTrue(repr(ev).endswith('[unset]>')) match = RGX_REPR.match(repr(ev)) self.assertEqual(match.group('extras'), 'unset') @@ -372,7 +387,8 @@ class EventTests(test_utils.TestCase): self.assertTrue(RGX_REPR.match(repr(ev))) def test_wait(self): - ev = asyncio.Event(loop=self.loop) + with self.assertWarns(DeprecationWarning): + ev = asyncio.Event(loop=self.loop) self.assertFalse(ev.is_set()) result = [] @@ -409,14 +425,16 @@ class EventTests(test_utils.TestCase): self.assertIsNone(t3.result()) def test_wait_on_set(self): - ev = asyncio.Event(loop=self.loop) + with self.assertWarns(DeprecationWarning): + ev = asyncio.Event(loop=self.loop) ev.set() res = self.loop.run_until_complete(ev.wait()) self.assertTrue(res) def test_wait_cancel(self): - ev = asyncio.Event(loop=self.loop) + with self.assertWarns(DeprecationWarning): + ev = asyncio.Event(loop=self.loop) wait = asyncio.Task(ev.wait(), loop=self.loop) self.loop.call_soon(wait.cancel) @@ -426,7 +444,8 @@ class EventTests(test_utils.TestCase): self.assertFalse(ev._waiters) def test_clear(self): - ev = asyncio.Event(loop=self.loop) + with self.assertWarns(DeprecationWarning): + ev = asyncio.Event(loop=self.loop) self.assertFalse(ev.is_set()) ev.set() @@ -436,7 +455,8 @@ class EventTests(test_utils.TestCase): self.assertFalse(ev.is_set()) def test_clear_with_waiters(self): - ev = asyncio.Event(loop=self.loop) + with self.assertWarns(DeprecationWarning): + ev = asyncio.Event(loop=self.loop) result = [] async def c1(result): @@ -472,19 +492,22 @@ class ConditionTests(test_utils.TestCase): def test_ctor_loop(self): loop = mock.Mock() - cond = asyncio.Condition(loop=loop) - self.assertIs(cond._loop, loop) + with self.assertWarns(DeprecationWarning): + 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) - cond = asyncio.Condition() - self.assertIs(cond._loop, self.loop) + with self.assertWarns(DeprecationWarning): + asyncio.set_event_loop(self.loop) + cond = asyncio.Condition() + self.assertIs(cond._loop, self.loop) def test_wait(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) result = [] async def c1(result): @@ -547,7 +570,8 @@ class ConditionTests(test_utils.TestCase): self.assertTrue(t3.result()) def test_wait_cancel(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) self.loop.run_until_complete(cond.acquire()) wait = asyncio.Task(cond.wait(), loop=self.loop) @@ -559,7 +583,8 @@ class ConditionTests(test_utils.TestCase): self.assertTrue(cond.locked()) def test_wait_cancel_contested(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) self.loop.run_until_complete(cond.acquire()) self.assertTrue(cond.locked()) @@ -585,7 +610,8 @@ class ConditionTests(test_utils.TestCase): def test_wait_cancel_after_notify(self): # See bpo-32841 - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) waited = False async def wait_on_cond(): @@ -609,13 +635,15 @@ class ConditionTests(test_utils.TestCase): self.assertTrue(waited) def test_wait_unacquired(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) self.assertRaises( RuntimeError, self.loop.run_until_complete, cond.wait()) def test_wait_for(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) presult = False def predicate(): @@ -652,7 +680,8 @@ class ConditionTests(test_utils.TestCase): self.assertTrue(t.result()) def test_wait_for_unacquired(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) # predicate can return true immediately res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3])) @@ -664,7 +693,8 @@ class ConditionTests(test_utils.TestCase): cond.wait_for(lambda: False)) def test_notify(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) result = [] async def c1(result): @@ -716,7 +746,8 @@ class ConditionTests(test_utils.TestCase): self.assertTrue(t3.result()) def test_notify_all(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) result = [] @@ -752,15 +783,18 @@ class ConditionTests(test_utils.TestCase): self.assertTrue(t2.result()) def test_notify_unacquired(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) self.assertRaises(RuntimeError, cond.notify) def test_notify_all_unacquired(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) self.assertRaises(RuntimeError, cond.notify_all) def test_repr(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) self.assertTrue('unlocked' in repr(cond)) self.assertTrue(RGX_REPR.match(repr(cond))) @@ -776,7 +810,8 @@ class ConditionTests(test_utils.TestCase): self.assertTrue(RGX_REPR.match(repr(cond))) def test_context_manager(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) with self.assertWarns(DeprecationWarning): @asyncio.coroutine @@ -790,7 +825,8 @@ class ConditionTests(test_utils.TestCase): self.assertFalse(cond.locked()) def test_context_manager_no_yield(self): - cond = asyncio.Condition(loop=self.loop) + with self.assertWarns(DeprecationWarning): + cond = asyncio.Condition(loop=self.loop) try: with cond: @@ -803,8 +839,9 @@ class ConditionTests(test_utils.TestCase): self.assertFalse(cond.locked()) def test_explicit_lock(self): - lock = asyncio.Lock(loop=self.loop) - cond = asyncio.Condition(lock, loop=self.loop) + with self.assertWarns(DeprecationWarning): + lock = asyncio.Lock(loop=self.loop) + cond = asyncio.Condition(lock, loop=self.loop) self.assertIs(cond._lock, lock) self.assertIs(cond._loop, lock._loop) @@ -812,10 +849,10 @@ class ConditionTests(test_utils.TestCase): def test_ambiguous_loops(self): loop = self.new_test_loop() self.addCleanup(loop.close) - - lock = asyncio.Lock(loop=self.loop) - with self.assertRaises(ValueError): - asyncio.Condition(lock, loop=loop) + with self.assertWarns(DeprecationWarning): + 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() @@ -827,7 +864,8 @@ class ConditionTests(test_utils.TestCase): with self.assertRaises(asyncio.TimeoutError): await asyncio.wait_for(condition.wait(), timeout=0.5) - loop.run_until_complete(task_timeout()) + with self.assertWarns(DeprecationWarning): + loop.run_until_complete(task_timeout()) class SemaphoreTests(test_utils.TestCase): @@ -838,10 +876,12 @@ class SemaphoreTests(test_utils.TestCase): def test_ctor_loop(self): loop = mock.Mock() - sem = asyncio.Semaphore(loop=loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(loop=loop) self.assertIs(sem._loop, loop) - sem = asyncio.Semaphore(loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(loop=self.loop) self.assertIs(sem._loop, self.loop) def test_ctor_noloop(self): @@ -850,11 +890,13 @@ class SemaphoreTests(test_utils.TestCase): self.assertIs(sem._loop, self.loop) def test_initial_value_zero(self): - sem = asyncio.Semaphore(0, loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(0, loop=self.loop) self.assertTrue(sem.locked()) def test_repr(self): - sem = asyncio.Semaphore(loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(loop=self.loop) self.assertTrue(repr(sem).endswith('[unlocked, value:1]>')) self.assertTrue(RGX_REPR.match(repr(sem))) @@ -872,7 +914,8 @@ class SemaphoreTests(test_utils.TestCase): self.assertTrue(RGX_REPR.match(repr(sem))) def test_semaphore(self): - sem = asyncio.Semaphore(loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(loop=self.loop) self.assertEqual(1, sem._value) with self.assertWarns(DeprecationWarning): @@ -895,7 +938,8 @@ class SemaphoreTests(test_utils.TestCase): self.assertRaises(ValueError, asyncio.Semaphore, -1) def test_acquire(self): - sem = asyncio.Semaphore(3, loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(3, loop=self.loop) result = [] self.assertTrue(self.loop.run_until_complete(sem.acquire())) @@ -956,7 +1000,8 @@ class SemaphoreTests(test_utils.TestCase): self.loop.run_until_complete(asyncio.gather(*race_tasks)) def test_acquire_cancel(self): - sem = asyncio.Semaphore(loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(loop=self.loop) self.loop.run_until_complete(sem.acquire()) acquire = asyncio.Task(sem.acquire(), loop=self.loop) @@ -968,7 +1013,8 @@ class SemaphoreTests(test_utils.TestCase): all(waiter.done() for waiter in sem._waiters)) def test_acquire_cancel_before_awoken(self): - sem = asyncio.Semaphore(value=0, loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(value=0, loop=self.loop) t1 = asyncio.Task(sem.acquire(), loop=self.loop) t2 = asyncio.Task(sem.acquire(), loop=self.loop) @@ -990,7 +1036,8 @@ class SemaphoreTests(test_utils.TestCase): test_utils.run_briefly(self.loop) def test_acquire_hang(self): - sem = asyncio.Semaphore(value=0, loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(value=0, loop=self.loop) t1 = asyncio.Task(sem.acquire(), loop=self.loop) t2 = asyncio.Task(sem.acquire(), loop=self.loop) @@ -1004,12 +1051,14 @@ class SemaphoreTests(test_utils.TestCase): self.assertTrue(sem.locked()) def test_release_not_acquired(self): - sem = asyncio.BoundedSemaphore(loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.BoundedSemaphore(loop=self.loop) self.assertRaises(ValueError, sem.release) def test_release_no_waiters(self): - sem = asyncio.Semaphore(loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(loop=self.loop) self.loop.run_until_complete(sem.acquire()) self.assertTrue(sem.locked()) @@ -1017,9 +1066,9 @@ class SemaphoreTests(test_utils.TestCase): self.assertFalse(sem.locked()) def test_context_manager(self): - sem = asyncio.Semaphore(2, loop=self.loop) - with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(2, loop=self.loop) + @asyncio.coroutine def acquire_lock(): with self.assertWarns(DeprecationWarning): @@ -1035,7 +1084,8 @@ class SemaphoreTests(test_utils.TestCase): self.assertEqual(2, sem._value) def test_context_manager_no_yield(self): - sem = asyncio.Semaphore(2, loop=self.loop) + with self.assertWarns(DeprecationWarning): + sem = asyncio.Semaphore(2, loop=self.loop) try: with sem: diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index a5cf37d..f327bbd 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -43,12 +43,13 @@ class BaseTest(test_utils.TestCase): class LockTests(BaseTest): def test_context_manager_async_with(self): - primitives = [ - asyncio.Lock(loop=self.loop), - asyncio.Condition(loop=self.loop), - asyncio.Semaphore(loop=self.loop), - asyncio.BoundedSemaphore(loop=self.loop), - ] + with self.assertWarns(DeprecationWarning): + primitives = [ + asyncio.Lock(loop=self.loop), + asyncio.Condition(loop=self.loop), + asyncio.Semaphore(loop=self.loop), + asyncio.BoundedSemaphore(loop=self.loop), + ] async def test(lock): await asyncio.sleep(0.01) @@ -65,12 +66,13 @@ class LockTests(BaseTest): self.assertFalse(primitive.locked()) def test_context_manager_with_await(self): - primitives = [ - asyncio.Lock(loop=self.loop), - asyncio.Condition(loop=self.loop), - asyncio.Semaphore(loop=self.loop), - asyncio.BoundedSemaphore(loop=self.loop), - ] + with self.assertWarns(DeprecationWarning): + primitives = [ + asyncio.Lock(loop=self.loop), + asyncio.Condition(loop=self.loop), + asyncio.Semaphore(loop=self.loop), + asyncio.BoundedSemaphore(loop=self.loop), + ] async def test(lock): await asyncio.sleep(0.01) diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index b0f0f9c..02e8e43 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -35,7 +35,8 @@ class QueueBasicTests(_QueueTestBase): loop = self.new_test_loop(gen) - q = asyncio.Queue(loop=loop) + with self.assertWarns(DeprecationWarning): + q = asyncio.Queue(loop=loop) self.assertTrue(fn(q).startswith('