bpo-42392: Remove loop parameter from asyncio.streams (GH-23517)

This commit is contained in:
Yurii Karabas 2020-11-26 09:36:37 +02:00 committed by GitHub
parent 87f7ab5359
commit f533cb80cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 109 deletions

View File

@ -23,7 +23,7 @@ _DEFAULT_LIMIT = 2 ** 16 # 64 KiB
async def open_connection(host=None, port=None, *, async def open_connection(host=None, port=None, *,
loop=None, limit=_DEFAULT_LIMIT, **kwds): limit=_DEFAULT_LIMIT, **kwds):
"""A wrapper for create_connection() returning a (reader, writer) pair. """A wrapper for create_connection() returning a (reader, writer) pair.
The reader returned is a StreamReader instance; the writer is a The reader returned is a StreamReader instance; the writer is a
@ -41,12 +41,7 @@ async def open_connection(host=None, port=None, *,
StreamReaderProtocol classes, just copy the code -- there's StreamReaderProtocol classes, just copy the code -- there's
really nothing special here except some convenience.) really nothing special here except some convenience.)
""" """
if loop is None: loop = events.get_running_loop()
loop = events.get_event_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
reader = StreamReader(limit=limit, loop=loop) reader = StreamReader(limit=limit, loop=loop)
protocol = StreamReaderProtocol(reader, loop=loop) protocol = StreamReaderProtocol(reader, loop=loop)
transport, _ = await loop.create_connection( transport, _ = await loop.create_connection(
@ -56,7 +51,7 @@ async def open_connection(host=None, port=None, *,
async def start_server(client_connected_cb, host=None, port=None, *, async def start_server(client_connected_cb, host=None, port=None, *,
loop=None, limit=_DEFAULT_LIMIT, **kwds): limit=_DEFAULT_LIMIT, **kwds):
"""Start a socket server, call back for each client connected. """Start a socket server, call back for each client connected.
The first parameter, `client_connected_cb`, takes two parameters: The first parameter, `client_connected_cb`, takes two parameters:
@ -78,12 +73,7 @@ async def start_server(client_connected_cb, host=None, port=None, *,
The return value is the same as loop.create_server(), i.e. a The return value is the same as loop.create_server(), i.e. a
Server object which can be used to stop the service. Server object which can be used to stop the service.
""" """
if loop is None: loop = events.get_running_loop()
loop = events.get_event_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
def factory(): def factory():
reader = StreamReader(limit=limit, loop=loop) reader = StreamReader(limit=limit, loop=loop)
@ -98,14 +88,10 @@ if hasattr(socket, 'AF_UNIX'):
# UNIX Domain Sockets are supported on this platform # UNIX Domain Sockets are supported on this platform
async def open_unix_connection(path=None, *, async def open_unix_connection(path=None, *,
loop=None, limit=_DEFAULT_LIMIT, **kwds): limit=_DEFAULT_LIMIT, **kwds):
"""Similar to `open_connection` but works with UNIX Domain Sockets.""" """Similar to `open_connection` but works with UNIX Domain Sockets."""
if loop is None: loop = events.get_running_loop()
loop = events.get_event_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
reader = StreamReader(limit=limit, loop=loop) reader = StreamReader(limit=limit, loop=loop)
protocol = StreamReaderProtocol(reader, loop=loop) protocol = StreamReaderProtocol(reader, loop=loop)
transport, _ = await loop.create_unix_connection( transport, _ = await loop.create_unix_connection(
@ -114,14 +100,9 @@ if hasattr(socket, 'AF_UNIX'):
return reader, writer return reader, writer
async def start_unix_server(client_connected_cb, path=None, *, async def start_unix_server(client_connected_cb, path=None, *,
loop=None, limit=_DEFAULT_LIMIT, **kwds): limit=_DEFAULT_LIMIT, **kwds):
"""Similar to `start_server` but works with UNIX Domain Sockets.""" """Similar to `start_server` but works with UNIX Domain Sockets."""
if loop is None: loop = events.get_running_loop()
loop = events.get_event_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
def factory(): def factory():
reader = StreamReader(limit=limit, loop=loop) reader = StreamReader(limit=limit, loop=loop)

View File

@ -1160,9 +1160,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'no IPv6 support') @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'no IPv6 support')
def test_create_server_ipv6(self): def test_create_server_ipv6(self):
async def main(): async def main():
with self.assertWarns(DeprecationWarning): srv = await asyncio.start_server(lambda: None, '::1', 0)
srv = await asyncio.start_server(
lambda: None, '::1', 0, loop=self.loop)
try: try:
self.assertGreater(len(srv.sockets), 0) self.assertGreater(len(srv.sockets), 0)
finally: finally:

View File

@ -45,9 +45,8 @@ class BaseStartServer(func_tests.FunctionalTestCaseMixin):
async with srv: async with srv:
await srv.serve_forever() await srv.serve_forever()
with self.assertWarns(DeprecationWarning): srv = self.loop.run_until_complete(asyncio.start_server(
srv = self.loop.run_until_complete(asyncio.start_server( serve, socket_helper.HOSTv4, 0, start_serving=False))
serve, socket_helper.HOSTv4, 0, loop=self.loop, start_serving=False))
self.assertFalse(srv.is_serving()) self.assertFalse(srv.is_serving())
@ -102,9 +101,8 @@ class SelectorStartServerTests(BaseStartServer, unittest.TestCase):
await srv.serve_forever() await srv.serve_forever()
with test_utils.unix_socket_path() as addr: with test_utils.unix_socket_path() as addr:
with self.assertWarns(DeprecationWarning): srv = self.loop.run_until_complete(asyncio.start_unix_server(
srv = self.loop.run_until_complete(asyncio.start_unix_server( serve, addr, start_serving=False))
serve, addr, loop=self.loop, start_serving=False))
main_task = self.loop.create_task(main(srv)) main_task = self.loop.create_task(main(srv))

View File

@ -657,13 +657,11 @@ class BaseStartTLS(func_tests.FunctionalTestCaseMixin):
sock.close() sock.close()
async def client(addr): async def client(addr):
with self.assertWarns(DeprecationWarning): reader, writer = await asyncio.open_connection(
reader, writer = await asyncio.open_connection( *addr,
*addr, ssl=client_sslctx,
ssl=client_sslctx, server_hostname='',
server_hostname='', ssl_handshake_timeout=1.0)
loop=self.loop,
ssl_handshake_timeout=1.0)
with self.tcp_server(server, with self.tcp_server(server,
max_clients=1, max_clients=1,
@ -697,13 +695,11 @@ class BaseStartTLS(func_tests.FunctionalTestCaseMixin):
sock.close() sock.close()
async def client(addr): async def client(addr):
with self.assertWarns(DeprecationWarning): reader, writer = await asyncio.open_connection(
reader, writer = await asyncio.open_connection( *addr,
*addr, ssl=client_sslctx,
ssl=client_sslctx, server_hostname='',
server_hostname='', ssl_handshake_timeout=support.LOOPBACK_TIMEOUT)
loop=self.loop,
ssl_handshake_timeout=support.LOOPBACK_TIMEOUT)
with self.tcp_server(server, with self.tcp_server(server,
max_clients=1, max_clients=1,
@ -734,12 +730,10 @@ class BaseStartTLS(func_tests.FunctionalTestCaseMixin):
sock.close() sock.close()
async def client(addr): async def client(addr):
with self.assertWarns(DeprecationWarning): reader, writer = await asyncio.open_connection(
reader, writer = await asyncio.open_connection( *addr,
*addr, ssl=client_sslctx,
ssl=client_sslctx, server_hostname='')
server_hostname='',
loop=self.loop)
self.assertEqual(await reader.readline(), b'A\n') self.assertEqual(await reader.readline(), b'A\n')
writer.write(b'B') writer.write(b'B')

View File

@ -48,8 +48,7 @@ class StreamTests(test_utils.TestCase):
def _basetest_open_connection(self, open_connection_fut): def _basetest_open_connection(self, open_connection_fut):
messages = [] messages = []
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
with self.assertWarns(DeprecationWarning): reader, writer = self.loop.run_until_complete(open_connection_fut)
reader, writer = self.loop.run_until_complete(open_connection_fut)
writer.write(b'GET / HTTP/1.0\r\n\r\n') writer.write(b'GET / HTTP/1.0\r\n\r\n')
f = reader.readline() f = reader.readline()
data = self.loop.run_until_complete(f) data = self.loop.run_until_complete(f)
@ -62,23 +61,20 @@ class StreamTests(test_utils.TestCase):
def test_open_connection(self): def test_open_connection(self):
with test_utils.run_test_server() as httpd: with test_utils.run_test_server() as httpd:
conn_fut = asyncio.open_connection(*httpd.address, conn_fut = asyncio.open_connection(*httpd.address)
loop=self.loop)
self._basetest_open_connection(conn_fut) self._basetest_open_connection(conn_fut)
@socket_helper.skip_unless_bind_unix_socket @socket_helper.skip_unless_bind_unix_socket
def test_open_unix_connection(self): def test_open_unix_connection(self):
with test_utils.run_test_unix_server() as httpd: with test_utils.run_test_unix_server() as httpd:
conn_fut = asyncio.open_unix_connection(httpd.address, conn_fut = asyncio.open_unix_connection(httpd.address)
loop=self.loop)
self._basetest_open_connection(conn_fut) self._basetest_open_connection(conn_fut)
def _basetest_open_connection_no_loop_ssl(self, open_connection_fut): def _basetest_open_connection_no_loop_ssl(self, open_connection_fut):
messages = [] messages = []
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
try: try:
with self.assertWarns(DeprecationWarning): reader, writer = self.loop.run_until_complete(open_connection_fut)
reader, writer = self.loop.run_until_complete(open_connection_fut)
finally: finally:
asyncio.set_event_loop(None) asyncio.set_event_loop(None)
writer.write(b'GET / HTTP/1.0\r\n\r\n') writer.write(b'GET / HTTP/1.0\r\n\r\n')
@ -94,8 +90,7 @@ class StreamTests(test_utils.TestCase):
with test_utils.run_test_server(use_ssl=True) as httpd: with test_utils.run_test_server(use_ssl=True) as httpd:
conn_fut = asyncio.open_connection( conn_fut = asyncio.open_connection(
*httpd.address, *httpd.address,
ssl=test_utils.dummy_ssl_context(), ssl=test_utils.dummy_ssl_context())
loop=self.loop)
self._basetest_open_connection_no_loop_ssl(conn_fut) self._basetest_open_connection_no_loop_ssl(conn_fut)
@ -107,15 +102,14 @@ class StreamTests(test_utils.TestCase):
httpd.address, httpd.address,
ssl=test_utils.dummy_ssl_context(), ssl=test_utils.dummy_ssl_context(),
server_hostname='', server_hostname='',
loop=self.loop) )
self._basetest_open_connection_no_loop_ssl(conn_fut) self._basetest_open_connection_no_loop_ssl(conn_fut)
def _basetest_open_connection_error(self, open_connection_fut): def _basetest_open_connection_error(self, open_connection_fut):
messages = [] messages = []
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
with self.assertWarns(DeprecationWarning): reader, writer = self.loop.run_until_complete(open_connection_fut)
reader, writer = self.loop.run_until_complete(open_connection_fut)
writer._protocol.connection_lost(ZeroDivisionError()) writer._protocol.connection_lost(ZeroDivisionError())
f = reader.read() f = reader.read()
with self.assertRaises(ZeroDivisionError): with self.assertRaises(ZeroDivisionError):
@ -126,15 +120,13 @@ class StreamTests(test_utils.TestCase):
def test_open_connection_error(self): def test_open_connection_error(self):
with test_utils.run_test_server() as httpd: with test_utils.run_test_server() as httpd:
conn_fut = asyncio.open_connection(*httpd.address, conn_fut = asyncio.open_connection(*httpd.address)
loop=self.loop)
self._basetest_open_connection_error(conn_fut) self._basetest_open_connection_error(conn_fut)
@socket_helper.skip_unless_bind_unix_socket @socket_helper.skip_unless_bind_unix_socket
def test_open_unix_connection_error(self): def test_open_unix_connection_error(self):
with test_utils.run_test_unix_server() as httpd: with test_utils.run_test_unix_server() as httpd:
conn_fut = asyncio.open_unix_connection(httpd.address, conn_fut = asyncio.open_unix_connection(httpd.address)
loop=self.loop)
self._basetest_open_connection_error(conn_fut) self._basetest_open_connection_error(conn_fut)
def test_feed_empty_data(self): def test_feed_empty_data(self):
@ -596,8 +588,7 @@ class StreamTests(test_utils.TestCase):
sock = socket.create_server(('127.0.0.1', 0)) sock = socket.create_server(('127.0.0.1', 0))
self.server = self.loop.run_until_complete( self.server = self.loop.run_until_complete(
asyncio.start_server(self.handle_client, asyncio.start_server(self.handle_client,
sock=sock, sock=sock))
loop=self.loop))
return sock.getsockname() return sock.getsockname()
def handle_client_callback(self, client_reader, client_writer): def handle_client_callback(self, client_reader, client_writer):
@ -610,8 +601,7 @@ class StreamTests(test_utils.TestCase):
sock.close() sock.close()
self.server = self.loop.run_until_complete( self.server = self.loop.run_until_complete(
asyncio.start_server(self.handle_client_callback, asyncio.start_server(self.handle_client_callback,
host=addr[0], port=addr[1], host=addr[0], port=addr[1]))
loop=self.loop))
return addr return addr
def stop(self): def stop(self):
@ -621,9 +611,7 @@ class StreamTests(test_utils.TestCase):
self.server = None self.server = None
async def client(addr): async def client(addr):
with self.assertWarns(DeprecationWarning): reader, writer = await asyncio.open_connection(*addr)
reader, writer = await asyncio.open_connection(
*addr, loop=self.loop)
# send a line # send a line
writer.write(b"hello world!\n") writer.write(b"hello world!\n")
# read it back # read it back
@ -637,16 +625,14 @@ class StreamTests(test_utils.TestCase):
# test the server variant with a coroutine as client handler # test the server variant with a coroutine as client handler
server = MyServer(self.loop) server = MyServer(self.loop)
with self.assertWarns(DeprecationWarning): addr = server.start()
addr = server.start()
msg = self.loop.run_until_complete(self.loop.create_task(client(addr))) msg = self.loop.run_until_complete(self.loop.create_task(client(addr)))
server.stop() server.stop()
self.assertEqual(msg, b"hello world!\n") self.assertEqual(msg, b"hello world!\n")
# test the server variant with a callback as client handler # test the server variant with a callback as client handler
server = MyServer(self.loop) server = MyServer(self.loop)
with self.assertWarns(DeprecationWarning): addr = server.start_callback()
addr = server.start_callback()
msg = self.loop.run_until_complete(self.loop.create_task(client(addr))) msg = self.loop.run_until_complete(self.loop.create_task(client(addr)))
server.stop() server.stop()
self.assertEqual(msg, b"hello world!\n") self.assertEqual(msg, b"hello world!\n")
@ -673,8 +659,7 @@ class StreamTests(test_utils.TestCase):
def start(self): def start(self):
self.server = self.loop.run_until_complete( self.server = self.loop.run_until_complete(
asyncio.start_unix_server(self.handle_client, asyncio.start_unix_server(self.handle_client,
path=self.path, path=self.path))
loop=self.loop))
def handle_client_callback(self, client_reader, client_writer): def handle_client_callback(self, client_reader, client_writer):
self.loop.create_task(self.handle_client(client_reader, self.loop.create_task(self.handle_client(client_reader,
@ -682,8 +667,7 @@ class StreamTests(test_utils.TestCase):
def start_callback(self): def start_callback(self):
start = asyncio.start_unix_server(self.handle_client_callback, start = asyncio.start_unix_server(self.handle_client_callback,
path=self.path, path=self.path)
loop=self.loop)
self.server = self.loop.run_until_complete(start) self.server = self.loop.run_until_complete(start)
def stop(self): def stop(self):
@ -693,9 +677,7 @@ class StreamTests(test_utils.TestCase):
self.server = None self.server = None
async def client(path): async def client(path):
with self.assertWarns(DeprecationWarning): reader, writer = await asyncio.open_unix_connection(path)
reader, writer = await asyncio.open_unix_connection(
path, loop=self.loop)
# send a line # send a line
writer.write(b"hello world!\n") writer.write(b"hello world!\n")
# read it back # read it back
@ -710,8 +692,7 @@ class StreamTests(test_utils.TestCase):
# test the server variant with a coroutine as client handler # test the server variant with a coroutine as client handler
with test_utils.unix_socket_path() as path: with test_utils.unix_socket_path() as path:
server = MyServer(self.loop, path) server = MyServer(self.loop, path)
with self.assertWarns(DeprecationWarning): server.start()
server.start()
msg = self.loop.run_until_complete( msg = self.loop.run_until_complete(
self.loop.create_task(client(path))) self.loop.create_task(client(path)))
server.stop() server.stop()
@ -720,8 +701,7 @@ class StreamTests(test_utils.TestCase):
# test the server variant with a callback as client handler # test the server variant with a callback as client handler
with test_utils.unix_socket_path() as path: with test_utils.unix_socket_path() as path:
server = MyServer(self.loop, path) server = MyServer(self.loop, path)
with self.assertWarns(DeprecationWarning): server.start_callback()
server.start_callback()
msg = self.loop.run_until_complete( msg = self.loop.run_until_complete(
self.loop.create_task(client(path))) self.loop.create_task(client(path)))
server.stop() server.stop()
@ -809,9 +789,7 @@ os.close(fd)
clt.close() clt.close()
async def client(host, port): async def client(host, port):
with self.assertWarns(DeprecationWarning): reader, writer = await asyncio.open_connection(host, port)
reader, writer = await asyncio.open_connection(
host, port, loop=self.loop)
while True: while True:
writer.write(b"foo\n") writer.write(b"foo\n")
@ -895,9 +873,8 @@ os.close(fd)
def test_wait_closed_on_close(self): def test_wait_closed_on_close(self):
with test_utils.run_test_server() as httpd: with test_utils.run_test_server() as httpd:
with self.assertWarns(DeprecationWarning): rd, wr = self.loop.run_until_complete(
rd, wr = self.loop.run_until_complete( asyncio.open_connection(*httpd.address))
asyncio.open_connection(*httpd.address, loop=self.loop))
wr.write(b'GET / HTTP/1.0\r\n\r\n') wr.write(b'GET / HTTP/1.0\r\n\r\n')
f = rd.readline() f = rd.readline()
@ -913,9 +890,8 @@ os.close(fd)
def test_wait_closed_on_close_with_unread_data(self): def test_wait_closed_on_close_with_unread_data(self):
with test_utils.run_test_server() as httpd: with test_utils.run_test_server() as httpd:
with self.assertWarns(DeprecationWarning): rd, wr = self.loop.run_until_complete(
rd, wr = self.loop.run_until_complete( asyncio.open_connection(*httpd.address))
asyncio.open_connection(*httpd.address, loop=self.loop))
wr.write(b'GET / HTTP/1.0\r\n\r\n') wr.write(b'GET / HTTP/1.0\r\n\r\n')
f = rd.readline() f = rd.readline()
@ -972,10 +948,8 @@ os.close(fd)
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
with test_utils.run_test_server() as httpd: with test_utils.run_test_server() as httpd:
with self.assertWarns(DeprecationWarning): rd, wr = self.loop.run_until_complete(
rd, wr = self.loop.run_until_complete( asyncio.open_connection(*httpd.address))
asyncio.open_connection(*httpd.address,
loop=self.loop))
wr.close() wr.close()
f = wr.wait_closed() f = wr.wait_closed()

View File

@ -0,0 +1,2 @@
Remove loop parameter from ``asyncio.open_connection`` and
``asyncio.start_server`` functions. Patch provided by Yurii Karabas.