diff options
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/py/objstr.c b/py/objstr.c index 80b0cf876..33bfcc375 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -54,6 +54,11 @@ STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str); STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str); STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len); STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in); +STATIC NORETURN void arg_type_mixup(); + +STATIC bool is_str_or_bytes(mp_obj_t o) { + return MP_OBJ_IS_STR(o) || MP_OBJ_IS_TYPE(o, &mp_type_bytes); +} /******************************************************************************/ /* str */ @@ -535,7 +540,8 @@ enum { LSTRIP, RSTRIP, STRIP }; STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) { assert(1 <= n_args && n_args <= 2); - assert(MP_OBJ_IS_STR(args[0])); + assert(is_str_or_bytes(args[0])); + const mp_obj_type_t *self_type = mp_obj_get_type(args[0]); const byte *chars_to_del; uint chars_to_del_len; @@ -545,7 +551,9 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) { chars_to_del = whitespace; chars_to_del_len = sizeof(whitespace); } else { - assert(MP_OBJ_IS_STR(args[1])); + if (mp_obj_get_type(args[1]) != self_type) { + arg_type_mixup(); + } GET_STR_DATA_LEN(args[1], s, l); chars_to_del = s; chars_to_del_len = l; @@ -589,7 +597,7 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) { assert(last_good_char_pos >= first_good_char_pos); //+1 to accomodate the last character machine_uint_t stripped_len = last_good_char_pos - first_good_char_pos + 1; - return mp_obj_new_str(orig_str + first_good_char_pos, stripped_len, false); + return str_new(self_type, orig_str + first_good_char_pos, stripped_len); } STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) { @@ -1326,9 +1334,12 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) { } STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, machine_int_t direction) { - assert(MP_OBJ_IS_STR(self_in)); - if (!MP_OBJ_IS_STR(arg)) { - bad_implicit_conversion(arg); + if (!is_str_or_bytes(self_in)) { + assert(0); + } + mp_obj_type_t *self_type = mp_obj_get_type(self_in); + if (self_type != mp_obj_get_type(arg)) { + arg_type_mixup(); } GET_STR_DATA_LEN(self_in, str, str_len); @@ -1349,9 +1360,9 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, machine_int_t di const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction); if (position_ptr != NULL) { machine_uint_t position = position_ptr - str; - result[0] = mp_obj_new_str(str, position, false); + result[0] = str_new(self_type, str, position); result[1] = arg; - result[2] = mp_obj_new_str(str + position + sep_len, str_len - position - sep_len, false); + result[2] = str_new(self_type, str + position + sep_len, str_len - position - sep_len); } return mp_obj_new_tuple(3, result); @@ -1586,6 +1597,10 @@ STATIC void bad_implicit_conversion(mp_obj_t self_in) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Can't convert '%s' object to str implicitly", mp_obj_get_type_str(self_in))); } +STATIC void arg_type_mixup() { + nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Can't mix str and bytes arguments")); +} + uint mp_obj_str_get_hash(mp_obj_t self_in) { // TODO: This has too big overhead for hash accessor if (MP_OBJ_IS_STR(self_in) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytes)) { @@ -1632,7 +1647,7 @@ const char *mp_obj_str_get_str(mp_obj_t self_in) { } const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len) { - if (MP_OBJ_IS_STR(self_in)) { + if (is_str_or_bytes(self_in)) { GET_STR_DATA_LEN(self_in, s, l); *len = l; return (const char*)s; |