Commit 537877d8 authored by Emmanuel Arias's avatar Emmanuel Arias Committed by Miss Islington (bot)

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
parent 9669931e
......@@ -59,6 +59,9 @@ Lock
finally:
lock.release()
.. deprecated-removed:: 3.8 3.10
The *loop* parameter.
.. coroutinemethod:: acquire()
Acquire the lock.
......@@ -101,6 +104,10 @@ Event
:meth:`clear` method. The :meth:`wait` method blocks until the
flag is set to *true*. The flag is set to *false* initially.
.. deprecated-removed:: 3.8 3.10
The *loop* parameter.
.. _asyncio_example_sync_event:
Example::
......@@ -173,6 +180,10 @@ Condition
``None``. In the latter case a new Lock object is created
automatically.
.. deprecated-removed:: 3.8 3.10
The *loop* parameter.
The preferred way to use a Condition is an :keyword:`async with`
statement::
......@@ -269,6 +280,10 @@ Semaphore
internal counter (``1`` by default). If the given value is
less than ``0`` a :exc:`ValueError` is raised.
.. deprecated-removed:: 3.8 3.10
The *loop* parameter.
The preferred way to use a Semaphore is an :keyword:`async with`
statement::
......@@ -322,6 +337,9 @@ BoundedSemaphore
increases the internal counter above the initial *value*.
.. deprecated-removed:: 3.8 3.10
The *loop* parameter.
---------
......
......@@ -160,10 +160,13 @@ class Lock(_ContextManagerMixin):
def __init__(self, *, loop=None):
self._waiters = None
self._locked = False
if loop is not None:
self._loop = loop
else:
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__()
......@@ -253,10 +256,13 @@ class Event:
def __init__(self, *, loop=None):
self._waiters = collections.deque()
self._value = False
if loop is not None:
self._loop = loop
else:
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__()
......@@ -317,10 +323,13 @@ class Condition(_ContextManagerMixin):
"""
def __init__(self, lock=None, *, loop=None):
if loop is not None:
self._loop = loop
else:
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)
if lock is None:
lock = Lock(loop=self._loop)
......@@ -445,10 +454,13 @@ class Semaphore(_ContextManagerMixin):
raise ValueError("Semaphore initial value must be >= 0")
self._value = value
self._waiters = collections.deque()
if loop is not None:
self._loop = loop
else:
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__()
......@@ -508,6 +520,11 @@ class BoundedSemaphore(Semaphore):
"""
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)
self._bound_value = value
super().__init__(value, loop=loop)
......
This diff is collapsed.
This diff is collapsed.
......@@ -43,12 +43,13 @@ class BaseTest(test_utils.TestCase):
class LockTests(BaseTest):
def test_context_manager_async_with(self):
primitives = [
asyncio.Lock(loop=self.loop),
asyncio.Condition(loop=self.loop),
asyncio.Semaphore(loop=self.loop),
asyncio.BoundedSemaphore(loop=self.loop),
]
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),
]
async def test(lock):
await asyncio.sleep(0.01)
......@@ -65,12 +66,13 @@ class LockTests(BaseTest):
self.assertFalse(primitive.locked())
def test_context_manager_with_await(self):
primitives = [
asyncio.Lock(loop=self.loop),
asyncio.Condition(loop=self.loop),
asyncio.Semaphore(loop=self.loop),
asyncio.BoundedSemaphore(loop=self.loop),
]
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),
]
async def test(lock):
await asyncio.sleep(0.01)
......
This diff is collapsed.
......@@ -1231,15 +1231,16 @@ class BaseTaskTests:
for f in asyncio.as_completed([b, c, a], loop=loop):
values.append(await f)
return values
res = loop.run_until_complete(self.new_task(loop, foo()))
with self.assertWarns(DeprecationWarning):
res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.15, loop.time())
self.assertTrue('a' in res[:2])
self.assertTrue('b' in res[:2])
self.assertEqual(res[2], 'c')
# Doing it again should take no time and exercise a different path.
res = loop.run_until_complete(self.new_task(loop, foo()))
with self.assertWarns(DeprecationWarning):
res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.15, loop.time())
def test_as_completed_with_timeout(self):
......@@ -1267,7 +1268,8 @@ class BaseTaskTests:
values.append((2, exc))
return values
res = loop.run_until_complete(self.new_task(loop, foo()))
with self.assertWarns(DeprecationWarning):
res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertEqual(len(res), 2, res)
self.assertEqual(res[0], (1, 'a'))
self.assertEqual(res[1][0], 2)
......@@ -1294,7 +1296,8 @@ class BaseTaskTests:
v = await f
self.assertEqual(v, 'a')
loop.run_until_complete(self.new_task(loop, foo()))
with self.assertWarns(DeprecationWarning):
loop.run_until_complete(self.new_task(loop, foo()))
def test_as_completed_reverse_wait(self):
......@@ -1308,7 +1311,9 @@ class BaseTaskTests:
a = asyncio.sleep(0.05, 'a')
b = asyncio.sleep(0.10, 'b')
fs = {a, b}
futs = list(asyncio.as_completed(fs, loop=loop))
with self.assertWarns(DeprecationWarning):
futs = list(asyncio.as_completed(fs, loop=loop))
self.assertEqual(len(futs), 2)
x = loop.run_until_complete(futs[1])
......@@ -1333,7 +1338,8 @@ class BaseTaskTests:
a = asyncio.sleep(0.05, 'a')
b = asyncio.sleep(0.05, 'b')
fs = {a, b}
futs = list(asyncio.as_completed(fs, loop=loop))
with self.assertWarns(DeprecationWarning):
futs = list(asyncio.as_completed(fs, loop=loop))
self.assertEqual(len(futs), 2)
waiter = asyncio.wait(futs)
done, pending = loop.run_until_complete(waiter)
......@@ -1356,8 +1362,9 @@ class BaseTaskTests:
result.append((yield from f))
return result
fut = self.new_task(self.loop, runner())
self.loop.run_until_complete(fut)
with self.assertWarns(DeprecationWarning):
fut = self.new_task(self.loop, runner())
self.loop.run_until_complete(fut)
result = fut.result()
self.assertEqual(set(result), {'ham', 'spam'})
self.assertEqual(len(result), 2)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment