@@ -81,14 +81,6 @@ class AresError(Exception):
81
81
pass
82
82
83
83
84
- def check_channel (f ):
85
- @functools .wraps (f )
86
- def wrapper (self , * args , ** kwds ):
87
- if not self .channel :
88
- raise AresError ("Channel has already been destroyed" )
89
- return f (self , * args , ** kwds )
90
- return wrapper
91
-
92
84
# callback helpers
93
85
94
86
_global_set = set ()
@@ -390,7 +382,7 @@ def __init__(self,
390
382
if r != _lib .ARES_SUCCESS :
391
383
raise AresError ('Failed to initialize c-ares channel' )
392
384
393
- self .channel = channel [0 ]
385
+ self ._channel = _ffi . gc ( channel , lambda x : _lib . ares_destroy ( x [0 ]))
394
386
395
387
if servers :
396
388
self ._set_servers (servers )
@@ -401,20 +393,9 @@ def __init__(self,
401
393
if local_dev :
402
394
self .set_local_dev (local_dev )
403
395
404
- def __del__ (self ):
405
- self .destroy ()
406
-
407
- def destroy (self ):
408
- if self .channel :
409
- _lib .ares_destroy (self .channel )
410
- self .channel = None
411
- self ._sock_state_cb_handle = None
412
-
413
- @check_channel
414
396
def cancel (self ):
415
- _lib .ares_cancel (self .channel )
397
+ _lib .ares_cancel (self ._channel [ 0 ] )
416
398
417
- @check_channel
418
399
def _set_servers (self , servers ):
419
400
c = _ffi .new ("struct ares_addr_node[%d]" % len (servers ))
420
401
for i , server in enumerate (servers ):
@@ -428,15 +409,14 @@ def _set_servers(self, servers):
428
409
if i > 0 :
429
410
c [i - 1 ].next = _ffi .addressof (c [i ])
430
411
431
- r = _lib .ares_set_servers (self .channel , c )
412
+ r = _lib .ares_set_servers (self ._channel [ 0 ] , c )
432
413
if r != _lib .ARES_SUCCESS :
433
414
raise AresError (r , errno .strerror (r ))
434
415
435
- @check_channel
436
416
def _get_servers (self ):
437
417
servers = _ffi .new ("struct ares_addr_node **" )
438
418
439
- r = _lib .ares_get_servers (self .channel , servers )
419
+ r = _lib .ares_get_servers (self ._channel [ 0 ] , servers )
440
420
if r != _lib .ARES_SUCCESS :
441
421
raise AresError (r , errno .strerror (r ))
442
422
@@ -457,12 +437,11 @@ def _get_servers(self):
457
437
458
438
servers = property (_get_servers , _set_servers )
459
439
460
- @check_channel
461
440
def getsock (self ):
462
441
rfds = []
463
442
wfds = []
464
443
socks = _ffi .new ("ares_socket_t [%d]" % _lib .ARES_GETSOCK_MAXNUM )
465
- bitmask = _lib .ares_getsock (self .channel , socks , _lib .ARES_GETSOCK_MAXNUM )
444
+ bitmask = _lib .ares_getsock (self ._channel [ 0 ] , socks , _lib .ARES_GETSOCK_MAXNUM )
466
445
for i in range (_lib .ARES_GETSOCK_MAXNUM ):
467
446
if _lib .ARES_GETSOCK_READABLE (bitmask , i ):
468
447
rfds .append (socks [i ])
@@ -471,12 +450,9 @@ def getsock(self):
471
450
472
451
return rfds , wfds
473
452
474
-
475
- @check_channel
476
453
def process_fd (self , read_fd , write_fd ):
477
- _lib .ares_process_fd (self .channel , _ffi .cast ("ares_socket_t" , read_fd ), _ffi .cast ("ares_socket_t" , write_fd ))
454
+ _lib .ares_process_fd (self ._channel [ 0 ] , _ffi .cast ("ares_socket_t" , read_fd ), _ffi .cast ("ares_socket_t" , write_fd ))
478
455
479
- @check_channel
480
456
def timeout (self , t = None ):
481
457
maxtv = _ffi .NULL
482
458
tv = _ffi .new ("struct timeval*" )
@@ -489,14 +465,13 @@ def timeout(self, t = None):
489
465
else :
490
466
raise ValueError ("timeout needs to be a positive number or None" )
491
467
492
- _lib .ares_timeout (self .channel , maxtv , tv )
468
+ _lib .ares_timeout (self ._channel [ 0 ] , maxtv , tv )
493
469
494
470
if tv == _ffi .NULL :
495
471
return 0.0
496
472
497
473
return (tv .tv_sec + tv .tv_usec / 1000000.0 )
498
474
499
- @check_channel
500
475
def gethostbyaddr (self , addr , callback ):
501
476
if not callable (callback ):
502
477
raise TypeError ("a callable is required" )
@@ -514,16 +489,15 @@ def gethostbyaddr(self, addr, callback):
514
489
515
490
userdata = _ffi .new_handle (callback )
516
491
_global_set .add (userdata )
517
- _lib .ares_gethostbyaddr (self .channel , address , _ffi .sizeof (address [0 ]), family , _host_cb , userdata )
492
+ _lib .ares_gethostbyaddr (self ._channel [ 0 ] , address , _ffi .sizeof (address [0 ]), family , _host_cb , userdata )
518
493
519
- @check_channel
520
494
def gethostbyname (self , name , family , callback ):
521
495
if not callable (callback ):
522
496
raise TypeError ("a callable is required" )
523
497
524
498
userdata = _ffi .new_handle (callback )
525
499
_global_set .add (userdata )
526
- _lib .ares_gethostbyname (self .channel , parse_name (name ), family , _host_cb , userdata )
500
+ _lib .ares_gethostbyname (self ._channel [ 0 ] , parse_name (name ), family , _host_cb , userdata )
527
501
528
502
def query (self , name , query_type , callback ):
529
503
if not callable (callback ):
@@ -534,20 +508,18 @@ def query(self, name, query_type, callback):
534
508
535
509
userdata = _ffi .new_handle ((callback , query_type ))
536
510
_global_set .add (userdata )
537
- _lib .ares_query (self .channel , parse_name (name ), _lib .C_IN , query_type , _query_cb , userdata )
511
+ _lib .ares_query (self ._channel [ 0 ] , parse_name (name ), _lib .C_IN , query_type , _query_cb , userdata )
538
512
539
- @check_channel
540
513
def set_local_ip (self , ip ):
541
514
addr4 = _ffi .new ("struct in_addr*" )
542
515
addr6 = _ffi .new ("struct ares_in6_addr*" )
543
516
if _lib .ares_inet_pton (socket .AF_INET , ascii_bytes (ip ), addr4 ) == 1 :
544
- _lib .ares_set_local_ip4 (self .channel , socket .ntohl (addr4 .s_addr ))
517
+ _lib .ares_set_local_ip4 (self ._channel [ 0 ] , socket .ntohl (addr4 .s_addr ))
545
518
elif _lib .ares_inet_pton (socket .AF_INET6 , ascii_bytes (ip ), addr6 ) == 1 :
546
- _lib .ares_set_local_ip6 (self .channel , addr6 )
519
+ _lib .ares_set_local_ip6 (self ._channel [ 0 ] , addr6 )
547
520
else :
548
521
raise ValueError ("invalid IP address" )
549
522
550
- @check_channel
551
523
def getnameinfo (self , ip_port , flags , callback ):
552
524
if not callable (callback ):
553
525
raise TypeError ("a callable is required" )
@@ -573,11 +545,10 @@ def getnameinfo(self, ip_port, flags, callback):
573
545
574
546
userdata = _ffi .new_handle (callback )
575
547
_global_set .add (userdata )
576
- _lib .ares_getnameinfo (self .channel , _ffi .cast ("struct sockaddr*" , sa ), _ffi .sizeof (sa [0 ]), flags , _nameinfo_cb , userdata )
548
+ _lib .ares_getnameinfo (self ._channel [ 0 ] , _ffi .cast ("struct sockaddr*" , sa ), _ffi .sizeof (sa [0 ]), flags , _nameinfo_cb , userdata )
577
549
578
- @check_channel
579
550
def set_local_dev (self , dev ):
580
- _lib .ares_set_local_dev (self .channel , dev )
551
+ _lib .ares_set_local_dev (self ._channel [ 0 ] , dev )
581
552
582
553
583
554
class AresResult :
0 commit comments