@@ -568,22 +568,57 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
568568 } else if (MP_OBJ_IS_TYPE (rhs , & complex_type )) {
569569 return mp_obj_complex_binary_op (op , lhs_val , 0 , rhs );
570570 }
571- } else {
572- if (MP_OBJ_IS_OBJ (lhs )) {
573- mp_obj_base_t * o = lhs ;
571+ }
572+
573+ /* deal with `in` and `not in`
574+ *
575+ * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
576+ * needs to go below
577+ */
578+ if (op == RT_COMPARE_OP_IN || op == RT_COMPARE_OP_NOT_IN ) {
579+ if (!MP_OBJ_IS_SMALL_INT (rhs )) {
580+ mp_obj_base_t * o = rhs ;
574581 if (o -> type -> binary_op != NULL ) {
575- mp_obj_t result = o -> type -> binary_op (op , lhs , rhs );
576- if (result != NULL ) {
577- return result ;
582+ mp_obj_t res = o -> type -> binary_op (op , rhs , lhs );
583+ if (res != NULL ) {
584+ return res ;
578585 }
579586 }
587+ if (o -> type -> getiter != NULL ) {
588+ /* second attempt, walk the iterator */
589+ mp_obj_t next = NULL ;
590+ mp_obj_t iter = rt_getiter (rhs );
591+ while ((next = rt_iternext (iter )) != mp_const_stop_iteration ) {
592+ if (mp_obj_equal (next , lhs )) {
593+ return MP_BOOL (op == RT_COMPARE_OP_IN );
594+ }
595+ }
596+ return MP_BOOL (op != RT_COMPARE_OP_IN );
597+ }
598+ }
599+
600+ nlr_jump (mp_obj_new_exception_msg_varg (
601+ MP_QSTR_TypeError , "'%s' object is not iterable" ,
602+ mp_obj_get_type_str (rhs )));
603+ return mp_const_none ;
604+ }
605+
606+ if (MP_OBJ_IS_OBJ (lhs )) {
607+ mp_obj_base_t * o = lhs ;
608+ if (o -> type -> binary_op != NULL ) {
609+ mp_obj_t result = o -> type -> binary_op (op , lhs , rhs );
610+ if (result != NULL ) {
611+ return result ;
612+ }
580613 }
614+ // TODO implement dispatch for reverse binary ops
581615 }
582616
583617 // TODO specify in error message what the operator is
584618 nlr_jump (mp_obj_new_exception_msg_varg (MP_QSTR_TypeError ,
585619 "unsupported operand types for binary operator: '%s', '%s'" ,
586620 mp_obj_get_type_str (lhs ), mp_obj_get_type_str (rhs )));
621+ return mp_const_none ;
587622}
588623
589624mp_obj_t rt_make_function_from_id (int unique_code_id ) {
0 commit comments