Skip to content

Commit 69f3eb2

Browse files
committed
objstr: Make .[r]partition() work with bytes.
1 parent 285683d commit 69f3eb2

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

py/objstr.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
5454
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
5555
STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len);
5656
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+
}
5762

5863
/******************************************************************************/
5964
/* str */
@@ -1326,9 +1331,12 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
13261331
}
13271332

13281333
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();
13321340
}
13331341

13341342
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
13491357
const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction);
13501358
if (position_ptr != NULL) {
13511359
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);
13531361
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);
13551363
}
13561364

13571365
return mp_obj_new_tuple(3, result);
@@ -1586,6 +1594,10 @@ STATIC void bad_implicit_conversion(mp_obj_t self_in) {
15861594
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)));
15871595
}
15881596

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+
15891601
uint mp_obj_str_get_hash(mp_obj_t self_in) {
15901602
// TODO: This has too big overhead for hash accessor
15911603
if (MP_OBJ_IS_STR(self_in) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytes)) {

tests/basics/string_partition.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@
2727
print("Raised ValueError")
2828
else:
2929
print("Did not raise ValueError")
30+
31+
# Bytes
32+
print(b"abba".partition(b'b'))
33+
try:
34+
print(b"abba".partition('b'))
35+
except TypeError:
36+
print("Raised TypeError")
37+
try:
38+
print("abba".partition(b'b'))
39+
except TypeError:
40+
print("Raised TypeError")

0 commit comments

Comments
 (0)