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).
This commit is contained in:
Damien George
2017-10-03 17:56:27 +11:00
parent 01978648fd
commit 2ac1364688
4 changed files with 16 additions and 14 deletions

View File

@@ -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);