bpo-42392: Remove loop parameter form asyncio locks and Queue (#23420)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
This commit is contained in:
Yurii Karabas 2020-11-24 20:08:54 +02:00 committed by GitHub
parent b0b428510c
commit 0ec34cab9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 304 additions and 451 deletions

View file

@ -26,24 +26,8 @@ class LockTests(test_utils.TestCase):
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 @@ class LockTests(test_utils.TestCase):
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 @@ 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)
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 @@ class LockTests(test_utils.TestCase):
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 @@ class LockTests(test_utils.TestCase):
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 @@ 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.
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 @@ class LockTests(test_utils.TestCase):
# 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 @@ class LockTests(test_utils.TestCase):
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 @@ class LockTests(test_utils.TestCase):
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 @@ class EventTests(test_utils.TestCase):
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 @@ class EventTests(test_utils.TestCase):
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 @@ class EventTests(test_utils.TestCase):
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 @@ class EventTests(test_utils.TestCase):
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 @@ class EventTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class ConditionTests(test_utils.TestCase):
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 @@ class SemaphoreTests(test_utils.TestCase):
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 @@ class SemaphoreTests(test_utils.TestCase):
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 @@ class SemaphoreTests(test_utils.TestCase):
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 @@ class SemaphoreTests(test_utils.TestCase):
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 @@ class SemaphoreTests(test_utils.TestCase):
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 @@ class SemaphoreTests(test_utils.TestCase):
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 @@ class SemaphoreTests(test_utils.TestCase):
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())