This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: getaddrinfo: inconsistent handling of port=None
Type: Stage: resolved
Components: Extension Modules Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: giampaolo.rodola, njs
Priority: normal Keywords:

Created on 2017-08-14 07:35 by njs, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg300236 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2017-08-14 07:35
socket.getaddrinfo accepts None as a port argument, and translates it into 0. This is handy, because bind() understands 0 to mean "pick a port for me", and if you want bind to pick a port for you and port=None is a slightly more obvious way to say that then port=0.

For example:

>>> socket.getaddrinfo("127.0.0.1", None)
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('127.0.0.1', 0))]

socket.getaddrinfo also accepts None as a host name; this is necessary because the underlying getaddrinfo(3) call has special handling for host=NULL, and we need some way to access it:

>>> socket.getaddrinfo(None, 0)
[(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_RAW: 3>, 0, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('127.0.0.1', 0))]

However, even though both of these features are supported separately... if you try to use them *together*, then socket.getaddrinfo errors out:

>>> socket.getaddrinfo(None, None)
socket.gaierror: [Errno -2] Name or service not known

I expected that last call to be equivalent to socket.getaddrinfo(None, 0).
msg300281 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2017-08-15 02:11
Ugh, apparently this weird behavior is actually mandated by the RFC :-(.

RFC 3493:

   The nodename and servname arguments are either null pointers or
   pointers to null-terminated strings.  One or both of these two
   arguments must be a non-null pointer.

So... never mind!
History
Date User Action Args
2022-04-11 14:58:50adminsetgithub: 75381
2017-08-15 02:11:14njssetstatus: open -> closed
resolution: not a bug
messages: + msg300281

stage: resolved
2017-08-14 07:36:20njssetnosy: + giampaolo.rodola
2017-08-14 07:35:10njscreate