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: socket.getaddrinfo() should return named tuples
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: cheryl.sabella, eric.smith, exarkun, flox, flub, giampaolo.rodola, pitrou, vstinner
Priority: normal Keywords: patch

Created on 2010-06-02 21:31 by giampaolo.rodola, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
getaddrinfo.patch flub, 2012-07-07 10:46 review
Messages (6)
msg106916 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2010-06-02 21:31
As of right now socket.getaddrinfo() returns a sequence of 5-tuples reflecting family, type, protocol, canonname, and address of a socket:

>>> socket.getaddrinfo(None, 0)
[(10, 1, 6, '', ('::1', 0, 0, 0)), (10, 2, 17, '', ('::1', 0, 0, 0)), (10, 3, 0, '', ('::1', 0, 0, 0)), (2, 1, 6, '', ('127.0.0.1', 0)), (2, 2, 17, '', ('127.0.0.1', 0)), (2, 3, 0, '', ('127.0.0.1', 0))]

For readability it would be good if they were named tuples instead:

>>> socket.getaddrinfo(None, 0)[0]
addr_info(family=10, type=1, protocol=6, canonname='', address=('::1', 0, 0, 0))

Optionally, integers can be replaced by socket constant names:

>>> socket.getaddrinfo(None, 0)[0]
addr_info(family=socket.AF_INET6, type=socket.SOCK_STREAM, protocol=socket.IPPROTO_TCP, canonname='', address=('::1', 0, 0, 0))
msg106919 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-06-02 21:52
Keep in mind this isn't an entirely backwards compatible change. See issue 8413, for example.
msg151200 - (view) Author: Floris Bruynooghe (flub) Date: 2012-01-13 22:14
I think the part which could possibly a problem is addressed in http://hg.python.org/cpython/rev/384f73a104e9/.  Bearing in mind that direct usage for string interpolation is a pretty strange use for the result of getaddrinfo.
msg151201 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-01-13 22:15
A reasonable request indeed.
msg164816 - (view) Author: Floris Bruynooghe (flub) Date: 2012-07-07 10:46
Attached in a patch for this, I've also changed the version to 3.4 since this is a feature and therefore probably too late to go in 3.3.  Please let me know if anything is inadequate.
msg310874 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-01-27 18:31
This one looks to be partly done due to #18720 and enums.

>>> for item in socket.getaddrinfo(None, 0):
...     print(item)
... 
(<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))

Is there a reason that Protocols weren't converted to be enums?  I added quick code to socket.py and it turned the above into:

(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, <Protocol.IPPROTO_TCP: 6>, '', ('::1', 0, 0, 0))
(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_DGRAM: 2>, <Protocol.IPPROTO_UDP: 17>, '', ('::1', 0, 0, 0))
(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_RAW: 3>, <Protocol.IPPROTO_HOPOPTS: 0>, '', ('::1', 0, 0, 0))
(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, <Protocol.IPPROTO_TCP: 6>, '', ('127.0.0.1', 0))
(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, <Protocol.IPPROTO_UDP: 17>, '', ('127.0.0.1', 0))
(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, <Protocol.IPPROTO_HOPOPTS: 0>, '', ('127.0.0.1', 0))

Would that be a responsible change to include?
History
Date User Action Args
2022-04-11 14:57:01adminsetgithub: 53127
2018-01-27 18:31:22cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg310874
2013-11-25 17:35:52vstinnersetnosy: + vstinner
2012-07-07 10:51:21floxsetnosy: + flox

stage: needs patch -> patch review
2012-07-07 10:46:19flubsetfiles: + getaddrinfo.patch
keywords: + patch
messages: + msg164816

versions: + Python 3.4, - Python 3.3
2012-01-13 22:15:47pitrousettype: enhancement
stage: needs patch
messages: + msg151201
versions: + Python 3.3, - Python 3.2
2012-01-13 22:14:38flubsetnosy: + flub
messages: + msg151200
2010-06-02 21:52:50eric.smithsetnosy: + eric.smith
messages: + msg106919
2010-06-02 21:31:27giampaolo.rodolacreate