bpo-32253: Deprecate with statement and bare await for asyncio locks (GH-4764)

* Add test for 'with (yield from lock)'
* Deprecate with statement for asyncio locks
* Document the deprecation
This commit is contained in:
Andrew Svetlov 2017-12-09 20:00:05 +02:00 committed by GitHub
parent a9f8df646a
commit 28d8d14013
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 108 additions and 55 deletions

View file

@ -42,7 +42,8 @@ class LockTests(test_utils.TestCase):
@asyncio.coroutine
def acquire_lock():
yield from lock
with self.assertWarns(DeprecationWarning):
yield from lock
self.loop.run_until_complete(acquire_lock())
self.assertTrue(repr(lock).endswith('[locked]>'))
@ -53,7 +54,8 @@ class LockTests(test_utils.TestCase):
@asyncio.coroutine
def acquire_lock():
return (yield from lock)
with self.assertWarns(DeprecationWarning):
return (yield from lock)
res = self.loop.run_until_complete(acquire_lock())
@ -63,6 +65,32 @@ class LockTests(test_utils.TestCase):
lock.release()
self.assertFalse(lock.locked())
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),
]
@asyncio.coroutine
def test(lock):
yield from asyncio.sleep(0.01, loop=loop)
self.assertFalse(lock.locked())
with self.assertWarns(DeprecationWarning):
with (yield from lock) as _lock:
self.assertIs(_lock, None)
self.assertTrue(lock.locked())
yield from asyncio.sleep(0.01, loop=loop)
self.assertTrue(lock.locked())
self.assertFalse(lock.locked())
for primitive in primitives:
loop.run_until_complete(test(primitive))
self.assertFalse(primitive.locked())
def test_acquire(self):
lock = asyncio.Lock(loop=self.loop)
result = []
@ -212,7 +240,8 @@ class LockTests(test_utils.TestCase):
@asyncio.coroutine
def acquire_lock():
return (yield from lock)
with self.assertWarns(DeprecationWarning):
return (yield from lock)
with self.loop.run_until_complete(acquire_lock()):
self.assertTrue(lock.locked())
@ -224,7 +253,8 @@ class LockTests(test_utils.TestCase):
@asyncio.coroutine
def acquire_lock():
return (yield from lock)
with self.assertWarns(DeprecationWarning):
return (yield from lock)
# This spells "yield from lock" outside a generator.
cm = self.loop.run_until_complete(acquire_lock())
@ -668,7 +698,8 @@ class ConditionTests(test_utils.TestCase):
@asyncio.coroutine
def acquire_cond():
return (yield from cond)
with self.assertWarns(DeprecationWarning):
return (yield from cond)
with self.loop.run_until_complete(acquire_cond()):
self.assertTrue(cond.locked())
@ -751,7 +782,8 @@ class SemaphoreTests(test_utils.TestCase):
@asyncio.coroutine
def acquire_lock():
return (yield from sem)
with self.assertWarns(DeprecationWarning):
return (yield from sem)
res = self.loop.run_until_complete(acquire_lock())
@ -893,7 +925,8 @@ class SemaphoreTests(test_utils.TestCase):
@asyncio.coroutine
def acquire_lock():
return (yield from sem)
with self.assertWarns(DeprecationWarning):
return (yield from sem)
with self.loop.run_until_complete(acquire_lock()):
self.assertFalse(sem.locked())