diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-02 23:04:09 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-02 23:04:09 +0000 |
commit | 6fd4b36bc5dfbb6c6a3f85e14269589e6613d26d (patch) | |
tree | b069fd33c8d9b3a7532736d6d08da1e28427a002 /py/objint_mpz.c | |
parent | 6e0b6d02dbe238e6e3d675b51e44b2ac798ddd20 (diff) |
py: Raise exception if trying to convert inf/nan to int.
Diffstat (limited to 'py/objint_mpz.c')
-rw-r--r-- | py/objint_mpz.c | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 23e300023..49a9a91e2 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -298,9 +298,16 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) { #if MICROPY_PY_BUILTINS_FLOAT mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { - mp_obj_int_t *o = mp_obj_int_new_mpz(); - mpz_set_from_float(&o->mpz, val); - return o; + int cl = fpclassify(val); + if (cl == FP_INFINITE) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int")); + } else if (cl == FP_NAN) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int")); + } else { + mp_obj_int_t *o = mp_obj_int_new_mpz(); + mpz_set_from_float(&o->mpz, val); + return o; + } } #endif |