From bf29fe2e138a30688cfb94ad3859f903f935ced1 Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 15 Mar 2017 12:17:38 +0100 Subject: [PATCH] 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'). --- py/objstr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/py/objstr.c b/py/objstr.c index 2331004a5b..1e71617bda 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2065,9 +2065,10 @@ STATIC void bad_implicit_conversion(mp_obj_t self_in) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError("can't convert to str implicitly"); } else { + const qstr src_name = mp_obj_get_type(self_in)->name; 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))); + "can't convert '%q' object to %q implicitly", + src_name, src_name == MP_QSTR_str ? MP_QSTR_bytes : MP_QSTR_str)); } }