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

Skip to content

Commit 016884c

Browse files
committed
Issue #16257: make test_create_connection() handle ENETUNREACH.
2 parents 5595ab5 + 45bb613 commit 016884c

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

Lib/test/test_socket.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4109,7 +4109,26 @@ def test_create_connection(self):
41094109
port = support.find_unused_port()
41104110
with self.assertRaises(socket.error) as cm:
41114111
socket.create_connection((HOST, port))
4112-
self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
4112+
4113+
# Issue #16257: create_connection() calls getaddrinfo() against
4114+
# 'localhost'. This may result in an IPV6 addr being returned
4115+
# as well as an IPV4 one:
4116+
# >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM)
4117+
# >>> [(2, 2, 0, '', ('127.0.0.1', 41230)),
4118+
# (26, 2, 0, '', ('::1', 41230, 0, 0))]
4119+
#
4120+
# create_connection() enumerates through all the addresses returned
4121+
# and if it doesn't successfully bind to any of them, it propagates
4122+
# the last exception it encountered.
4123+
#
4124+
# On Solaris, ENETUNREACH is returned in this circumstance instead
4125+
# of ECONNREFUSED. So, if that errno exists, add it to our list of
4126+
# expected errnos.
4127+
expected_errnos = [ errno.ECONNREFUSED, ]
4128+
if hasattr(errno, 'ENETUNREACH'):
4129+
expected_errnos.append(errno.ENETUNREACH)
4130+
4131+
self.assertIn(cm.exception.errno, expected_errnos)
41134132

41144133
def test_create_connection_timeout(self):
41154134
# Issue #9792: create_connection() should not recast timeout errors

0 commit comments

Comments
 (0)