Skip to content

Commit be8e99c

Browse files
committed
py: Allow bytes object as argument to some str methods.
This turns failing assertions to type exceptions for things like b"123".find(...). We still don't support operations like this on bytes objects (unlike CPython), but at least it no longer crashes.
1 parent a65c03c commit be8e99c

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

py/objstr.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,12 @@ STATIC mp_obj_t str_rsplit(mp_uint_t n_args, const mp_obj_t *args) {
621621
STATIC mp_obj_t str_finder(mp_uint_t n_args, const mp_obj_t *args, mp_int_t direction, bool is_index) {
622622
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
623623
assert(2 <= n_args && n_args <= 4);
624-
assert(MP_OBJ_IS_STR(args[0]));
625-
assert(MP_OBJ_IS_STR(args[1]));
624+
assert(MP_OBJ_IS_STR_OR_BYTES(args[0]));
625+
626+
// check argument type
627+
if (!MP_OBJ_IS_STR(args[1])) {
628+
bad_implicit_conversion(args[1]);
629+
}
626630

627631
GET_STR_DATA_LEN(args[0], haystack, haystack_len);
628632
GET_STR_DATA_LEN(args[1], needle, needle_len);
@@ -828,7 +832,7 @@ static mp_obj_t arg_as_int(mp_obj_t arg) {
828832
}
829833

830834
mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args) {
831-
assert(MP_OBJ_IS_STR(args[0]));
835+
assert(MP_OBJ_IS_STR_OR_BYTES(args[0]));
832836

833837
GET_STR_DATA_LEN(args[0], str, len);
834838
int arg_i = 0;
@@ -1190,7 +1194,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args) {
11901194
}
11911195

11921196
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_obj_t *args, mp_obj_t dict) {
1193-
assert(MP_OBJ_IS_STR(pattern));
1197+
assert(MP_OBJ_IS_STR_OR_BYTES(pattern));
11941198

11951199
GET_STR_DATA_LEN(pattern, str, len);
11961200
const byte *start_str = str;
@@ -1378,7 +1382,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
13781382
}
13791383

13801384
STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
1381-
assert(MP_OBJ_IS_STR(args[0]));
1385+
assert(MP_OBJ_IS_STR_OR_BYTES(args[0]));
13821386

13831387
mp_int_t max_rep = -1;
13841388
if (n_args == 4) {
@@ -1482,8 +1486,12 @@ STATIC mp_obj_t str_replace(mp_uint_t n_args, const mp_obj_t *args) {
14821486
STATIC mp_obj_t str_count(mp_uint_t n_args, const mp_obj_t *args) {
14831487
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
14841488
assert(2 <= n_args && n_args <= 4);
1485-
assert(MP_OBJ_IS_STR(args[0]));
1486-
assert(MP_OBJ_IS_STR(args[1]));
1489+
assert(MP_OBJ_IS_STR_OR_BYTES(args[0]));
1490+
1491+
// check argument type
1492+
if (!MP_OBJ_IS_STR(args[1])) {
1493+
bad_implicit_conversion(args[1]);
1494+
}
14871495

14881496
GET_STR_DATA_LEN(args[0], haystack, haystack_len);
14891497
GET_STR_DATA_LEN(args[1], needle, needle_len);
@@ -1872,7 +1880,7 @@ STATIC void arg_type_mixup() {
18721880

18731881
mp_uint_t mp_obj_str_get_hash(mp_obj_t self_in) {
18741882
// TODO: This has too big overhead for hash accessor
1875-
if (MP_OBJ_IS_STR(self_in) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytes)) {
1883+
if (MP_OBJ_IS_STR_OR_BYTES(self_in)) {
18761884
GET_STR_HASH(self_in, h);
18771885
return h;
18781886
} else {
@@ -1882,7 +1890,7 @@ mp_uint_t mp_obj_str_get_hash(mp_obj_t self_in) {
18821890

18831891
mp_uint_t mp_obj_str_get_len(mp_obj_t self_in) {
18841892
// TODO This has a double check for the type, one in obj.c and one here
1885-
if (MP_OBJ_IS_STR(self_in) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytes)) {
1893+
if (MP_OBJ_IS_STR_OR_BYTES(self_in)) {
18861894
GET_STR_LEN(self_in, l);
18871895
return l;
18881896
} else {

0 commit comments

Comments
 (0)