Commit f20e334a authored by Jason Madden's avatar Jason Madden

Fix tests on Python 2, and fix tests with DNSPython.

parent e0da583e
...@@ -9,4 +9,5 @@ use modern Cython idioms. ...@@ -9,4 +9,5 @@ use modern Cython idioms.
A few minor errors and discrepancies were fixed as well, such as A few minor errors and discrepancies were fixed as well, such as
``gethostbyaddr(''localhost')`` working on Python 3 and failing on ``gethostbyaddr(''localhost')`` working on Python 3 and failing on
Python 2. Python 2. The DNSpython resolver now raises the expected TypeError in
more cases instead of an AttributeError.
...@@ -142,9 +142,9 @@ def _ipv6_inet_aton(text, ...@@ -142,9 +142,9 @@ def _ipv6_inet_aton(text,
def _is_addr(host, parse=_ipv4_inet_aton): def _is_addr(host, parse=_ipv4_inet_aton):
if not host: if not host or not isinstance(host, hostname_types):
return False return False
assert isinstance(host, hostname_types), repr(host)
try: try:
parse(host) parse(host)
except AddressSyntaxError: except AddressSyntaxError:
...@@ -158,7 +158,7 @@ is_ipv4_addr = _is_addr ...@@ -158,7 +158,7 @@ is_ipv4_addr = _is_addr
def is_ipv6_addr(host): def is_ipv6_addr(host):
# Return True if host is a valid IPv6 address # Return True if host is a valid IPv6 address
if host: if host and isinstance(host, hostname_types):
s = '%' if isinstance(host, str) else b'%' s = '%' if isinstance(host, str) else b'%'
host = host.split(s, 1)[0] host = host.split(s, 1)[0]
return _is_addr(host, _ipv6_inet_aton) return _is_addr(host, _ipv6_inet_aton)
...@@ -318,7 +318,7 @@ class Resolver(AbstractResolver): ...@@ -318,7 +318,7 @@ class Resolver(AbstractResolver):
raise raise
def _getnameinfo(self, sockaddr, flags): def _getnameinfo(self, sockaddr, flags):
if not isinstance(flags, int): if not isinstance(flags, integer_types):
raise TypeError('an integer is required') raise TypeError('an integer is required')
if not isinstance(sockaddr, tuple): if not isinstance(sockaddr, tuple):
raise TypeError('getnameinfo() argument 1 must be a tuple') raise TypeError('getnameinfo() argument 1 must be a tuple')
...@@ -331,7 +331,7 @@ class Resolver(AbstractResolver): ...@@ -331,7 +331,7 @@ class Resolver(AbstractResolver):
raise TypeError('sockaddr[0] must be a string, not %s' % type(address).__name__) raise TypeError('sockaddr[0] must be a string, not %s' % type(address).__name__)
port = sockaddr[1] port = sockaddr[1]
if not isinstance(port, int): if not isinstance(port, integer_types):
raise TypeError('port must be an integer, not %s' % type(port)) raise TypeError('port must be an integer, not %s' % type(port))
if len(sockaddr) > 2: if len(sockaddr) > 2:
...@@ -340,7 +340,7 @@ class Resolver(AbstractResolver): ...@@ -340,7 +340,7 @@ class Resolver(AbstractResolver):
if flowinfo > 0xfffff: if flowinfo > 0xfffff:
raise OverflowError("getnameinfo(): flowinfo must be 0-1048575.") raise OverflowError("getnameinfo(): flowinfo must be 0-1048575.")
result = self._getaddrinfo(address, str(sockaddr[1]), result = self._getaddrinfo(address, port,
family=AF_UNSPEC, socktype=SOCK_DGRAM, fill_in_type_proto=False) family=AF_UNSPEC, socktype=SOCK_DGRAM, fill_in_type_proto=False)
if len(result) != 1: if len(result) != 1:
raise error('sockaddr resolved to multiple addresses') raise error('sockaddr resolved to multiple addresses')
......
...@@ -393,13 +393,17 @@ class Resolver(AbstractResolver): ...@@ -393,13 +393,17 @@ class Resolver(AbstractResolver):
aliases = self._resolver.hosts_resolver.getaliases(hostname) aliases = self._resolver.hosts_resolver.getaliases(hostname)
net_resolver = self._resolver.network_resolver net_resolver = self._resolver.network_resolver
rdtype = _family_to_rdtype(family) rdtype = _family_to_rdtype(family)
while True: while 1:
try: try:
ans = net_resolver.query(hostname, dns.rdatatype.CNAME, rdtype) ans = net_resolver.query(hostname, dns.rdatatype.CNAME, rdtype)
except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN, dns.resolver.NoNameservers): except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN, dns.resolver.NoNameservers):
break break
except dTimeout: except dTimeout:
break break
except AttributeError as ex:
if hostname is None or isinstance(hostname, int):
raise TypeError(ex)
raise
else: else:
aliases.extend(str(rr.target) for rr in ans.rrset) aliases.extend(str(rr.target) for rr in ans.rrset)
hostname = ans[0].target hostname = ans[0].target
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment