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