summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-10-03 17:56:27 +1100
committerDamien George <damien.p.george@gmail.com>2017-10-03 17:56:27 +1100
commit2ac1364688cd3ee313661e82a336663551986fc8 (patch)
tree4e69bc7e8d09cb3e4456275e7eba8fcffcd4de0c /py
parent01978648fdc317c13b17ba186c82df4fb8a5cbac (diff)
downloadmicropython-2ac1364688cd3ee313661e82a336663551986fc8.tar.gz
micropython-2ac1364688cd3ee313661e82a336663551986fc8.zip
py/objset: Check that RHS of a binary op is a set/frozenset.
CPython docs explicitly state that the RHS of a set/frozenset binary op must be a set to prevent user errors. It also preserves commutativity of the ops, eg: "abc" & set() is a TypeError, and so should be set() & "abc". This change actually decreases unix (x64) code by 160 bytes; it increases stm32 by 4 bytes and esp8266 by 28 bytes (but previous patch already introduced a much large saving).
Diffstat (limited to 'py')
-rw-r--r--py/objset.c4
1 files changed, 4 insertions, 0 deletions
diff --git a/py/objset.c b/py/objset.c
index 6dede887c9..80ed263340 100644
--- a/py/objset.c
+++ b/py/objset.c
@@ -463,6 +463,10 @@ STATIC mp_obj_t set_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
#else
bool update = true;
#endif
+ if (op != MP_BINARY_OP_IN && !is_set_or_frozenset(rhs)) {
+ // For all ops except containment the RHS must be a set/frozenset
+ return MP_OBJ_NULL;
+ }
switch (op) {
case MP_BINARY_OP_OR:
return set_union(lhs, rhs);