]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
remove unneed name tests; more circular import fixes
authorBob Halley <halley@dnspython.org>
Wed, 24 Dec 2025 01:48:33 +0000 (17:48 -0800)
committerBob Halley <halley@dnspython.org>
Wed, 24 Dec 2025 01:48:33 +0000 (17:48 -0800)
dns/_render_util.py [new file with mode: 0644]
dns/message.py
dns/name.py
dns/rdataset.py
dns/renderer.py
tests/test_name.py

diff --git a/dns/_render_util.py b/dns/_render_util.py
new file mode 100644 (file)
index 0000000..90920bf
--- /dev/null
@@ -0,0 +1,21 @@
+import contextlib
+
+import dns.exception
+
+
+@contextlib.contextmanager
+def prefixed_length(output, length_length):
+    output.write(b"\00" * length_length)
+    start = output.tell()
+    yield
+    end = output.tell()
+    length = end - start
+    if length > 0:
+        try:
+            output.seek(start - length_length)
+            try:
+                output.write(length.to_bytes(length_length, "big"))
+            except OverflowError:
+                raise dns.exception.FormError
+        finally:
+            output.seek(end)
index a86b9e8a11c47602ba487235010b2e5d97d0e64a..e754093d01b82e0c22657d99865ccc092bb8adce 100644 (file)
@@ -125,7 +125,7 @@ class MessageError:
         self.offset = offset
 
 
