diff options
author | Damien George <damien@micropython.org> | 2023-05-23 18:02:32 +1000 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2023-06-01 15:11:06 +1000 |
commit | 69dd013919461272b3669a516ef98d60accd3165 (patch) | |
tree | 31f5264eae7d4aac34f2c4c33b60ec740e4ed780 /py | |
parent | 66dc1397c92f6accf102bcd15c6395902fd46c8b (diff) |
py/objint: Allow int() to parse anything with the buffer protocol.
This generalises and simplifies the code and follows CPython behaviour.
See similar change for floats in a07fc5b6403b9a8bf7e7cb64f857272e5346d7e2.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py')
-rw-r--r-- | py/objint.c | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/py/objint.c b/py/objint.c index e54cdb89b..be5f4653a 100644 --- a/py/objint.c +++ b/py/objint.c @@ -49,14 +49,13 @@ STATIC mp_obj_t mp_obj_int_make_new(const mp_obj_type_t *type_in, size_t n_args, return MP_OBJ_NEW_SMALL_INT(0); case 1: { + mp_buffer_info_t bufinfo; mp_obj_t o = mp_unary_op(MP_UNARY_OP_INT_MAYBE, args[0]); if (o != MP_OBJ_NULL) { return o; - } else if (mp_obj_is_str_or_bytes(args[0])) { - // a string, parse it - size_t l; - const char *s = mp_obj_str_get_data(args[0], &l); - return mp_parse_num_integer(s, l, 0, NULL); + } else if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ)) { + // a textual representation, parse it + return mp_parse_num_integer(bufinfo.buf, bufinfo.len, 0, NULL); #if MICROPY_PY_BUILTINS_FLOAT } else if (mp_obj_is_float(args[0])) { return mp_obj_new_int_from_float(mp_obj_float_get(args[0])); |