Skip to content

Commit c13d4f8

Browse files
committed
Stop passing loop= parameter (deprecated in asyncio 3.8)
1 parent 33e6de3 commit c13d4f8

16 files changed

+115
-127
lines changed

asyncpg/_testbase/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def wrapper(self, *args, __meth__=meth, **kwargs):
8080
coro = __meth__(self, *args, **kwargs)
8181
timeout = getattr(__meth__, '__timeout__', mcls.TEST_TIMEOUT)
8282
if timeout:
83-
coro = asyncio.wait_for(coro, timeout, loop=self.loop)
83+
coro = asyncio.wait_for(coro, timeout)
8484
try:
8585
self.loop.run_until_complete(coro)
8686
except asyncio.TimeoutError:

asyncpg/_testbase/fuzzer.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ def __init__(self, *, listening_addr: str='127.0.0.1',
3636
self.listen_task = None
3737

3838
async def _wait(self, work):
39-
work_task = asyncio.ensure_future(work, loop=self.loop)
40-
stop_event_task = asyncio.ensure_future(self.stop_event.wait(),
41-
loop=self.loop)
39+
work_task = asyncio.ensure_future(work)
40+
stop_event_task = asyncio.ensure_future(self.stop_event.wait())
4241

4342
try:
4443
await asyncio.wait(
4544
[work_task, stop_event_task],
46-
return_when=asyncio.FIRST_COMPLETED,
47-
loop=self.loop)
45+
return_when=asyncio.FIRST_COMPLETED)
4846

4947
if self.stop_event.is_set():
5048
raise StopServer()
@@ -58,7 +56,8 @@ async def _wait(self, work):
5856

5957
def start(self):
6058
started = threading.Event()
61-
self.thread = threading.Thread(target=self._start, args=(started,))
59+
self.thread = threading.Thread(
60+
target=self._start_thread, args=(started,))
6261
self.thread.start()
6362
if not started.wait(timeout=2):
6463
raise RuntimeError('fuzzer proxy failed to start')
@@ -70,13 +69,14 @@ def stop(self):
7069
def _stop(self):
7170
self.stop_event.set()
7271

73-
def _start(self, started_event):
72+
def _start_thread(self, started_event):
7473
self.loop = asyncio.new_event_loop()
74+
asyncio.set_event_loop(self.loop)
7575

76-
self.connectivity = asyncio.Event(loop=self.loop)
76+
self.connectivity = asyncio.Event()
7777
self.connectivity.set()
78-
self.connectivity_loss = asyncio.Event(loop=self.loop)
79-
self.stop_event = asyncio.Event(loop=self.loop)
78+
self.connectivity_loss = asyncio.Event()
79+
self.stop_event = asyncio.Event()
8080

8181
if self.listening_port is None:
8282
self.listening_port = cluster.find_available_port()
@@ -92,15 +92,15 @@ def _start(self, started_event):
9292
self.loop.close()
9393

9494
async def _main(self, started_event):
95-
self.listen_task = asyncio.ensure_future(self.listen(), loop=self.loop)
95+
self.listen_task = asyncio.ensure_future(self.listen())
9696
# Notify the main thread that we are ready to go.
9797
started_event.set()
9898
try:
9999
await self.listen_task
100100
finally:
101101
for c in list(self.connections):
102102
c.close()
103-
await asyncio.sleep(0.01, loop=self.loop)
103+
await asyncio.sleep(0.01)
104104
if hasattr(self.loop, 'remove_reader'):
105105
self.loop.remove_reader(self.sock.fileno())
106106
self.sock.close()
@@ -176,15 +176,15 @@ def close(self):
176176

177177
async def handle(self):
178178
self.proxy_to_backend_task = asyncio.ensure_future(
179-
self.proxy_to_backend(), loop=self.loop)
179+
self.proxy_to_backend())
180180

181181
self.proxy_from_backend_task = asyncio.ensure_future(
182-
self.proxy_from_backend(), loop=self.loop)
182+
self.proxy_from_backend())
183183

