Reject invalid strings in from_str()

Closes #885
This commit is contained in:
Sergey B Kirpichev
2024-11-05 07:04:16 +03:00
parent 5235f441db
commit 30e8001e27
2 changed files with 4 additions and 1 deletions
+3 -1
View File
@@ -1307,9 +1307,11 @@ def str_to_man_exp(x, base=10):
parts = x.split(sep)
if len(parts) == 1:
exp = 0
else: # == 2
elif len(parts) == 2:
x = parts[0]
exp = int(parts[1])
else:
raise ValueError("couldn't convert a str to mpf")
# Look for radix point in mantissa
parts = x.split('.')
if len(parts) == 2:
+1
View File
@@ -79,6 +79,7 @@ def test_to_str():
x = mpf('1234.567891')._mpf_
pytest.raises(ValueError, lambda: to_str(x, 6, binary_exp=True))
pytest.raises(ValueError, lambda: to_str(x, 6, rounding='Y'))
pytest.raises(ValueError, lambda: to_str('1e400e2', 6))
assert to_str(x, 5, rounding='n') == '1234.6'
assert to_str(x, 5, rounding='d') == '1234.5'
assert to_str(x, 5, rounding='u') == '1234.6'