py: Simplify and remove redundant code for __iter__ method lookup.
This commit is contained in:
parent
d1c3788375
commit
f6532bb9e0
2
py/obj.h
2
py/obj.h
@ -285,7 +285,7 @@ struct _mp_obj_type_t {
|
||||
// value=MP_OBJ_NULL means delete, value=MP_OBJ_SENTINEL means load, else store
|
||||
// can return MP_OBJ_NULL if op not supported
|
||||
|
||||
mp_fun_1_t getiter;
|
||||
mp_fun_1_t getiter; // corresponds to __iter__ special method
|
||||
mp_fun_1_t iternext; // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration() (with no args)
|
||||
|
||||
mp_buffer_p_t buffer_p;
|
||||
|
14
py/objtype.c
14
py/objtype.c
@ -644,21 +644,13 @@ STATIC mp_obj_t instance_getiter(mp_obj_t self_in) {
|
||||
};
|
||||
mp_obj_class_lookup(&lookup, self->base.type);
|
||||
if (member[0] == MP_OBJ_NULL) {
|
||||
// This kinda duplicates code in mp_getiter()
|
||||
lookup.attr = MP_QSTR___getitem__;
|
||||
lookup.meth_offset = 0; // TODO
|
||||
mp_obj_class_lookup(&lookup, self->base.type);
|
||||
if (member[0] != MP_OBJ_NULL) {
|
||||
// __getitem__ exists, create an iterator
|
||||
return mp_obj_new_getitem_iter(member);
|
||||
}
|
||||
return MP_OBJ_NULL;
|
||||
}
|
||||
if (member[0] == MP_OBJ_SENTINEL) {
|
||||
} else if (member[0] == MP_OBJ_SENTINEL) {
|
||||
mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
|
||||
return type->getiter(self->subobj[0]);
|
||||
} else {
|
||||
return mp_call_method_n_kw(0, 0, member);
|
||||
}
|
||||
return mp_call_method_n_kw(0, 0, member);
|
||||
}
|
||||
|
||||
STATIC mp_int_t instance_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
||||
|
46
py/runtime.c
46
py/runtime.c
@ -957,37 +957,31 @@ 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);
|
||||
|
||||
// check for native getiter (corresponds to __iter__)
|
||||
mp_obj_type_t *type = mp_obj_get_type(o_in);
|
||||
if (type->getiter != NULL) {
|
||||
mp_obj_t iter = type->getiter(o_in);
|
||||
if (iter == MP_OBJ_NULL) {
|
||||
goto not_iterable;
|
||||
if (iter != MP_OBJ_NULL) {
|
||||
return iter;
|
||||
}
|
||||
return iter;
|
||||
}
|
||||
|
||||
// check for __getitem__
|
||||
mp_obj_t dest[2];
|
||||
mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
|
||||
if (dest[0] != MP_OBJ_NULL) {
|
||||
// __getitem__ exists, create and return an iterator
|
||||
return mp_obj_new_getitem_iter(dest);
|
||||
}
|
||||
|
||||
// object not iterable
|
||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
||||
"object not iterable"));
|
||||
} else {
|
||||
// check for __iter__ method
|
||||
mp_obj_t dest[2];
|
||||
mp_load_method_maybe(o_in, MP_QSTR___iter__, dest);
|
||||
if (dest[0] != MP_OBJ_NULL) {
|
||||
// __iter__ exists, call it and return its result
|
||||
return mp_call_method_n_kw(0, 0, dest);
|
||||
} else {
|
||||
mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
|
||||
if (dest[0] != MP_OBJ_NULL) {
|
||||
// __getitem__ exists, create an iterator
|
||||
return mp_obj_new_getitem_iter(dest);
|
||||
} else {
|
||||
// object not iterable
|
||||
not_iterable:
|
||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
||||
"object not iterable"));
|
||||
} else {
|
||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object is not iterable", mp_obj_get_type_str(o_in)));
|
||||
}
|
||||
}
|
||||
}
|
||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object is not iterable", mp_obj_get_type_str(o_in)));
|
||||
}
|
||||
}
|
||||
|
||||
|
6
tests/basics/iter0.py
Normal file
6
tests/basics/iter0.py
Normal file
@ -0,0 +1,6 @@
|
||||
# builtin type that is not iterable
|
||||
try:
|
||||
for i in 1:
|
||||
pass
|
||||
except TypeError:
|
||||
print('TypeError')
|
@ -1,5 +1,14 @@
|
||||
# test user defined iterators
|
||||
|
||||
# this class is not iterable
|
||||
class NotIterable:
|
||||
pass
|
||||
try:
|
||||
for i in NotIterable():
|
||||
pass
|
||||
except TypeError:
|
||||
print('TypeError')
|
||||
|
||||
class MyStopIteration(StopIteration):
|
||||
pass
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user