184184
try:
185185
await asyncio.wait(
186186
[self.proxy_to_backend_task, self.proxy_from_backend_task],
187-
loop=self.loop, return_when=asyncio.FIRST_COMPLETED)
187+
return_when=asyncio.FIRST_COMPLETED)
188188

189189
finally:
190190
# Asyncio fails to properly remove the readers and writers
@@ -201,17 +201,14 @@ async def handle(self):
201201

202202
async def _read(self, sock, n):
203203
read_task = asyncio.ensure_future(
204-
self.loop.sock_recv(sock, n),
205-
loop=self.loop)
204+
self.loop.sock_recv(sock, n))
206205
conn_event_task = asyncio.ensure_future(
207-
self.connectivity_loss.wait(),
208-
loop=self.loop)
206+
self.connectivity_loss.wait())
209207

210208
try:
211209
await asyncio.wait(
212210
[read_task, conn_event_task],
213-
return_when=asyncio.FIRST_COMPLETED,
214-
loop=self.loop)
211+
return_when=asyncio.FIRST_COMPLETED)
215212

216213
if self.connectivity_loss.is_set():
217214
return None
@@ -225,15 +222,14 @@ async def _read(self, sock, n):
225222

226223
async def _write(self, sock, data):
227224
write_task = asyncio.ensure_future(
228-
self.loop.sock_sendall(sock, data), loop=self.loop)
225+
self.loop.sock_sendall(sock, data))
229226
conn_event_task = asyncio.ensure_future(
230-
self.connectivity_loss.wait(), loop=self.loop)
227+
self.connectivity_loss.wait())
231228

232229
try:
233230
await asyncio.wait(
234231
[write_task, conn_event_task],
235-
return_when=asyncio.FIRST_COMPLETED,
236-
loop=self.loop)
232+
return_when=asyncio.FIRST_COMPLETED)
237233

238234
if self.connectivity_loss.is_set():
239235
return None

