diff options
author | Damien George <damien.p.george@gmail.com> | 2017-11-24 13:04:24 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-11-24 14:48:23 +1100 |
commit | 5e34a113eaaf736fb4f703a3ee0892e1705d0a63 (patch) | |
tree | 835e0a77f839a4bcffb689c901d989957dced749 /py/objstr.c | |
parent | 5b2f62aff312949c9c7edec6cfaaf4f97d93c442 (diff) |
py/runtime: Add MP_BINARY_OP_CONTAINS as reverse of MP_BINARY_OP_IN.
Before this patch MP_BINARY_OP_IN had two meanings: coming from bytecode it
meant that the args needed to be swapped, but coming from within the
runtime meant that the args were already in the correct order. This lead
to some confusion in the code and comments stating how args were reversed.
It also lead to 2 bugs: 1) containment for a subclass of a native type
didn't work; 2) the expression "{True} in True" would illegally succeed and
return True. In both of these cases it was because the args to
MP_BINARY_OP_IN ended up being reversed twice.
To fix these things this patch introduces MP_BINARY_OP_CONTAINS which
corresponds exactly to the __contains__ special method, and this is the
operator that built-in types should implement. MP_BINARY_OP_IN is now only
emitted by the compiler and is converted to MP_BINARY_OP_CONTAINS by
swapping the arguments.
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 3 |
1 files changed, 1 insertions, 2 deletions
diff --git a/py/objstr.c b/py/objstr.c index 1ff5132d2..b4f15b38d 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -384,8 +384,7 @@ mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i return mp_obj_new_str_from_vstr(lhs_type, &vstr); } - case MP_BINARY_OP_IN: - /* NOTE `a in b` is `b.__contains__(a)` */ + case MP_BINARY_OP_CONTAINS: return mp_obj_new_bool(find_subbytes(lhs_data, lhs_len, rhs_data, rhs_len, 1) != NULL); //case MP_BINARY_OP_NOT_EQUAL: // This is never passed here |