Title: socket.getaddrinfo() should take an address tuple.
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.2
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: amak, giampaolo.rodola
Priority: normal Keywords:

Created on 2007-03-22 13:10 by amak, last changed 2010-07-10 11:12 by giampaolo.rodola. This issue is now closed.

Messages (3)
msg31620 - (view) Author: Alan Kennedy (amak) * Date: 2007-03-22 13:10
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):

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"


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):

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.
msg109848 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2010-07-10 10:53
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:

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.
msg109851 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2010-07-10 11:12
I'm closing this out as rejected.
Date User Action Args
2010-07-10 11:12:48giampaolo.rodolasetstatus: open -> closed
resolution: rejected
messages: + msg109851
2010-07-10 10:53:28giampaolo.rodolasetnosy: + giampaolo.rodola
messages: + msg109848
2010-07-10 10:01:24BreamoreBoysetversions: + Python 3.2, - Python 3.1, Python 2.7
2009-04-06 09:41:51ajaksu2setstage: test needed
versions: + Python 3.1, Python 2.7
2007-03-22 13:10:31amakcreate