Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/pyfloyd/datafile/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,10 @@ def parse_number(self, val: str) -> Any:
if s.startswith('0o'):
return int(s, base=8)
if '.' in s or 'e' in s or 'E' in s:
return float(s)
try:
return float(s)
except ValueError:
raise DatafileError(f'Invalid number "{val}"') from None
return int(s)

def parse_numword(self, val: str, as_key: bool) -> Any:
Expand Down
10 changes: 10 additions & 0 deletions tests/datafile/decode_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ def test_long_str(self):
self.check("L'=='foo'=='", 'foo')
self.check("L'=='f'='o'=='", "f'='o")

def test_invalid_number(self):
# The grammar's `exp` rule allows an exponent with no digits (e.g.,
# "2.5e"), so a number token can reach the float() conversion and
# fail there. That should be reported as a DatafileError, like any
# other invalid document, not escape as a raw ValueError.
for s in ('2.5e', '2e', '1e+', '2.5e+', '[2.5e]', '{k: 2.5e}'):
with self.assertRaises(datafile.DatafileError) as cm:
datafile.loads(s)
self.assertIn('Invalid number', str(cm.exception))

def test_bare_word(self):
self.check('foo', 'foo')
self.check('@foo', '@foo')
Expand Down