diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-07-22 00:34:34 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-07-22 00:34:34 +0300 |
commit | 707cae749424104660cd24e7c1f1e16d67b99b0f (patch) | |
tree | 792de897348ae678d4c9908870a328636f25ae6c /py | |
parent | 26b7d8a7be48211557cf009f3f792d926d03cdf2 (diff) | |
download | micropython-707cae749424104660cd24e7c1f1e16d67b99b0f.tar.gz micropython-707cae749424104660cd24e7c1f1e16d67b99b0f.zip |
py/obj: Issue a warning when str and bytes objects are compared.
Something like:
if foo == "bar":
will be always false if foo is b"bar". In CPython, warning is issued if
interpreter is started as "python3 -b". In MicroPython,
MICROPY_PY_STR_BYTES_CMP_WARN setting controls it.
Diffstat (limited to 'py')
-rw-r--r-- | py/mpconfig.h | 5 | ||||
-rw-r--r-- | py/obj.c | 8 |
2 files changed, 12 insertions, 1 deletions
diff --git a/py/mpconfig.h b/py/mpconfig.h index 19e290c37e..2ba5f1acf2 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -590,6 +590,11 @@ typedef double mp_float_t; #define MICROPY_PY_ASYNC_AWAIT (1) #endif +// Support for async/await/async for/async with +#ifndef MICROPY_PY_STR_BYTES_CMP_WARN +#define MICROPY_PY_STR_BYTES_CMP_WARN (0) +#endif + // Whether str object is proper unicode #ifndef MICROPY_PY_BUILTINS_STR_UNICODE #define MICROPY_PY_BUILTINS_STR_UNICODE (0) @@ -192,10 +192,16 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) { return mp_obj_str_equal(o1, o2); } else { // a string is never equal to anything else - return false; + goto str_cmp_err; } } else if (MP_OBJ_IS_STR(o2)) { // o1 is not a string (else caught above), so the objects are not equal + str_cmp_err: + #if MICROPY_PY_STR_BYTES_CMP_WARN + if (MP_OBJ_IS_TYPE(o1, &mp_type_bytes) || MP_OBJ_IS_TYPE(o2, &mp_type_bytes)) { + mp_warning("BytesWarning: Comparison between bytes and str"); + } + #endif return false; } |