py: Use mp_obj_get_array where sequence may be a tuple or a list.

This commit is contained in:
Krzysztof Blazewicz 2017-03-04 12:29:20 +01:00 committed by Damien George
parent 1215dc47e2
commit 7e480e8a30
3 changed files with 10 additions and 27 deletions

View File

@ -511,17 +511,11 @@ STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
// convert float to int (could also pass in float registers)
return (mp_int_t)mp_obj_float_get(obj);
#endif
} else if (type == &mp_type_tuple) {
} else if (type == &mp_type_tuple || type == &mp_type_list) {
// pointer to start of tuple (could pass length, but then could use len(x) for that)
mp_uint_t len;
mp_obj_t *items;
mp_obj_tuple_get(obj, &len, &items);
return (mp_uint_t)items;
} else if (type == &mp_type_list) {
// pointer to start of list (could pass length, but then could use len(x) for that)
mp_uint_t len;
mp_obj_t *items;
mp_obj_list_get(obj, &len, &items);
mp_obj_get_array(obj, &len, &items);
return (mp_uint_t)items;
} else {
mp_buffer_info_t bufinfo;

View File

@ -430,16 +430,13 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
// process args
mp_uint_t seq_len;
mp_obj_t *seq_items;
if (MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
mp_obj_tuple_get(arg, &seq_len, &seq_items);
} else {
if (!MP_OBJ_IS_TYPE(arg, &mp_type_list)) {
// arg is not a list, try to convert it to one
// TODO: Try to optimize?
arg = mp_type_list.make_new(&mp_type_list, 1, 0, &arg);
}
mp_obj_list_get(arg, &seq_len, &seq_items);
if (!MP_OBJ_IS_TYPE(arg, &mp_type_list) && !MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
// arg is not a list nor a tuple, try to convert it to a list
// TODO: Try to optimize?
arg = mp_type_list.make_new(&mp_type_list, 1, 0, &arg);
}
mp_obj_get_array(arg, &seq_len, &seq_items);
// count required length
size_t required_len = 0;

View File

@ -797,11 +797,7 @@ void mp_unpack_sequence(mp_obj_t seq_in, size_t num, mp_obj_t *items) {
mp_uint_t seq_len;
if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
mp_obj_t *seq_items;
if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
} else {
mp_obj_list_get(seq_in, &seq_len, &seq_items);
}
mp_obj_get_array(seq_in, &seq_len, &seq_items);
if (seq_len < num) {
goto too_short;
} else if (seq_len > num) {
@ -851,11 +847,7 @@ void mp_unpack_ex(mp_obj_t seq_in, size_t num_in, mp_obj_t *items) {
mp_uint_t seq_len;
if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
mp_obj_t *seq_items;
if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
} else {
mp_obj_list_get(seq_in, &seq_len, &seq_items);
}
mp_obj_get_array(seq_in, &seq_len, &seq_items);
if (seq_len < num_left + num_right) {
goto too_short;
}