From: Bob Halley Date: Wed, 17 Jun 2020 02:37:01 +0000 (-0700) Subject: The dns.inet.AF_* portability scheme is no longer needed. X-Git-Tag: v2.0.0rc1~72 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=651592faa7d305edde5d47100e179246d83d2a79;p=thirdparty%2Fdnspython.git The dns.inet.AF_* portability scheme is no longer needed. --- diff --git a/dns/edns.py b/dns/edns.py index d3df6f94..7d781378 100644 --- a/dns/edns.py +++ b/dns/edns.py @@ -18,6 +18,7 @@ """EDNS Options""" import math +import socket import struct import dns.enum @@ -186,11 +187,11 @@ class ECSOption(Option): super().__init__(OptionType.ECS) af = dns.inet.af_for_address(address) - if af == dns.inet.AF_INET6: + if af == socket.AF_INET6: self.family = 2 if srclen is None: srclen = 56 - elif af == dns.inet.AF_INET: + elif af == socket.AF_INET: self.family = 1 if srclen is None: srclen = 24 diff --git a/dns/inet.py b/dns/inet.py index 048a80c2..25d99c2c 100644 --- a/dns/inet.py +++ b/dns/inet.py @@ -22,18 +22,12 @@ import socket import dns.ipv4 import dns.ipv6 -# We assume that AF_INET is always defined. +# We assume that AF_INET and AF_INET6 are always defined. We keep +# these here for the benefit of any old code (unlikely though that +# is!). AF_INET = socket.AF_INET - -# AF_INET6 might not be defined in the socket module, but we need it. -# We'll try to use the socket module's value, and if it doesn't work, -# we'll use our own value. - -try: - AF_INET6 = socket.AF_INET6 -except AttributeError: - AF_INET6 = 9999 # type: ignore +AF_INET6 = socket.AF_INET6 def inet_pton(family, text): diff --git a/dns/query.py b/dns/query.py index 3c4bba09..05796f87 100644 --- a/dns/query.py +++ b/dns/query.py @@ -228,9 +228,9 @@ def _destination_and_source(where, port, source, source_port, # Caller has specified a source_port but not an address, so we # need to return a source, and we need to use the appropriate # wildcard address as the address. - if af == dns.inet.AF_INET: + if af == socket.AF_INET: source = '0.0.0.0' - elif af == dns.inet.AF_INET6: + elif af == socket.AF_INET6: source = '::' else: raise ValueError('source_port specified but address family is ' @@ -316,9 +316,9 @@ def https(q, where, timeout=None, port=443, source=None, source_port=0, } try: where_af = dns.inet.af_for_address(where) - if where_af == dns.inet.AF_INET: + if where_af == socket.AF_INET: url = 'https://{}:{}{}'.format(where, port, path) - elif where_af == dns.inet.AF_INET6: + elif where_af == socket.AF_INET6: url = 'https://[{}]:{}{}'.format(where, port, path) except ValueError: if bootstrap_address is not None: diff --git a/tests/test_ntoaaton.py b/tests/test_ntoaaton.py index b5d425f9..4a5818e6 100644 --- a/tests/test_ntoaaton.py +++ b/tests/test_ntoaaton.py @@ -261,8 +261,8 @@ class NtoAAtoNTestCase(unittest.TestCase): self.assertRaises(dns.exception.SyntaxError, bad) def test_ptontop(self): - for (af, a) in [(dns.inet.AF_INET, '1.2.3.4'), - (dns.inet.AF_INET6, '2001:db8:0:1:1:1:1:1')]: + for (af, a) in [(socket.AF_INET, '1.2.3.4'), + (socket.AF_INET6, '2001:db8:0:1:1:1:1:1')]: self.assertEqual(dns.inet.inet_ntop(af, dns.inet.inet_pton(af, a)), a) diff --git a/tests/test_query.py b/tests/test_query.py index 8016c8b3..ab2e5e62 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -197,16 +197,16 @@ class DestinationAndSourceTests(unittest.TestCase): def test_af_inferred_from_where(self): (af, d, s) = _d_and_s('1.2.3.4', 53, None, 0) - self.assertEqual(af, dns.inet.AF_INET) + self.assertEqual(af, socket.AF_INET) def test_af_inferred_from_where(self): (af, d, s) = _d_and_s('1::2', 53, None, 0) - self.assertEqual(af, dns.inet.AF_INET6) + self.assertEqual(af, socket.AF_INET6) def test_af_inferred_from_source(self): (af, d, s) = _d_and_s('https://example/dns-query', 443, '1.2.3.4', 0, False) - self.assertEqual(af, dns.inet.AF_INET) + self.assertEqual(af, socket.AF_INET) def test_af_mismatch(self): def bad():