Commit ef63ab57 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

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'".
parent 0a4eb4db
......@@ -390,6 +390,7 @@ typedef enum {
PRINT_REPR = 1,
PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
PRINT_JSON = 3,
PRINT_RAW = 4, // Special format for printing bytes as an undercorated string
PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses
} mp_print_kind_t;
......
......@@ -121,7 +121,7 @@ STATIC void str_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t
#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 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
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 @@ not_enough_args:
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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment