mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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:
parent
a9f8df646a
commit
28d8d14013
5 changed files with 108 additions and 55 deletions
|
@ -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())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue