Skip to content

Commit b48c094

Browse files
reject overlong decimal integer instead of coercing to inf (#538)
* reject overlong decimal integer instead of coercing to inf * skip overlong-integer test when the int digit limit is unavailable Python 3.9.13 and earlier lack the int-from-string conversion limit (backported in 3.9.14), so int() succeeds on any digit count and the parser never rejects the literal. The macOS and Windows 3.9 CI jobs run 3.9.13, which is why they failed while Ubuntu's 3.9 passed.
1 parent cc05afa commit b48c094

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎tests/test_parser.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import sys
2+
13
import pytest
24

35
from tomlkit.exceptions import EmptyTableNameError
46
from tomlkit.exceptions import InternalParserError
7+
from tomlkit.exceptions import InvalidNumberError
58
from tomlkit.exceptions import InvalidUnicodeValueError
69
from tomlkit.exceptions import ParseError
710
from tomlkit.exceptions import UnexpectedCharError
11+
from tomlkit.items import Integer
812
from tomlkit.items import StringType
913
from tomlkit.parser import Parser
1014

@@ -214,3 +218,20 @@ def test_parser_rejects_aot_header_missing_second_bracket(content: str) -> None:
214218
parser = Parser(content)
215219
with pytest.raises(ParseError):
216220
parser.parse()
221+
222+
223+
@pytest.mark.skipif(
224+
not hasattr(sys, "get_int_max_str_digits"),
225+
reason="requires the int-from-string digit limit (3.9.14+/3.10.7+/3.11+)",
226+
)
227+
def test_parser_rejects_overlong_decimal_integer() -> None:
228+
# A decimal integer with more digits than Python's int-from-string limit
229+
# raises ValueError in int(); it must be reported as an invalid number, not
230+
# silently fall through to float() and become inf.
231+
parser = Parser("a = " + "9" * 4301)
232+
with pytest.raises(InvalidNumberError):
233+
parser.parse()
234+
235+
# the value just under the limit is still a normal integer
236+
value = Parser("a = " + "9" * 4300).parse()["a"]
237+
assert isinstance(value, Integer)

‎tomlkit/parser.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,20 @@ def _parse_number(self, raw: str, trivia: Trivia) -> Item | None:
783783
try:
784784
return Integer(int(sign + clean, base), trivia, sign + raw)
785785
except ValueError:
786+
pass
787+
788+
# Only fall back to float for an actual float literal (a fractional
789+
# dot, a base-10 exponent, or inf/nan). A decimal integer whose digit
790+
# count exceeds Python's int-from-string conversion limit also raises
791+
# ValueError above; it must be rejected, not silently coerced to inf.
792+
if base == 10 and ("." in clean or "e" in clean or clean in ("inf", "nan")):
786793
try:
787794
return Float(float(sign + clean), trivia, sign + raw)
788795
except ValueError:
789796
return None
790797

798+
return None
799+
791800
def _parse_literal_string(self) -> String:
792801
with self._state:
793802
return self._parse_string(StringType.SLL)

0 commit comments

Comments
 (0)