Skip to content

Commit 9c2e30d

Browse files
committed
gh-153392: Raise TOMLDecodeError for over-long integers in tomllib
The number branch of tomllib's value parser called match_to_number without wrapping ValueError, so an integer with more digits than the interpreter's integer string conversion limit escaped as a bare ValueError. The date and time branch already wraps ValueError into TOMLDecodeError; do the same for the number branch so tomllib only raises TOMLDecodeError on a malformed document.
1 parent e933987 commit 9c2e30d

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

Lib/test/test_tomllib/test_error.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ def test_missing_value(self):
3434
tomllib.loads("\n\nfwfw=")
3535
self.assertEqual(str(exc_info.exception), "Invalid value (at end of document)")
3636

37+
def test_overlong_integer(self):
38+
# An integer with more digits than the int-to-str conversion limit
39+
# must raise TOMLDecodeError, not the bare ValueError from int().
40+
from test.support import adjust_int_max_str_digits
41+
with adjust_int_max_str_digits(1000):
42+
digits = "9" * 1001
43+
for src in (f"x = {digits}", f"x = -{digits}"):
44+
with self.assertRaises(tomllib.TOMLDecodeError):
45+
tomllib.loads(src)
46+
3747
def test_invalid_char_quotes(self):
3848
with self.assertRaises(tomllib.TOMLDecodeError) as exc_info:
3949
tomllib.loads("v = '\n'")

Lib/tomllib/_parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,11 @@ def parse_value(
738738
# char, so needs to be located after handling of dates and times.
739739
number_match = RE_NUMBER.match(src, pos)
740740
if number_match:
741-
return number_match.end(), match_to_number(number_match, parse_float)
741+
try:
742+
number = match_to_number(number_match, parse_float)
743+
except ValueError as e:
744+
raise TOMLDecodeError("Invalid number", src, pos) from e
745+
return number_match.end(), number
742746

743747
# Special floats
744748
first_three = src[pos : pos + 3]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`tomllib.loads` now raises :exc:`tomllib.TOMLDecodeError` instead of
2+
:exc:`ValueError` when a document contains an integer with more digits than the
3+
interpreter's integer string conversion limit.

0 commit comments

Comments
 (0)