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 ...@@ -59,6 +59,9 @@ Lock
finally: finally:
lock.release() lock.release()
.. deprecated-removed:: 3.8 3.10
The *loop* parameter.
.. coroutinemethod:: acquire() .. coroutinemethod:: acquire()
Acquire the lock. Acquire the lock.
...@@ -101,6 +104,10 @@ Event ...@@ -101,6 +104,10 @@ Event
:meth:`clear` method. The :meth:`wait` method blocks until the :meth:`clear` method. The :meth:`wait` method blocks until the
flag is set to *true*. The flag is set to *false* initially. 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: .. _asyncio_example_sync_event:
Example:: Example::
...@@ -173,6 +180,10 @@ Condition ...@@ -173,6 +180,10 @@ Condition
``None``. In the latter case a new Lock object is created ``None``. In the latter case a new Lock object is created
automatically. automatically.
.. deprecated-removed:: 3.8 3.10
The *loop* parameter.
The preferred way to use a Condition is an :keyword:`async with` The preferred way to use a Condition is an :keyword:`async with`
statement:: statement::
...@@ -269,6 +280,10 @@ Semaphore ...@@ -269,6 +280,10 @@ Semaphore
internal counter (``1`` by default). If the given value is internal counter (``1`` by default). If the given value is
less than ``0`` a :exc:`ValueError` is raised. 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` The preferred way to use a Semaphore is an :keyword:`async with`
statement:: statement::
...@@ -322,6 +337,9 @@ BoundedSemaphore ...@@ -322,6 +337,9 @@ BoundedSemaphore
increases the internal counter above the initial *value*. increases the internal counter above the initial *value*.
.. deprecated-removed:: 3.8 3.10
The *loop* parameter.
--------- ---------
......
...@@ -160,10 +160,13 @@ class Lock(_ContextManagerMixin): ...@@ -160,10 +160,13 @@ class Lock(_ContextManagerMixin):
def __init__(self, *, loop=None): def __init__(self, *, loop=None):
self._waiters = None self._waiters = None
self._locked = False self._locked = False
if loop is not None: if loop is None:
self._loop = loop
else:
self._loop = events.get_event_loop() 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): def __repr__(self):
res = super().__repr__() res = super().__repr__()
...@@ -253,10 +256,13 @@ class Event: ...@@ -253,10 +256,13 @@ class Event:
def __init__(self, *, loop=None): def __init__(self, *, loop=None):
self._waiters = collections.deque() self._waiters = collections.deque()
self._value = False self._value = False
if loop is not None: if loop is None:
self._loop = loop
else:
self._loop = events.get_event_loop() 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): def __repr__(self):
res = super().__repr__() res = super().__repr__()
...@@ -317,10 +323,13 @@ class Condition(_ContextManagerMixin): ...@@ -317,10 +323,13 @@ class Condition(_ContextManagerMixin):
""" """
def __init__(self, lock=None, *, loop=None): def __init__(self, lock=None, *, loop=None):
if loop is not None: if loop is None:
self._loop = loop
else:
self._loop = events.get_event_loop() 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: if lock is None:
lock = Lock(loop=self._loop) lock = Lock(loop=self._loop)
...@@ -445,10 +454,13 @@ class Semaphore(_ContextManagerMixin): ...@@ -445,10 +454,13 @@ class Semaphore(_ContextManagerMixin):
raise ValueError("Semaphore initial value must be >= 0") raise ValueError("Semaphore initial value must be >= 0")
self._value = value self._value = value
self._waiters = collections.deque() self._waiters = collections.deque()
if loop is not None: if loop is None:
self._loop = loop
else:
self._loop = events.get_event_loop() 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): def __repr__(self):
res = super().__repr__() res = super().__repr__()
...@@ -508,6 +520,11 @@ class BoundedSemaphore(Semaphore): ...@@ -508,6 +520,11 @@ class BoundedSemaphore(Semaphore):
""" """
def __init__(self, value=1, *, loop=None): 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 self._bound_value = value
super().__init__(value, loop=loop) super().__init__(value, loop=loop)
......
...@@ -1719,6 +1719,7 @@ class SubprocessTestsMixin: ...@@ -1719,6 +1719,7 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_exec( connect = self.loop.subprocess_exec(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
sys.executable, prog) sys.executable, prog)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.loop.run_until_complete(proto.connected) self.loop.run_until_complete(proto.connected)
...@@ -1739,6 +1740,8 @@ class SubprocessTestsMixin: ...@@ -1739,6 +1740,8 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_exec( connect = self.loop.subprocess_exec(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
sys.executable, prog) sys.executable, prog)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.loop.run_until_complete(proto.connected) self.loop.run_until_complete(proto.connected)
...@@ -1760,6 +1763,7 @@ class SubprocessTestsMixin: ...@@ -1760,6 +1763,7 @@ class SubprocessTestsMixin:
self.check_killed(proto.returncode) self.check_killed(proto.returncode)
def test_subprocess_shell(self): def test_subprocess_shell(self):
with self.assertWarns(DeprecationWarning):
connect = self.loop.subprocess_shell( connect = self.loop.subprocess_shell(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
'echo Python') 'echo Python')
...@@ -1779,6 +1783,8 @@ class SubprocessTestsMixin: ...@@ -1779,6 +1783,8 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_shell( connect = self.loop.subprocess_shell(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
'exit 7', stdin=None, stdout=None, stderr=None) 'exit 7', stdin=None, stdout=None, stderr=None)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.loop.run_until_complete(proto.completed) self.loop.run_until_complete(proto.completed)
...@@ -1789,6 +1795,7 @@ class SubprocessTestsMixin: ...@@ -1789,6 +1795,7 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_shell( connect = self.loop.subprocess_shell(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
'exit 7', stdin=None, stdout=None, stderr=None) 'exit 7', stdin=None, stdout=None, stderr=None)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.assertIsNone(transp.get_pipe_transport(0)) self.assertIsNone(transp.get_pipe_transport(0))
...@@ -1804,6 +1811,8 @@ class SubprocessTestsMixin: ...@@ -1804,6 +1811,8 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_exec( connect = self.loop.subprocess_exec(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
sys.executable, prog) sys.executable, prog)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.loop.run_until_complete(proto.connected) self.loop.run_until_complete(proto.connected)
...@@ -1819,6 +1828,8 @@ class SubprocessTestsMixin: ...@@ -1819,6 +1828,8 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_exec( connect = self.loop.subprocess_exec(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
sys.executable, prog) sys.executable, prog)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.loop.run_until_complete(proto.connected) self.loop.run_until_complete(proto.connected)
...@@ -1840,6 +1851,8 @@ class SubprocessTestsMixin: ...@@ -1840,6 +1851,8 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_exec( connect = self.loop.subprocess_exec(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
sys.executable, prog) sys.executable, prog)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.loop.run_until_complete(proto.connected) self.loop.run_until_complete(proto.connected)
...@@ -1857,6 +1870,8 @@ class SubprocessTestsMixin: ...@@ -1857,6 +1870,8 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_exec( connect = self.loop.subprocess_exec(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
sys.executable, prog) sys.executable, prog)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.loop.run_until_complete(proto.connected) self.loop.run_until_complete(proto.connected)
...@@ -1877,6 +1892,8 @@ class SubprocessTestsMixin: ...@@ -1877,6 +1892,8 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_exec( connect = self.loop.subprocess_exec(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
sys.executable, prog, stderr=subprocess.STDOUT) sys.executable, prog, stderr=subprocess.STDOUT)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.loop.run_until_complete(proto.connected) self.loop.run_until_complete(proto.connected)
...@@ -1900,6 +1917,7 @@ class SubprocessTestsMixin: ...@@ -1900,6 +1917,7 @@ class SubprocessTestsMixin:
connect = self.loop.subprocess_exec( connect = self.loop.subprocess_exec(
functools.partial(MySubprocessProtocol, self.loop), functools.partial(MySubprocessProtocol, self.loop),
sys.executable, prog) sys.executable, prog)
with self.assertWarns(DeprecationWarning):
transp, proto = self.loop.run_until_complete(connect) transp, proto = self.loop.run_until_complete(connect)
self.assertIsInstance(proto, MySubprocessProtocol) self.assertIsInstance(proto, MySubprocessProtocol)
self.loop.run_until_complete(proto.connected) self.loop.run_until_complete(proto.connected)
...@@ -1939,7 +1957,6 @@ class SubprocessTestsMixin: ...@@ -1939,7 +1957,6 @@ class SubprocessTestsMixin:
self.assertEqual(7, proto.returncode) self.assertEqual(7, proto.returncode)
def test_subprocess_exec_invalid_args(self): def test_subprocess_exec_invalid_args(self):
async def connect(**kwds): async def connect(**kwds):
await self.loop.subprocess_exec( await self.loop.subprocess_exec(
asyncio.SubprocessProtocol, asyncio.SubprocessProtocol,
......
This diff is collapsed.
...@@ -43,6 +43,7 @@ class BaseTest(test_utils.TestCase): ...@@ -43,6 +43,7 @@ class BaseTest(test_utils.TestCase):
class LockTests(BaseTest): class LockTests(BaseTest):
def test_context_manager_async_with(self): def test_context_manager_async_with(self):
with self.assertWarns(DeprecationWarning):
primitives = [ primitives = [
asyncio.Lock(loop=self.loop), asyncio.Lock(loop=self.loop),
asyncio.Condition(loop=self.loop), asyncio.Condition(loop=self.loop),
...@@ -65,6 +66,7 @@ class LockTests(BaseTest): ...@@ -65,6 +66,7 @@ class LockTests(BaseTest):
self.assertFalse(primitive.locked()) self.assertFalse(primitive.locked())
def test_context_manager_with_await(self): def test_context_manager_with_await(self):
with self.assertWarns(DeprecationWarning):
primitives = [ primitives = [
asyncio.Lock(loop=self.loop), asyncio.Lock(loop=self.loop),
asyncio.Condition(loop=self.loop), asyncio.Condition(loop=self.loop),
......
This diff is collapsed.
...@@ -1231,7 +1231,7 @@ class BaseTaskTests: ...@@ -1231,7 +1231,7 @@ class BaseTaskTests:
for f in asyncio.as_completed([b, c, a], loop=loop): for f in asyncio.as_completed([b, c, a], loop=loop):
values.append(await f) values.append(await f)
return values return values
with self.assertWarns(DeprecationWarning):
res = loop.run_until_complete(self.new_task(loop, foo())) res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.15, loop.time()) self.assertAlmostEqual(0.15, loop.time())
self.assertTrue('a' in res[:2]) self.assertTrue('a' in res[:2])
...@@ -1239,6 +1239,7 @@ class BaseTaskTests: ...@@ -1239,6 +1239,7 @@ class BaseTaskTests:
self.assertEqual(res[2], 'c') self.assertEqual(res[2], 'c')
# Doing it again should take no time and exercise a different path. # Doing it again should take no time and exercise a different path.
with self.assertWarns(DeprecationWarning):
res = loop.run_until_complete(self.new_task(loop, foo())) res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertAlmostEqual(0.15, loop.time()) self.assertAlmostEqual(0.15, loop.time())
...@@ -1267,6 +1268,7 @@ class BaseTaskTests: ...@@ -1267,6 +1268,7 @@ class BaseTaskTests:
values.append((2, exc)) values.append((2, exc))
return values return values
with self.assertWarns(DeprecationWarning):
res = loop.run_until_complete(self.new_task(loop, foo())) res = loop.run_until_complete(self.new_task(loop, foo()))
self.assertEqual(len(res), 2, res) self.assertEqual(len(res), 2, res)
self.assertEqual(res[0], (1, 'a')) self.assertEqual(res[0], (1, 'a'))
...@@ -1294,6 +1296,7 @@ class BaseTaskTests: ...@@ -1294,6 +1296,7 @@ class BaseTaskTests:
v = await f v = await f
self.assertEqual(v, 'a') self.assertEqual(v, 'a')
with self.assertWarns(DeprecationWarning):
loop.run_until_complete(self.new_task(loop, foo())) loop.run_until_complete(self.new_task(loop, foo()))
def test_as_completed_reverse_wait(self): def test_as_completed_reverse_wait(self):
...@@ -1308,6 +1311,8 @@ class BaseTaskTests: ...@@ -1308,6 +1311,8 @@ class BaseTaskTests:
a = asyncio.sleep(0.05, 'a') a = asyncio.sleep(0.05, 'a')
b = asyncio.sleep(0.10, 'b') b = asyncio.sleep(0.10, 'b')
fs = {a, b} fs = {a, b}
with self.assertWarns(DeprecationWarning):
futs = list(asyncio.as_completed(fs, loop=loop)) futs = list(asyncio.as_completed(fs, loop=loop))
self.assertEqual(len(futs), 2) self.assertEqual(len(futs), 2)
...@@ -1333,6 +1338,7 @@ class BaseTaskTests: ...@@ -1333,6 +1338,7 @@ class BaseTaskTests:
a = asyncio.sleep(0.05, 'a') a = asyncio.sleep(0.05, 'a')
b = asyncio.sleep(0.05, 'b') b = asyncio.sleep(0.05, 'b')
fs = {a, b} fs = {a, b}
with self.assertWarns(DeprecationWarning):
futs = list(asyncio.as_completed(fs, loop=loop)) futs = list(asyncio.as_completed(fs, loop=loop))
self.assertEqual(len(futs), 2) self.assertEqual(len(futs), 2)
waiter = asyncio.wait(futs) waiter = asyncio.wait(futs)
...@@ -1356,6 +1362,7 @@ class BaseTaskTests: ...@@ -1356,6 +1362,7 @@ class BaseTaskTests:
result.append((yield from f)) result.append((yield from f))
return result return result
with self.assertWarns(DeprecationWarning):
fut = self.new_task(self.loop, runner()) fut = self.new_task(self.loop, runner())
self.loop.run_until_complete(fut) self.loop.run_until_complete(fut)
result = fut.result() result = fut.result()
......
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