From: Bob Halley Date: Sun, 25 Sep 2016 19:28:04 +0000 (-0700) Subject: de-lint IDNA X-Git-Tag: v1.15.0~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=466ac0d89f584f8050a92add5ea3208860f5e92f;p=thirdparty%2Fdnspython.git de-lint IDNA --- diff --git a/dns/name.py b/dns/name.py index 2a33ebf9..e1259b95 100644 --- a/dns/name.py +++ b/dns/name.py @@ -177,7 +177,7 @@ class IDNA2008Codec(IDNACodec): if self.uts_46: label = idna.uts46_remap(label, False, False) return _escapify(idna.ulabel(label), True) - except IDNAError as e: + except idna.IDNAError as e: raise IDNAException(idna_exception=e) @@ -468,7 +468,7 @@ class Name(object): s = b'.'.join(map(_escapify, l)) return s - def to_unicode(self, omit_final_dot=False, idna=None): + def to_unicode(self, omit_final_dot=False, idna_codec=None): """Convert name to Unicode text format. IDN ACE labels are converted to Unicode. @@ -476,9 +476,10 @@ class Name(object): @param omit_final_dot: If True, don't emit the final dot (denoting the root label) for absolute names. The default is False. @type omit_final_dot: bool - @param: idna: IDNA encoder/decoder. If None, the default IDNA 2003 + @param: idna_codec: IDNA encoder/decoder. If None, the default IDNA + 2003 encoder/decoder is used. - @type idna: dns.name.IDNA + @type idna_codec: dns.name.IDNA @rtype: string """ @@ -490,9 +491,9 @@ class Name(object): l = self.labels[:-1] else: l = self.labels - if idna is None: - idna = IDNA_2003 - return u'.'.join([idna.decode(x) for x in l]) + if idna_codec is None: + idna_codec = IDNA_2003 + return u'.'.join([idna_codec.decode(x) for x in l]) def to_digestable(self, origin=None): """Convert name to a format suitable for digesting in hashes. @@ -677,7 +678,7 @@ root = Name([b'']) empty = Name([]) -def from_unicode(text, origin=root, idna=None): +def from_unicode(text, origin=root, idna_codec=None): """Convert unicode text into a Name object. Labels are encoded in IDN ACE form. @@ -686,9 +687,9 @@ def from_unicode(text, origin=root, idna=None): @type text: Unicode string @param origin: The origin to append to non-absolute names. @type origin: dns.name.Name - @param: idna: IDNA encoder/decoder. If None, the default IDNA 2003 + @param: idna_codec: IDNA encoder/decoder. If None, the default IDNA 2003 encoder/decoder is used. - @type idna: dns.name.IDNA + @type idna_codec: dns.name.IDNA @rtype: dns.name.Name object """ @@ -701,8 +702,8 @@ def from_unicode(text, origin=root, idna=None): escaping = False edigits = 0 total = 0 - if idna is None: - idna = IDNA_2003 + if idna_codec is None: + idna_codec = IDNA_2003 if text == u'@': text = u'' if text: @@ -729,7 +730,7 @@ def from_unicode(text, origin=root, idna=None): elif c in [u'.', u'\u3002', u'\uff0e', u'\uff61']: if len(label) == 0: raise EmptyLabel - labels.append(idna.encode(label)) + labels.append(idna_codec.encode(label)) label = u'' elif c == u'\\': escaping = True @@ -740,7 +741,7 @@ def from_unicode(text, origin=root, idna=None): if escaping: raise BadEscape if len(label) > 0: - labels.append(idna.encode(label)) + labels.append(idna_codec.encode(label)) else: labels.append(b'') @@ -749,21 +750,21 @@ def from_unicode(text, origin=root, idna=None): return Name(labels) -def from_text(text, origin=root, idna=None): +def from_text(text, origin=root, idna_codec=None): """Convert text into a Name object. @param text: The text to convert into a name. @type text: string @param origin: The origin to append to non-absolute names. @type origin: dns.name.Name - @param: idna: IDNA encoder/decoder. If None, the default IDNA 2003 + @param: idna_codec: IDNA encoder/decoder. If None, the default IDNA 2003 encoder/decoder is used. - @type idna: dns.name.IDNA + @type idna_codec: dns.name.IDNA @rtype: dns.name.Name object """ if isinstance(text, text_type): - return from_unicode(text, origin, idna) + return from_unicode(text, origin, idna_codec) if not isinstance(text, binary_type): raise ValueError("input to from_text() must be a string") if not (origin is None or isinstance(origin, Name)): diff --git a/dns/rrset.py b/dns/rrset.py index b2fb301d..d0f8f937 100644 --- a/dns/rrset.py +++ b/dns/rrset.py @@ -120,7 +120,7 @@ class RRset(dns.rdataset.Rdataset): def from_text_list(name, ttl, rdclass, rdtype, text_rdatas, - idna=None): + idna_codec=None): """Create an RRset with the specified name, TTL, class, and type, and with the specified list of rdatas in text format. @@ -128,7 +128,7 @@ def from_text_list(name, ttl, rdclass, rdtype, text_rdatas, """ if isinstance(name, string_types): - name = dns.name.from_text(name, None, idna=idna) + name = dns.name.from_text(name, None, idna_codec=idna_codec) if isinstance(rdclass, string_types): rdclass = dns.rdataclass.from_text(rdclass) if isinstance(rdtype, string_types): @@ -151,7 +151,7 @@ def from_text(name, ttl, rdclass, rdtype, *text_rdatas): return from_text_list(name, ttl, rdclass, rdtype, text_rdatas) -def from_rdata_list(name, ttl, rdatas, idna=None): +def from_rdata_list(name, ttl, rdatas, idna_codec=None): """Create an RRset with the specified name and TTL, and with the specified list of rdata objects. @@ -159,7 +159,7 @@ def from_rdata_list(name, ttl, rdatas, idna=None): """ if isinstance(name, string_types): - name = dns.name.from_text(name, None, idna=idna) + name = dns.name.from_text(name, None, idna_codec=idna_codec) if len(rdatas) == 0: raise ValueError("rdata list must not be empty") diff --git a/tests/test_name.py b/tests/test_name.py index 07d808d7..11504c71 100644 --- a/tests/test_name.py +++ b/tests/test_name.py @@ -27,9 +27,6 @@ import dns.name import dns.reversename import dns.e164 -if dns.name.have_idna_2008: - import idna - # pylint: disable=line-too-long @@ -657,7 +654,7 @@ class NameTestCase(unittest.TestCase): def testFromUnicodeIDNA2003Explicit(self): t = u'Königsgäßchen' - e = dns.name.from_unicode(t, idna=dns.name.IDNA_2003) + e = dns.name.from_unicode(t, idna_codec=dns.name.IDNA_2003) self.assertEqual(str(e), 'xn--knigsgsschen-lcb0w.') def testFromUnicodeIDNA2003Default(self): @@ -669,12 +666,13 @@ class NameTestCase(unittest.TestCase): if dns.name.have_idna_2008: t = u'Königsgäßchen' def bad(): - return dns.name.from_unicode(t, - idna=dns.name.IDNA_2008_Strict) + codec = dns.name.IDNA_2008_Strict + return dns.name.from_unicode(t, idna_codec=codec) self.failUnlessRaises(dns.name.IDNAException, bad) - e1 = dns.name.from_unicode(t, idna=dns.name.IDNA_2008) + e1 = dns.name.from_unicode(t, idna_codec=dns.name.IDNA_2008) self.assertEqual(str(e1), 'xn--knigsgchen-b4a3dun.') - e2 = dns.name.from_unicode(t, idna=dns.name.IDNA_2008_Transitional) + c2 = dns.name.IDNA_2008_Transitional + e2 = dns.name.from_unicode(t, idna_codec=c2) self.assertEqual(str(e2), 'xn--knigsgsschen-lcb0w.') def testToUnicode1(self): @@ -694,7 +692,8 @@ class NameTestCase(unittest.TestCase): def testToUnicode4(self): if dns.name.have_idna_2008: - n = dns.name.from_text(u'ドメイン.テスト', idna=dns.name.IDNA_2008) + n = dns.name.from_text(u'ドメイン.テスト', + idna_codec=dns.name.IDNA_2008) s = n.to_unicode() self.assertEqual(str(n), 'xn--eckwd4c7c.xn--zckzah.') self.assertEqual(s, u'ドメイン.テスト.')