diff options
| author | Bob Halley <halley@dnspython.org> | 2020-06-15 17:19:18 -0700 |
|---|---|---|
| committer | Bob Halley <halley@dnspython.org> | 2020-06-15 17:19:18 -0700 |
| commit | a867ef1c8af00734138c8e72321782ef3a798013 (patch) | |
| tree | d0a15f26c6ad75f8fd3cc19966826e06fd32b00a /dns/inet.py | |
| parent | 39f5a456e22c0ec4bf661ec0f255223036472552 (diff) | |
| download | dnspython-a867ef1c8af00734138c8e72321782ef3a798013.tar.gz | |
avoid getaddrinfo() when we can
Diffstat (limited to 'dns/inet.py')
| -rw-r--r-- | dns/inet.py | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/dns/inet.py b/dns/inet.py index 71782ac..d434948 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}') |
