py/objstr: Make sure that b"%s" % b"foo" uses undecorated bytes value.
I.e. the expected result for above is b"foo", whereas previously we got
b"b'foo'".
diff --git a/py/objstr.c b/py/objstr.c
index 7765f42..fbe61a2 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -121,7 +121,7 @@
#else
bool is_bytes = true;
#endif
- if (!MICROPY_PY_BUILTINS_STR_UNICODE && kind == PRINT_STR && !is_bytes) {
+ if (kind == PRINT_RAW || (!MICROPY_PY_BUILTINS_STR_UNICODE && kind == PRINT_STR && !is_bytes)) {
mp_printf(print, "%.*s", str_len, str_data);
} else {
if (is_bytes) {
@@ -1296,6 +1296,7 @@
GET_STR_DATA_LEN(pattern, str, len);
const byte *start_str = str;
+ bool is_bytes = MP_OBJ_IS_TYPE(pattern, &mp_type_bytes);
int arg_i = 0;
vstr_t vstr;
mp_print_t print;
@@ -1444,7 +1445,13 @@
vstr_t arg_vstr;
mp_print_t arg_print;
vstr_init_print(&arg_vstr, 16, &arg_print);
- mp_obj_print_helper(&arg_print, arg, *str == 'r' ? PRINT_REPR : PRINT_STR);
+ mp_print_kind_t print_kind = (*str == 'r' ? PRINT_REPR : PRINT_STR);
+ if (print_kind == PRINT_STR && is_bytes && MP_OBJ_IS_TYPE(arg, &mp_type_bytes)) {
+ // If we have something like b"%s" % b"1", bytes arg should be
+ // printed undecorated.
+ print_kind = PRINT_RAW;
+ }
+ mp_obj_print_helper(&arg_print, arg, print_kind);
uint vlen = arg_vstr.len;
if (prec < 0) {
prec = vlen;