Skip to content

Commit 46f6881

Browse files
committed
Validate with isascii() + isdigit() instead of a per-character check
1 parent e780e2c commit 46f6881

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

Lib/_pydatetime.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,11 @@ def _parse_isoformat_date(dtstr):
360360
# see the comment on Modules/_datetimemodule.c:_find_isoformat_datetime_separator
361361
if len(dtstr) not in (7, 8, 10):
362362
raise ValueError("Invalid isoformat string")
363+
if not dtstr.isascii():
364+
raise ValueError(f"Invalid isoformat string: {dtstr!r}")
363365
def _read(s, n):
364-
# Require exactly n ASCII digits, as the C parse_digits() does.
365-
if len(s) != n or not all(map(_is_ascii_digit, s)):
366+
# dtstr is ASCII, so isdigit() matches only ASCII digits.
367+
if len(s) != n or not s.isdigit():
366368
raise ValueError(f"Invalid isoformat string: {dtstr!r}")
367369
return int(s)
368370

0 commit comments

Comments
 (0)