diff --git a/mpmath/libmp/libmpf.py b/mpmath/libmp/libmpf.py index 18399b15..366909d2 100644 --- a/mpmath/libmp/libmpf.py +++ b/mpmath/libmp/libmpf.py @@ -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: diff --git a/mpmath/tests/test_convert.py b/mpmath/tests/test_convert.py index 1c40af0f..bdb5ca97 100644 --- a/mpmath/tests/test_convert.py +++ b/mpmath/tests/test_convert.py @@ -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'