]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Fix other isdigit() instances that are better as isdecimal().
authorBob Halley <halley@dnspython.org>
Fri, 3 Jul 2026 15:18:39 +0000 (08:18 -0700)
committerBob Halley <halley@dnspython.org>
Fri, 3 Jul 2026 15:18:39 +0000 (08:18 -0700)
Both raised exceptions, but the latter raises a
dns.exception.SyntaxError in cases where the old code raised
ValueError.

This change also fixes the tokenizer's get_int() method, which
did not work for bases bigger than 10.

13 files changed:
dns/e164.py
dns/enum.py
dns/flags.py
dns/grange.py
dns/inet.py
dns/name.py
dns/rdtypes/ANY/GPOS.py
dns/rdtypes/ANY/LOC.py
dns/rdtypes/IN/WKS.py
dns/rdtypes/rrsigbase.py
dns/rdtypes/svcbbase.py
dns/tokenizer.py
tests/test_tokenizer.py

index c0ac3e1c6ebe49b21d5af5053d5f2d3028c6aa45..321eda40bc042363fd21abb4ab676dc72bffebfb 100644 (file)
@@ -44,7 +44,7 @@ def from_e164(
     :rtype: :py:class:`dns.name.Name`
     """
 
-    parts = [d for d in text if d.isdigit()]
+    parts = [d for d in text if d.isdecimal()]
     parts.reverse()
     return dns.name.from_text(".".join(parts), origin=origin)
 
index ca3c62f04eeafbf0be28bc8468d5b0bf1f699c36..604421c08a99d9015ef899802e5162414bff58f2 100644 (file)
@@ -50,7 +50,7 @@ class IntEnum(enum.IntEnum):
         if value:
             return value
         prefix = cls._prefix()
-        if text.startswith(prefix) and text[len(prefix) :].isdigit():
+        if text.startswith(prefix) and text[len(prefix) :].isdecimal():
             value = int(text[len(prefix) :])
             cls._check_value(value)
             return cls(value)
index 9fd1d0e6bdf3baf7ba5af03981d3831b4a013818..4317a6e74b8a8ae8549016e7efcd1f222dc3bd10 100644 (file)
@@ -62,7 +62,7 @@ def _from_text(text: str, enum_class: Any) -> int:
     tokens = text.split()
     for t in tokens:
         token = t.upper()
-        if token.startswith("FLAG") and token[4:].isdigit():
+        if token.startswith("FLAG") and token[4:].isdecimal():
             # An unnamed flag, rendered by _to_text() as FLAGn (see below).
             flags |= 1 << int(token[4:])
         else:
index 7a217f8d8a987ea85c3d3a4ec787c17f1138b980..bbfa9d949230b1063c3b26f6ce6d0df639bdc96c 100644 (file)
@@ -49,7 +49,7 @@ def from_text(text: str) -> tuple[int, int, int]:
             stop = int(cur)
             cur = ""
             state = 2
-        elif c.isdigit():
+        elif c.isdecimal():
             cur += c
         else:
             raise dns.exception.SyntaxError(f"Could not parse {c}")
index 8d80c2f968473ed918d60e13ead96dbd173fee73..764309a87bb618352223b534edb437496885eb8d 100644 (file)
@@ -149,7 +149,7 @@ def low_level_address_tuple(high_tuple: tuple[str, int], af: int | None = None)
         # try to avoid getaddrinfo()
         addrpart = address[:i]
         scope = address[i + 1 :]
-        if scope.isdigit():
+        if scope.isdecimal():
             return (addrpart, port, 0, int(scope))
         try:
             return (addrpart, port, 0, socket.if_nametoindex(scope))
index aeb506823c04621a925a56d60dff806614d62178..526e539d8416e9ca04a433509fb48624085fb9e4 100644 (file)
@@ -1004,14 +1004,14 @@ def from_unicode(
         for c in text:
             if escaping:
                 if edigits == 0:
-                    if c.isdigit():
+                    if c.isdecimal():
                         total = int(c)
                         edigits += 1
                     else:
                         label += c
                         escaping = False
                 else:
-                    if not c.isdigit():
+                    if not c.isdecimal():
                         raise BadEscape
                     total *= 10
                     total += int(c)
index 8a6caea300bd3bc56a8a86261dd4fccef099a01b..7eb8fcfccc3cc5d05de9b8ed8d0649c3f5fa49c6 100644 (file)
@@ -36,9 +36,9 @@ def _validate_float_string(what):
         raise dns.exception.FormError
     if left == b"" and right == b"":
         raise dns.exception.FormError
-    if not left == b"" and not left.decode().isdigit():
+    if not left == b"" and not left.isdigit():
         raise dns.exception.FormError
-    if not right == b"" and not right.decode().isdigit():
+    if not right == b"" and not right.isdigit():
         raise dns.exception.FormError
 
 
index 02b0a2c37a0ad0bbc2880009453e2acbed746adf..f377cee67c9e3166f921c272aab362ec93fa4a63 100644 (file)
@@ -196,16 +196,16 @@ class LOC(dns.rdata.Rdata):
 
         latitude[0] = tok.get_int()
         t = tok.get_string()
-        if t.isdigit():
+        if t.isdecimal():
             latitude[1] = int(t)
             t = tok.get_string()
             if "." in t:
                 seconds, milliseconds = t.split(".")
-                if not seconds.isdigit():
+                if not seconds.isdecimal():
                     raise dns.exception.SyntaxError("bad latitude seconds value")
                 latitude[2] = int(seconds)
                 l = len(milliseconds)
-                if l == 0 or l > 3 or not milliseconds.isdigit():
+                if l == 0 or l > 3 or not milliseconds.isdecimal():
                     raise dns.exception.SyntaxError("bad latitude milliseconds value")
                 if l == 1:
                     m = 100
@@ -215,7 +215,7 @@ class LOC(dns.rdata.Rdata):
                     m = 1
                 latitude[3] = m * int(milliseconds)
                 t = tok.get_string()
-            elif t.isdigit():
+            elif t.isdecimal():
                 latitude[2] = int(t)
                 t = tok.get_string()
         if t == "S":
@@ -225,16 +225,16 @@ class LOC(dns.rdata.Rdata):
 
         longitude[0] = tok.get_int()
         t = tok.get_string()
-        if t.isdigit():
+        if t.isdecimal():
             longitude[1] = int(t)
             t = tok.get_string()
             if "." in t:
                 seconds, milliseconds = t.split(".")
-                if not seconds.isdigit():
+                if not seconds.isdecimal():
                     raise dns.exception.SyntaxError("bad longitude seconds value")
                 longitude[2] = int(seconds)
                 l = len(milliseconds)
-                if l == 0 or l > 3 or not milliseconds.isdigit():
+                if l == 0 or l > 3 or not milliseconds.isdecimal():
                     raise dns.exception.SyntaxError("bad longitude milliseconds value")
                 if l == 1:
                     m = 100
@@ -244,7 +244,7 @@ class LOC(dns.rdata.Rdata):
                     m = 1
                 longitude[3] = m * int(milliseconds)
                 t = tok.get_string()
-            elif t.isdigit():
+            elif t.isdecimal():
                 longitude[2] = int(t)
                 t = tok.get_string()
         if t == "W":
index 634386fc6c1e5f2fb23adc146b080e7b1ce5429c..1ab7dba5c58acf0d0542a444e2d7209666c58657 100644 (file)
@@ -60,14 +60,14 @@ class WKS(dns.rdata.Rdata):
     ):
         address = tok.get_string()
         protocol = tok.get_string()
-        if protocol.isdigit():
+        if protocol.isdecimal():
             protocol = int(protocol)
         else:
             protocol = socket.getprotobyname(protocol)
         bitmap = bytearray()
         for token in tok.get_remaining():
             value = token.unescape().value
-            if value.isdigit():
+            if value.isdecimal():
                 serv = int(value)
             else:
                 if protocol != _proto_udp and protocol != _proto_tcp:
index 2b7208fdf0477c7f9f8fe2e244d530d5f0a13e9c..d65ef396324903106d7ea27cd5a2988d3447b7ef 100644 (file)
@@ -34,7 +34,7 @@ class BadSigTime(dns.exception.DNSException):
 
 
 def sigtime_to_posixtime(what):
-    if len(what) <= 10 and what.isdigit():
+    if len(what) <= 10 and what.isdecimal():
         return int(what)
     if len(what) != 14:
         raise BadSigTime
index c97adda6d39d983f69bd6e526f8cfb432bca1adb..0acdd6469a36caf3651893a5ffc1d0fd709f417f 100644 (file)
@@ -113,7 +113,7 @@ def _unescape(value: str) -> bytes:
                 raise dns.exception.UnexpectedEnd
             c = value[i]
             i += 1
-            if c.isdigit():
+            if c.isdecimal():
                 if i >= l:
                     raise dns.exception.UnexpectedEnd
                 c2 = value[i]
@@ -122,7 +122,7 @@ def _unescape(value: str) -> bytes:
                     raise dns.exception.UnexpectedEnd
                 c3 = value[i]
                 i += 1
-                if not (c2.isdigit() and c3.isdigit()):
+                if not (c2.isdecimal() and c3.isdecimal()):
                     raise dns.exception.SyntaxError
                 codepoint = int(c) * 100 + int(c2) * 10 + int(c3)
                 if codepoint > 255:
index 42bf45c34d8fd7820b9eb0a616a0d9840d290fd4..5c0638e514a409c59711d0f2b3c7c1c1bc1cda17 100644 (file)
@@ -114,7 +114,7 @@ class Token:
                     raise dns.exception.UnexpectedEnd
                 c = self.value[i]
                 i += 1
-                if c.isdigit():
+                if c.isdecimal():
                     if i >= l:
                         raise dns.exception.UnexpectedEnd
                     c2 = self.value[i]
@@ -123,7 +123,7 @@ class Token:
                         raise dns.exception.UnexpectedEnd
                     c3 = self.value[i]
                     i += 1
-                    if not (c2.isdigit() and c3.isdigit()):
+                    if not (c2.isdecimal() and c3.isdecimal()):
                         raise dns.exception.SyntaxError
                     codepoint = int(c) * 100 + int(c2) * 10 + int(c3)
                     if codepoint > 255:
@@ -168,7 +168,7 @@ class Token:
                     raise dns.exception.UnexpectedEnd
                 c = self.value[i]
                 i += 1
-                if c.isdigit():
+                if c.isdecimal():
                     if i >= l:
                         raise dns.exception.UnexpectedEnd
                     c2 = self.value[i]
@@ -177,7 +177,7 @@ class Token:
                         raise dns.exception.UnexpectedEnd
                     c3 = self.value[i]
                     i += 1
-                    if not (c2.isdigit() and c3.isdigit()):
+                    if not (c2.isdecimal() and c3.isdecimal()):
                         raise dns.exception.SyntaxError
                     codepoint = int(c) * 100 + int(c2) * 10 + int(c3)
                     if codepoint > 255:
@@ -631,9 +631,13 @@ class Tokenizer:
 
         if not token.is_identifier():
             raise dns.exception.SyntaxError("expecting an identifier")
-        if not token.value.isdigit():
+        try:
+            value = int(token.value, base)
+            if value < 0:
+                raise ValueError
+        except ValueError:
             raise dns.exception.SyntaxError("expecting an integer")
-        return int(token.value, base)
+        return value
 
     def as_uint8(self, token: Token) -> int:
         """Try to interpret the token as an unsigned 8-bit integer.
index d8b1723dd72e95864d98ed75a7af5cd3cebde664..392b1e4d36d23da7cdb939897870d206b86c50fc 100644 (file)
@@ -238,6 +238,9 @@ class TokenizerTestCase(unittest.TestCase):
         with self.assertRaises(dns.exception.SyntaxError):
             tok = dns.tokenizer.Tokenizer("200000")
             tok.get_uint16(base=8)
+        tok = dns.tokenizer.Tokenizer("ff")
+        v = tok.get_int(16)
+        self.assertEqual(v, 255)
 
     def testGetString(self):
         tok = dns.tokenizer.Tokenizer("foo")