@@ -224,6 +224,17 @@ cdef __static_getaddrinfo(object host, object port,
224
224
return (family, type , proto)
225
225
226
226
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
+
227
238
cdef __static_getaddrinfo_pyaddr(object host, object port,
228
239
int family, int type ,
229
240
int proto, int flags):
@@ -245,7 +256,20 @@ cdef __static_getaddrinfo_pyaddr(object host, object port,
245
256
except Exception :
246
257
return
247
258
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
+ )
249
273
250
274
251
275
@ cython.freelist (DEFAULT_FREELIST_SIZE)
@@ -276,8 +300,8 @@ cdef class AddrInfo:
276
300
while ptr != NULL :
277
301
if ptr.ai_addr.sa_family in (uv.AF_INET, uv.AF_INET6):
278
302
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) ,
281
305
ptr.ai_protocol,
282
306
(' ' if ptr.ai_canonname is NULL else
283
307
(< bytes> ptr.ai_canonname).decode()),
@@ -370,6 +394,13 @@ cdef class NameInfoRequest(UVRequest):
370
394
self .callback(convert_error(err))
371
395
372
396
397
+ cdef _intenum_converter(value, enum_klass):
398
+ try :
399
+ return enum_klass(value)
400
+ except ValueError :
401
+ return value
402
+
403
+
373
404
cdef void __on_addrinfo_resolved(uv.uv_getaddrinfo_t * resolver,
374
405
int status, system.addrinfo * res) with gil:
375
406
0 commit comments