@@ -54,6 +54,11 @@ STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
54
54
STATIC mp_obj_t mp_obj_new_bytes_iterator (mp_obj_t str );
55
55
STATIC mp_obj_t str_new (const mp_obj_type_t * type , const byte * data , uint len );
56
56
STATIC NORETURN void bad_implicit_conversion (mp_obj_t self_in );
57
+ STATIC NORETURN void arg_type_mixup ();
58
+
59
+ STATIC bool is_str_or_bytes (mp_obj_t o ) {
60
+ return MP_OBJ_IS_STR (o ) || MP_OBJ_IS_TYPE (o , & mp_type_bytes );
61
+ }
57
62
58
63
/******************************************************************************/
59
64
/* str */
@@ -1326,9 +1331,12 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
1326
1331
}
1327
1332
1328
1333
STATIC mp_obj_t str_partitioner (mp_obj_t self_in , mp_obj_t arg , machine_int_t direction ) {
1329
- assert (MP_OBJ_IS_STR (self_in ));
1330
- if (!MP_OBJ_IS_STR (arg )) {
1331
- bad_implicit_conversion (arg );
1334
+ if (!is_str_or_bytes (self_in )) {
1335
+ assert (0 );
1336
+ }
1337
+ mp_obj_type_t * self_type = mp_obj_get_type (self_in );
1338
+ if (self_type != mp_obj_get_type (arg )) {
1339
+ arg_type_mixup ();
1332
1340
}
1333
1341
1334
1342
GET_STR_DATA_LEN (self_in , str , str_len );
@@ -1349,9 +1357,9 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, machine_int_t di
1349
1357
const byte * position_ptr = find_subbytes (str , str_len , sep , sep_len , direction );
1350
1358
if (position_ptr != NULL ) {
1351
1359
machine_uint_t position = position_ptr - str ;
1352
- result [0 ] = mp_obj_new_str ( str , position , false );
1360
+ result [0 ] = str_new ( self_type , str , position );
1353
1361
result [1 ] = arg ;
1354
- result [2 ] = mp_obj_new_str ( str + position + sep_len , str_len - position - sep_len , false );
1362
+ result [2 ] = str_new ( self_type , str + position + sep_len , str_len - position - sep_len );
1355
1363
}
1356
1364
1357
1365
return mp_obj_new_tuple (3 , result );
@@ -1586,6 +1594,10 @@ STATIC void bad_implicit_conversion(mp_obj_t self_in) {
1586
1594
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 )));
1587
1595
}
1588
1596
1597
+ STATIC void arg_type_mixup () {
1598
+ nlr_raise (mp_obj_new_exception_msg (& mp_type_TypeError , "Can't mix str and bytes arguments" ));
1599
+ }
1600
+
1589
1601
uint mp_obj_str_get_hash (mp_obj_t self_in ) {
1590
1602
// TODO: This has too big overhead for hash accessor
1591
1603
if (MP_OBJ_IS_STR (self_in ) || MP_OBJ_IS_TYPE (self_in , & mp_type_bytes )) {
0 commit comments