diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-03 11:00:54 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-03 11:00:54 +0000 |
commit | 8270e3853dc167d2d7946bb0de7a0f0bb2adde48 (patch) | |
tree | e261ee333b53fbe4560c94e37a531eaae2c0d60e /py/obj.c | |
parent | a58a7aefbd330261cc5c79c9fc9d5c6a12d2aeeb (diff) | |
download | micropython-8270e3853dc167d2d7946bb0de7a0f0bb2adde48.tar.gz micropython-8270e3853dc167d2d7946bb0de7a0f0bb2adde48.zip |
py: More robust int conversion and overflow checking.
Diffstat (limited to 'py/obj.c')
-rw-r--r-- | py/obj.c | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -203,6 +203,24 @@ machine_int_t mp_obj_get_int(mp_obj_t arg) { } } +// returns false if arg is not of integral type +// returns true and sets *value if it is of integral type +// can throw OverflowError if arg is of integral type, but doesn't fit in a machine_int_t +bool mp_obj_get_int_maybe(mp_obj_t arg, machine_int_t *value) { + if (arg == mp_const_false) { + *value = 0; + } else if (arg == mp_const_true) { + *value = 1; + } else if (MP_OBJ_IS_SMALL_INT(arg)) { + *value = MP_OBJ_SMALL_INT_VALUE(arg); + } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) { + *value = mp_obj_int_get_checked(arg); + } else { + return false; + } + return true; +} + #if MICROPY_ENABLE_FLOAT mp_float_t mp_obj_get_float(mp_obj_t arg) { if (arg == mp_const_false) { |