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/objtype.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/objtype.c')
-rw-r--r-- | py/objtype.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/py/objtype.c b/py/objtype.c index 01d248256..267cae815 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -424,7 +424,7 @@ const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = { [MP_BINARY_OP_LESS_EQUAL] = MP_QSTR___le__, [MP_BINARY_OP_MORE_EQUAL] = MP_QSTR___ge__, // MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result - [MP_BINARY_OP_IN] = MP_QSTR___contains__, + [MP_BINARY_OP_CONTAINS] = MP_QSTR___contains__, // All inplace methods are optional, and normal methods will be used // as a fallback. |