@@ -1638,7 +1638,26 @@ def test_create_connection(self):
16381638 port = support .find_unused_port ()
16391639 with self .assertRaises (socket .error ) as cm :
16401640 socket .create_connection ((HOST , port ))
1641- self .assertEqual (cm .exception .errno , errno .ECONNREFUSED )
1641+
1642+ # Issue #16257: create_connection() calls getaddrinfo() against
1643+ # 'localhost'. This may result in an IPV6 addr being returned
1644+ # as well as an IPV4 one:
1645+ # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM)
1646+ # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)),
1647+ # (26, 2, 0, '', ('::1', 41230, 0, 0))]
1648+ #
1649+ # create_connection() enumerates through all the addresses returned
1650+ # and if it doesn't successfully bind to any of them, it propagates
1651+ # the last exception it encountered.
1652+ #
1653+ # On Solaris, ENETUNREACH is returned in this circumstance instead
1654+ # of ECONNREFUSED. So, if that errno exists, add it to our list of
1655+ # expected errnos.
1656+ expected_errnos = [ errno .ECONNREFUSED , ]
1657+ if hasattr (errno , 'ENETUNREACH' ):
1658+ expected_errnos .append (errno .ENETUNREACH )
1659+
1660+ self .assertIn (cm .exception .errno , expected_errnos )
16421661
16431662 def test_create_connection_timeout (self ):
16441663 # Issue #9792: create_connection() should not recast timeout errors
0 commit comments