From: Bob Halley Date: Tue, 16 Jun 2020 00:19:18 +0000 (-0700) Subject: avoid getaddrinfo() when we can X-Git-Tag: v2.0.0rc1~100 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a867ef1c8af00734138c8e72321782ef3a798013;p=thirdparty%2Fdnspython.git avoid getaddrinfo() when we can --- diff --git a/dns/inet.py b/dns/inet.py index 71782ac3..d434948f 100644 --- a/dns/inet.py +++ b/dns/inet.py @@ -157,8 +157,20 @@ def low_level_address_tuple(high_tuple, af=None): if af == AF_INET: return (address, port) elif af == AF_INET6: - ai_flags = socket.AI_NUMERICHOST - ((*_, tup), *_) = socket.getaddrinfo(address, port, flags=ai_flags) - return tup + i = address.find('%') + if i < 0: + # no scope, shortcut! + return (address, port, 0, 0) + # try to avoid getaddrinfo() + addrpart = address[:i] + scope = address[i+1:] + if scope.isdigit(): + return (addrpart, port, 0, int(scope)) + try: + return (addrpart, port, 0, socket.if_nametoindex(scope)) + except AttributeError: + ai_flags = socket.AI_NUMERICHOST + ((*_, tup), *_) = socket.getaddrinfo(address, port, flags=ai_flags) + return tup else: raise NotImplementedError(f'unknown address family {af}')