Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_tomllib/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def test_missing_value(self):
tomllib.loads("\n\nfwfw=")
self.assertEqual(str(exc_info.exception), "Invalid value (at end of document)")

def test_overlong_integer(self):
# An integer with more digits than the int-to-str conversion limit
# must raise TOMLDecodeError, not the bare ValueError from int().
from test.support import adjust_int_max_str_digits
with adjust_int_max_str_digits(1000):
digits = "9" * 1001
for src in (f"x = {digits}", f"x = -{digits}"):
with self.assertRaises(tomllib.TOMLDecodeError):
tomllib.loads(src)

def test_invalid_char_quotes(self):
with self.assertRaises(tomllib.TOMLDecodeError) as exc_info:
tomllib.loads("v = '\n'")
Expand Down
9 changes: 8 additions & 1 deletion Lib/tomllib/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,14 @@ def parse_value(
# char, so needs to be located after handling of dates and times.
number_match = RE_NUMBER.match(src, pos)
if number_match:
return number_match.end(), match_to_number(number_match, parse_float)
try:
number = match_to_number(number_match, parse_float)
except ValueError as e:
if number_match.group("floatpart"):
# A failing parse_float raises its own error unchanged.
raise
raise TOMLDecodeError("Invalid number", src, pos) from e
return number_match.end(), number

# Special floats
first_three = src[pos : pos + 3]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`tomllib.loads` now raises :exc:`tomllib.TOMLDecodeError` instead of
:exc:`ValueError` when a document contains an integer with more digits than the
interpreter's integer string conversion limit.
Loading