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/runtime0.h | |
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/runtime0.h')
-rw-r--r-- | py/runtime0.h | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/py/runtime0.h b/py/runtime0.h index a72b7feb7..960532d17 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -131,6 +131,10 @@ typedef enum { #endif , + // The runtime will convert MP_BINARY_OP_IN to this operator with swapped args. + // A type should implement this containment operator instead of MP_BINARY_OP_IN. + MP_BINARY_OP_CONTAINS, + MP_BINARY_OP_NUM_RUNTIME, // These 2 are not supported by the runtime and must be synthesised by the emitter |