Skip to content

Commit 9783ac2

Browse files
committed
py/runtime: Simplify handling of containment binary operator.
In mp_binary_op, there is no need to explicitly check for type->getiter being non-null and raising an exception because this is handled exactly by mp_getiter(). So just call the latter unconditionally.
1 parent 067bf84 commit 9783ac2

1 file changed

Lines changed: 9 additions & 17 deletions

File tree

py/runtime.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -536,25 +536,17 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
536536
return res;
537537
}
538538
}
539-
if (type->getiter != NULL) {
540-
/* second attempt, walk the iterator */
541-
mp_obj_iter_buf_t iter_buf;
542-
mp_obj_t iter = mp_getiter(rhs, &iter_buf);
543-
mp_obj_t next;
544-
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
545-
if (mp_obj_equal(next, lhs)) {
546-
return mp_const_true;
547-
}
548-
}
549-
return mp_const_false;
550-
}
551539

552-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
553-
mp_raise_TypeError("object not iterable");
554-
} else {
555-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
556-
"'%s' object is not iterable", mp_obj_get_type_str(rhs)));
540+
// final attempt, walk the iterator (will raise if rhs is not iterable)
541+
mp_obj_iter_buf_t iter_buf;
542+
mp_obj_t iter = mp_getiter(rhs, &iter_buf);
543+
mp_obj_t next;
544+
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
545+
if (mp_obj_equal(next, lhs)) {
546+
return mp_const_true;
547+
}
557548
}
549+
return mp_const_false;
558550
}
559551

560552
// generic binary_op supplied by type

0 commit comments

Comments
 (0)