py: Don't expect that type->getiter() always returns iterator, check for NULL.

This is better than forcing each getiter() implementation to raise exception.
This commit is contained in:
Paul Sokolovsky 2014-05-11 20:32:39 +03:00
parent 0f570cfccf
commit c48d6f7add
2 changed files with 8 additions and 1 deletions

View File

@ -884,9 +884,14 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
}
mp_obj_t mp_getiter(mp_obj_t o_in) {
assert(o_in);
mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->getiter != NULL) {
return type->getiter(o_in);
mp_obj_t iter = type->getiter(o_in);
if (iter == MP_OBJ_NULL) {
goto not_iterable;
}
return iter;
} else {
// check for __iter__ method
mp_obj_t dest[2];
@ -901,6 +906,7 @@ mp_obj_t mp_getiter(mp_obj_t o_in) {
return mp_obj_new_getitem_iter(dest);
} else {
// object not iterable
not_iterable:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
}
}

View File

@ -663,6 +663,7 @@ unwind_jump:
ENTRY(MP_BC_FOR_ITER):
DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
save_sp = sp;
assert(TOP());
obj1 = mp_iternext_allow_raise(TOP());
if (obj1 == MP_OBJ_STOP_ITERATION) {
--sp; // pop the exhausted iterator