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
This commit is contained in:
Emmanuel Arias 2019-09-10 07:55:07 -03:00 committed by Miss Islington (bot)
parent 9669931e5e
commit 537877d85d
7 changed files with 419 additions and 265 deletions

View file

@ -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: