From: Bob Halley Date: Sun, 9 Dec 2018 20:06:24 +0000 (-0800) Subject: Remove _compat module. X-Git-Tag: v2.0.0rc1~392 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=129f9e79ff508e36bac08fd588769525d9214a57;p=thirdparty%2Fdnspython.git Remove _compat module. --- diff --git a/Makefile b/Makefile index 958e684c..d20eed21 100644 --- a/Makefile +++ b/Makefile @@ -60,19 +60,14 @@ tags: check: test test: - cd tests; make PYTHON=${PYTHON} test - -test2: - cd tests; make PYTHON=python test - -test3: cd tests; make PYTHON=${PYTHON3} test -lint: - pylint dns tests examples/*.py +test3: test -lint3: +lint: pylint3 dns tests examples/*.py +lint3: lint + typecheck: if [ $(shell python -c "import sys; print(sys.version_info[0])") -ne 2 ]; then pip install mypy; mypy examples tests; else echo Skipping typecheck on Python 2; fi diff --git a/differences.md b/differences.md new file mode 100644 index 00000000..0896e8fc --- /dev/null +++ b/differences.md @@ -0,0 +1,6 @@ +# Incompatible differences between dnspython 1.x and 2.x + +## Rounding + +dnspython 2.0 rounds in the standard python 3 fashion; dnspython 1.x rounded +in the python 2 style on both python 2 and 3. diff --git a/dns/_compat.py b/dns/_compat.py deleted file mode 100644 index ca0931c2..00000000 --- a/dns/_compat.py +++ /dev/null @@ -1,59 +0,0 @@ -import sys -import decimal -from decimal import Context - -PY3 = sys.version_info[0] == 3 -PY2 = sys.version_info[0] == 2 - - -if PY3: - long = int - xrange = range -else: - long = long # pylint: disable=long-builtin - xrange = xrange # pylint: disable=xrange-builtin - -# unicode / binary types -if PY3: - text_type = str - binary_type = bytes - string_types = (str,) - unichr = chr - def maybe_decode(x): - return x.decode() - def maybe_encode(x): - return x.encode() - def maybe_chr(x): - return x - def maybe_ord(x): - return x -else: - text_type = unicode # pylint: disable=unicode-builtin, undefined-variable - binary_type = str - string_types = ( - basestring, # pylint: disable=basestring-builtin, undefined-variable - ) - unichr = unichr # pylint: disable=unichr-builtin - def maybe_decode(x): - return x - def maybe_encode(x): - return x - def maybe_chr(x): - return chr(x) - def maybe_ord(x): - return ord(x) - - -def round_py2_compat(what): - """ - Python 2 and Python 3 use different rounding strategies in round(). This - function ensures that results are python2/3 compatible and backward - compatible with previous py2 releases - :param what: float - :return: rounded long - """ - d = Context( - prec=len(str(long(what))), # round to integer with max precision - rounding=decimal.ROUND_HALF_UP - ).create_decimal(str(what)) # str(): python 2.6 compat - return long(d) diff --git a/dns/dnssec.py b/dns/dnssec.py index 35da6b5a..d5bd3fe7 100644 --- a/dns/dnssec.py +++ b/dns/dnssec.py @@ -28,8 +28,6 @@ import dns.rdataset import dns.rdata import dns.rdatatype import dns.rdataclass -from ._compat import string_types - class UnsupportedAlgorithm(dns.exception.DNSException): """The DNSSEC algorithm is not supported.""" @@ -172,7 +170,7 @@ def make_ds(name, key, algorithm, origin=None): else: raise UnsupportedAlgorithm('unsupported algorithm "%s"' % algorithm) - if isinstance(name, string_types): + if isinstance(name, str): name = dns.name.from_text(name, origin) hash.update(name.canonicalize().to_wire()) hash.update(_to_rdata(key, origin)) @@ -292,7 +290,7 @@ def _validate_rrsig(rrset, rrsig, keys, origin=None, now=None): in seconds since the UNIX epoch. The default is the current time. """ - if isinstance(origin, string_types): + if isinstance(origin, str): origin = dns.name.from_text(origin, dns.name.root) candidate_keys = _find_candidate_keys(keys, rrsig) @@ -443,7 +441,7 @@ def _validate(rrset, rrsigset, keys, origin=None, now=None): in seconds since the UNIX epoch. The default is the current time. """ - if isinstance(origin, string_types): + if isinstance(origin, str): origin = dns.name.from_text(origin, dns.name.root) if isinstance(rrset, tuple): diff --git a/dns/e164.py b/dns/e164.py index 758c47a7..ab14041d 100644 --- a/dns/e164.py +++ b/dns/e164.py @@ -20,7 +20,6 @@ import dns.exception import dns.name import dns.resolver -from ._compat import string_types, maybe_decode #: The public E.164 domain. public_enum_domain = dns.name.from_text('e164.arpa.') @@ -75,7 +74,7 @@ def to_e164(name, origin=public_enum_domain, want_plus_prefix=True): text = b''.join(dlabels) if want_plus_prefix: text = b'+' + text - return maybe_decode(text) + return text.decode() def query(number, domains, resolver=None): @@ -95,7 +94,7 @@ def query(number, domains, resolver=None): resolver = dns.resolver.get_default_resolver() e_nx = dns.resolver.NXDOMAIN() for domain in domains: - if isinstance(domain, string_types): + if isinstance(domain, str): domain = dns.name.from_text(domain) qname = dns.e164.from_e164(number, domain) try: diff --git a/dns/entropy.py b/dns/entropy.py index 00c6a4b3..3b4194ec 100644 --- a/dns/entropy.py +++ b/dns/entropy.py @@ -18,7 +18,6 @@ import os import random import time -from ._compat import long, binary_type try: import threading as _threading except ImportError: @@ -97,7 +96,7 @@ class EntropyPool(object): try: self._maybe_seed() if self.digest is None or self.next_byte == self.hash_len: - self.hash.update(binary_type(self.pool)) + self.hash.update(bytes(self.pool)) self.digest = bytearray(self.hash.digest()) self.stir(self.digest, True) self.next_byte = 0 @@ -115,11 +114,11 @@ class EntropyPool(object): def random_between(self, first, last): size = last - first + 1 - if size > long(4294967296): + if size > 4294967296: raise ValueError('too big') if size > 65536: rand = self.random_32 - max = long(4294967295) + max = 4294967295 elif size > 256: rand = self.random_16 max = 65535 diff --git a/dns/inet.py b/dns/inet.py index c8d7c1b4..e7729f2f 100644 --- a/dns/inet.py +++ b/dns/inet.py @@ -22,8 +22,6 @@ import socket import dns.ipv4 import dns.ipv6 -from ._compat import maybe_ord - # We assume that AF_INET is always defined. AF_INET = socket.AF_INET @@ -114,11 +112,11 @@ def is_multicast(text): """ try: - first = maybe_ord(dns.ipv4.inet_aton(text)[0]) + first = dns.ipv4.inet_aton(text)[0] return first >= 224 and first <= 239 except Exception: try: - first = maybe_ord(dns.ipv6.inet_aton(text)[0]) + first = dns.ipv6.inet_aton(text)[0] return first == 255 except Exception: raise ValueError diff --git a/dns/ipv4.py b/dns/ipv4.py index 8fc4f7dc..c35775b4 100644 --- a/dns/ipv4.py +++ b/dns/ipv4.py @@ -20,14 +20,13 @@ import struct import dns.exception -from ._compat import binary_type def inet_ntoa(address): """Convert an IPv4 address in binary form to text form. - *address*, a ``binary``, the IPv4 address in binary form. + *address*, a ``bytes``, the IPv4 address in binary form. - Returns a ``text``. + Returns a ``str``. """ if len(address) != 4: @@ -40,12 +39,12 @@ def inet_ntoa(address): def inet_aton(text): """Convert an IPv4 address in text form to binary form. - *text*, a ``text``, the IPv4 address in textual form. + *text*, a ``str``, the IPv4 address in textual form. - Returns a ``binary``. + Returns a ``bytes``. """ - if not isinstance(text, binary_type): + if not isinstance(text, bytes): text = text.encode() parts = text.split(b'.') if len(parts) != 4: @@ -57,7 +56,7 @@ def inet_aton(text): # No leading zeros raise dns.exception.SyntaxError try: - bytes = [int(part) for part in parts] - return struct.pack('BBBB', *bytes) + b = [int(part) for part in parts] + return struct.pack('BBBB', *b) except: raise dns.exception.SyntaxError diff --git a/dns/ipv6.py b/dns/ipv6.py index 128e56c8..e501a41a 100644 --- a/dns/ipv6.py +++ b/dns/ipv6.py @@ -22,17 +22,16 @@ import binascii import dns.exception import dns.ipv4 -from ._compat import xrange, binary_type, maybe_decode _leading_zero = re.compile(r'0+([0-9a-f]+)') def inet_ntoa(address): """Convert an IPv6 address in binary form to text form. - *address*, a ``binary``, the IPv6 address in binary form. + *address*, a ``bytes``, the IPv6 address in binary form. Raises ``ValueError`` if the address isn't 16 bytes long. - Returns a ``text``. + Returns a ``str``. """ if len(address) != 16: @@ -42,7 +41,7 @@ def inet_ntoa(address): i = 0 l = len(hex) while i < l: - chunk = maybe_decode(hex[i : i + 4]) + chunk = hex[i : i + 4].decode() # strip leading zeros. we do this with an re instead of # with lstrip() because lstrip() didn't support chars until # python 2.2.2 @@ -58,7 +57,7 @@ def inet_ntoa(address): best_len = 0 start = -1 last_was_zero = False - for i in xrange(8): + for i in range(8): if chunks[i] != '0': if last_was_zero: end = i @@ -102,13 +101,13 @@ def inet_aton(text): *text*, a ``text``, the IPv6 address in textual form. - Returns a ``binary``. + Returns a ``bytes``. """ # # Our aim here is not something fast; we just want something that works. # - if not isinstance(text, binary_type): + if not isinstance(text, bytes): text = text.encode() if text == b'::': @@ -147,7 +146,7 @@ def inet_aton(text): if seen_empty: raise dns.exception.SyntaxError seen_empty = True - for i in xrange(0, 8 - l + 1): + for i in range(0, 8 - l + 1): canonical.append(b'0000') else: lc = len(c) @@ -173,7 +172,7 @@ _mapped_prefix = b'\x00' * 10 + b'\xff\xff' def is_mapped(address): """Is the specified address a mapped IPv4 address? - *address*, a ``binary`` is an IPv6 address in binary form. + *address*, a ``bytes`` is an IPv6 address in binary form. Returns a ``bool``. """ diff --git a/dns/message.py b/dns/message.py index 9d2b2f43..a5ed5239 100644 --- a/dns/message.py +++ b/dns/message.py @@ -38,8 +38,6 @@ import dns.renderer import dns.tsig import dns.wiredata -from ._compat import long, xrange, string_types - class ShortHeader(dns.exception.FormError): """The DNS packet passed to from_wire() is too short.""" @@ -472,7 +470,7 @@ class Message(object): if keyname is None: self.keyname = list(self.keyring.keys())[0] else: - if isinstance(keyname, string_types): + if isinstance(keyname, str): keyname = dns.name.from_text(keyname) self.keyname = keyname self.keyalgorithm = algorithm @@ -520,7 +518,7 @@ class Message(object): options = [] else: # make sure the EDNS version in ednsflags agrees with edns - ednsflags &= long(0xFF00FFFF) + ednsflags &= 0xFF00FFFF ednsflags |= (edns << 16) if options is None: options = [] @@ -561,7 +559,7 @@ class Message(object): (value, evalue) = dns.rcode.to_flags(rcode) self.flags &= 0xFFF0 self.flags |= value - self.ednsflags &= long(0x00FFFFFF) + self.ednsflags &= 0x00FFFFFF self.ednsflags |= evalue if self.ednsflags != 0 and self.edns < 0: self.edns = 0 @@ -617,7 +615,7 @@ class _WireReader(object): if self.updating and qcount > 1: raise dns.exception.FormError - for i in xrange(0, qcount): + for i in range(0, qcount): (qname, used) = dns.name.from_wire(self.wire, self.current) if self.message.origin is not None: qname = qname.relativize(self.message.origin) @@ -645,7 +643,7 @@ class _WireReader(object): else: force_unique = False seen_opt = False - for i in xrange(0, count): + for i in range(0, count): rr_start = self.current (name, used) = dns.name.from_wire(self.wire, self.current) absolute_name = name @@ -1041,7 +1039,7 @@ def from_file(f): Returns a ``dns.message.Message object`` """ - str_type = string_types + str_type = str opts = 'rU' if isinstance(f, str_type): @@ -1099,11 +1097,11 @@ def make_query(qname, rdtype, rdclass=dns.rdataclass.IN, use_edns=None, Returns a ``dns.message.Message`` """ - if isinstance(qname, string_types): + if isinstance(qname, str): qname = dns.name.from_text(qname) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) - if isinstance(rdclass, string_types): + if isinstance(rdclass, str): rdclass = dns.rdataclass.from_text(rdclass) m = Message() m.flags |= dns.flags.RD diff --git a/dns/name.py b/dns/name.py index 0bcfd834..10167435 100644 --- a/dns/name.py +++ b/dns/name.py @@ -32,8 +32,6 @@ except ImportError: import dns.exception import dns.wiredata -from ._compat import long, binary_type, text_type, unichr, maybe_decode - try: maxint = sys.maxint # pylint: disable=sys-max-int except AttributeError: @@ -122,7 +120,7 @@ class IDNACodec(object): except Exception as e: raise IDNAException(idna_exception=e) else: - label = maybe_decode(label) + label = label.decode() return _escapify(label, True) @@ -248,7 +246,7 @@ def _escapify(label, unicode_mode=False): @rtype: string""" if not unicode_mode: text = '' - if isinstance(label, text_type): + if isinstance(label, str): label = label.encode() for c in bytearray(label): if c in _escaped: @@ -260,7 +258,7 @@ def _escapify(label, unicode_mode=False): return text.encode() text = u'' - if isinstance(label, binary_type): + if isinstance(label, bytes): label = label.decode() for c in label: if c > u'\x20' and c < u'\x7f': @@ -308,9 +306,9 @@ def _maybe_convert_to_binary(label): """ - if isinstance(label, binary_type): + if isinstance(label, bytes): return label - if isinstance(label, text_type): + if isinstance(label, str): return label.encode() raise ValueError @@ -374,11 +372,11 @@ class Name(object): Returns an ``int``. """ - h = long(0) + h = 0 for label in self.labels: for c in bytearray(label.lower()): h += (h << 3) + c - return int(h % maxint) + return h % maxint def fullcompare(self, other): """Compare two names, returning a 3-tuple @@ -544,15 +542,15 @@ class Name(object): """ if len(self.labels) == 0: - return maybe_decode(b'@') + return '@' if len(self.labels) == 1 and self.labels[0] == b'': - return maybe_decode(b'.') + return '.' if omit_final_dot and self.is_absolute(): l = self.labels[:-1] else: l = self.labels s = b'.'.join(map(_escapify, l)) - return maybe_decode(s) + return s.decode() def to_unicode(self, omit_final_dot=False, idna_codec=None): """Convert name to Unicode text format. @@ -813,7 +811,7 @@ def from_unicode(text, origin=root, idna_codec=None): Returns a ``dns.name.Name``. """ - if not isinstance(text, text_type): + if not isinstance(text, str): raise ValueError("input to from_unicode() must be a unicode string") if not (origin is None or isinstance(origin, Name)): raise ValueError("origin must be a Name or None") @@ -846,7 +844,7 @@ def from_unicode(text, origin=root, idna_codec=None): edigits += 1 if edigits == 3: escaping = False - label += unichr(total) + label += chr(total) elif c in [u'.', u'\u3002', u'\uff0e', u'\uff61']: if len(label) == 0: raise EmptyLabel @@ -885,9 +883,9 @@ def from_text(text, origin=root, idna_codec=None): Returns a ``dns.name.Name``. """ - if isinstance(text, text_type): + if isinstance(text, str): return from_unicode(text, origin, idna_codec) - if not isinstance(text, binary_type): + if not isinstance(text, bytes): raise ValueError("input to from_text() must be a string") if not (origin is None or isinstance(origin, Name)): raise ValueError("origin must be a Name or None") @@ -961,7 +959,7 @@ def from_wire(message, current): which were consumed reading it. """ - if not isinstance(message, binary_type): + if not isinstance(message, bytes): raise ValueError("input to from_wire() must be a byte string") message = dns.wiredata.maybe_wrap(message) labels = [] diff --git a/dns/namedict.py b/dns/namedict.py index 37a13104..f44cf6f9 100644 --- a/dns/namedict.py +++ b/dns/namedict.py @@ -29,7 +29,6 @@ import collections import dns.name -from ._compat import xrange class NameDict(collections.MutableMapping): @@ -100,7 +99,7 @@ class NameDict(collections.MutableMapping): depth = len(name) if depth > self.max_depth: depth = self.max_depth - for i in xrange(-depth, 0): + for i in range(-depth, 0): n = dns.name.Name(name[i:]) if n in self: return (n, self[n]) diff --git a/dns/query.py b/dns/query.py index c0c517cc..20c953cb 100644 --- a/dns/query.py +++ b/dns/query.py @@ -33,12 +33,6 @@ import dns.message import dns.rcode import dns.rdataclass import dns.rdatatype -from ._compat import long, string_types, PY3 - -if PY3: - select_error = OSError -else: - select_error = select.error # Function used to create a socket. Can be overridden if needed in special # situations. @@ -87,7 +81,7 @@ def _poll_for(fd, readable, writable, error, timeout): pollable.register(fd, event_mask) if timeout: - event_list = pollable.poll(long(timeout * 1000)) + event_list = pollable.poll(timeout * 1000) else: event_list = pollable.poll() @@ -130,7 +124,7 @@ def _wait_for(fd, readable, writable, error, expiration): try: if not _polling_backend(fd, readable, writable, error, timeout): raise dns.exception.Timeout - except select_error as e: + except OSError as e: if e.args[0] != errno.EINTR: raise e done = True @@ -562,9 +556,9 @@ def xfr(where, zone, rdtype=dns.rdatatype.AXFR, rdclass=dns.rdataclass.IN, Returns a generator of ``dns.message.Message`` objects. """ - if isinstance(zone, string_types): + if isinstance(zone, str): zone = dns.name.from_text(zone) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) q = dns.message.make_query(zone, rdtype, rdclass) if rdtype == dns.rdatatype.IXFR: diff --git a/dns/rcode.py b/dns/rcode.py index 5191e1b1..2215bef1 100644 --- a/dns/rcode.py +++ b/dns/rcode.py @@ -18,7 +18,6 @@ """DNS Result Codes.""" import dns.exception -from ._compat import long #: No error NOERROR = 0 @@ -122,7 +121,7 @@ def to_flags(value): if value < 0 or value > 4095: raise ValueError('rcode must be >= 0 and <= 4095') v = value & 0xf - ev = long(value & 0xff0) << 20 + ev = (value & 0xff0) << 20 return (v, ev) diff --git a/dns/rdata.py b/dns/rdata.py index ea1971dc..01930a4f 100644 --- a/dns/rdata.py +++ b/dns/rdata.py @@ -27,7 +27,6 @@ import dns.rdataclass import dns.rdatatype import dns.tokenizer import dns.wiredata -from ._compat import xrange, string_types, text_type try: import threading as _threading @@ -65,7 +64,7 @@ __escaped = bytearray(b'"\\') def _escapify(qstring): """Escape the characters in a quoted string which need it.""" - if isinstance(qstring, text_type): + if isinstance(qstring, str): qstring = qstring.encode() if not isinstance(qstring, bytearray): qstring = bytearray(qstring) @@ -86,7 +85,7 @@ def _truncate_bitmap(what): return the bitmap that contains all the bytes less than that index. """ - for i in xrange(len(what) - 1, -1, -1): + for i in range(len(what) - 1, -1, -1): if what[i] != 0: return what[0: i + 1] return what[0:1] @@ -370,7 +369,7 @@ def from_text(rdclass, rdtype, tok, origin=None, relativize=True): Returns an instance of the chosen Rdata subclass. """ - if isinstance(tok, string_types): + if isinstance(tok, str): tok = dns.tokenizer.Tokenizer(tok) cls = get_rdata_class(rdclass, rdtype) if cls != GenericRdata: diff --git a/dns/rdataset.py b/dns/rdataset.py index f1afe241..7f4339c0 100644 --- a/dns/rdataset.py +++ b/dns/rdataset.py @@ -26,7 +26,6 @@ import dns.rdatatype import dns.rdataclass import dns.rdata import dns.set -from ._compat import string_types # define SimpleSet here for backwards compatibility SimpleSet = dns.set.Set @@ -297,9 +296,9 @@ def from_text_list(rdclass, rdtype, ttl, text_rdatas): Returns a ``dns.rdataset.Rdataset`` object. """ - if isinstance(rdclass, string_types): + if isinstance(rdclass, str): rdclass = dns.rdataclass.from_text(rdclass) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) r = Rdataset(rdclass, rdtype) r.update_ttl(ttl) diff --git a/dns/rdtypes/ANY/CSYNC.py b/dns/rdtypes/ANY/CSYNC.py index 06292fb2..878befac 100644 --- a/dns/rdtypes/ANY/CSYNC.py +++ b/dns/rdtypes/ANY/CSYNC.py @@ -21,7 +21,6 @@ import dns.exception import dns.rdata import dns.rdatatype import dns.name -from dns._compat import xrange class CSYNC(dns.rdata.Rdata): @@ -46,9 +45,8 @@ class CSYNC(dns.rdata.Rdata): text = '' for (window, bitmap) in self.windows: bits = [] - for i in xrange(0, len(bitmap)): - byte = bitmap[i] - for j in xrange(0, 8): + for (i, byte) in enumerate(bitmap): + for j in range(0, 8): if byte & (0x80 >> j): bits.append(dns.rdatatype.to_text(window * 256 + i * 8 + j)) diff --git a/dns/rdtypes/ANY/GPOS.py b/dns/rdtypes/ANY/GPOS.py index 422822f0..2448420c 100644 --- a/dns/rdtypes/ANY/GPOS.py +++ b/dns/rdtypes/ANY/GPOS.py @@ -20,7 +20,6 @@ import struct import dns.exception import dns.rdata import dns.tokenizer -from dns._compat import long, text_type def _validate_float_string(what): @@ -38,7 +37,7 @@ def _validate_float_string(what): def _sanitize(value): - if isinstance(value, text_type): + if isinstance(value, str): return value.encode() return value @@ -60,16 +59,13 @@ class GPOS(dns.rdata.Rdata): def __init__(self, rdclass, rdtype, latitude, longitude, altitude): super(GPOS, self).__init__(rdclass, rdtype) if isinstance(latitude, float) or \ - isinstance(latitude, int) or \ - isinstance(latitude, long): + isinstance(latitude, int): latitude = str(latitude) if isinstance(longitude, float) or \ - isinstance(longitude, int) or \ - isinstance(longitude, long): + isinstance(longitude, int): longitude = str(longitude) if isinstance(altitude, float) or \ - isinstance(altitude, int) or \ - isinstance(altitude, long): + isinstance(altitude, int): altitude = str(altitude) latitude = _sanitize(latitude) longitude = _sanitize(longitude) diff --git a/dns/rdtypes/ANY/HINFO.py b/dns/rdtypes/ANY/HINFO.py index e4e0b34a..7bf8edd3 100644 --- a/dns/rdtypes/ANY/HINFO.py +++ b/dns/rdtypes/ANY/HINFO.py @@ -20,7 +20,6 @@ import struct import dns.exception import dns.rdata import dns.tokenizer -from dns._compat import text_type class HINFO(dns.rdata.Rdata): @@ -37,11 +36,11 @@ class HINFO(dns.rdata.Rdata): def __init__(self, rdclass, rdtype, cpu, os): super(HINFO, self).__init__(rdclass, rdtype) - if isinstance(cpu, text_type): + if isinstance(cpu, str): self.cpu = cpu.encode() else: self.cpu = cpu - if isinstance(os, text_type): + if isinstance(os, str): self.os = os.encode() else: self.os = os diff --git a/dns/rdtypes/ANY/ISDN.py b/dns/rdtypes/ANY/ISDN.py index f5f5f8b9..4d1b8662 100644 --- a/dns/rdtypes/ANY/ISDN.py +++ b/dns/rdtypes/ANY/ISDN.py @@ -20,7 +20,6 @@ import struct import dns.exception import dns.rdata import dns.tokenizer -from dns._compat import text_type class ISDN(dns.rdata.Rdata): @@ -37,11 +36,11 @@ class ISDN(dns.rdata.Rdata): def __init__(self, rdclass, rdtype, address, subaddress): super(ISDN, self).__init__(rdclass, rdtype) - if isinstance(address, text_type): + if isinstance(address, str): self.address = address.encode() else: self.address = address - if isinstance(address, text_type): + if isinstance(address, str): self.subaddress = subaddress.encode() else: self.subaddress = subaddress diff --git a/dns/rdtypes/ANY/LOC.py b/dns/rdtypes/ANY/LOC.py index da9bb03a..10455131 100644 --- a/dns/rdtypes/ANY/LOC.py +++ b/dns/rdtypes/ANY/LOC.py @@ -21,10 +21,9 @@ import struct import dns.exception import dns.rdata -from dns._compat import long, xrange, round_py2_compat -_pows = tuple(long(10**i) for i in range(0, 11)) +_pows = tuple(10**i for i in range(0, 11)) # default values are in centimeters _default_size = 100.0 @@ -36,8 +35,8 @@ def _exponent_of(what, desc): if what == 0: return 0 exp = None - for i in xrange(len(_pows)): - if what // _pows[i] == long(0): + for (i, pow) in enumerate(_pows): + if what // pow == 0: exp = i - 1 break if exp is None or exp < 0: @@ -51,7 +50,7 @@ def _float_to_tuple(what): what *= -1 else: sign = 1 - what = round_py2_compat(what * 3600000) + what = round(what * 3600000) # pylint: disable=round-builtin degrees = int(what // 3600000) what -= degrees * 3600000 minutes = int(what // 60000) @@ -71,7 +70,7 @@ def _tuple_to_float(what): def _encode_size(what, desc): - what = long(what) + what = int(what) exponent = _exponent_of(what, desc) & 0xF base = what // pow(10, exponent) & 0xF return base * 16 + exponent @@ -84,7 +83,7 @@ def _decode_size(what, desc): base = (what & 0xF0) >> 4 if base > 9: raise dns.exception.SyntaxError("bad %s base" % desc) - return long(base) * pow(10, exponent) + return base * pow(10, exponent) class LOC(dns.rdata.Rdata): @@ -122,12 +121,12 @@ class LOC(dns.rdata.Rdata): and vertical precision are specified in centimeters.""" super(LOC, self).__init__(rdclass, rdtype) - if isinstance(latitude, int) or isinstance(latitude, long): + if isinstance(latitude, int): latitude = float(latitude) if isinstance(latitude, float): latitude = _float_to_tuple(latitude) self.latitude = latitude - if isinstance(longitude, int) or isinstance(longitude, long): + if isinstance(longitude, int): longitude = float(longitude) if isinstance(longitude, float): longitude = _float_to_tuple(longitude) @@ -271,13 +270,13 @@ class LOC(dns.rdata.Rdata): self.latitude[1] * 60000 + self.latitude[2] * 1000 + self.latitude[3]) * self.latitude[4] - latitude = long(0x80000000) + milliseconds + latitude = 0x80000000 + milliseconds milliseconds = (self.longitude[0] * 3600000 + self.longitude[1] * 60000 + self.longitude[2] * 1000 + self.longitude[3]) * self.longitude[4] - longitude = long(0x80000000) + milliseconds - altitude = long(self.altitude) + long(10000000) + longitude = 0x80000000 + milliseconds + altitude = int(self.altitude) + 10000000 size = _encode_size(self.size, "size") hprec = _encode_size(self.horizontal_precision, "horizontal precision") vprec = _encode_size(self.vertical_precision, "vertical precision") @@ -289,16 +288,16 @@ class LOC(dns.rdata.Rdata): def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): (version, size, hprec, vprec, latitude, longitude, altitude) = \ struct.unpack("!BBBBIII", wire[current: current + rdlen]) - if latitude > long(0x80000000): - latitude = float(latitude - long(0x80000000)) / 3600000 + if latitude > 0x80000000: + latitude = float(latitude - 0x80000000) / 3600000 else: - latitude = -1 * float(long(0x80000000) - latitude) / 3600000 + latitude = -1 * float(0x80000000 - latitude) / 3600000 if latitude < -90.0 or latitude > 90.0: raise dns.exception.FormError("bad latitude") - if longitude > long(0x80000000): - longitude = float(longitude - long(0x80000000)) / 3600000 + if longitude > 0x80000000: + longitude = float(longitude - 0x80000000) / 3600000 else: - longitude = -1 * float(long(0x80000000) - longitude) / 3600000 + longitude = -1 * float(0x80000000 - longitude) / 3600000 if longitude < -180.0 or longitude > 180.0: raise dns.exception.FormError("bad longitude") altitude = float(altitude) - 10000000.0 diff --git a/dns/rdtypes/ANY/NSEC.py b/dns/rdtypes/ANY/NSEC.py index 4e3da729..a138d4d2 100644 --- a/dns/rdtypes/ANY/NSEC.py +++ b/dns/rdtypes/ANY/NSEC.py @@ -21,7 +21,6 @@ import dns.exception import dns.rdata import dns.rdatatype import dns.name -from dns._compat import xrange class NSEC(dns.rdata.Rdata): @@ -45,9 +44,8 @@ class NSEC(dns.rdata.Rdata): text = '' for (window, bitmap) in self.windows: bits = [] - for i in xrange(0, len(bitmap)): - byte = bitmap[i] - for j in xrange(0, 8): + for (i, byte) in enumerate(bitmap): + for j in range(0, 8): if byte & (0x80 >> j): bits.append(dns.rdatatype.to_text(window * 256 + i * 8 + j)) diff --git a/dns/rdtypes/ANY/NSEC3.py b/dns/rdtypes/ANY/NSEC3.py index 1c281c4a..e33cc7c3 100644 --- a/dns/rdtypes/ANY/NSEC3.py +++ b/dns/rdtypes/ANY/NSEC3.py @@ -17,28 +17,18 @@ import base64 import binascii -import string import struct import dns.exception import dns.rdata import dns.rdatatype -from dns._compat import xrange, text_type, PY3 - -# pylint: disable=deprecated-string-function -if PY3: - b32_hex_to_normal = bytes.maketrans(b'0123456789ABCDEFGHIJKLMNOPQRSTUV', - b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567') - b32_normal_to_hex = bytes.maketrans(b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', - b'0123456789ABCDEFGHIJKLMNOPQRSTUV') -else: - b32_hex_to_normal = string.maketrans('0123456789ABCDEFGHIJKLMNOPQRSTUV', - 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567') - b32_normal_to_hex = string.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', - '0123456789ABCDEFGHIJKLMNOPQRSTUV') -# pylint: enable=deprecated-string-function +b32_hex_to_normal = bytes.maketrans(b'0123456789ABCDEFGHIJKLMNOPQRSTUV', + b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567') +b32_normal_to_hex = bytes.maketrans(b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', + b'0123456789ABCDEFGHIJKLMNOPQRSTUV') + # hash algorithm constants SHA1 = 1 @@ -71,7 +61,7 @@ class NSEC3(dns.rdata.Rdata): self.algorithm = algorithm self.flags = flags self.iterations = iterations - if isinstance(salt, text_type): + if isinstance(salt, str): self.salt = salt.encode() else: self.salt = salt @@ -88,9 +78,8 @@ class NSEC3(dns.rdata.Rdata): text = u'' for (window, bitmap) in self.windows: bits = [] - for i in xrange(0, len(bitmap)): - byte = bitmap[i] - for j in xrange(0, 8): + for (i, byte) in enumerate(bitmap): + for j in range(0, 8): if byte & (0x80 >> j): bits.append(dns.rdatatype.to_text(window * 256 + i * 8 + j)) diff --git a/dns/rdtypes/ANY/NSEC3PARAM.py b/dns/rdtypes/ANY/NSEC3PARAM.py index 87c36e56..356fff46 100644 --- a/dns/rdtypes/ANY/NSEC3PARAM.py +++ b/dns/rdtypes/ANY/NSEC3PARAM.py @@ -20,7 +20,6 @@ import binascii import dns.exception import dns.rdata -from dns._compat import text_type class NSEC3PARAM(dns.rdata.Rdata): @@ -43,7 +42,7 @@ class NSEC3PARAM(dns.rdata.Rdata): self.algorithm = algorithm self.flags = flags self.iterations = iterations - if isinstance(salt, text_type): + if isinstance(salt, str): self.salt = salt.encode() else: self.salt = salt diff --git a/dns/rdtypes/ANY/URI.py b/dns/rdtypes/ANY/URI.py index f5b65ed6..4c6569ce 100644 --- a/dns/rdtypes/ANY/URI.py +++ b/dns/rdtypes/ANY/URI.py @@ -21,7 +21,6 @@ import struct import dns.exception import dns.rdata import dns.name -from dns._compat import text_type class URI(dns.rdata.Rdata): @@ -44,7 +43,7 @@ class URI(dns.rdata.Rdata): self.weight = weight if len(target) < 1: raise dns.exception.SyntaxError("URI target cannot be empty") - if isinstance(target, text_type): + if isinstance(target, str): self.target = target.encode() else: self.target = target diff --git a/dns/rdtypes/ANY/X25.py b/dns/rdtypes/ANY/X25.py index e530a2c2..1323ba4d 100644 --- a/dns/rdtypes/ANY/X25.py +++ b/dns/rdtypes/ANY/X25.py @@ -20,7 +20,6 @@ import struct import dns.exception import dns.rdata import dns.tokenizer -from dns._compat import text_type class X25(dns.rdata.Rdata): @@ -35,7 +34,7 @@ class X25(dns.rdata.Rdata): def __init__(self, rdclass, rdtype, address): super(X25, self).__init__(rdclass, rdtype) - if isinstance(address, text_type): + if isinstance(address, str): self.address = address.encode() else: self.address = address diff --git a/dns/rdtypes/IN/APL.py b/dns/rdtypes/IN/APL.py index 48faf88a..56489086 100644 --- a/dns/rdtypes/IN/APL.py +++ b/dns/rdtypes/IN/APL.py @@ -23,8 +23,6 @@ import dns.exception import dns.inet import dns.rdata import dns.tokenizer -from dns._compat import xrange, maybe_chr - class APLItem(object): @@ -65,8 +63,8 @@ class APLItem(object): # Truncate least significant zero bytes. # last = 0 - for i in xrange(len(address) - 1, -1, -1): - if address[i] != maybe_chr(0): + for i in range(len(address) - 1, -1, -1): + if address[i] != 0: last = i + 1 break address = address[0: last] diff --git a/dns/rdtypes/IN/NAPTR.py b/dns/rdtypes/IN/NAPTR.py index 32fa4745..79e5e5f3 100644 --- a/dns/rdtypes/IN/NAPTR.py +++ b/dns/rdtypes/IN/NAPTR.py @@ -20,7 +20,6 @@ import struct import dns.exception import dns.name import dns.rdata -from dns._compat import xrange, text_type def _write_string(file, s): @@ -31,7 +30,7 @@ def _write_string(file, s): def _sanitize(value): - if isinstance(value, text_type): + if isinstance(value, str): return value.encode() return value @@ -103,7 +102,7 @@ class NAPTR(dns.rdata.Rdata): current += 4 rdlen -= 4 strings = [] - for i in xrange(3): + for i in range(3): l = wire[current] current += 1 rdlen -= 1 diff --git a/dns/rdtypes/IN/WKS.py b/dns/rdtypes/IN/WKS.py index 96f98ada..c3a47985 100644 --- a/dns/rdtypes/IN/WKS.py +++ b/dns/rdtypes/IN/WKS.py @@ -20,7 +20,6 @@ import struct import dns.ipv4 import dns.rdata -from dns._compat import xrange _proto_tcp = socket.getprotobyname('tcp') _proto_udp = socket.getprotobyname('udp') @@ -51,9 +50,9 @@ class WKS(dns.rdata.Rdata): def to_text(self, origin=None, relativize=True, **kw): bits = [] - for i in xrange(0, len(self.bitmap)): + for i in range(0, len(self.bitmap)): byte = self.bitmap[i] - for j in xrange(0, 8): + for j in range(0, 8): if byte & (0x80 >> j): bits.append(str(i * 8 + j)) text = ' '.join(bits) @@ -85,7 +84,7 @@ class WKS(dns.rdata.Rdata): i = serv // 8 l = len(bitmap) if l < i + 1: - for j in xrange(l, i + 1): + for j in range(l, i + 1): bitmap.append(0) bitmap[i] = bitmap[i] | (0x80 >> (serv % 8)) bitmap = dns.rdata._truncate_bitmap(bitmap) diff --git a/dns/rdtypes/euibase.py b/dns/rdtypes/euibase.py index cc5fdaa6..4eaecbf8 100644 --- a/dns/rdtypes/euibase.py +++ b/dns/rdtypes/euibase.py @@ -17,7 +17,6 @@ import binascii import dns.rdata -from dns._compat import xrange class EUIBase(dns.rdata.Rdata): @@ -50,8 +49,7 @@ class EUIBase(dns.rdata.Rdata): if len(text) != cls.text_len: raise dns.exception.SyntaxError( 'Input text must have %s characters' % cls.text_len) - expected_dash_idxs = xrange(2, cls.byte_len * 3 - 1, 3) - for i in expected_dash_idxs: + for i in range(2, cls.byte_len * 3 - 1, 3): if text[i] != '-': raise dns.exception.SyntaxError('Dash expected at position %s' % i) diff --git a/dns/rdtypes/txtbase.py b/dns/rdtypes/txtbase.py index 645a57ec..fd858713 100644 --- a/dns/rdtypes/txtbase.py +++ b/dns/rdtypes/txtbase.py @@ -22,7 +22,6 @@ import struct import dns.exception import dns.rdata import dns.tokenizer -from dns._compat import binary_type, string_types class TXTBase(dns.rdata.Rdata): @@ -37,12 +36,12 @@ class TXTBase(dns.rdata.Rdata): def __init__(self, rdclass, rdtype, strings): super(TXTBase, self).__init__(rdclass, rdtype) - if isinstance(strings, binary_type) or \ - isinstance(strings, string_types): + if isinstance(strings, bytes) or \ + isinstance(strings, str): strings = [strings] self.strings = [] for string in strings: - if isinstance(string, string_types): + if isinstance(string, str): string = string.encode() self.strings.append(string) @@ -66,7 +65,7 @@ class TXTBase(dns.rdata.Rdata): if len(token.value) > 255: raise dns.exception.SyntaxError("string too long") value = token.value - if isinstance(value, binary_type): + if isinstance(value, bytes): strings.append(value) else: strings.append(value.encode()) diff --git a/dns/renderer.py b/dns/renderer.py index d7ef8c7f..2325e775 100644 --- a/dns/renderer.py +++ b/dns/renderer.py @@ -24,7 +24,6 @@ import time import dns.exception import dns.tsig -from ._compat import long QUESTION = 0 @@ -172,7 +171,7 @@ class Renderer(object): """Add an EDNS OPT record to the message.""" # make sure the EDNS version in ednsflags agrees with edns - ednsflags &= long(0xFF00FFFF) + ednsflags &= 0xFF00FFFF ednsflags |= (edns << 16) self._set_section(ADDITIONAL) before = self.output.tell() diff --git a/dns/resolver.py b/dns/resolver.py index 806e5b2b..74828bd1 100644 --- a/dns/resolver.py +++ b/dns/resolver.py @@ -21,7 +21,6 @@ import socket import sys import time import random - try: import threading as _threading except ImportError: @@ -39,7 +38,6 @@ import dns.rdataclass import dns.rdatatype import dns.reversename import dns.tsig -from ._compat import xrange, string_types if sys.platform == 'win32': try: @@ -207,7 +205,7 @@ class Answer(object): self.response = response min_ttl = -1 rrset = None - for count in xrange(0, 15): + for count in range(0, 15): try: rrset = response.find_rrset(response.answer, qname, rdclass, rdtype) @@ -573,7 +571,7 @@ class Resolver(object): a ``text``, it is used as the name of the file to open; otherwise it is treated as the file itself.""" - if isinstance(f, string_types): + if isinstance(f, str): try: f = open(f, 'r') except IOError: @@ -844,13 +842,13 @@ class Resolver(object): Returns a ``dns.resolver.Answer`` instance. """ - if isinstance(qname, string_types): + if isinstance(qname, str): qname = dns.name.from_text(qname, None) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) if dns.rdatatype.is_metatype(rdtype): raise NoMetaqueries - if isinstance(rdclass, string_types): + if isinstance(rdclass, str): rdclass = dns.rdataclass.from_text(rdclass) if dns.rdataclass.is_metaclass(rdclass): raise NoMetaqueries @@ -1121,7 +1119,7 @@ def zone_for_name(name, rdclass=dns.rdataclass.IN, tcp=False, resolver=None): Returns a ``dns.name.Name``. """ - if isinstance(name, string_types): + if isinstance(name, str): name = dns.name.from_text(name, dns.name.root) if resolver is None: resolver = get_default_resolver() diff --git a/dns/reversename.py b/dns/reversename.py index 8f095fa9..60618cc9 100644 --- a/dns/reversename.py +++ b/dns/reversename.py @@ -23,8 +23,6 @@ import dns.name import dns.ipv6 import dns.ipv4 -from dns._compat import PY3 - ipv4_reverse_domain = dns.name.from_text('in-addr.arpa.') ipv6_reverse_domain = dns.name.from_text('ip6.arpa.') @@ -44,10 +42,7 @@ def from_address(text): try: v6 = dns.ipv6.inet_aton(text) if dns.ipv6.is_mapped(v6): - if PY3: - parts = ['%d' % byte for byte in v6[12:]] - else: - parts = ['%d' % ord(byte) for byte in v6[12:]] + parts = ['%d' % byte for byte in v6[12:]] origin = ipv4_reverse_domain else: parts = [x for x in str(binascii.hexlify(v6).decode())] diff --git a/dns/rrset.py b/dns/rrset.py index a53ec324..8be43396 100644 --- a/dns/rrset.py +++ b/dns/rrset.py @@ -22,7 +22,6 @@ import dns.name import dns.rdataset import dns.rdataclass import dns.renderer -from ._compat import string_types class RRset(dns.rdataset.Rdataset): @@ -134,11 +133,11 @@ def from_text_list(name, ttl, rdclass, rdtype, text_rdatas, Returns a ``dns.rrset.RRset`` object. """ - if isinstance(name, string_types): + if isinstance(name, str): name = dns.name.from_text(name, None, idna_codec=idna_codec) - if isinstance(rdclass, string_types): + if isinstance(rdclass, str): rdclass = dns.rdataclass.from_text(rdclass) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) r = RRset(name, rdclass, rdtype) r.update_ttl(ttl) @@ -165,7 +164,7 @@ def from_rdata_list(name, ttl, rdatas, idna_codec=None): Returns a ``dns.rrset.RRset`` object. """ - if isinstance(name, string_types): + if isinstance(name, str): name = dns.name.from_text(name, None, idna_codec=idna_codec) if len(rdatas) == 0: diff --git a/dns/tokenizer.py b/dns/tokenizer.py index 880b71ce..74674a65 100644 --- a/dns/tokenizer.py +++ b/dns/tokenizer.py @@ -23,7 +23,6 @@ import sys import dns.exception import dns.name import dns.ttl -from ._compat import long, text_type, binary_type _DELIMITERS = { ' ': True, @@ -189,11 +188,11 @@ class Tokenizer(object): will return. """ - if isinstance(f, text_type): + if isinstance(f, str): f = StringIO(f) if filename is None: filename = '' - elif isinstance(f, binary_type): + elif isinstance(f, bytes): f = StringIO(f.decode()) if filename is None: filename = '' @@ -497,8 +496,8 @@ class Tokenizer(object): raise dns.exception.SyntaxError('expecting an identifier') if not token.value.isdigit(): raise dns.exception.SyntaxError('expecting an integer') - value = long(token.value) - if value < 0 or value > long(4294967296): + value = int(token.value) + if value < 0 or value > 4294967296: raise dns.exception.SyntaxError( '%d is not an unsigned 32-bit integer' % value) return value diff --git a/dns/tsig.py b/dns/tsig.py index 3daa3878..25171620 100644 --- a/dns/tsig.py +++ b/dns/tsig.py @@ -24,7 +24,6 @@ import struct import dns.exception import dns.rdataclass import dns.name -from ._compat import long, string_types, text_type class BadTime(dns.exception.DNSException): @@ -97,7 +96,7 @@ def sign(wire, keyname, secret, time, fudge, original_id, error, @raises NotImplementedError: I{algorithm} is not supported """ - if isinstance(other_data, text_type): + if isinstance(other_data, str): other_data = other_data.encode() (algorithm_name, digestmod) = get_algorithm(algorithm) if first: @@ -113,9 +112,8 @@ def sign(wire, keyname, secret, time, fudge, original_id, error, ctx.update(keyname.to_digestable()) ctx.update(struct.pack('!H', dns.rdataclass.ANY)) ctx.update(struct.pack('!I', 0)) - long_time = time + long(0) - upper_time = (long_time >> 32) & long(0xffff) - lower_time = long_time & long(0xffffffff) + upper_time = (time >> 32) & 0xffff + lower_time = time & 0xffffffff time_mac = struct.pack('!HIH', upper_time, lower_time, fudge) pre_mac = algorithm_name + time_mac ol = len(other_data) @@ -167,7 +165,7 @@ def validate(wire, keyname, secret, now, request_mac, tsig_start, tsig_rdata, current = current + used (upper_time, lower_time, fudge, mac_size) = \ struct.unpack("!HIHH", wire[current:current + 10]) - time = ((upper_time + long(0)) << 32) + (lower_time + long(0)) + time = (upper_time << 32) + lower_time current += 10 mac = wire[current:current + mac_size] current += mac_size @@ -209,7 +207,7 @@ def get_algorithm(algorithm): @raises NotImplementedError: I{algorithm} is not supported """ - if isinstance(algorithm, string_types): + if isinstance(algorithm, str): algorithm = dns.name.from_text(algorithm) try: diff --git a/dns/tsigkeyring.py b/dns/tsigkeyring.py index 5e5fe1cb..db8c59fa 100644 --- a/dns/tsigkeyring.py +++ b/dns/tsigkeyring.py @@ -17,8 +17,6 @@ """A place to store TSIG keys.""" -from dns._compat import maybe_decode, maybe_encode - import base64 import dns.name @@ -32,7 +30,7 @@ def from_text(textring): keyring = {} for keytext in textring: keyname = dns.name.from_text(keytext) - secret = base64.decodestring(maybe_encode(textring[keytext])) + secret = base64.decodestring(textring[keytext].encode()) keyring[keyname] = secret return keyring @@ -44,7 +42,7 @@ def to_text(keyring): textring = {} for keyname in keyring: - keytext = maybe_decode(keyname.to_text()) - secret = maybe_decode(base64.encodestring(keyring[keyname])) + keytext = keyname.to_text().decode() + secret = base64.encodestring(keyring[keyname]).decode() textring[keytext] = secret return textring diff --git a/dns/ttl.py b/dns/ttl.py index 4be16bee..2b1ced0a 100644 --- a/dns/ttl.py +++ b/dns/ttl.py @@ -18,7 +18,6 @@ """DNS TTL conversion.""" import dns.exception -from ._compat import long class BadTTL(dns.exception.SyntaxError): @@ -38,26 +37,26 @@ def from_text(text): """ if text.isdigit(): - total = long(text) + total = int(text) else: if not text[0].isdigit(): raise BadTTL - total = long(0) - current = long(0) + total = 0 + current = 0 for c in text: if c.isdigit(): current *= 10 - current += long(c) + current += int(c) else: c = c.lower() if c == 'w': - total += current * long(604800) + total += current * 604800 elif c == 'd': - total += current * long(86400) + total += current * 86400 elif c == 'h': - total += current * long(3600) + total += current * 3600 elif c == 'm': - total += current * long(60) + total += current * 60 elif c == 's': total += current else: @@ -65,6 +64,6 @@ def from_text(text): current = 0 if not current == 0: raise BadTTL("trailing integer") - if total < long(0) or total > long(2147483647): + if total < 0 or total > 2147483647: raise BadTTL("TTL should be between 0 and 2^31 - 1 (inclusive)") return total diff --git a/dns/update.py b/dns/update.py index 96a00d5d..9e591ae3 100644 --- a/dns/update.py +++ b/dns/update.py @@ -25,7 +25,6 @@ import dns.rdata import dns.rdataclass import dns.rdataset import dns.tsig -from ._compat import string_types class Update(dns.message.Message): @@ -56,10 +55,10 @@ class Update(dns.message.Message): """ super(Update, self).__init__() self.flags |= dns.opcode.to_flags(dns.opcode.UPDATE) - if isinstance(zone, string_types): + if isinstance(zone, str): zone = dns.name.from_text(zone) self.origin = zone - if isinstance(rdclass, string_types): + if isinstance(rdclass, str): rdclass = dns.rdataclass.from_text(rdclass) self.zone_rdclass = rdclass self.find_rrset(self.question, self.origin, rdclass, dns.rdatatype.SOA, @@ -93,7 +92,7 @@ class Update(dns.message.Message): - ttl, rdtype, string... """ - if isinstance(name, string_types): + if isinstance(name, str): name = dns.name.from_text(name, None) if isinstance(args[0], dns.rdataset.Rdataset): for rds in args: @@ -111,7 +110,7 @@ class Update(dns.message.Message): self._add_rr(name, ttl, rd, section=section) else: rdtype = args.pop(0) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) if replace: self.delete(name, rdtype) @@ -150,7 +149,7 @@ class Update(dns.message.Message): - rdtype, [string...] """ - if isinstance(name, string_types): + if isinstance(name, str): name = dns.name.from_text(name, None) if len(args) == 0: self.find_rrset(self.authority, name, dns.rdataclass.ANY, @@ -167,7 +166,7 @@ class Update(dns.message.Message): self._add_rr(name, 0, rd, dns.rdataclass.NONE) else: rdtype = args.pop(0) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) if len(args) == 0: self.find_rrset(self.authority, name, @@ -214,7 +213,7 @@ class Update(dns.message.Message): - rdtype, string... """ - if isinstance(name, string_types): + if isinstance(name, str): name = dns.name.from_text(name, None) if len(args) == 0: self.find_rrset(self.answer, name, @@ -231,7 +230,7 @@ class Update(dns.message.Message): self._add(False, self.answer, name, *args) else: rdtype = args[0] - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) self.find_rrset(self.answer, name, dns.rdataclass.ANY, rdtype, @@ -242,7 +241,7 @@ class Update(dns.message.Message): """Require that an owner name (and optionally an rdata type) does not exist as a prerequisite to the execution of the update.""" - if isinstance(name, string_types): + if isinstance(name, str): name = dns.name.from_text(name, None) if rdtype is None: self.find_rrset(self.answer, name, @@ -250,7 +249,7 @@ class Update(dns.message.Message): dns.rdatatype.NONE, None, True, True) else: - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) self.find_rrset(self.answer, name, dns.rdataclass.NONE, rdtype, diff --git a/dns/wiredata.py b/dns/wiredata.py index ea3c1e67..85412691 100644 --- a/dns/wiredata.py +++ b/dns/wiredata.py @@ -18,7 +18,6 @@ """DNS Wire Data Helper""" import dns.exception -from ._compat import binary_type, string_types, PY2 # Figure out what constant python passes for an unspecified slice bound. # It's supposed to be sys.maxint, yet on 64-bit windows sys.maxint is 2^31 - 1 @@ -27,19 +26,15 @@ from ._compat import binary_type, string_types, PY2 # out what constant Python will use. -class _SliceUnspecifiedBound(binary_type): +class _SliceUnspecifiedBound(bytes): def __getitem__(self, key): return key.stop - if PY2: - def __getslice__(self, i, j): # pylint: disable=getslice-method - return self.__getitem__(slice(i, j)) - _unspecified_bound = _SliceUnspecifiedBound()[1:] -class WireData(binary_type): +class WireData(bytes): # WireData is a binary type with stricter slicing def __getitem__(self, key): @@ -51,24 +46,11 @@ class WireData(binary_type): start = key.start stop = key.stop - if PY2: - if stop == _unspecified_bound: - # handle the case where the right bound is unspecified - stop = len(self) - - if start < 0 or stop < 0: + for index in (start, stop): + if index is None: + continue + elif abs(index) > len(self): raise dns.exception.FormError - # If it's not an empty slice, access left and right bounds - # to make sure they're valid - if start != stop: - super(WireData, self).__getitem__(start) - super(WireData, self).__getitem__(stop - 1) - else: - for index in (start, stop): - if index is None: - continue - elif abs(index) > len(self): - raise dns.exception.FormError return WireData(super(WireData, self).__getitem__( slice(start, stop))) @@ -76,10 +58,6 @@ class WireData(binary_type): except IndexError: raise dns.exception.FormError - if PY2: - def __getslice__(self, i, j): # pylint: disable=getslice-method - return self.__getitem__(slice(i, j)) - def __iter__(self): i = 0 while 1: @@ -90,14 +68,14 @@ class WireData(binary_type): raise StopIteration def unwrap(self): - return binary_type(self) + return bytes(self) def maybe_wrap(wire): if isinstance(wire, WireData): return wire - elif isinstance(wire, binary_type): + elif isinstance(wire, bytes): return WireData(wire) - elif isinstance(wire, string_types): + elif isinstance(wire, str): return WireData(wire.encode()) raise ValueError("unhandled type %s" % type(wire)) diff --git a/dns/zone.py b/dns/zone.py index 1e2fe781..3da8208b 100644 --- a/dns/zone.py +++ b/dns/zone.py @@ -35,7 +35,6 @@ import dns.rrset import dns.tokenizer import dns.ttl import dns.grange -from ._compat import string_types, text_type, PY3 class BadZone(dns.exception.DNSException): @@ -95,7 +94,7 @@ class Zone(object): @type rdclass: int""" if origin is not None: - if isinstance(origin, string_types): + if isinstance(origin, str): origin = dns.name.from_text(origin) elif not isinstance(origin, dns.name.Name): raise ValueError("origin parameter must be convertible to a " @@ -129,7 +128,7 @@ class Zone(object): return not self.__eq__(other) def _validate_name(self, name): - if isinstance(name, string_types): + if isinstance(name, str): name = dns.name.from_text(name, None) elif not isinstance(name, dns.name.Name): raise KeyError("name parameter must be convertible to a DNS name") @@ -157,19 +156,13 @@ class Zone(object): return self.nodes.__iter__() def iterkeys(self): - if PY3: - return self.nodes.keys() # pylint: disable=dict-keys-not-iterating - else: - return self.nodes.iterkeys() # pylint: disable=dict-iter-method + return self.nodes.keys() # pylint: disable=dict-keys-not-iterating def keys(self): return self.nodes.keys() # pylint: disable=dict-keys-not-iterating def itervalues(self): - if PY3: - return self.nodes.values() # pylint: disable=dict-values-not-iterating - else: - return self.nodes.itervalues() # pylint: disable=dict-iter-method + return self.nodes.values() # pylint: disable=dict-values-not-iterating def values(self): return self.nodes.values() # pylint: disable=dict-values-not-iterating @@ -265,9 +258,9 @@ class Zone(object): """ name = self._validate_name(name) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) - if isinstance(covers, string_types): + if isinstance(covers, str): covers = dns.rdatatype.from_text(covers) node = self.find_node(name, create) return node.find_rdataset(self.rdclass, rdtype, covers, create) @@ -328,9 +321,9 @@ class Zone(object): """ name = self._validate_name(name) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) - if isinstance(covers, string_types): + if isinstance(covers, str): covers = dns.rdatatype.from_text(covers) node = self.get_node(name) if node is not None: @@ -391,9 +384,9 @@ class Zone(object): """ name = self._validate_name(name) - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) - if isinstance(covers, string_types): + if isinstance(covers, str): covers = dns.rdatatype.from_text(covers) rdataset = self.nodes[name].find_rdataset(self.rdclass, rdtype, covers) rrset = dns.rrset.RRset(name, self.rdclass, rdtype, covers) @@ -447,9 +440,9 @@ class Zone(object): @type covers: int or string """ - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) - if isinstance(covers, string_types): + if isinstance(covers, str): covers = dns.rdatatype.from_text(covers) for (name, node) in self.iteritems(): # pylint: disable=dict-iter-method for rds in node: @@ -470,9 +463,9 @@ class Zone(object): @type covers: int or string """ - if isinstance(rdtype, string_types): + if isinstance(rdtype, str): rdtype = dns.rdatatype.from_text(rdtype) - if isinstance(covers, string_types): + if isinstance(covers, str): covers = dns.rdatatype.from_text(covers) for (name, node) in self.iteritems(): # pylint: disable=dict-iter-method for rds in node: @@ -499,7 +492,7 @@ class Zone(object): @type nl: string or None """ - if isinstance(f, string_types): + if isinstance(f, str): f = open(f, 'wb') want_close = True else: @@ -514,7 +507,7 @@ class Zone(object): if nl is None: nl_b = os.linesep.encode(file_enc) # binary mode, '\n' is not enough nl = u'\n' - elif isinstance(nl, string_types): + elif isinstance(nl, str): nl_b = nl.encode(file_enc) else: nl_b = nl @@ -529,7 +522,7 @@ class Zone(object): for n in names: l = self[n].to_text(n, origin=self.origin, relativize=relativize) - if isinstance(l, text_type): + if isinstance(l, str): l_b = l.encode(file_enc) else: l_b = l @@ -619,7 +612,7 @@ class _MasterReader(object): def __init__(self, tok, origin, rdclass, relativize, zone_factory=Zone, allow_include=False, check_origin=True): - if isinstance(origin, string_types): + if isinstance(origin, str): origin = dns.name.from_text(origin) self.tok = tok self.current_origin = origin @@ -1060,16 +1053,10 @@ def from_file(f, origin=None, rdclass=dns.rdataclass.IN, @rtype: dns.zone.Zone object """ - str_type = string_types - if PY3: - opts = 'r' - else: - opts = 'rU' - - if isinstance(f, str_type): + if isinstance(f, str): if filename is None: filename = f - f = open(f, opts) + f = open(f, 'r') want_close = True else: if filename is None: diff --git a/tests/test_generate.py b/tests/test_generate.py index 592e5b87..e06e9c64 100644 --- a/tests/test_generate.py +++ b/tests/test_generate.py @@ -26,8 +26,6 @@ import dns.rdataclass import dns.rdatatype import dns.rrset import dns.zone -from dns._compat import long - import pprint @@ -237,42 +235,42 @@ class GenerateTestCase(unittest.TestCase): l = list(z.iterate_rdatas()) l.sort(key=_rdata_sort) exl = [(dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns1')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns2')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, 'foo bar 1 2 3 4 5')), (dns.name.from_text('bar.foo', None), - long(300), + 300, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, '0 blaz.foo')), (dns.name.from_text('ns1', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.1')), (dns.name.from_text('ns2', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.2')), (dns.name.from_text('wp-db01.services.mozilla.com', None), - long(0), + 0, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, 'SERVER.FOOBAR.')), (dns.name.from_text('wp-db02.services.mozilla.com', None), - long(0), + 0, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, 'SERVER.FOOBAR.')), (dns.name.from_text('wp-db03.services.mozilla.com', None), - long(0), + 0, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, 'SERVER.FOOBAR.'))] exl.sort(key=_rdata_sort) @@ -283,39 +281,39 @@ class GenerateTestCase(unittest.TestCase): l = list(z.iterate_rdatas()) l.sort(key=_rdata_sort) exl = [(dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns1')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns2')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, 'foo bar 1 2 3 4 5')), (dns.name.from_text('bar.foo', None), - long(300), + 300, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, '0 blaz.foo')), (dns.name.from_text('ns1', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.1')), (dns.name.from_text('ns2', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.2')), - (dns.name.from_text('wp-db21.services.mozilla.com', None), long(0), + (dns.name.from_text('wp-db21.services.mozilla.com', None), 0, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, 'SERVER.FOOBAR.')), - (dns.name.from_text('wp-db22.services.mozilla.com', None), long(0), + (dns.name.from_text('wp-db22.services.mozilla.com', None), 0, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, 'SERVER.FOOBAR.')), - (dns.name.from_text('wp-db23.services.mozilla.com', None), long(0), + (dns.name.from_text('wp-db23.services.mozilla.com', None), 0, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, 'SERVER.FOOBAR.'))] exl.sort(key=_rdata_sort) @@ -327,38 +325,38 @@ class GenerateTestCase(unittest.TestCase): l.sort(key=_rdata_sort) exl = [(dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns1')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns2')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, 'foo bar 1 2 3 4 5')), (dns.name.from_text('bar.foo', None), - long(300), + 300, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, '0 blaz.foo')), (dns.name.from_text('ns1', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.1')), (dns.name.from_text('ns2', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.2')), - (dns.name.from_text('wp-db21.services.mozilla.com', None), long(0), + (dns.name.from_text('wp-db21.services.mozilla.com', None), 0, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, 'SERVER.FOOBAR.')), - (dns.name.from_text('wp-db22.services.mozilla.com', None), long(0), + (dns.name.from_text('wp-db22.services.mozilla.com', None), 0, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, 'SERVER.FOOBAR.')), - (dns.name.from_text('wp-db23.services.mozilla.com', None), long(0), + (dns.name.from_text('wp-db23.services.mozilla.com', None), 0, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, 'SERVER.FOOBAR.'))] exl.sort(key=_rdata_sort) @@ -369,39 +367,39 @@ class GenerateTestCase(unittest.TestCase): l = list(z.iterate_rdatas()) l.sort(key=_rdata_sort) exl = [(dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns1')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns2')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, 'foo bar 1 2 3 4 5')), (dns.name.from_text('bar.foo', None), - long(300), + 300, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, '0 blaz.foo')), (dns.name.from_text('ns1', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.1')), (dns.name.from_text('ns2', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.2')), - (dns.name.from_text('sync1.db', None), long(3600), + (dns.name.from_text('sync1.db', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.10.16.0')), - (dns.name.from_text('sync2.db', None), long(3600), + (dns.name.from_text('sync2.db', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.10.16.0')), - (dns.name.from_text('sync3.db', None), long(3600), + (dns.name.from_text('sync3.db', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.10.16.0'))] exl.sort(key=_rdata_sort) @@ -412,46 +410,46 @@ class GenerateTestCase(unittest.TestCase): l = list(z.iterate_rdatas()) l.sort(key=_rdata_sort) exl = [(dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns1')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns2')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, 'foo bar 1 2 3 4 5')), (dns.name.from_text('bar.foo', None), - long(300), + 300, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, '0 blaz.foo')), (dns.name.from_text('ns1', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.1')), (dns.name.from_text('ns2', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.2')), - (dns.name.from_text('wp-db01', None), long(3600), + (dns.name.from_text('wp-db01', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.10.16.0')), - (dns.name.from_text('wp-db02', None), long(3600), + (dns.name.from_text('wp-db02', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.10.16.0')), - (dns.name.from_text('sync1.db', None), long(3600), + (dns.name.from_text('sync1.db', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.10.16.0')), - (dns.name.from_text('sync2.db', None), long(3600), + (dns.name.from_text('sync2.db', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.10.16.0')), - (dns.name.from_text('sync3.db', None), long(3600), + (dns.name.from_text('sync3.db', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.10.16.0'))] exl.sort(key=_rdata_sort) @@ -462,35 +460,35 @@ class GenerateTestCase(unittest.TestCase): l = list(z.iterate_rdatas()) l.sort(key=_rdata_sort) exl = [(dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns1')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, 'ns2')), (dns.name.from_text('@', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, 'foo bar 1 2 3 4 5')), (dns.name.from_text('bar.foo', None), - long(300), + 300, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, '0 blaz.foo')), (dns.name.from_text('ns1', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.1')), (dns.name.from_text('ns2', None), - long(3600), + 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.2')), - (dns.name.from_text('27.2', None), long(3600), + (dns.name.from_text('27.2', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.PTR, 'zlb1.oob')), - (dns.name.from_text('28.2', None), long(3600), + (dns.name.from_text('28.2', None), 3600, dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.PTR, 'zlb2.oob'))] diff --git a/tests/test_message.py b/tests/test_message.py index a6f39a28..d60e711c 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -24,7 +24,6 @@ import dns.message import dns.name import dns.rdataclass import dns.rdatatype -from dns._compat import xrange query_text = """id 1234 opcode QUERY @@ -136,7 +135,7 @@ class MessageTestCase(unittest.TestCase): def test_TooBig(self): def bad(): q = dns.message.from_text(query_text) - for i in xrange(0, 25): + for i in range(0, 25): rrset = dns.rrset.from_text('foo%d.' % i, 3600, dns.rdataclass.IN, dns.rdatatype.A, diff --git a/tests/test_resolver.py b/tests/test_resolver.py index 1738c1a3..b36992cb 100644 --- a/tests/test_resolver.py +++ b/tests/test_resolver.py @@ -27,7 +27,6 @@ import dns.name import dns.rdataclass import dns.rdatatype import dns.resolver -from dns._compat import xrange, PY3 # Some tests require the internet to be available to run, so let's # skip those if it's not there. @@ -182,11 +181,11 @@ class BaseResolverTests(unittest.TestCase): def testLRUReplace(self): cache = dns.resolver.LRUCache(4) - for i in xrange(0, 5): + for i in range(0, 5): name = dns.name.from_text('example%d.' % i) answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) - for i in xrange(0, 5): + for i in range(0, 5): name = dns.name.from_text('example%d.' % i) if i == 0: self.failUnless(cache.get((name, dns.rdatatype.A, @@ -199,7 +198,7 @@ class BaseResolverTests(unittest.TestCase): def testLRUDoesLRU(self): cache = dns.resolver.LRUCache(4) - for i in xrange(0, 4): + for i in range(0, 4): name = dns.name.from_text('example%d.' % i) answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) @@ -209,7 +208,7 @@ class BaseResolverTests(unittest.TestCase): name = dns.name.from_text('example4.') answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) - for i in xrange(0, 5): + for i in range(0, 5): name = dns.name.from_text('example%d.' % i) if i == 1: self.failUnless(cache.get((name, dns.rdatatype.A, @@ -222,12 +221,12 @@ class BaseResolverTests(unittest.TestCase): def testLRUExpiration(self): cache = dns.resolver.LRUCache(4) - for i in xrange(0, 4): + for i in range(0, 4): name = dns.name.from_text('example%d.' % i) answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) time.sleep(2) - for i in xrange(0, 4): + for i in range(0, 4): name = dns.name.from_text('example%d.' % i) self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) @@ -283,9 +282,6 @@ class NXDOMAINExceptionTestCase(unittest.TestCase): try: raise dns.resolver.NXDOMAIN except dns.exception.DNSException as e: - if not PY3: - # pylint: disable=exception-message-attribute - self.assertTrue((e.message == e.__doc__)) self.assertTrue((e.args == (e.__doc__,))) self.assertTrue(('kwargs' in dir(e))) self.assertTrue((str(e) == e.__doc__), str(e)) @@ -295,9 +291,6 @@ class NXDOMAINExceptionTestCase(unittest.TestCase): try: raise dns.resolver.NXDOMAIN("errmsg") except dns.exception.DNSException as e: - if not PY3: - # pylint: disable=exception-message-attribute - self.assertTrue((e.message == "errmsg")) self.assertTrue((e.args == ("errmsg",))) self.assertTrue(('kwargs' in dir(e))) self.assertTrue((str(e) == "errmsg"), str(e)) @@ -307,9 +300,6 @@ class NXDOMAINExceptionTestCase(unittest.TestCase): try: raise dns.resolver.NXDOMAIN("errmsg", -1) except dns.exception.DNSException as e: - if not PY3: - # pylint: disable=exception-message-attribute - self.assertTrue((e.message == "")) self.assertTrue((e.args == ("errmsg", -1))) self.assertTrue(('kwargs' in dir(e))) self.assertTrue((str(e) == "('errmsg', -1)"), str(e)) @@ -335,9 +325,6 @@ class NXDOMAINExceptionTestCase(unittest.TestCase): raise dns.resolver.NXDOMAIN(qnames=[n1]) except dns.exception.DNSException as e: MSG = "The DNS query name does not exist: a.b." - if not PY3: - # pylint: disable=exception-message-attribute - self.assertTrue((e.message == MSG), e.message) self.assertTrue((e.args == (MSG,)), repr(e.args)) self.assertTrue(('kwargs' in dir(e))) self.assertTrue((str(e) == MSG), str(e)) @@ -352,9 +339,6 @@ class NXDOMAINExceptionTestCase(unittest.TestCase): e0 = dns.resolver.NXDOMAIN("errmsg") e = e0 + e MSG = "None of DNS query names exist: a.b.s., a.b." - if not PY3: - # pylint: disable=exception-message-attribute - self.assertTrue((e.message == MSG), e.message) self.assertTrue((e.args == (MSG,)), repr(e.args)) self.assertTrue(('kwargs' in dir(e))) self.assertTrue((str(e) == MSG), str(e)) @@ -372,9 +356,6 @@ class NXDOMAINExceptionTestCase(unittest.TestCase): raise dns.resolver.NXDOMAIN(qnames=[n1], responses={n1: 'r1.1'}) except dns.resolver.NXDOMAIN as e: MSG = "The DNS query name does not exist: a.b." - if not PY3: - # pylint: disable=exception-message-attribute - self.assertTrue((e.message == MSG), e.message) self.assertTrue((e.args == (MSG,)), repr(e.args)) self.assertTrue(('kwargs' in dir(e))) self.assertTrue((str(e) == MSG), str(e))