diff options
author | Damien George <damien.p.george@gmail.com> | 2019-02-21 14:07:44 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2019-05-09 13:40:28 +1000 |
commit | dac9d4767175541aaaaf0e2f873322f5f1db3b0c (patch) | |
tree | 7ea2c8dcf3a26eaa1928f8a22b6800169dea7efc /py | |
parent | 29865e3e58a89eace83fc1910221724363f55d64 (diff) |
py/objgenerator: Fix handling of None passed as 2nd arg to throw().
Fixes issue #4527.
Diffstat (limited to 'py')
-rw-r--r-- | py/objgenerator.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/py/objgenerator.c b/py/objgenerator.c index c306bb0c5..92bafea6a 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -252,7 +252,22 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send); STATIC mp_obj_t gen_instance_close(mp_obj_t self_in); STATIC mp_obj_t gen_instance_throw(size_t n_args, const mp_obj_t *args) { - mp_obj_t exc = (n_args == 2) ? args[1] : args[2]; + // The signature of this function is: throw(type[, value[, traceback]]) + // CPython will pass all given arguments through the call chain and process them + // at the point they are used (native generators will handle them differently to + // user-defined generators with a throw() method). To save passing multiple + // values, MicroPython instead does partial processing here to reduce it down to + // one argument and passes that through: + // - if only args[1] is given, or args[2] is given but is None, args[1] is + // passed through (in the standard case it is an exception class or instance) + // - if args[2] is given and not None it is passed through (in the standard + // case it would be an exception instance and args[1] its corresponding class) + // - args[3] is always ignored + + mp_obj_t exc = args[1]; + if (n_args > 2 && args[2] != mp_const_none) { + exc = args[2]; + } mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc); if (ret == MP_OBJ_STOP_ITERATION) { |