diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index f1ce7324785ba9..6f322c258cfe4f 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -3,10 +3,9 @@ __all__ = ('Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore') import collections -import warnings -from . import events from . import exceptions +from . import mixins class _ContextManagerMixin: @@ -20,7 +19,7 @@ async def __aexit__(self, exc_type, exc, tb): self.release() -class Lock(_ContextManagerMixin): +class Lock(_ContextManagerMixin, mixins._LoopBoundedMixin): """Primitive lock objects. A primitive lock is a synchronization primitive that is not owned @@ -74,16 +73,9 @@ class Lock(_ContextManagerMixin): """ - def __init__(self, *, loop=None): + def __init__(self): self._waiters = None self._locked = False - 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__() @@ -109,7 +101,7 @@ async def acquire(self): if self._waiters is None: self._waiters = collections.deque() - fut = self._loop.create_future() + fut = self._get_loop().create_future() self._waiters.append(fut) # Finally block should be called before the CancelledError @@ -161,7 +153,7 @@ def _wake_up_first(self): fut.set_result(True) -class Event: +class Event(mixins._LoopBoundedMixin): """Asynchronous equivalent to threading.Event. Class implementing event objects. An event manages a flag that can be set @@ -170,16 +162,9 @@ class Event: false. """ - def __init__(self, *, loop=None): + def __init__(self): self._waiters = collections.deque() self._value = False - 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__() @@ -220,7 +205,7 @@ async def wait(self): if self._value: return True - fut = self._loop.create_future() + fut = self._get_loop().create_future() self._waiters.append(fut) try: await fut @@ -229,7 +214,7 @@ async def wait(self): self._waiters.remove(fut) -class Condition(_ContextManagerMixin): +class Condition(_ContextManagerMixin, mixins._LoopBoundedMixin): """Asynchronous equivalent to threading.Condition. This class implements condition variable objects. A condition variable @@ -239,18 +224,10 @@ class Condition(_ContextManagerMixin): A new Lock object is created and used as the underlying lock. """ - def __init__(self, lock=None, *, loop=None): - 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 __init__(self, lock=None): if lock is None: - lock = Lock(loop=loop) - elif lock._loop is not self._loop: + lock = Lock() + elif lock._loop is not self._get_loop(): raise ValueError("loop argument must agree with lock") self._lock = lock @@ -284,7 +261,7 @@ async def wait(self): self.release() try: - fut = self._loop.create_future() + fut = self._get_loop().create_future() self._waiters.append(fut) try: await fut @@ -351,7 +328,7 @@ def notify_all(self): self.notify(len(self._waiters)) -class Semaphore(_ContextManagerMixin): +class Semaphore(_ContextManagerMixin, mixins._LoopBoundedMixin): """A Semaphore implementation. A semaphore manages an internal counter which is decremented by each @@ -366,18 +343,11 @@ class Semaphore(_ContextManagerMixin): ValueError is raised. """ - def __init__(self, value=1, *, loop=None): + def __init__(self, value=1): if value < 0: raise ValueError("Semaphore initial value must be >= 0") self._value = value self._waiters = collections.deque() - 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__() @@ -407,7 +377,7 @@ async def acquire(self): True. """ while self._value <= 0: - fut = self._loop.create_future() + fut = self._get_loop().create_future() self._waiters.append(fut) try: await fut @@ -436,14 +406,9 @@ class BoundedSemaphore(Semaphore): above the initial value. """ - 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) - + def __init__(self, value=1): self._bound_value = value - super().__init__(value, loop=loop) + super().__init__(value) def release(self): if self._value >= self._bound_value: diff --git a/Lib/asyncio/mixins.py b/Lib/asyncio/mixins.py new file mode 100644 index 00000000000000..dbc4b5faccb019 --- /dev/null +++ b/Lib/asyncio/mixins.py @@ -0,0 +1,21 @@ +"""Event loop mixins.""" + +import threading +from . import events + +_global_lock = threading.Lock() + + +class _LoopBoundedMixin: + _loop = None + + def _get_loop(self): + loop = events._get_running_loop() + + if self._loop is None: + with _global_lock: + if self._loop is None: + self._loop = loop + if loop is not self._loop: + raise RuntimeError(f'{type(self).__name__} have already bounded to another loop') + return loop diff --git a/Lib/asyncio/queues.py b/Lib/asyncio/queues.py index cd3f7c6a567891..78ae9e99ccf0e9 100644 --- a/Lib/asyncio/queues.py +++ b/Lib/asyncio/queues.py @@ -2,10 +2,9 @@ import collections import heapq -import warnings -from . import events from . import locks +from . import mixins class QueueEmpty(Exception): @@ -18,7 +17,7 @@ class QueueFull(Exception): pass -class Queue: +class Queue(mixins._LoopBoundedMixin): """A queue, useful for coordinating producer and consumer coroutines. If maxsize is less than or equal to zero, the queue size is infinite. If it @@ -30,14 +29,7 @@ class Queue: interrupted between calling qsize() and doing an operation on the Queue. """ - def __init__(self, maxsize=0, *, loop=None): - 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 __init__(self, maxsize=0): self._maxsize = maxsize # Futures. @@ -45,7 +37,7 @@ def __init__(self, maxsize=0, *, loop=None): # Futures. self._putters = collections.deque() self._unfinished_tasks = 0 - self._finished = locks.Event(loop=loop) + self._finished = locks.Event() self._finished.set() self._init(maxsize) @@ -122,7 +114,7 @@ async def put(self, item): slot is available before adding item. """ while self.full(): - putter = self._loop.create_future() + putter = self._get_loop().create_future() self._putters.append(putter) try: await putter @@ -160,7 +152,7 @@ async def get(self): If queue is empty, wait until an item is available. """ while self.empty(): - getter = self._loop.create_future() + getter = self._get_loop().create_future() self._getters.append(getter) try: await getter diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index f486b672294111..03d8451fa1744a 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -578,7 +578,7 @@ def as_completed(fs, *, loop=None, timeout=None): raise TypeError(f"expect an iterable of futures, not {type(fs).__name__}") from .queues import Queue # Import here to avoid circular import problem. - done = Queue(loop=loop) + done = Queue() if loop is None: loop = events.get_event_loop() diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index b8fe466cd53dfc..7f76011d2b92dc 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -206,8 +206,8 @@ def __init__(self, loop): self.disconnects = {fd: loop.create_future() for fd in range(3)} self.data = {1: b'', 2: b''} self.returncode = None - self.got_data = {1: asyncio.Event(loop=loop), - 2: asyncio.Event(loop=loop)} + self.got_data = {1: asyncio.Event(), + 2: asyncio.Event()} def connection_made(self, transport): self.transport = transport @@ -1740,20 +1740,20 @@ def test_subprocess_exec(self): connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - 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]) + 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]) def test_subprocess_interactive(self): prog = os.path.join(os.path.dirname(__file__), 'echo.py') @@ -1762,51 +1762,48 @@ def test_subprocess_interactive(self): functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - 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) + 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]) + 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]) - 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.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) + with test_utils.disable_logger(): + transp.close() + self.loop.run_until_complete(proto.completed) + self.check_killed(proto.returncode) def test_subprocess_shell(self): - 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) + 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) - with self.assertWarns(DeprecationWarning): - transp, proto = self.loop.run_until_complete(connect) + transp, proto = self.loop.run_until_complete(connect) self.assertIsInstance(proto, MySubprocessProtocol) self.loop.run_until_complete(proto.completed) self.assertEqual(7, proto.returncode) @@ -1816,8 +1813,8 @@ def test_subprocess_close_after_finish(self): connect = self.loop.subprocess_shell( functools.partial(MySubprocessProtocol, self.loop), 'exit 7', stdin=None, stdout=None, stderr=None) - with self.assertWarns(DeprecationWarning): - transp, proto = self.loop.run_until_complete(connect) + + 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)) @@ -1833,15 +1830,14 @@ def test_subprocess_kill(self): functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - with self.assertWarns(DeprecationWarning): - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) + 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() + 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') @@ -1850,15 +1846,14 @@ def test_subprocess_terminate(self): functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - with self.assertWarns(DeprecationWarning): - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) + 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() + 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): @@ -1873,15 +1868,15 @@ def test_subprocess_send_signal(self): functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - 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() + 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) @@ -1892,20 +1887,19 @@ def test_subprocess_stderr(self): functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - with self.assertWarns(DeprecationWarning): - transp, proto = self.loop.run_until_complete(connect) - self.assertIsInstance(proto, MySubprocessProtocol) - self.loop.run_until_complete(proto.connected) + 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') + stdin = transp.get_pipe_transport(0) + stdin.write(b'test') - self.loop.run_until_complete(proto.completed) + 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) + 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') @@ -1914,23 +1908,23 @@ def test_subprocess_stderr_redirect_to_stdout(self): functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog, stderr=subprocess.STDOUT) - 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) - self.assertIsNotNone(transp.get_pipe_transport(1)) - self.assertIsNone(transp.get_pipe_transport(2)) + 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') @@ -1938,33 +1932,33 @@ def test_subprocess_close_client_stream(self): connect = self.loop.subprocess_exec( functools.partial(MySubprocessProtocol, self.loop), sys.executable, prog) - 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]) + transp, proto = self.loop.run_until_complete(connect) + self.assertIsInstance(proto, MySubprocessProtocol) + self.loop.run_until_complete(proto.connected) - 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) + 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) def test_subprocess_wait_no_same_group(self): # start the new process in a new session diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index 8c93fae2b51c6c..6c34ef60e30b51 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -26,24 +26,8 @@ def setUp(self): super().setUp() self.loop = self.new_test_loop() - def test_ctor_loop(self): - loop = mock.Mock() - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=loop) - self.assertIs(lock._loop, loop) - - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) - self.assertIs(lock._loop, self.loop) - - def test_ctor_noloop(self): - asyncio.set_event_loop(self.loop) - lock = asyncio.Lock() - self.assertIs(lock._loop, self.loop) - def test_repr(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock() self.assertTrue(repr(lock).endswith('[unlocked]>')) self.assertTrue(RGX_REPR.match(repr(lock))) @@ -52,9 +36,9 @@ def test_repr(self): self.assertTrue(RGX_REPR.match(repr(lock))) def test_lock(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock() + with self.assertWarns(DeprecationWarning): @asyncio.coroutine def acquire_lock(): return (yield from lock) @@ -70,14 +54,14 @@ def acquire_lock(): def test_lock_by_with_statement(self): loop = asyncio.new_event_loop() # don't use TestLoop quirks self.set_event_loop(loop) - with self.assertWarns(DeprecationWarning): - primitives = [ - asyncio.Lock(loop=loop), - asyncio.Condition(loop=loop), - asyncio.Semaphore(loop=loop), - asyncio.BoundedSemaphore(loop=loop), - ] + primitives = [ + asyncio.Lock(), + asyncio.Condition(), + asyncio.Semaphore(), + asyncio.BoundedSemaphore(), + ] + with self.assertWarns(DeprecationWarning): @asyncio.coroutine def test(lock): yield from asyncio.sleep(0.01) @@ -95,8 +79,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() result = [] self.assertTrue(self.loop.run_until_complete(lock.acquire())) @@ -147,8 +130,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() self.assertTrue(self.loop.run_until_complete(lock.acquire())) task = self.loop.create_task(lock.acquire()) @@ -173,8 +155,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() async def lockit(name, blocker): await lock.acquire() @@ -210,8 +191,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() lock_count = 0 call_count = 0 @@ -256,8 +236,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() ta = self.loop.create_task(lock.acquire()) test_utils.run_briefly(self.loop) @@ -279,14 +258,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() self.assertRaises(RuntimeError, lock.release) def test_release_no_waiters(self): - with self.assertWarns(DeprecationWarning): - lock = asyncio.Lock(loop=self.loop) + lock = asyncio.Lock() self.loop.run_until_complete(lock.acquire()) self.assertTrue(lock.locked()) @@ -312,24 +289,8 @@ def setUp(self): super().setUp() self.loop = self.new_test_loop() - def test_ctor_loop(self): - loop = mock.Mock() - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=loop) - self.assertIs(ev._loop, loop) - - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=self.loop) - self.assertIs(ev._loop, self.loop) - - def test_ctor_noloop(self): - asyncio.set_event_loop(self.loop) - ev = asyncio.Event() - self.assertIs(ev._loop, self.loop) - def test_repr(self): - with self.assertWarns(DeprecationWarning): - ev = asyncio.Event(loop=self.loop) + ev = asyncio.Event() self.assertTrue(repr(ev).endswith('[unset]>')) match = RGX_REPR.match(repr(ev)) self.assertEqual(match.group('extras'), 'unset') @@ -343,8 +304,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() self.assertFalse(ev.is_set()) result = [] @@ -381,16 +341,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() 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() wait = self.loop.create_task(ev.wait()) self.loop.call_soon(wait.cancel) @@ -400,8 +358,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() self.assertFalse(ev.is_set()) ev.set() @@ -411,8 +368,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() result = [] async def c1(result): @@ -446,23 +402,8 @@ def setUp(self): super().setUp() self.loop = self.new_test_loop() - 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=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) - def test_wait(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition() result = [] async def c1(result): @@ -525,8 +466,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() self.loop.run_until_complete(cond.acquire()) wait = self.loop.create_task(cond.wait()) @@ -538,8 +478,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() self.loop.run_until_complete(cond.acquire()) self.assertTrue(cond.locked()) @@ -565,10 +504,11 @@ 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) waited = False + cond = asyncio.Condition() + cond._loop = self.loop + async def wait_on_cond(): nonlocal waited async with cond: @@ -590,15 +530,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() 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() presult = False def predicate(): @@ -635,8 +573,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() # predicate can return true immediately res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3])) @@ -648,8 +585,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() result = [] async def c1(result): @@ -701,8 +637,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() result = [] @@ -738,18 +673,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() self.assertRaises(RuntimeError, cond.notify) def test_notify_all_unacquired(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition() self.assertRaises(RuntimeError, cond.notify_all) def test_repr(self): - with self.assertWarns(DeprecationWarning): - cond = asyncio.Condition(loop=self.loop) + cond = asyncio.Condition() self.assertTrue('unlocked' in repr(cond)) self.assertTrue(RGX_REPR.match(repr(cond))) @@ -775,9 +707,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() + cond = asyncio.Condition(lock) self.assertIs(cond._lock, lock) self.assertIs(cond._loop, lock._loop) @@ -785,23 +716,27 @@ 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) + + lock = asyncio.Lock() + lock._loop = loop + + async def _create_condition(): with self.assertRaises(ValueError): - asyncio.Condition(lock, loop=loop) + asyncio.Condition(lock) + + self.loop.run_until_complete(_create_condition()) def test_timeout_in_block(self): loop = asyncio.new_event_loop() self.addCleanup(loop.close) async def task_timeout(): - condition = asyncio.Condition(loop=loop) + condition = asyncio.Condition() async with condition: 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): @@ -810,29 +745,12 @@ def setUp(self): super().setUp() self.loop = self.new_test_loop() - def test_ctor_loop(self): - loop = mock.Mock() - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=loop) - self.assertIs(sem._loop, loop) - - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=self.loop) - self.assertIs(sem._loop, self.loop) - - def test_ctor_noloop(self): - asyncio.set_event_loop(self.loop) - sem = asyncio.Semaphore() - 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) self.assertTrue(sem.locked()) def test_repr(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=self.loop) + sem = asyncio.Semaphore() self.assertTrue(repr(sem).endswith('[unlocked, value:1]>')) self.assertTrue(RGX_REPR.match(repr(sem))) @@ -850,8 +768,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) + sem = asyncio.Semaphore() self.assertEqual(1, sem._value) with self.assertWarns(DeprecationWarning): @@ -872,8 +789,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) result = [] self.assertTrue(self.loop.run_until_complete(sem.acquire())) @@ -934,8 +850,7 @@ 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() self.loop.run_until_complete(sem.acquire()) acquire = self.loop.create_task(sem.acquire()) @@ -947,8 +862,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) t1 = self.loop.create_task(sem.acquire()) t2 = self.loop.create_task(sem.acquire()) @@ -970,8 +884,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) t1 = self.loop.create_task(sem.acquire()) t2 = self.loop.create_task(sem.acquire()) @@ -985,14 +898,12 @@ def test_acquire_hang(self): self.assertTrue(sem.locked()) def test_release_not_acquired(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.BoundedSemaphore(loop=self.loop) + sem = asyncio.BoundedSemaphore() self.assertRaises(ValueError, sem.release) def test_release_no_waiters(self): - with self.assertWarns(DeprecationWarning): - sem = asyncio.Semaphore(loop=self.loop) + sem = asyncio.Semaphore() self.loop.run_until_complete(sem.acquire()) self.assertTrue(sem.locked()) diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index c5e3a5c1483573..4bd50f4123e579 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -43,13 +43,12 @@ def setUp(self): class LockTests(BaseTest): def test_context_manager_async_with(self): - 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), - ] + primitives = [ + asyncio.Lock(), + asyncio.Condition(), + asyncio.Semaphore(), + asyncio.BoundedSemaphore(), + ] async def test(lock): await asyncio.sleep(0.01) @@ -66,13 +65,12 @@ async def test(lock): self.assertFalse(primitive.locked()) def test_context_manager_with_await(self): - 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), - ] + primitives = [ + asyncio.Lock(), + asyncio.Condition(), + asyncio.Semaphore(), + asyncio.BoundedSemaphore(), + ] 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 5c9aaa82c311a5..dc97673b6f1945 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -35,14 +35,13 @@ def gen(): loop = self.new_test_loop(gen) - with self.assertWarns(DeprecationWarning): - q = asyncio.Queue(loop=loop) + q = asyncio.Queue() self.assertTrue(fn(q).startswith('