diff --git a/CHANGELOG.md b/CHANGELOG.md index 29373b09..31a3ec0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Speed up parsing of comments: the comment text is scanned in one pass up to the line break or the first invalid control character, instead of one character at a time. ([#723](https://github.com/python-poetry/tomlkit/pull/723)) +### Fixed + +- Reject a number written with non-ASCII digits (e.g. `1.٠`) or followed by a whitespace control character (e.g. `1` and a vertical tab), which `int()` and `float()` accepted but TOML does not allow. ([#676](https://github.com/python-poetry/tomlkit/pull/676)) + ## [0.15.3] - 2026-11-24 ### Changed diff --git a/tests/test_parser.py b/tests/test_parser.py index f3335e6a..cb5e94ec 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -297,6 +297,25 @@ def test_parser_accepts_uppercase_exponent_after_leading_zero() -> None: assert value == float(raw) +@pytest.mark.parametrize( + "raw", + [ + "\u0660", # ARABIC-INDIC DIGIT ZERO + "+\u0660", + "1.\u0660", + "1e\u0660", + "\u0661\u0662", + "1\x0b", # vertical tab + "1\x0c", # form feed + "1.5\x0b", + ], +) +def test_parser_rejects_non_ascii_digits_and_whitespace_in_numbers(raw: str) -> None: + # int() and float() accept these, but TOML numbers are ASCII digits only. + with pytest.raises(ParseError): + parse(f"v = {raw}\n") + + @pytest.mark.parametrize("sign", ["", "+", "-"]) @pytest.mark.parametrize( "raw", diff --git a/tomlkit/parser.py b/tomlkit/parser.py index f2d235f0..cda278de 100644 --- a/tomlkit/parser.py +++ b/tomlkit/parser.py @@ -777,6 +777,11 @@ def _parse_number(self, raw: str, trivia: Trivia) -> Item | None: if "_" in clean: return None + # int() and float() also accept non-ASCII digits and surrounding + # whitespace, which are not valid in a TOML number. + if not clean.isascii() or any(ch.isspace() for ch in clean): + return None + # Special float values must be lowercase in the original token. if clean in ("inf", "nan") and raw != clean: return None