]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
de-lint IDNA
authorBob Halley <halley@dnspython.org>
Sun, 25 Sep 2016 19:28:04 +0000 (12:28 -0700)
committerBob Halley <halley@dnspython.org>
Sun, 25 Sep 2016 19:28:04 +0000 (12:28 -0700)
dns/name.py
dns/rrset.py
tests/test_name.py

index 2a33ebf96cfcd29fc7239aa36f6332fbaed2b758..e1259b958bc3cf90d879f84463ef84b73f833575 100644 (file)
@@ -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)):
index b2fb301d25d3643531a6047df51358503b5c6505..d0f8f93776063d03d5d52bd5f9c589a27948abf6 100644 (file)
@@ -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")
index 07d808d7f8434482081fcea555616fc43333723f..11504c710d90cc37645c7a8b07b7b41eefd92c25 100644 (file)
@@ -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'ドメイン.テスト.')