From a2109d93210c391d45a0ced2ce8a85f2471d3543 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 31 Mar 2014 04:14:30 +0300 Subject: mp_resume: Elaborate handling of .throw() for objects which lack it. In this case, the exception is just re-thrown - the ideas is that object doesn't handle this exception specially, so it will propagated per Python semantics. --- py/runtime.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'py/runtime.c') diff --git a/py/runtime.c b/py/runtime.c index f827fd831e..f7e08e37f5 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -960,9 +960,20 @@ mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t th return MP_VM_RETURN_NORMAL; } } - mp_load_method(self_in, MP_QSTR_throw, dest); - *ret_val = mp_call_method_n_kw(1, 0, &throw_value); - return MP_VM_RETURN_YIELD; + mp_load_method_maybe(self_in, MP_QSTR_throw, dest); + if (dest[0] != MP_OBJ_NULL) { + *ret_val = mp_call_method_n_kw(1, 0, &throw_value); + // If .throw() method returned, we assume it's value to yield + // - any exception would be thrown with nlr_jump(). + return MP_VM_RETURN_YIELD; + } + // If there's nowhere to throw exception into, then we assume that object + // is just incapable to handle it, so any exception thrown into it + // will be propagated up. This behavior is approved by test_pep380.py + // test_delegation_of_close_to_non_generator(), + // test_delegating_throw_to_non_generator() + *ret_val = throw_value; + return MP_VM_RETURN_EXCEPTION; } assert(0); -- cgit v1.2.3 From 7da0660516e1406808ad212d35faa95de3316fc4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 31 Mar 2014 04:19:12 +0300 Subject: mp_resume: Dare to pass send_value of NULL. There was thinkos that either send_value or throw_value is specified, but there were cases with both. Note that send_value is pushed onto generator's stack - but that's probably only good, because if we throw exception into gen, it should not ever use send_value, and that will be just extra "assert". --- py/runtime.c | 1 + py/vm.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'py/runtime.c') diff --git a/py/runtime.c b/py/runtime.c index f7e08e37f5..65bb808a9b 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -915,6 +915,7 @@ mp_obj_t mp_iternext(mp_obj_t o_in) { // TODO: Unclear what to do with StopIterarion exception here. mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) { + assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL)); mp_obj_type_t *type = mp_obj_get_type(self_in); if (type == &mp_type_gen_instance) { diff --git a/py/vm.c b/py/vm.c index 599a1d862e..3ffeb138c4 100644 --- a/py/vm.c +++ b/py/vm.c @@ -764,7 +764,7 @@ yield: if (inject_exc != MP_OBJ_NULL) { t_exc = inject_exc; inject_exc = MP_OBJ_NULL; - ret_kind = mp_resume(TOP(), mp_const_none, t_exc, &obj2); + ret_kind = mp_resume(TOP(), MP_OBJ_NULL, t_exc, &obj2); } else { ret_kind = mp_resume(TOP(), obj1, MP_OBJ_NULL, &obj2); } -- cgit v1.2.3