Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5604446

Browse files
committed
Merge 3.5 (issue #26338)
2 parents 06495ff + e076ffb commit 5604446

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

Lib/asyncio/base_events.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,10 @@ def create_server(self, protocol_factory, host=None, port=None,
880880
to host and port.
881881
882882
The host parameter can also be a sequence of strings and in that case
883-
the TCP server is bound to all hosts of the sequence.
883+
the TCP server is bound to all hosts of the sequence. If a host
884+
appears multiple times (possibly indirectly e.g. when hostnames
885+
resolve to the same IP address), the server is only bound once to that
886+
host.
884887
885888
Return a Server object which can be used to stop the service.
886889
@@ -909,7 +912,7 @@ def create_server(self, protocol_factory, host=None, port=None,
909912
flags=flags)
910913
for host in hosts]
911914
infos = yield from tasks.gather(*fs, loop=self)
912-
infos = itertools.chain.from_iterable(infos)
915+
infos = set(itertools.chain.from_iterable(infos))
913916

914917
completed = False
915918
try:

Lib/test/test_asyncio/test_events.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -749,34 +749,37 @@ def create_server_multiple_hosts(self, family, hosts, mock_sock):
749749
@asyncio.coroutine
750750
def getaddrinfo(host, port, *args, **kw):
751751
if family == socket.AF_INET:
752-
return [[family, socket.SOCK_STREAM, 6, '', (host, port)]]
752+
return [(family, socket.SOCK_STREAM, 6, '', (host, port))]
753753
else:
754-
return [[family, socket.SOCK_STREAM, 6, '', (host, port, 0, 0)]]
754+
return [(family, socket.SOCK_STREAM, 6, '', (host, port, 0, 0))]
755755

756756
def getaddrinfo_task(*args, **kwds):
757757
return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop)
758758

759+
unique_hosts = set(hosts)
760+
759761
if family == socket.AF_INET:
760-
mock_sock.socket().getsockbyname.side_effect = [(host, 80)
761-
for host in hosts]
762+
mock_sock.socket().getsockbyname.side_effect = [
763+
(host, 80) for host in unique_hosts]
762764
else:
763-
mock_sock.socket().getsockbyname.side_effect = [(host, 80, 0, 0)
764-
for host in hosts]
765+
mock_sock.socket().getsockbyname.side_effect = [
766+
(host, 80, 0, 0) for host in unique_hosts]
765767
self.loop.getaddrinfo = getaddrinfo_task
766768
self.loop._start_serving = mock.Mock()
767769
self.loop._stop_serving = mock.Mock()
768770
f = self.loop.create_server(lambda: MyProto(self.loop), hosts, 80)
769771
server = self.loop.run_until_complete(f)
770772
self.addCleanup(server.close)
771-
server_hosts = [sock.getsockbyname()[0] for sock in server.sockets]
772-
self.assertEqual(server_hosts, hosts)
773+
server_hosts = {sock.getsockbyname()[0] for sock in server.sockets}
774+
self.assertEqual(server_hosts, unique_hosts)
773775

774776
def test_create_server_multiple_hosts_ipv4(self):
775777
self.create_server_multiple_hosts(socket.AF_INET,
776-
['1.2.3.4', '5.6.7.8'])
778+
['1.2.3.4', '5.6.7.8', '1.2.3.4'])
777779

778780
def test_create_server_multiple_hosts_ipv6(self):
779-
self.create_server_multiple_hosts(socket.AF_INET6, ['::1', '::2'])
781+
self.create_server_multiple_hosts(socket.AF_INET6,
782+
['::1', '::2', '::1'])
780783

781784
def test_create_server(self):
782785
proto = MyProto(self.loop)

0 commit comments

Comments
 (0)