diff options
author | Damien George <damien.p.george@gmail.com> | 2015-03-14 22:07:30 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-03-14 22:07:30 +0000 |
commit | 6837d46c1da0e7dd9cf73969bb58649222f455a4 (patch) | |
tree | dd23d5419797b3f849bae0285199fc65b712d56f /py/objint.c | |
parent | 26a9975fba2bbd8875f2671495003b9bdcb8d8b2 (diff) |
py: Fix builtin abs so it works for bools and bignum.
Diffstat (limited to 'py/objint.c')
-rw-r--r-- | py/objint.c | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/py/objint.c b/py/objint.c index 0fb2a3826..26e1cc048 100644 --- a/py/objint.c +++ b/py/objint.c @@ -266,6 +266,16 @@ bool mp_obj_int_is_positive(mp_obj_t self_in) { return mp_obj_get_int(self_in) >= 0; } +// This must handle int and bool types, and must raise a +// TypeError if the argument is not integral +mp_obj_t mp_obj_int_abs(mp_obj_t self_in) { + mp_int_t val = mp_obj_get_int(self_in); + if (val < 0) { + val = -val; + } + return MP_OBJ_NEW_SMALL_INT(val); +} + // This is called for operations on SMALL_INT that are not handled by mp_unary_op mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) { return MP_OBJ_NULL; // op not supported |