From cca5795c65886c2352de36ecbeb261babde56f34 Mon Sep 17 00:00:00 2001 From: Sergey Ninua Date: Sun, 29 Oct 2017 11:55:16 +0300 Subject: [PATCH 1/4] fixed ValueError for AF_INET6 sockets --- aiohttp/web.py | 2 +- tests/test_run_app.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/aiohttp/web.py b/aiohttp/web.py index cc89d9304a8..28c0cf82510 100644 --- a/aiohttp/web.py +++ b/aiohttp/web.py @@ -421,7 +421,7 @@ def _make_server_creators(handler, *, loop, ssl_context, if hasattr(socket, 'AF_UNIX') and sock.family == socket.AF_UNIX: uris.append('{}://unix:{}:'.format(scheme, sock.getsockname())) else: - host, port = sock.getsockname() + host, port = sock.getsockname()[:2] uris.append(str(base_url.with_host(host).with_port(port))) return server_creations, uris diff --git a/tests/test_run_app.py b/tests/test_run_app.py index 89629de36a1..4b3768627cd 100644 --- a/tests/test_run_app.py +++ b/tests/test_run_app.py @@ -42,6 +42,16 @@ ) del _has_unix_domain_socks, _abstract_path_failed +has_ipv6 = socket.has_ipv6 +if has_ipv6: + # The socket.has_ipv6 flag may be True if Python was built with IPv6 + # support, but the target system still may not have it. + # So let's ensure that we really have IPv6 support. + try: + socket.socket(socket.AF_INET6, socket.SOCK_STREAM) + except OSError: + has_ipv6 = False + # tokio event loop does not allow to override attributes def skip_if_no_dict(loop): @@ -451,6 +461,31 @@ def test_run_app_preexisting_inet_socket(loop, mocker): assert "http://0.0.0.0:{}".format(port) in printer.call_args[0][0] +@pytest.mark.skipif(not has_ipv6, reason="IPv6 is not available") +def test_run_app_preexisting_inet6_socket(loop, mocker): + skip_if_no_dict(loop) + + mocker.spy(loop, 'create_server') + + app = web.Application() + mocker.spy(app, 'startup') + + sock = socket.socket(socket.AF_INET6) + with contextlib.closing(sock): + sock.bind(('::', 0)) + port = sock.getsockname()[1] + + printer = mock.Mock(wraps=stopper(loop)) + web.run_app(app, loop=loop, sock=sock, print=printer) + + assert not loop.is_closed() + loop.create_server.assert_called_with( + mock.ANY, sock=sock, backlog=128, ssl=None + ) + app.startup.assert_called_once_with() + assert "http://:::{}".format(port) in printer.call_args[0][0] + + @skip_if_no_unix_socks def test_run_app_preexisting_unix_socket(loop, mocker): skip_if_no_dict(loop) From 00bbd4b0f35ac86321733ec28e567acd5fd155e3 Mon Sep 17 00:00:00 2001 From: Sergey Ninua Date: Sun, 29 Oct 2017 12:32:59 +0300 Subject: [PATCH 2/4] changelog entry added for #2431 --- CHANGES/2431.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/2431.bugfix diff --git a/CHANGES/2431.bugfix b/CHANGES/2431.bugfix new file mode 100644 index 00000000000..20ed222c628 --- /dev/null +++ b/CHANGES/2431.bugfix @@ -0,0 +1 @@ +fixed ValueError for AF_INET6 sockets From ab2683172359f88952517f2be71bc46833062f22 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sun, 29 Oct 2017 20:15:24 +0200 Subject: [PATCH 3/4] Update 2431.bugfix --- CHANGES/2431.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES/2431.bugfix b/CHANGES/2431.bugfix index 20ed222c628..99e5c17db92 100644 --- a/CHANGES/2431.bugfix +++ b/CHANGES/2431.bugfix @@ -1 +1 @@ -fixed ValueError for AF_INET6 sockets +fixed ValueError for AF_INET6 sockets if a preexisting INET6 socket to the `aiohttp.web.run_app` function. From 2f577bb414d1062298b1abc6418f522705f781cb Mon Sep 17 00:00:00 2001 From: Sergey Ninua Date: Sun, 29 Oct 2017 22:20:12 +0300 Subject: [PATCH 4/4] rename has_ipv6 to HAS_IPV6: it's a contant --- tests/test_run_app.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_run_app.py b/tests/test_run_app.py index 4b3768627cd..44e90c63783 100644 --- a/tests/test_run_app.py +++ b/tests/test_run_app.py @@ -42,15 +42,15 @@ ) del _has_unix_domain_socks, _abstract_path_failed -has_ipv6 = socket.has_ipv6 -if has_ipv6: +HAS_IPV6 = socket.has_ipv6 +if HAS_IPV6: # The socket.has_ipv6 flag may be True if Python was built with IPv6 # support, but the target system still may not have it. # So let's ensure that we really have IPv6 support. try: socket.socket(socket.AF_INET6, socket.SOCK_STREAM) except OSError: - has_ipv6 = False + HAS_IPV6 = False # tokio event loop does not allow to override attributes @@ -461,7 +461,7 @@ def test_run_app_preexisting_inet_socket(loop, mocker): assert "http://0.0.0.0:{}".format(port) in printer.call_args[0][0] -@pytest.mark.skipif(not has_ipv6, reason="IPv6 is not available") +@pytest.mark.skipif(not HAS_IPV6, reason="IPv6 is not available") def test_run_app_preexisting_inet6_socket(loop, mocker): skip_if_no_dict(loop)