Skip to content

Commit 40ff526

Browse files
committed
Fix other isdigit() instances that are better as isdecimal().
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.
1 parent 7cc53f6 commit 40ff526

13 files changed

Lines changed: 35 additions & 28 deletions

File tree

dns/e164.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def from_e164(
4444
:rtype: :py:class:`dns.name.Name`
4545
"""
4646

47-
parts = [d for d in text if d.isdigit()]
47+
parts = [d for d in text if d.isdecimal()]
4848
parts.reverse()
4949
return dns.name.from_text(".".join(parts), origin=origin)
5050

dns/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def from_text(cls: type[TIntEnum], text: str) -> TIntEnum:
5050
if value:
5151
return value
5252
prefix = cls._prefix()
53-
if text.startswith(prefix) and text[len(prefix) :].isdigit():
53+
if text.startswith(prefix) and text[len(prefix) :].isdecimal():
5454
value = int(text[len(prefix) :])
5555
cls._check_value(value)
5656
return cls(value)

dns/flags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _from_text(text: str, enum_class: Any) -> int:
6262
tokens = text.split()
6363
for t in tokens:
6464
token = t.upper()
65-
if token.startswith("FLAG") and token[4:].isdigit():
65+
if token.startswith("FLAG") and token[4:].isdecimal():
6666
# An unnamed flag, rendered by _to_text() as FLAGn (see below).
6767
flags |= 1 << int(token[4:])
6868
else:

dns/grange.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def from_text(text: str) -> tuple[int, int, int]:
4949
stop = int(cur)
5050
cur = ""
5151
state = 2
52-
elif c.isdigit():
52+
elif c.isdecimal():
5353
cur += c
5454
else:
5555
raise dns.exception.SyntaxError(f"Could not parse {c}")

dns/inet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def low_level_address_tuple(high_tuple: tuple[str, int], af: int | None = None)
149149
# try to avoid getaddrinfo()
150150
addrpart = address[:i]
151151
scope = address[i + 1 :]
152-
if scope.isdigit():
152+
if scope.isdecimal():
153153
return (addrpart, port, 0, int(scope))
154154
try:
155155
return (addrpart, port, 0, socket.if_nametoindex(scope))

dns/name.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,14 +1004,14 @@ def from_unicode(
10041004
for c in text:
10051005
if escaping:
10061006
if edigits == 0:
1007-
if c.isdigit():
1007+
if c.isdecimal():
10081008
total = int(c)
10091009
edigits += 1
10101010
else:
10111011
label += c
10121012
escaping = False
10131013
else:
1014-
if not c.isdigit():
1014+
if not c.isdecimal():
10151015
raise BadEscape
10161016
total *= 10
10171017
total += int(c)

dns/rdtypes/ANY/GPOS.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ def _validate_float_string(what):
3636
raise dns.exception.FormError
3737
if left == b"" and right == b"":
3838
raise dns.exception.FormError
39-
if not left == b"" and not left.decode().isdigit():
39+
if not left == b"" and not left.isdigit():
4040
raise dns.exception.FormError
41-
if not right == b"" and not right.decode().isdigit():
41+
if not right == b"" and not right.isdigit():
4242
raise dns.exception.FormError
4343

4444

dns/rdtypes/ANY/LOC.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,16 @@ def from_text(
196196

197197
latitude[0] = tok.get_int()
198198
t = tok.get_string()
199-
if t.isdigit():
199+
if t.isdecimal():
200200
latitude[1] = int(t)
201201
t = tok.get_string()
202202
if "." in t:
203203
seconds, milliseconds = t.split(".")
204-
if not seconds.isdigit():
204+
if not seconds.isdecimal():
205205
raise dns.exception.SyntaxError("bad latitude seconds value")
206206
latitude[2] = int(seconds)
207207
l = len(milliseconds)
208-
if l == 0 or l > 3 or not milliseconds.isdigit():
208+
if l == 0 or l > 3 or not milliseconds.isdecimal():
209209
raise dns.exception.SyntaxError("bad latitude milliseconds value")
210210
if l == 1:
211211
m = 100
@@ -215,7 +215,7 @@ def from_text(
215215
m = 1
216216
latitude[3] = m * int(milliseconds)
217217
t = tok.get_string()
218-
elif t.isdigit():
218+
elif t.isdecimal():
219219
latitude[2] = int(t)
220220
t = tok.get_string()
221221
if t == "S":
@@ -225,16 +225,16 @@ def from_text(
225225

226226
longitude[0] = tok.get_int()
227227
t = tok.get_string()
228-
if t.isdigit():
228+
if t.isdecimal():
229229
longitude[1] = int(t)
230230
t = tok.get_string()
231231
if "." in t:
232232
seconds, milliseconds = t.split(".")
233-
if not seconds.isdigit():
233+
if not seconds.isdecimal():
234234
raise dns.exception.SyntaxError("bad longitude seconds value")
235235
longitude[2] = int(seconds)
236236
l = len(milliseconds)
237-
if l == 0 or l > 3 or not milliseconds.isdigit():
237+
if l == 0 or l > 3 or not milliseconds.isdecimal():
238238
raise dns.exception.SyntaxError("bad longitude milliseconds value")
239239
if l == 1:
240240
m = 100
@@ -244,7 +244,7 @@ def from_text(
244244
m = 1
245245
longitude[3] = m * int(milliseconds)
246246
t = tok.get_string()
247-
elif t.isdigit():
247+
elif t.isdecimal():
248248
longitude[2] = int(t)
249249
t = tok.get_string()
250250
if t == "W":

dns/rdtypes/IN/WKS.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ def from_text(
6060
):
6161
address = tok.get_string()
6262
protocol = tok.get_string()
63-
if protocol.isdigit():
63+
if protocol.isdecimal():
6464
protocol = int(protocol)
6565
else:
6666
protocol = socket.getprotobyname(protocol)
6767
bitmap = bytearray()
6868
for token in tok.get_remaining():
6969
value = token.unescape().value
70-
if value.isdigit():
70+
if value.isdecimal():
7171
serv = int(value)
7272
else:
7373
if protocol != _proto_udp and protocol != _proto_tcp:

dns/rdtypes/rrsigbase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BadSigTime(dns.exception.DNSException):
3434

3535

3636
def sigtime_to_posixtime(what):
37-
if len(what) <= 10 and what.isdigit():
37+
if len(what) <= 10 and what.isdecimal():
3838
return int(what)
3939
if len(what) != 14:
4040
raise BadSigTime

0 commit comments

Comments
 (0)