Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c48d6f7

Browse files
committed
py: Don't expect that type->getiter() always returns iterator, check for NULL.
This is better than forcing each getiter() implementation to raise exception.
1 parent 0f570cf commit c48d6f7

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

py/runtime.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,9 +884,14 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
884884
}
885885

886886
mp_obj_t mp_getiter(mp_obj_t o_in) {
887+
assert(o_in);
887888
mp_obj_type_t *type = mp_obj_get_type(o_in);
888889
if (type->getiter != NULL) {
889-
return type->getiter(o_in);
890+
mp_obj_t iter = type->getiter(o_in);
891+
if (iter == MP_OBJ_NULL) {
892+
goto not_iterable;
893+
}
894+
return iter;
890895
} else {
891896
// check for __iter__ method
892897
mp_obj_t dest[2];
@@ -901,6 +906,7 @@ mp_obj_t mp_getiter(mp_obj_t o_in) {
901906
return mp_obj_new_getitem_iter(dest);
902907
} else {
903908
// object not iterable
909+
not_iterable:
904910
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
905911
}
906912
}

py/vm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ mp_vm_return_kind_t mp_execute_bytecode2(const byte *code_info, const byte **ip_
663663
ENTRY(MP_BC_FOR_ITER):
664664
DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
665665
save_sp = sp;
666+
assert(TOP());
666667
obj1 = mp_iternext_allow_raise(TOP());
667668
if (obj1 == MP_OBJ_STOP_ITERATION) {
668669
--sp; // pop the exhausted iterator

0 commit comments

Comments
 (0)