-DEFAULT_EDNS_PAYLOAD = 1232
+DEFAULT_EDNS_PAYLOAD = dns.renderer.DEFAULT_EDNS_PAYLOAD
 MAX_CHAIN = 16
 
 IndexKeyType = tuple[
@@ -664,18 +664,9 @@ class Message:
     def _make_tsig(
         keyname, algorithm, time_signed, fudge, mac, original_id, error, other
     ):
-        tsig = dns.rdtypes.ANY.TSIG.TSIG(
-            dns.rdataclass.ANY,
-            dns.rdatatype.TSIG,
-            algorithm,
-            time_signed,
-            fudge,
-            mac,
-            original_id,
-            error,
-            other,
+        return dns.renderer._make_tsig(
+            keyname, algorithm, time_signed, fudge, mac, original_id, error, other
         )
-        return dns.rrset.from_rdata(keyname, 0, tsig)
 
     def use_tsig(
         self,
@@ -786,8 +777,7 @@ class Message:
 
     @staticmethod
     def _make_opt(flags=0, payload=DEFAULT_EDNS_PAYLOAD, options=None):
-        opt = dns.rdtypes.ANY.OPT.OPT(payload, dns.rdatatype.OPT, options or ())
-        return dns.rrset.from_rdata(dns.name.root, int(flags), opt)
+        return dns.renderer._make_opt(flags, payload, options)
 
     def use_edns(
         self,
index f8bc0f15cca363d8cd6a5f9f00012105c09d103b..d87acb4346e8d83bfc8c9c569c2187b7f59f4a82 100644 (file)
@@ -918,10 +918,6 @@ def from_unicode(
     Returns a ``dns.name.Name``.
     """
 
-    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")
     labels = []
     label = ""
     escaping = False
@@ -1014,10 +1010,6 @@ def from_text(
         # then it's still "all ASCII" even though the domain name has
         # codepoints > 127.
         text = text.encode("ascii")
-    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")
     labels = []
     label = b""
     escaping = False
index 63e72ea7ece30c1e804ab05c800bcbd4e6bb884b..dd8fa6aeee211314c8edeaa13c94e465616333de 100644 (file)
@@ -29,9 +29,9 @@ import dns.name
 import dns.rdata
 import dns.rdataclass
 import dns.rdatatype
-import dns.renderer
 import dns.set
 import dns.ttl
+from dns._render_util import prefixed_length
 
 # define SimpleSet here for backwards compatibility
 SimpleSet = dns.set.Set
@@ -321,7 +321,7 @@ class Rdataset(dns.set.Set):
             for rd in l:
                 name.to_wire(file, compress, origin)
                 file.write(struct.pack("!HHI", self.rdtype, rdclass, self.ttl))
-                with dns.renderer.prefixed_length(file, 2):
+                with prefixed_length(file, 2):
                     rd.to_wire(file, compress, origin)
             return len(self)
 
index 4885348ed70bc5d6a31fa6946217da3f50904878..e6957daa6f4dcecfd5f6aa3e543badd19172d07a 100644 (file)
@@ -25,11 +25,16 @@ import time
 
 import dns.edns
 import dns.exception
+import dns.name
 import dns.rdataclass
 import dns.rdatatype
+import dns.rdtypes.ANY.OPT
+import dns.rdtypes.ANY.TSIG
+import dns.rrset
 import dns.tsig
+from dns._render_util import prefixed_length as prefixed_length  # type: ignore
 
-# Note we can't import dns.message for cicularity reasons
+DEFAULT_EDNS_PAYLOAD = 1232
 
 QUESTION = 0
 ANSWER = 1
@@ -37,22 +42,24 @@ AUTHORITY = 2
 ADDITIONAL = 3
 
 
-@contextlib.contextmanager
-def prefixed_length(output, length_length):
-    output.write(b"\00" * length_length)
-    start = output.tell()
-    yield
-    end = output.tell()
-    length = end - start
-    if length > 0:
-        try:
-            output.seek(start - length_length)
-            try:
-                output.write(length.to_bytes(length_length, "big"))
-            except OverflowError:
-                raise dns.exception.FormError
-        finally:
-            output.seek(end)
+def _make_opt(flags=0, payload=DEFAULT_EDNS_PAYLOAD, options=None):
+    opt = dns.rdtypes.ANY.OPT.OPT(payload, dns.rdatatype.OPT, options or ())
+    return dns.rrset.from_rdata(dns.name.root, int(flags), opt)
+
+
+def _make_tsig(keyname, algorithm, time_signed, fudge, mac, original_id, error, other):
+    tsig = dns.rdtypes.ANY.TSIG.TSIG(
+        dns.rdataclass.ANY,
+        dns.rdatatype.TSIG,
+        algorithm,
+        time_signed,
+        fudge,
+        mac,
+        original_id,
+        error,
+        other,
+    )
+    return dns.rrset.from_rdata(keyname, 0, tsig)
 
 
 class Renderer:
@@ -219,9 +226,7 @@ class Renderer:
                 pad = b""
             options = list(opt_rdata.options)
             options.append(dns.edns.GenericOption(dns.edns.OptionType.PADDING, pad))
-            opt = dns.message.Message._make_opt(  # type: ignore
-                ttl, opt_rdata.rdclass, options
-            )
+            opt = _make_opt(ttl, opt_rdata.rdclass, options)  # type: ignore
             self.was_padded = True
         self.add_rrset(ADDITIONAL, opt)
 
@@ -231,7 +236,7 @@ class Renderer:
         # make sure the EDNS version in ednsflags agrees with edns
         ednsflags &= 0xFF00FFFF
         ednsflags |= edns << 16
-        opt = dns.message.Message._make_opt(ednsflags, payload, options)  # type: ignore
+        opt = _make_opt(ednsflags, payload, options)  # type: ignore
         self.add_opt(opt)
 
     def add_tsig(
@@ -253,7 +258,7 @@ class Renderer:
             key = secret
         else:
             key = dns.tsig.Key(keyname, secret, algorithm)
-        tsig = dns.message.Message._make_tsig(  # type: ignore
+        tsig = _make_tsig(  # type: ignore
             keyname, algorithm, 0, fudge, b"", id, tsig_error, other_data
         )
         (tsig, _) = dns.tsig.sign(s, key, tsig[0], int(time.time()), request_mac)
@@ -285,7 +290,7 @@ class Renderer:
             key = secret
         else:
             key = dns.tsig.Key(keyname, secret, algorithm)
-        tsig = dns.message.Message._make_tsig(  # type: ignore
+        tsig = _make_tsig(  # type: ignore
             keyname, algorithm, 0, fudge, b"", id, tsig_error, other_data
         )
         (tsig, ctx) = dns.tsig.sign(
index e9a06e8aeb8e6c4c02d19d814079f151775ed661..67d02dbbe762ec6053c351d7631d793897dd3246 100644 (file)
@@ -1068,18 +1068,6 @@ class NameTestCase(unittest.TestCase):
 
         self.assertRaises(dns.name.BadEscape, bad2)
 
-    def testFromUnicodeNotString(self):
-        def bad():
-            dns.name.from_unicode(b"123")  # type: ignore
-
-        self.assertRaises(ValueError, bad)
-
-    def testFromUnicodeBadOrigin(self):
-        def bad():
-            dns.name.from_unicode("example", 123)  # type: ignore
-
-        self.assertRaises(ValueError, bad)
-
     def testFromUnicodeEmptyLabel(self):
         def bad():
             dns.name.from_unicode("a..b.example")
@@ -1089,18 +1077,6 @@ class NameTestCase(unittest.TestCase):
     def testFromUnicodeEmptyName(self):
         self.assertEqual(dns.name.from_unicode("@", None), dns.name.empty)
 
-    def testFromTextNotString(self):
-        def bad():
-            dns.name.from_text(123)  # type: ignore
-
-        self.assertRaises(ValueError, bad)
-
-    def testFromTextBadOrigin(self):
-        def bad():
-            dns.name.from_text("example", 123)  # type: ignore
-
-        self.assertRaises(ValueError, bad)
-
     def testBadPunycode(self):
         c = dns.name.IDNACodec()
         with self.assertRaises(dns.name.IDNAException):