mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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:
parent
9669931e5e
commit
537877d85d
7 changed files with 419 additions and 265 deletions
|
@ -35,7 +35,8 @@ class QueueBasicTests(_QueueTestBase):
|
|||
|
||||
loop = self.new_test_loop(gen)
|
||||
|
||||
q = asyncio.Queue(loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=loop)
|
||||
self.assertTrue(fn(q).startswith('<Queue'), fn(q))
|
||||
id_is_present = hex(id(q)) in fn(q)
|
||||
self.assertEqual(expect_id, id_is_present)
|
||||
|
@ -50,7 +51,8 @@ class QueueBasicTests(_QueueTestBase):
|
|||
# resume q.get coroutine to finish generator
|
||||
q.put_nowait(0)
|
||||
|
||||
loop.run_until_complete(add_getter())
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
loop.run_until_complete(add_getter())
|
||||
|
||||
async def add_putter():
|
||||
q = asyncio.Queue(maxsize=1, loop=loop)
|
||||
|
@ -63,23 +65,26 @@ class QueueBasicTests(_QueueTestBase):
|
|||
# resume q.put coroutine to finish generator
|
||||
q.get_nowait()
|
||||
|
||||
loop.run_until_complete(add_putter())
|
||||
|
||||
q = asyncio.Queue(loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
loop.run_until_complete(add_putter())
|
||||
q = asyncio.Queue(loop=loop)
|
||||
q.put_nowait(1)
|
||||
self.assertTrue('_queue=[1]' in fn(q))
|
||||
|
||||
def test_ctor_loop(self):
|
||||
loop = mock.Mock()
|
||||
q = asyncio.Queue(loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=loop)
|
||||
self.assertIs(q._loop, loop)
|
||||
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
self.assertIs(q._loop, self.loop)
|
||||
|
||||
def test_ctor_noloop(self):
|
||||
asyncio.set_event_loop(self.loop)
|
||||
q = asyncio.Queue()
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue()
|
||||
self.assertIs(q._loop, self.loop)
|
||||
|
||||
def test_repr(self):
|
||||
|
@ -89,7 +94,8 @@ class QueueBasicTests(_QueueTestBase):
|
|||
self._test_repr_or_str(str, False)
|
||||
|
||||
def test_empty(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
self.assertTrue(q.empty())
|
||||
q.put_nowait(1)
|
||||
self.assertFalse(q.empty())
|
||||
|
@ -97,15 +103,18 @@ class QueueBasicTests(_QueueTestBase):
|
|||
self.assertTrue(q.empty())
|
||||
|
||||
def test_full(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
self.assertFalse(q.full())
|
||||
|
||||
q = asyncio.Queue(maxsize=1, loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(maxsize=1, loop=self.loop)
|
||||
q.put_nowait(1)
|
||||
self.assertTrue(q.full())
|
||||
|
||||
def test_order(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
for i in [1, 3, 2]:
|
||||
q.put_nowait(i)
|
||||
|
||||
|
@ -123,7 +132,8 @@ class QueueBasicTests(_QueueTestBase):
|
|||
|
||||
loop = self.new_test_loop(gen)
|
||||
|
||||
q = asyncio.Queue(maxsize=2, loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(maxsize=2, loop=loop)
|
||||
self.assertEqual(2, q.maxsize)
|
||||
have_been_put = []
|
||||
|
||||
|
@ -157,7 +167,8 @@ class QueueBasicTests(_QueueTestBase):
|
|||
class QueueGetTests(_QueueTestBase):
|
||||
|
||||
def test_blocking_get(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
q.put_nowait(1)
|
||||
|
||||
async def queue_get():
|
||||
|
@ -167,7 +178,8 @@ class QueueGetTests(_QueueTestBase):
|
|||
self.assertEqual(1, res)
|
||||
|
||||
def test_get_with_putters(self):
|
||||
q = asyncio.Queue(1, loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(1, loop=self.loop)
|
||||
q.put_nowait(1)
|
||||
|
||||
waiter = asyncio.Future(loop=self.loop)
|
||||
|
@ -187,8 +199,9 @@ class QueueGetTests(_QueueTestBase):
|
|||
|
||||
loop = self.new_test_loop(gen)
|
||||
|
||||
q = asyncio.Queue(loop=loop)
|
||||
started = asyncio.Event(loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=loop)
|
||||
started = asyncio.Event(loop=loop)
|
||||
finished = False
|
||||
|
||||
async def queue_get():
|
||||
|
@ -212,12 +225,14 @@ class QueueGetTests(_QueueTestBase):
|
|||
self.assertAlmostEqual(0.01, loop.time())
|
||||
|
||||
def test_nonblocking_get(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
q.put_nowait(1)
|
||||
self.assertEqual(1, q.get_nowait())
|
||||
|
||||
def test_nonblocking_get_exception(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
self.assertRaises(asyncio.QueueEmpty, q.get_nowait)
|
||||
|
||||
def test_get_cancelled(self):
|
||||
|
@ -231,7 +246,8 @@ class QueueGetTests(_QueueTestBase):
|
|||
|
||||
loop = self.new_test_loop(gen)
|
||||
|
||||
q = asyncio.Queue(loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=loop)
|
||||
|
||||
async def queue_get():
|
||||
return await asyncio.wait_for(q.get(), 0.051)
|
||||
|
@ -246,7 +262,8 @@ class QueueGetTests(_QueueTestBase):
|
|||
self.assertAlmostEqual(0.06, loop.time())
|
||||
|
||||
def test_get_cancelled_race(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
|
||||
t1 = asyncio.Task(q.get(), loop=self.loop)
|
||||
t2 = asyncio.Task(q.get(), loop=self.loop)
|
||||
|
@ -260,7 +277,8 @@ class QueueGetTests(_QueueTestBase):
|
|||
self.assertEqual(t2.result(), 'a')
|
||||
|
||||
def test_get_with_waiting_putters(self):
|
||||
q = asyncio.Queue(loop=self.loop, maxsize=1)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop, maxsize=1)
|
||||
asyncio.Task(q.put('a'), loop=self.loop)
|
||||
asyncio.Task(q.put('b'), loop=self.loop)
|
||||
test_utils.run_briefly(self.loop)
|
||||
|
@ -280,7 +298,9 @@ class QueueGetTests(_QueueTestBase):
|
|||
|
||||
queue_size = 1
|
||||
producer_num_items = 5
|
||||
q = asyncio.Queue(queue_size, loop=self.loop)
|
||||
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(queue_size, loop=self.loop)
|
||||
|
||||
self.loop.run_until_complete(
|
||||
asyncio.gather(producer(q, producer_num_items),
|
||||
|
@ -301,7 +321,8 @@ class QueueGetTests(_QueueTestBase):
|
|||
except asyncio.TimeoutError:
|
||||
pass
|
||||
|
||||
queue = asyncio.Queue(loop=self.loop, maxsize=5)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
queue = asyncio.Queue(loop=self.loop, maxsize=5)
|
||||
self.loop.run_until_complete(self.loop.create_task(consumer(queue)))
|
||||
self.assertEqual(len(queue._getters), 0)
|
||||
|
||||
|
@ -309,7 +330,8 @@ class QueueGetTests(_QueueTestBase):
|
|||
class QueuePutTests(_QueueTestBase):
|
||||
|
||||
def test_blocking_put(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
|
||||
async def queue_put():
|
||||
# No maxsize, won't block.
|
||||
|
@ -326,8 +348,9 @@ class QueuePutTests(_QueueTestBase):
|
|||
|
||||
loop = self.new_test_loop(gen)
|
||||
|
||||
q = asyncio.Queue(maxsize=1, loop=loop)
|
||||
started = asyncio.Event(loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(maxsize=1, loop=loop)
|
||||
started = asyncio.Event(loop=loop)
|
||||
finished = False
|
||||
|
||||
async def queue_put():
|
||||
|
@ -349,7 +372,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
self.assertAlmostEqual(0.01, loop.time())
|
||||
|
||||
def test_nonblocking_put(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
q.put_nowait(1)
|
||||
self.assertEqual(1, q.get_nowait())
|
||||
|
||||
|
@ -360,7 +384,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
|
||||
loop = self.new_test_loop(gen)
|
||||
|
||||
q = asyncio.Queue(loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=loop)
|
||||
|
||||
reader = loop.create_task(q.get())
|
||||
|
||||
|
@ -389,7 +414,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
loop = self.new_test_loop(gen)
|
||||
loop.set_debug(True)
|
||||
|
||||
q = asyncio.Queue(loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=loop)
|
||||
|
||||
reader1 = loop.create_task(q.get())
|
||||
reader2 = loop.create_task(q.get())
|
||||
|
@ -418,7 +444,9 @@ class QueuePutTests(_QueueTestBase):
|
|||
yield 0.1
|
||||
|
||||
loop = self.new_test_loop(gen)
|
||||
q = asyncio.Queue(1, loop=loop)
|
||||
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(1, loop=loop)
|
||||
|
||||
q.put_nowait(1)
|
||||
|
||||
|
@ -442,18 +470,21 @@ class QueuePutTests(_QueueTestBase):
|
|||
self.assertEqual(q.qsize(), 0)
|
||||
|
||||
def test_nonblocking_put_exception(self):
|
||||
q = asyncio.Queue(maxsize=1, loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(maxsize=1, loop=self.loop)
|
||||
q.put_nowait(1)
|
||||
self.assertRaises(asyncio.QueueFull, q.put_nowait, 2)
|
||||
|
||||
def test_float_maxsize(self):
|
||||
q = asyncio.Queue(maxsize=1.3, loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(maxsize=1.3, loop=self.loop)
|
||||
q.put_nowait(1)
|
||||
q.put_nowait(2)
|
||||
self.assertTrue(q.full())
|
||||
self.assertRaises(asyncio.QueueFull, q.put_nowait, 3)
|
||||
|
||||
q = asyncio.Queue(maxsize=1.3, loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(maxsize=1.3, loop=self.loop)
|
||||
|
||||
async def queue_put():
|
||||
await q.put(1)
|
||||
|
@ -462,7 +493,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
self.loop.run_until_complete(queue_put())
|
||||
|
||||
def test_put_cancelled(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
|
||||
async def queue_put():
|
||||
await q.put(1)
|
||||
|
@ -477,7 +509,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
self.assertTrue(t.result())
|
||||
|
||||
def test_put_cancelled_race(self):
|
||||
q = asyncio.Queue(loop=self.loop, maxsize=1)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop, maxsize=1)
|
||||
|
||||
put_a = asyncio.Task(q.put('a'), loop=self.loop)
|
||||
put_b = asyncio.Task(q.put('b'), loop=self.loop)
|
||||
|
@ -497,7 +530,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
self.loop.run_until_complete(put_b)
|
||||
|
||||
def test_put_with_waiting_getters(self):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.Queue(loop=self.loop)
|
||||
t = asyncio.Task(q.get(), loop=self.loop)
|
||||
test_utils.run_briefly(self.loop)
|
||||
self.loop.run_until_complete(q.put('a'))
|
||||
|
@ -506,7 +540,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
def test_why_are_putters_waiting(self):
|
||||
# From issue #265.
|
||||
|
||||
queue = asyncio.Queue(2, loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
queue = asyncio.Queue(2, loop=self.loop)
|
||||
|
||||
async def putter(item):
|
||||
await queue.put(item)
|
||||
|
@ -532,7 +567,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
loop = self.new_test_loop(a_generator)
|
||||
|
||||
# Full queue.
|
||||
queue = asyncio.Queue(loop=loop, maxsize=1)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
queue = asyncio.Queue(loop=loop, maxsize=1)
|
||||
queue.put_nowait(1)
|
||||
|
||||
# Task waiting for space to put an item in the queue.
|
||||
|
@ -555,7 +591,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
loop = self.new_test_loop(gen)
|
||||
|
||||
# Full Queue.
|
||||
queue = asyncio.Queue(1, loop=loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
queue = asyncio.Queue(1, loop=loop)
|
||||
queue.put_nowait(1)
|
||||
|
||||
# Task waiting for space to put a item in the queue.
|
||||
|
@ -578,7 +615,8 @@ class QueuePutTests(_QueueTestBase):
|
|||
class LifoQueueTests(_QueueTestBase):
|
||||
|
||||
def test_order(self):
|
||||
q = asyncio.LifoQueue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.LifoQueue(loop=self.loop)
|
||||
for i in [1, 3, 2]:
|
||||
q.put_nowait(i)
|
||||
|
||||
|
@ -589,7 +627,8 @@ class LifoQueueTests(_QueueTestBase):
|
|||
class PriorityQueueTests(_QueueTestBase):
|
||||
|
||||
def test_order(self):
|
||||
q = asyncio.PriorityQueue(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = asyncio.PriorityQueue(loop=self.loop)
|
||||
for i in [1, 3, 2]:
|
||||
q.put_nowait(i)
|
||||
|
||||
|
@ -602,11 +641,13 @@ class _QueueJoinTestMixin:
|
|||
q_class = None
|
||||
|
||||
def test_task_done_underflow(self):
|
||||
q = self.q_class(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = self.q_class(loop=self.loop)
|
||||
self.assertRaises(ValueError, q.task_done)
|
||||
|
||||
def test_task_done(self):
|
||||
q = self.q_class(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = self.q_class(loop=self.loop)
|
||||
for i in range(100):
|
||||
q.put_nowait(i)
|
||||
|
||||
|
@ -641,7 +682,8 @@ class _QueueJoinTestMixin:
|
|||
self.loop.run_until_complete(asyncio.wait(tasks))
|
||||
|
||||
def test_join_empty_queue(self):
|
||||
q = self.q_class(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = self.q_class(loop=self.loop)
|
||||
|
||||
# Test that a queue join()s successfully, and before anything else
|
||||
# (done twice for insurance).
|
||||
|
@ -653,7 +695,8 @@ class _QueueJoinTestMixin:
|
|||
self.loop.run_until_complete(join())
|
||||
|
||||
def test_format(self):
|
||||
q = self.q_class(loop=self.loop)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
q = self.q_class(loop=self.loop)
|
||||
self.assertEqual(q._format(), 'maxsize=0')
|
||||
|
||||
q._unfinished_tasks = 2
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue