Issue 1685962: socket.getaddrinfo() should take an address tuple. (original) (raw)
The getaddrinfo call should not take separate host and port parameters. Instead, it should take the same address tuples as every other python socket function, i.e. it's signature should be
getaddrinfo( address_tuple[, family[, socktype[, proto[, flags]]]])
This is because all python socket calls take a (host, port) tuple as an address.
However, functions that take a tuple are then forced to unpack that tuple in order to call getaddrinfo. The problem is that this is error prone. Consider the following code
def my_trivial_socket_function(address_tuple, a=None): host, port = address_tuple for result in getaddrinfo(host, port, 0, 0, 0): whatever(*result)
I can then call this function like so, and get a weird error
my_trivial_socket_function("lo", 80)
because the outcome of
host, port = "lo"
is
host = "l" ; port = "o"
One solution to this problem is to force every socket function, trivial or otherwise, to do error checking on the address tuple, like so
def my_trivial_socket_function(address_tuple,a=None): assert type(address_tuple) is type( () ) assert type(address_tuple[0]) is type("") assert type(address_tuple[1]) is type(0) host, port = address_tuple for result in getaddrinfo(host, port, 0, 0, 0): pass
But since the only reason the function has to unpack the tuple is to pass it to getaddrinfo, then the correct solution is to make getaddrinfo accept the same address format as every other python socket function.
I think the original reason why getaddrinfo() has been designed to accept separate host and port args instead of a tuple is because the original C call does the same: http://www2.research.att.com/~gsf/man/man3/getaddrinfo.html
Despite this apparently looks somewhat inconsistent with other calls like bind(), connect() and connect_ex() which take an address tuple, they reflect the original C behavior in the same manner, also because they're fundamentally different than getaddrinfo(), so I see no valid reason to change this situation.