diff options
author | Damien George <damien.p.george@gmail.com> | 2015-05-12 23:05:53 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-05-12 23:05:53 +0100 |
commit | c50772d19fe2804a6d0258f89dd9967d3b516fd3 (patch) | |
tree | 9db781cc50f27fd4d07f9ef1db9a862a29690d53 /py | |
parent | c2a4e4effc81d8ab21bb014e34355643e5ca0da2 (diff) | |
download | micropython-c50772d19fe2804a6d0258f89dd9967d3b516fd3.tar.gz micropython-c50772d19fe2804a6d0258f89dd9967d3b516fd3.zip |
py: Add mp_obj_get_int_truncated and use it where appropriate.
mp_obj_get_int_truncated will raise a TypeError if the argument is not
an integral type. Use mp_obj_int_get_truncated only when you know the
argument is a small or big int.
Diffstat (limited to 'py')
-rw-r--r-- | py/obj.c | 8 | ||||
-rw-r--r-- | py/obj.h | 1 | ||||
-rw-r--r-- | py/objtype.c | 2 | ||||
-rw-r--r-- | py/stream.c | 2 |
4 files changed, 11 insertions, 2 deletions
@@ -231,6 +231,14 @@ mp_int_t mp_obj_get_int(mp_const_obj_t arg) { } } +mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) { + if (MP_OBJ_IS_INT(arg)) { + return mp_obj_int_get_truncated(arg); + } else { + return mp_obj_get_int(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 mp_int_t @@ -511,6 +511,7 @@ bool mp_obj_is_callable(mp_obj_t o_in); bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2); mp_int_t mp_obj_get_int(mp_const_obj_t arg); +mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg); bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value); #if MICROPY_PY_BUILTINS_FLOAT mp_float_t mp_obj_get_float(mp_obj_t self_in); diff --git a/py/objtype.c b/py/objtype.c index 7b293c9598..f593271fb5 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -359,7 +359,7 @@ STATIC mp_obj_t instance_unary_op(mp_uint_t op, mp_obj_t self_in) { mp_obj_t val = mp_call_function_1(member[0], self_in); // __hash__ must return a small int if (op == MP_UNARY_OP_HASH) { - val = MP_OBJ_NEW_SMALL_INT(mp_obj_int_get_truncated(val)); + val = MP_OBJ_NEW_SMALL_INT(mp_obj_get_int_truncated(val)); } return val; } else { diff --git a/py/stream.c b/py/stream.c index 640659ec5a..258f916e08 100644 --- a/py/stream.c +++ b/py/stream.c @@ -225,7 +225,7 @@ STATIC mp_obj_t stream_readinto(mp_uint_t n_args, const mp_obj_t *args) { // https://docs.python.org/3/library/socket.html#socket.socket.recv_into mp_uint_t len = bufinfo.len; if (n_args > 2) { - len = mp_obj_int_get_truncated(args[2]); + len = mp_obj_get_int(args[2]); if (len > bufinfo.len) { len = bufinfo.len; } |