diff options
Diffstat (limited to 'py')
-rw-r--r-- | py/objgenerator.c | 12 | ||||
-rw-r--r-- | py/objtype.c | 4 | ||||
-rw-r--r-- | py/runtime.c | 18 | ||||
-rw-r--r-- | py/vm.c | 2 |
4 files changed, 24 insertions, 12 deletions
diff --git a/py/objgenerator.c b/py/objgenerator.c index 8f6bb17cfd..78694c866b 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -127,13 +127,16 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) { case MP_VM_RETURN_NORMAL: // Optimize return w/o value in case generator is used in for loop - if (ret == mp_const_none) { + if (ret == mp_const_none || ret == MP_OBJ_NULL) { return MP_OBJ_NULL; } else { nlr_jump(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret)); } case MP_VM_RETURN_YIELD: + if (throw_value != MP_OBJ_NULL && mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) { + nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit")); + } return ret; case MP_VM_RETURN_EXCEPTION: @@ -171,13 +174,6 @@ STATIC mp_obj_t gen_instance_close(mp_obj_t self_in); STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) { mp_obj_t exc = (n_args == 2) ? args[1] : args[2]; exc = mp_make_raise_obj(exc); - if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_GeneratorExit)) { - // Throwing GeneratorExit is equivalent of calling close aka - // GeneratorExit should be handled specially - // TODO: Calling .close() will throw new exception instance, not one - // given to throw, which is not ok. - return gen_instance_close(args[0]); - } mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc); if (ret == MP_OBJ_NULL) { diff --git a/py/objtype.c b/py/objtype.c index 1275124803..ee3ce12abe 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -297,6 +297,10 @@ STATIC mp_obj_t type_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj STATIC void type_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_type)); mp_obj_type_t *self = self_in; + if (attr == MP_QSTR___name__) { + dest[0] = MP_OBJ_NEW_QSTR(self->name); + return; + } mp_obj_t member = mp_obj_class_lookup(self, attr); if (member != MP_OBJ_NULL) { // check if the methods are functions, static or class methods diff --git a/py/runtime.c b/py/runtime.c index 53dccd8ece..daa555e4a2 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -877,6 +877,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) { @@ -922,9 +923,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); @@ -769,7 +769,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); } |