@@ -46,7 +46,6 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
4646mp_obj_t mp_obj_new_str_iterator (mp_obj_t str );
4747STATIC mp_obj_t mp_obj_new_bytes_iterator (mp_obj_t str );
4848STATIC NORETURN void bad_implicit_conversion (mp_obj_t self_in );
49- STATIC NORETURN void arg_type_mixup (void );
5049
5150/******************************************************************************/
5251/* str */
@@ -525,7 +524,7 @@ STATIC mp_obj_t str_split(mp_uint_t n_args, const mp_obj_t *args) {
525524 } else {
526525 // sep given
527526 if (mp_obj_get_type (sep ) != self_type ) {
528- arg_type_mixup ( );
527+ bad_implicit_conversion ( sep );
529528 }
530529
531530 mp_uint_t sep_len ;
@@ -627,7 +626,7 @@ STATIC mp_obj_t str_finder(mp_uint_t n_args, const mp_obj_t *args, mp_int_t dire
627626 assert (MP_OBJ_IS_STR_OR_BYTES (args [0 ]));
628627
629628 // check argument type
630- if (! MP_OBJ_IS_STR (args [1 ])) {
629+ if (mp_obj_get_type (args [1 ]) != self_type ) {
631630 bad_implicit_conversion (args [1 ]);
632631 }
633632
@@ -720,7 +719,7 @@ STATIC mp_obj_t str_uni_strip(int type, mp_uint_t n_args, const mp_obj_t *args)
720719 chars_to_del_len = sizeof (whitespace );
721720 } else {
722721 if (mp_obj_get_type (args [1 ]) != self_type ) {
723- arg_type_mixup ( );
722+ bad_implicit_conversion ( args [ 1 ] );
724723 }
725724 GET_STR_DATA_LEN (args [1 ], s , l );
726725 chars_to_del = s ;
@@ -759,7 +758,11 @@ STATIC mp_obj_t str_uni_strip(int type, mp_uint_t n_args, const mp_obj_t *args)
759758
760759 if (!first_good_char_pos_set ) {
761760 // string is all whitespace, return ''
762- return MP_OBJ_NEW_QSTR (MP_QSTR_ );
761+ if (self_type == & mp_type_str ) {
762+ return MP_OBJ_NEW_QSTR (MP_QSTR_ );
763+ } else {
764+ return mp_const_empty_bytes ;
765+ }
763766 }
764767
765768 assert (last_good_char_pos >= first_good_char_pos );
@@ -1470,11 +1473,13 @@ STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
14701473
14711474 // check argument types
14721475
1473- if (!MP_OBJ_IS_STR (args [1 ])) {
1476+ const mp_obj_type_t * self_type = mp_obj_get_type (args [0 ]);
1477+
1478+ if (mp_obj_get_type (args [1 ]) != self_type ) {
14741479 bad_implicit_conversion (args [1 ]);
14751480 }
14761481
1477- if (! MP_OBJ_IS_STR (args [2 ])) {
1482+ if (mp_obj_get_type (args [2 ]) != self_type ) {
14781483 bad_implicit_conversion (args [2 ]);
14791484 }
14801485
@@ -1543,7 +1548,7 @@ STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
15431548 return args [0 ];
15441549 } else {
15451550 // substr found, allocate new string
1546- replaced_str = mp_obj_str_builder_start (mp_obj_get_type ( args [ 0 ]) , replaced_str_index , & data );
1551+ replaced_str = mp_obj_str_builder_start (self_type , replaced_str_index , & data );
15471552 assert (data != NULL );
15481553 }
15491554 } else {
@@ -1561,7 +1566,7 @@ STATIC mp_obj_t str_count(mp_uint_t n_args, const mp_obj_t *args) {
15611566 assert (MP_OBJ_IS_STR_OR_BYTES (args [0 ]));
15621567
15631568 // check argument type
1564- if (! MP_OBJ_IS_STR (args [1 ])) {
1569+ if (mp_obj_get_type (args [1 ]) != self_type ) {
15651570 bad_implicit_conversion (args [1 ]);
15661571 }
15671572
@@ -1597,12 +1602,10 @@ STATIC mp_obj_t str_count(mp_uint_t n_args, const mp_obj_t *args) {
15971602}
15981603
15991604STATIC mp_obj_t str_partitioner (mp_obj_t self_in , mp_obj_t arg , mp_int_t direction ) {
1600- if (!MP_OBJ_IS_STR_OR_BYTES (self_in )) {
1601- assert (0 );
1602- }
1605+ assert (MP_OBJ_IS_STR_OR_BYTES (self_in ));
16031606 mp_obj_type_t * self_type = mp_obj_get_type (self_in );
16041607 if (self_type != mp_obj_get_type (arg )) {
1605- arg_type_mixup ( );
1608+ bad_implicit_conversion ( arg );
16061609 }
16071610
16081611 GET_STR_DATA_LEN (self_in , str , str_len );
@@ -1612,7 +1615,16 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, mp_int_t directi
16121615 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , "empty separator" ));
16131616 }
16141617
1615- mp_obj_t result [] = {MP_OBJ_NEW_QSTR (MP_QSTR_ ), MP_OBJ_NEW_QSTR (MP_QSTR_ ), MP_OBJ_NEW_QSTR (MP_QSTR_ )};
1618+ mp_obj_t result [3 ];
1619+ if (self_type == & mp_type_str ) {
1620+ result [0 ] = MP_OBJ_NEW_QSTR (MP_QSTR_ );
1621+ result [1 ] = MP_OBJ_NEW_QSTR (MP_QSTR_ );
1622+ result [2 ] = MP_OBJ_NEW_QSTR (MP_QSTR_ );
1623+ } else {
1624+ result [0 ] = mp_const_empty_bytes ;
1625+ result [1 ] = mp_const_empty_bytes ;
1626+ result [2 ] = mp_const_empty_bytes ;
1627+ }
16161628
16171629 if (direction > 0 ) {
16181630 result [0 ] = self_in ;
@@ -1953,10 +1965,6 @@ STATIC void bad_implicit_conversion(mp_obj_t self_in) {
19531965 }
19541966}
19551967
1956- STATIC void arg_type_mixup (void ) {
1957- nlr_raise (mp_obj_new_exception_msg (& mp_type_TypeError , "Can't mix str and bytes arguments" ));
1958- }
1959-
19601968mp_uint_t mp_obj_str_get_hash (mp_obj_t self_in ) {
19611969 // TODO: This has too big overhead for hash accessor
19621970 if (MP_OBJ_IS_STR_OR_BYTES (self_in )) {
0 commit comments