diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2015-12-20 16:44:36 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2015-12-20 16:51:59 +0200 |
commit | ef63ab5724dd7c5696ee5c07bf30ac7444df9fb6 (patch) | |
tree | 5b9a76f5453c4fb482dc45c604a446d05f150b19 | |
parent | 0a4eb4dbf2df34b9a2efcf55855b9db7c7132bf7 (diff) | |
download | micropython-ef63ab5724dd7c5696ee5c07bf30ac7444df9fb6.tar.gz micropython-ef63ab5724dd7c5696ee5c07bf30ac7444df9fb6.zip |
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'".
-rw-r--r-- | py/obj.h | 1 | ||||
-rw-r--r-- | py/objstr.c | 11 |
2 files changed, 10 insertions, 2 deletions
@@ -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; diff --git a/py/objstr.c b/py/objstr.c index 7765f426d9..fbe61a26f1 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -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; |