Skip to content

Commit bf29fe2

Browse files
stinosdpgeorge
authored andcommitted
py/objstr: Use better msg in bad implicit str/bytes conversion exception
Instead of always reporting some object cannot be implicitly be converted to a 'str', even when it is a 'bytes' object, adjust the logic so that when trying to convert str to bytes it is shown like that. This will still report bad implicit conversion from e.g. 'int to bytes' as 'int to str' but it will not result in the confusing 'can't convert 'str' object to str implicitly' anymore for calls like b'somestring'.count('a').
1 parent 9b80a1e commit bf29fe2

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

py/objstr.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,9 +2065,10 @@ STATIC void bad_implicit_conversion(mp_obj_t self_in) {
20652065
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
20662066
mp_raise_TypeError("can't convert to str implicitly");
20672067
} else {
2068+
const qstr src_name = mp_obj_get_type(self_in)->name;
20682069
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
2069-
"can't convert '%s' object to str implicitly",
2070-
mp_obj_get_type_str(self_in)));
2070+
"can't convert '%q' object to %q implicitly",
2071+
src_name, src_name == MP_QSTR_str ? MP_QSTR_bytes : MP_QSTR_str));
20712072
}
20722073
}
20732074

0 commit comments

Comments
 (0)