summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-09-14 00:44:06 +1000
committerDamien George <damien.p.george@gmail.com>2018-09-20 15:36:59 +1000
commit3f6ffe059f64b3ebc44dc0bbc63452cb8850702b (patch)
tree5bf2fe77ad624afccd08011ab05b19ab321b5d9c /py
parent17f7c683d2790880ec9da70f7bc7e40c490c8796 (diff)
py/objgenerator: Implement PEP479, StopIteration convs to RuntimeError.
This commit implements PEP479 which disallows raising StopIteration inside a generator to signal that it should be finished. Instead, the generator should simply return when it is complete. See https://www.python.org/dev/peps/pep-0479/ for details.
Diffstat (limited to 'py')
-rw-r--r--py/objgenerator.c18
1 files changed, 6 insertions, 12 deletions
diff --git a/py/objgenerator.c b/py/objgenerator.c
index c45bebacd..341967dc0 100644
--- a/py/objgenerator.c
+++ b/py/objgenerator.c
@@ -145,6 +145,10 @@ mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_
size_t n_state = mp_decode_uint_value(self->code_state.fun_bc->bytecode);
self->code_state.ip = 0;
*ret_val = self->code_state.state[n_state - 1];
+ // PEP479: if StopIteration is raised inside a generator it is replaced with RuntimeError
+ if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(*ret_val)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
+ *ret_val = mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator raised StopIteration");
+ }
break;
}
}
@@ -168,15 +172,6 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o
return ret;
case MP_VM_RETURN_EXCEPTION:
- // TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
- // of mp_iternext() protocol, but this function is called by other methods
- // too, which may not handled MP_OBJ_STOP_ITERATION.
- if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
- mp_obj_t val = mp_obj_exception_get_value(ret);
- if (val == mp_const_none) {
- return MP_OBJ_STOP_ITERATION;
- }
- }
nlr_raise(ret);
}
}
@@ -216,11 +211,10 @@ STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
case MP_VM_RETURN_YIELD:
mp_raise_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit");
- // Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
+ // Swallow GeneratorExit (== successful close), and re-raise any other
case MP_VM_RETURN_EXCEPTION:
// ret should always be an instance of an exception class
- if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit)) ||
- mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
+ if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
return mp_const_none;
}
nlr_raise(ret);