@@ -224,6 +224,17 @@ cdef __static_getaddrinfo(object host, object port,
224224 return (family, type , proto)
225225
226226
227+ # This bitmask is used in __static_getaddrinfo_pyaddr() to manage
228+ # if ai_canonname should be set if AI_CANONNAME flag is set.
229+ # This bitmask is lazily set in loop.getaddrinfo() to make sure that
230+ # __static_getaddrinfo_pyaddr() behaves consistently as libc getaddrinfo().
231+ # 1 << 0 : If 1 << 1 is set
232+ # 1 << 1 : If ai_canonname should be set if AI_CANONNAME is set
233+ # 1 << 2 : If 1 << 3 is set
234+ # 1 << 3 : If ai_canonname should be set if AI_CANONNAME is not set
235+ cdef int __static_getaddrinfo_canonname_mode = 0
236+
237+
227238cdef __static_getaddrinfo_pyaddr(object host, object port,
228239 int family, int type ,
229240 int proto, int flags):
@@ -245,7 +256,20 @@ cdef __static_getaddrinfo_pyaddr(object host, object port,
245256 except Exception :
246257 return
247258
248- return af, type , proto, ' ' , pyaddr
259+ if __static_getaddrinfo_canonname_mode & (
260+ 1 << 1 if flags & socket_AI_CANONNAME else 1 << 3
261+ ):
262+ if isinstance (host, (bytes, bytearray)):
263+ host = host.decode(' ascii' )
264+ else :
265+ host = ' '
266+ return (
267+ _intenum_converter(af, socket_AddressFamily),
268+ _intenum_converter(type , socket_SocketKind),
269+ proto,
270+ host,
271+ pyaddr,
272+ )
249273
250274
251275@ cython.freelist (DEFAULT_FREELIST_SIZE)
@@ -276,8 +300,8 @@ cdef class AddrInfo:
276300 while ptr != NULL :
277301 if ptr.ai_addr.sa_family in (uv.AF_INET, uv.AF_INET6):
278302 result.append((
279- ptr.ai_family,
280- ptr.ai_socktype,
303+ _intenum_converter( ptr.ai_family, socket_AddressFamily) ,
304+ _intenum_converter( ptr.ai_socktype, socket_SocketKind) ,
281305 ptr.ai_protocol,
282306 (' ' if ptr.ai_canonname is NULL else
283307 (< bytes> ptr.ai_canonname).decode()),
@@ -370,6 +394,13 @@ cdef class NameInfoRequest(UVRequest):
370394 self .callback(convert_error(err))
371395
372396
397+ cdef _intenum_converter(value, enum_klass):
398+ try :
399+ return enum_klass(value)
400+ except ValueError :
401+ return value
402+
403+
373404cdef void __on_addrinfo_resolved(uv.uv_getaddrinfo_t * resolver,
374405 int status, system.addrinfo * res) with gil:
375406
0 commit comments