diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-11-15 01:10:34 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-11-15 01:10:34 +0300 |
commit | 79d996a57b351e0ef354eb1e2f644b194433cc73 (patch) | |
tree | 1939aa5437868c457648d41de8ec15dbcb256bf2 | |
parent | a392b3aa75c9309afedd7e9d9f6aeb739c9d9dab (diff) | |
download | micropython-79d996a57b351e0ef354eb1e2f644b194433cc73.tar.gz micropython-79d996a57b351e0ef354eb1e2f644b194433cc73.zip |
py/runtime: mp_resume: Handle exceptions in Python __next__().
This includes StopIteration and thus are important to make Python-coded
iterables work with yield from/await.
Exceptions in Python send() are still not handled and left for future
consideration and optimization.
-rw-r--r-- | py/runtime.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/py/runtime.c b/py/runtime.c index c255574643..4bab03b80c 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -1191,17 +1191,31 @@ mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t th mp_obj_t dest[3]; // Reserve slot for send() arg + // Python instance iterator protocol if (send_value == mp_const_none) { mp_load_method_maybe(self_in, MP_QSTR___next__, dest); if (dest[0] != MP_OBJ_NULL) { - *ret_val = mp_call_method_n_kw(0, 0, dest); - return MP_VM_RETURN_YIELD; + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + *ret_val = mp_call_method_n_kw(0, 0, dest); + nlr_pop(); + return MP_VM_RETURN_YIELD; + } else { + *ret_val = nlr.ret_val; + return MP_VM_RETURN_EXCEPTION; + } } } + // Either python instance generator protocol, or native object + // generator protocol. if (send_value != MP_OBJ_NULL) { mp_load_method(self_in, MP_QSTR_send, dest); dest[2] = send_value; + // TODO: This should have exception wrapping like __next__ case + // above. Not done right away to think how to optimize native + // generators better, see: + // https://github.com/micropython/micropython/issues/2628 *ret_val = mp_call_method_n_kw(1, 0, dest); return MP_VM_RETURN_YIELD; } |