From eff85bb1dcca0602cef055986ea3b8192dea1d71 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 28 Apr 2016 01:54:23 +0300 Subject: [PATCH] py/vm: "yield from" didn't handle MP_OBJ_STOP_ITERATION optimization. E.g. crashed when yielding from already stopped generators. --- py/objgenerator.c | 1 + py/vm.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/py/objgenerator.c b/py/objgenerator.c index 93df7ce13f..2480b0a4b8 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -99,6 +99,7 @@ mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_ assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance)); mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in); if (self->code_state.ip == 0) { + // Trying to resume already stopped generator *ret_val = MP_OBJ_STOP_ITERATION; return MP_VM_RETURN_NORMAL; } diff --git a/py/vm.c b/py/vm.c index b8d38f78e4..bd5bae115e 100644 --- a/py/vm.c +++ b/py/vm.c @@ -1144,7 +1144,8 @@ yield: if (ret_kind == MP_VM_RETURN_NORMAL) { // Pop exhausted gen sp--; - if (ret_value == MP_OBJ_NULL) { + // TODO: When ret_value can be MP_OBJ_NULL here?? + if (ret_value == MP_OBJ_NULL || ret_value == MP_OBJ_STOP_ITERATION) { // Optimize StopIteration // TODO: get StopIteration's value PUSH(mp_const_none);