asyncpg/connect_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ async def _connect_addr(*, addr, loop, timeout, params, config,
531531
before = time.monotonic()
532532
try:
533533
tr, pr = await asyncio.wait_for(
534-
connector, timeout=timeout, loop=loop)
534+
connector, timeout=timeout)
535535
except asyncio.CancelledError:
536536
connector.add_done_callback(_close_leaked_connection)
537537
raise
@@ -540,7 +540,7 @@ async def _connect_addr(*, addr, loop, timeout, params, config,
540540
try:
541541
if timeout <= 0:
542542
raise asyncio.TimeoutError
543-
await asyncio.wait_for(connected, loop=loop, timeout=timeout)
543+
await asyncio.wait_for(connected, timeout=timeout)
544544
except (Exception, asyncio.CancelledError):
545545
tr.close()
546546
raise
@@ -581,7 +581,7 @@ async def _negotiate_ssl_connection(host, port, conn_factory, *, loop, ssl,
581581
# accept SSLRequests. If the SSLRequest is accepted but either the SSL
582582
# negotiation fails or the PostgreSQL user isn't permitted to use SSL,
583583
# there's nothing that would attempt to reconnect with a non-SSL socket.
584-
reader, writer = await asyncio.open_connection(host, port, loop=loop)
584+
reader, writer = await asyncio.open_connection(host, port)
585585

586586
tr = writer.transport
587587
try:
@@ -632,18 +632,18 @@ async def _create_ssl_connection(protocol_factory, host, port, *,
632632

633633
async def _open_connection(*, loop, addr, params: _ConnectionParameters):
634634
if isinstance(addr, str):
635-
r, w = await asyncio.open_unix_connection(addr, loop=loop)
635+
r, w = await asyncio.open_unix_connection(addr)
636636
else:
637637
if params.ssl:
638638
r, w = await _negotiate_ssl_connection(
639639
*addr,
640-
functools.partial(asyncio.open_connection, loop=loop),
640+
asyncio.open_connection,
641641
loop=loop,
642642
ssl=params.ssl,
643643
server_hostname=addr[0],
644644
ssl_is_advisory=params.ssl_is_advisory)
645645
else:
646-
r, w = await asyncio.open_connection(*addr, loop=loop)
646+
r, w = await asyncio.open_connection(*addr)
647647
_set_nodelay(_get_socket(w.transport))
648648

649649
return r, w

asyncpg/pool.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ async def release(self, timeout):
199199
started = time.monotonic()
200200
await asyncio.wait_for(
201201
self._con._protocol._wait_for_cancellation(),
202-
budget, loop=self._pool._loop)
202+
budget)
203203
if budget is not None:
204204
budget -= time.monotonic() - started
205205

@@ -362,7 +362,7 @@ def __init__(self, *connect_args,
362362
self._holders = []
363363
self._initialized = False
364364
self._initializing = False
365-
self._queue = asyncio.LifoQueue(maxsize=self._maxsize, loop=self._loop)
365+
self._queue = asyncio.LifoQueue(maxsize=self._maxsize)
366366

367367
self._working_addr = None
368368
self._working_config = None
@@ -424,7 +424,7 @@ async def _initialize(self):
424424
break
425425
connect_tasks.append(ch.connect())
426426

427-
await asyncio.gather(*connect_tasks, loop=self._loop)
427+
await asyncio.gather(*connect_tasks)
428428

429429
def set_connect_args(self, dsn=None, **connect_kwargs):
430430
r"""Set the new connection arguments for this pool.
@@ -604,7 +604,7 @@ async def _acquire_impl():
604604
return await _acquire_impl()
605605
else:
606606
return await asyncio.wait_for(
607-
_acquire_impl(), timeout=timeout, loop=self._loop)
607+
_acquire_impl(), timeout=timeout)
608608

609609
async def release(self, connection, *, timeout=None):
610610
"""Release a database connection back to the pool.
@@ -642,7 +642,7 @@ async def release(self, connection, *, timeout=None):
642642
# Use asyncio.shield() to guarantee that task cancellation
643643
# does not prevent the connection from being returned to the
644644
# pool properly.
645-
return await asyncio.shield(ch.release(timeout), loop=self._loop)
645+
return await asyncio.shield(ch.release(timeout))
646646

647647
async def close(self):
648648
"""Attempt to gracefully close all connections in the pool.
@@ -673,11 +673,11 @@ async def close(self):
673673

674674
release_coros = [
675675
ch.wait_until_released() for ch in self._holders]
676-
await asyncio.gather(*release_coros, loop=self._loop)
676+
await asyncio.gather(*release_coros)
677677

678678
close_coros = [
679679
ch.close() for ch in self._holders]
680-
await asyncio.gather(*close_coros, loop=self._loop)
680+
await asyncio.gather(*close_coros)
681681

682682
except (Exception, asyncio.CancelledError):
683683
self.terminate()

asyncpg/protocol/protocol.pyx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ cdef class BaseProtocol(CoreProtocol):
9393

9494
self.closing = False
9595
self.is_reading = True
96-
self.writing_allowed = asyncio.Event(loop=self.loop)
96+
self.writing_allowed = asyncio.Event()
9797
self.writing_allowed.set()
9898

9999
self.timeout_handle = None
@@ -348,8 +348,7 @@ cdef class BaseProtocol(CoreProtocol):
348348
with timer:
349349
await asyncio.wait_for(
350350
sink(buffer),
351-
timeout=timer.get_remaining_budget(),
352-
loop=self.loop)
351+
timeout=timer.get_remaining_budget())
353352
except (Exception, asyncio.CancelledError) as ex:
354353
# Abort the COPY operation on any error in
355354
# output sink.
@@ -456,8 +455,7 @@ cdef class BaseProtocol(CoreProtocol):
456455
with timer:
457456
chunk = await asyncio.wait_for(
458457
iterator.__anext__(),
459-
timeout=timer.get_remaining_budget(),
460-
loop=self.loop)
458+
timeout=timer.get_remaining_budget())
461459
self._write_copy_data_msg(chunk)
462460
except builtins.StopAsyncIteration:
463461
pass

tests/test_adversity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ def kill_connectivity():
6262
workers = [worker(pool) for _ in range(concurrency)]
6363
self.loop.call_later(1, kill_connectivity)
6464
await asyncio.gather(
65-
*workers, loop=self.loop, return_exceptions=True)
65+
*workers, return_exceptions=True)
6666
finally:
6767
pool.terminate()

tests/test_cancellation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def test8():
5555
with self.subTest(testfunc=test), self.assertRunUnder(1):
5656
st = await self.con.prepare('SELECT pg_sleep(20)')
5757
task = self.loop.create_task(st.fetch())
58-
await asyncio.sleep(0.05, loop=self.loop)
58+
await asyncio.sleep(0.05)
5959
task.cancel()
6060

6161
with self.assertRaises(asyncio.CancelledError):
@@ -67,7 +67,7 @@ async def test8():
6767
async def test_cancellation_02(self):
6868
st = await self.con.prepare('SELECT 1')
6969
task = self.loop.create_task(st.fetch())
70-
await asyncio.sleep(0.05, loop=self.loop)
70+
await asyncio.sleep(0.05)
7171
task.cancel()
7272
self.assertEqual(await task, [(1,)])
7373

@@ -76,7 +76,7 @@ async def test_cancellation_03(self):
7676
async with self.con.transaction():
7777
task = self.loop.create_task(
7878
self.con.fetch('SELECT pg_sleep(20)'))
79-
await asyncio.sleep(0.05, loop=self.loop)
79+
await asyncio.sleep(0.05)
8080
task.cancel()
8181

8282
with self.assertRaises(asyncio.CancelledError):
@@ -90,7 +90,7 @@ async def test_cancellation_03(self):
9090

9191
async def test_cancellation_04(self):
9292
await self.con.fetchval('SELECT pg_sleep(0)')
93-
waiter = asyncio.Future(loop=self.loop)
93+
waiter = asyncio.Future()
9494
self.con._cancel_current_command(waiter)
9595
await waiter
9696
self.assertEqual(await self.con.fetchval('SELECT 42'), 42)

tests/test_connect.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -934,14 +934,14 @@ def test_connect_pgpass_inaccessible_directory(self):
934934
async def test_connect_args_validation(self):
935935
for val in {-1, 'a', True, False, 0}:
936936
with self.assertRaisesRegex(ValueError, 'greater than 0'):
937-
await asyncpg.connect(command_timeout=val, loop=self.loop)
937+
await asyncpg.connect(command_timeout=val)
938938

939939
for arg in {'max_cacheable_statement_size',
940940
'max_cached_statement_lifetime',
941941
'statement_cache_size'}:
942942
for val in {None, -1, True, False}:
943943
with self.assertRaisesRegex(ValueError, 'greater or equal'):
944-
await asyncpg.connect(**{arg: val}, loop=self.loop)
944+
await asyncpg.connect(**{arg: val})
945945

946946

947947
class TestConnection(tb.ConnectedTestCase):
@@ -1040,8 +1040,7 @@ async def test_connection_implicit_host(self):
10401040
con = await asyncpg.connect(
10411041
port=conn_spec.get('port'),
10421042
database=conn_spec.get('database'),
1043-
user=conn_spec.get('user'),
1044-
loop=self.loop)
1043+
user=conn_spec.get('user'))
10451044
await con.close()
10461045

10471046

@@ -1207,7 +1206,7 @@ async def worker():
12071206
self.assertEqual(await con.fetchval('SELECT 43'), 43)
12081207

12091208
tasks = [worker() for _ in range(100)]
1210-
await asyncio.gather(*tasks, loop=self.loop)
1209+
await asyncio.gather(*tasks)
12111210
await pool.close()
12121211

12131212

0 commit comments

Comments
 (0)