2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2021-04-22 20:55:39 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013-2019 Damien P. George
|
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2014-2017 Paul Sokolovsky
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/bc.h"
|
2021-04-23 15:26:42 -04:00
|
|
|
#include "py/objstr.h"
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/objgenerator.h"
|
|
|
|
#include "py/objfun.h"
|
2018-04-01 13:15:35 -04:00
|
|
|
#include "py/stackctrl.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2018-08-08 21:24:49 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
// Instance of GeneratorExit exception - needed by generator.close()
|
|
|
|
const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, 0, 0, NULL, (mp_obj_tuple_t *)&mp_const_empty_tuple_obj};
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* generator wrapper */
|
|
|
|
|
|
|
|
typedef struct _mp_obj_gen_wrap_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t *fun;
|
2020-07-21 03:01:22 -04:00
|
|
|
bool coroutine_generator;
|
2013-12-21 13:17:45 -05:00
|
|
|
} mp_obj_gen_wrap_t;
|
|
|
|
|
2014-06-11 13:17:38 -04:00
|
|
|
typedef struct _mp_obj_gen_instance_t {
|
|
|
|
mp_obj_base_t base;
|
2021-04-23 15:26:42 -04:00
|
|
|
// mp_const_none: Not-running, no exception.
|
|
|
|
// MP_OBJ_NULL: Running, no exception.
|
|
|
|
// other: Not running, pending exception.
|
|
|
|
mp_obj_t pend_exc;
|
2020-07-21 03:01:22 -04:00
|
|
|
bool coroutine_generator;
|
2016-08-27 09:21:00 -04:00
|
|
|
mp_code_state_t code_state;
|
2014-06-11 13:17:38 -04:00
|
|
|
} mp_obj_gen_instance_t;
|
2014-03-30 13:21:28 -04:00
|
|
|
|
2018-09-30 23:07:04 -04:00
|
|
|
/******************************************************************************/
|
|
|
|
// native generator wrapper
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
|
|
|
|
STATIC mp_obj_t native_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2021-04-20 21:04:23 -04:00
|
|
|
mp_obj_gen_wrap_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)self->fun;
|
2018-09-30 23:07:04 -04:00
|
|
|
|
|
|
|
// Determine start of prelude, and extract n_state from it
|
2021-04-30 16:30:13 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
2021-04-20 01:22:44 -04:00
|
|
|
uintptr_t prelude_offset = ((uintptr_t *)self_fun->bytecode)[0];
|
2021-04-30 16:30:13 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2021-04-23 15:26:42 -04:00
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE_PRELUDE_AS_BYTES_OBJ
|
|
|
|
// Prelude is in bytes object in const_table, at index prelude_offset
|
|
|
|
mp_obj_str_t *prelude_bytes = MP_OBJ_TO_PTR(self_fun->const_table[prelude_offset]);
|
|
|
|
prelude_offset = (const byte *)prelude_bytes->data - self_fun->bytecode;
|
|
|
|
#endif
|
|
|
|
const uint8_t *ip = self_fun->bytecode + prelude_offset;
|
|
|
|
size_t n_state, n_exc_stack_unused, scope_flags, n_pos_args, n_kwonly_args, n_def_args;
|
|
|
|
MP_BC_PRELUDE_SIG_DECODE_INTO(ip, n_state, n_exc_stack_unused, scope_flags, n_pos_args, n_kwonly_args, n_def_args);
|
2018-09-30 23:07:04 -04:00
|
|
|
size_t n_exc_stack = 0;
|
|
|
|
|
|
|
|
// Allocate the generator object, with room for local stack and exception stack
|
|
|
|
mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte,
|
|
|
|
n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
|
|
|
|
o->base.type = &mp_type_gen_instance;
|
|
|
|
|
|
|
|
// Parse the input arguments and set up the code state
|
2021-04-23 15:26:42 -04:00
|
|
|
o->pend_exc = mp_const_none;
|
2018-09-30 23:07:04 -04:00
|
|
|
o->code_state.fun_bc = self_fun;
|
2021-04-20 01:22:44 -04:00
|
|
|
o->code_state.ip = (const byte *)prelude_offset;
|
2021-04-23 15:26:42 -04:00
|
|
|
o->code_state.n_state = n_state;
|
2018-09-30 23:07:04 -04:00
|
|
|
mp_setup_code_state(&o->code_state, n_args, n_kw, args);
|
|
|
|
|
|
|
|
// Indicate we are a native function, which doesn't use this variable
|
2021-04-23 15:26:42 -04:00
|
|
|
o->code_state.exc_sp_idx = MP_CODE_STATE_EXC_SP_IDX_SENTINEL;
|
2018-09-30 23:07:04 -04:00
|
|
|
|
|
|
|
// Prepare the generator instance for execution
|
2021-04-30 16:30:13 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
2021-04-20 01:22:44 -04:00
|
|
|
uintptr_t start_offset = ((uintptr_t *)self_fun->bytecode)[1];
|
2021-04-30 16:30:13 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2021-04-20 01:22:44 -04:00
|
|
|
o->code_state.ip = MICROPY_MAKE_POINTER_CALLABLE((void *)(self_fun->bytecode + start_offset));
|
2018-09-30 23:07:04 -04:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:04:23 -04:00
|
|
|
#endif // MICROPY_EMIT_NATIVE
|
|
|
|
|
|
|
|
STATIC mp_obj_t bc_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_gen_wrap_t *self = MP_OBJ_TO_PTR(self_in);
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)self->fun;
|
2015-11-27 12:01:44 -05:00
|
|
|
assert(self_fun->base.type == &mp_type_fun_bc);
|
2013-12-30 17:32:17 -05:00
|
|
|
|
2014-06-11 13:17:38 -04:00
|
|
|
// bytecode prelude: get state size and exception stack size
|
2021-04-23 15:26:42 -04:00
|
|
|
const uint8_t *ip = self_fun->bytecode;
|
|
|
|
MP_BC_PRELUDE_SIG_DECODE(ip);
|
2014-06-11 13:17:38 -04:00
|
|
|
|
|
|
|
// allocate the generator object, with room for local stack and exception stack
|
|
|
|
mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte,
|
|
|
|
n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
|
|
|
|
o->base.type = &mp_type_gen_instance;
|
|
|
|
|
2020-07-21 03:01:22 -04:00
|
|
|
o->coroutine_generator = self->coroutine_generator;
|
2021-04-23 15:26:42 -04:00
|
|
|
o->pend_exc = mp_const_none;
|
2017-03-16 23:54:53 -04:00
|
|
|
o->code_state.fun_bc = self_fun;
|
|
|
|
o->code_state.ip = 0;
|
2021-04-23 15:26:42 -04:00
|
|
|
o->code_state.n_state = n_state;
|
2017-03-16 23:54:53 -04:00
|
|
|
mp_setup_code_state(&o->code_state, n_args, n_kw, args);
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:04:23 -04:00
|
|
|
STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
|
|
mp_obj_gen_wrap_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)self->fun;
|
|
|
|
if (self_fun->base.type == &mp_type_fun_native) {
|
|
|
|
return native_gen_wrap_call(self, n_args, n_kw, args);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return bc_gen_wrap_call(self, n_args, n_kw, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MICROPY_PY_FUNCTION_ATTRS
|
|
|
|
static void gen_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
|
|
|
mp_obj_gen_wrap_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)self->fun;
|
|
|
|
mp_obj_fun_bc_attr(self_fun, attr, dest);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-29 09:43:38 -04:00
|
|
|
const mp_obj_type_t mp_type_gen_wrap = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_generator,
|
2014-01-18 09:10:48 -05:00
|
|
|
.call = gen_wrap_call,
|
2017-07-06 21:47:38 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2018-09-30 23:07:04 -04:00
|
|
|
#if MICROPY_PY_FUNCTION_ATTRS
|
2021-04-20 21:04:23 -04:00
|
|
|
.attr = gen_attr,
|
2018-09-30 23:07:04 -04:00
|
|
|
#endif
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2021-04-20 21:04:23 -04:00
|
|
|
|
2020-07-21 03:01:22 -04:00
|
|
|
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun, bool is_coroutine) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
|
2014-03-29 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_gen_wrap;
|
2015-11-27 12:01:44 -05:00
|
|
|
o->fun = MP_OBJ_TO_PTR(fun);
|
2020-07-21 03:01:22 -04:00
|
|
|
o->coroutine_generator = is_coroutine;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* generator instance */
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_ASYNC_AWAIT
|
2020-07-21 03:01:22 -04:00
|
|
|
if (self->coroutine_generator) {
|
|
|
|
mp_printf(print, "<coroutine object '%q' at %p>", mp_obj_fun_get_name(MP_OBJ_FROM_PTR(self->code_state.fun_bc)), self);
|
2020-07-22 01:34:34 -04:00
|
|
|
return;
|
2020-07-21 03:01:22 -04:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2020-07-22 01:34:34 -04:00
|
|
|
mp_printf(print, "<generator object '%q' at %p>", mp_obj_fun_get_name(MP_OBJ_FROM_PTR(self->code_state.fun_bc)), self);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
2014-03-26 15:33:23 -04:00
|
|
|
mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
|
2018-04-01 13:15:35 -04:00
|
|
|
MP_STACK_CHECK();
|
2021-04-22 20:55:39 -04:00
|
|
|
mp_check_self(mp_obj_is_type(self_in, &mp_type_gen_instance));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-05-31 09:50:46 -04:00
|
|
|
if (self->code_state.ip == 0) {
|
2016-04-27 18:54:23 -04:00
|
|
|
// Trying to resume already stopped generator
|
2014-04-17 18:19:36 -04:00
|
|
|
*ret_val = MP_OBJ_STOP_ITERATION;
|
2014-03-26 15:33:23 -04:00
|
|
|
return MP_VM_RETURN_NORMAL;
|
2014-01-26 18:01:37 -05:00
|
|
|
}
|
2021-04-23 15:26:42 -04:00
|
|
|
|
|
|
|
// Ensure the generator cannot be reentered during execution
|
|
|
|
if (self->pend_exc == MP_OBJ_NULL) {
|
|
|
|
mp_raise_ValueError(translate("generator already executing"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MICROPY_PY_GENERATOR_PEND_THROW
|
|
|
|
// If exception is pending (set using .pend_throw()), process it now.
|
|
|
|
if (self->pend_exc != mp_const_none) {
|
|
|
|
throw_value = self->pend_exc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If the generator is started, allow sending a value.
|
2014-05-31 09:50:46 -04:00
|
|
|
if (self->code_state.sp == self->code_state.state - 1) {
|
2014-01-26 13:50:11 -05:00
|
|
|
if (send_value != mp_const_none) {
|
2018-08-08 21:24:49 -04:00
|
|
|
mp_raise_TypeError(translate("can't send non-None value to a just-started generator"));
|
2014-01-26 13:50:11 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-23 15:26:42 -04:00
|
|
|
*self->code_state.sp = send_value;
|
2014-01-26 13:50:11 -05:00
|
|
|
}
|
2018-05-22 02:54:03 -04:00
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
// Mark as running
|
|
|
|
self->pend_exc = MP_OBJ_NULL;
|
2018-05-22 02:54:03 -04:00
|
|
|
|
|
|
|
// Set up the correct globals context for the generator and execute it
|
2018-05-22 02:39:19 -04:00
|
|
|
self->code_state.old_globals = mp_globals_get();
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_globals_set(self->code_state.fun_bc->globals);
|
2018-09-30 23:07:04 -04:00
|
|
|
|
|
|
|
mp_vm_return_kind_t ret_kind;
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
2021-04-23 15:26:42 -04:00
|
|
|
if (self->code_state.exc_sp_idx == MP_CODE_STATE_EXC_SP_IDX_SENTINEL) {
|
2018-09-30 23:07:04 -04:00
|
|
|
// A native generator, with entry point 2 words into the "bytecode" pointer
|
2021-04-20 01:22:44 -04:00
|
|
|
typedef uintptr_t (*mp_fun_native_gen_t)(void *, mp_obj_t);
|
|
|
|
mp_fun_native_gen_t fun = MICROPY_MAKE_POINTER_CALLABLE((const void *)(self->code_state.fun_bc->bytecode + 2 * sizeof(uintptr_t)));
|
|
|
|
ret_kind = fun((void *)&self->code_state, throw_value);
|
2018-09-30 23:07:04 -04:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// A bytecode generator
|
|
|
|
ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
|
|
|
|
}
|
|
|
|
|
2018-05-22 02:39:19 -04:00
|
|
|
mp_globals_set(self->code_state.old_globals);
|
2014-03-23 15:48:29 -04:00
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
// Mark as not running
|
|
|
|
self->pend_exc = mp_const_none;
|
|
|
|
|
2014-03-26 15:33:23 -04:00
|
|
|
switch (ret_kind) {
|
2014-02-15 17:55:00 -05:00
|
|
|
case MP_VM_RETURN_NORMAL:
|
2015-01-14 16:32:42 -05:00
|
|
|
default:
|
2014-02-15 17:55:00 -05:00
|
|
|
// Explicitly mark generator as completed. If we don't do this,
|
|
|
|
// subsequent next() may re-execute statements after last yield
|
|
|
|
// again and again, leading to side effects.
|
2014-05-31 09:50:46 -04:00
|
|
|
self->code_state.ip = 0;
|
|
|
|
*ret_val = *self->code_state.sp;
|
2014-03-26 15:33:23 -04:00
|
|
|
break;
|
2014-03-23 15:48:29 -04:00
|
|
|
|
|
|
|
case MP_VM_RETURN_YIELD:
|
2014-05-31 09:50:46 -04:00
|
|
|
*ret_val = *self->code_state.sp;
|
2017-10-21 05:13:44 -04:00
|
|
|
#if MICROPY_PY_GENERATOR_PEND_THROW
|
|
|
|
*self->code_state.sp = mp_const_none;
|
|
|
|
#endif
|
2014-03-26 15:33:23 -04:00
|
|
|
break;
|
2014-03-23 15:48:29 -04:00
|
|
|
|
2017-03-16 23:54:53 -04:00
|
|
|
case MP_VM_RETURN_EXCEPTION: {
|
2014-05-31 09:50:46 -04:00
|
|
|
self->code_state.ip = 0;
|
2018-09-29 09:25:08 -04:00
|
|
|
*ret_val = self->code_state.state[0];
|
2018-09-13 10:44:06 -04:00
|
|
|
// 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))) {
|
2021-04-20 21:04:23 -04:00
|
|
|
*ret_val = mp_obj_new_exception_msg(&mp_type_RuntimeError, translate("generator raised StopIteration"));
|
2018-09-13 10:44:06 -04:00
|
|
|
}
|
2014-03-26 15:33:23 -04:00
|
|
|
break;
|
2017-03-16 23:54:53 -04:00
|
|
|
}
|
2014-03-23 15:48:29 -04:00
|
|
|
}
|
2014-03-26 15:33:23 -04:00
|
|
|
|
|
|
|
return ret_kind;
|
2014-03-23 15:48:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) {
|
2014-03-26 15:33:23 -04:00
|
|
|
mp_obj_t ret;
|
|
|
|
switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
|
2014-03-23 15:48:29 -04:00
|
|
|
case MP_VM_RETURN_NORMAL:
|
2015-01-14 16:32:42 -05:00
|
|
|
default:
|
2014-03-23 15:48:29 -04:00
|
|
|
// Optimize return w/o value in case generator is used in for loop
|
2014-04-17 18:19:36 -04:00
|
|
|
if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
|
|
|
|
return MP_OBJ_STOP_ITERATION;
|
2014-02-15 17:55:00 -05:00
|
|
|
} else {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
|
2014-02-15 17:55:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case MP_VM_RETURN_YIELD:
|
2014-03-23 15:48:29 -04:00
|
|
|
return ret;
|
2014-02-15 17:55:00 -05:00
|
|
|
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
2015-05-10 19:59:25 -04:00
|
|
|
nlr_raise(ret);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-26 13:50:11 -05:00
|
|
|
|
2014-12-10 17:07:04 -05:00
|
|
|
STATIC mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_ASYNC_AWAIT
|
2020-07-22 01:34:34 -04:00
|
|
|
// This translate is literally too much for m0 boards
|
2020-07-21 03:01:22 -04:00
|
|
|
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (self->coroutine_generator) {
|
|
|
|
mp_raise_TypeError(translate("'coroutine' object is not an iterator"));
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2014-03-23 15:48:29 -04:00
|
|
|
return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
|
2014-01-26 18:01:37 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
|
2014-03-23 15:48:29 -04:00
|
|
|
mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
|
2014-04-17 18:19:36 -04:00
|
|
|
if (ret == MP_OBJ_STOP_ITERATION) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
|
2014-02-15 11:10:44 -05:00
|
|
|
} else {
|
|
|
|
return ret;
|
2014-01-26 18:01:37 -05:00
|
|
|
}
|
2014-01-26 13:50:11 -05:00
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
|
2014-01-26 13:50:11 -05:00
|
|
|
|
2020-08-05 22:55:40 -04:00
|
|
|
#if MICROPY_PY_ASYNC_AWAIT
|
|
|
|
STATIC mp_obj_t gen_instance_await(mp_obj_t self_in) {
|
|
|
|
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
|
2021-03-15 09:57:36 -04:00
|
|
|
if (!self->coroutine_generator) {
|
2020-08-05 22:55:40 -04:00
|
|
|
// Pretend like a generator does not have this coroutine behavior.
|
|
|
|
// Pay no attention to the dir() behind the curtain
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
|
|
|
|
translate("type object 'generator' has no attribute '__await__'")));
|
|
|
|
}
|
2020-10-11 02:39:32 -04:00
|
|
|
// You can directly call send on a coroutine generator or you can __await__ then send on the return of that.
|
|
|
|
return self;
|
2020-08-05 22:55:40 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_await_obj, gen_instance_await);
|
|
|
|
#endif
|
|
|
|
|
2014-03-30 06:13:12 -04:00
|
|
|
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t gen_instance_throw(size_t n_args, const mp_obj_t *args) {
|
2021-04-22 20:55:39 -04:00
|
|
|
// 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];
|
|
|
|
}
|
2014-03-30 06:13:12 -04:00
|
|
|
|
|
|
|
mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
|
2014-04-17 18:19:36 -04:00
|
|
|
if (ret == MP_OBJ_STOP_ITERATION) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
|
2014-03-22 11:50:12 -04:00
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
|
|
|
|
|
2014-03-23 15:48:29 -04:00
|
|
|
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
|
2014-03-26 15:33:23 -04:00
|
|
|
mp_obj_t ret;
|
2015-11-27 12:01:44 -05:00
|
|
|
switch (mp_obj_gen_resume(self_in, mp_const_none, MP_OBJ_FROM_PTR(&mp_const_GeneratorExit_obj), &ret)) {
|
2014-03-26 15:33:23 -04:00
|
|
|
case MP_VM_RETURN_YIELD:
|
2018-08-08 21:24:49 -04:00
|
|
|
mp_raise_RuntimeError(translate("generator ignored GeneratorExit"));
|
2014-03-23 15:48:29 -04:00
|
|
|
|
2018-09-13 10:44:06 -04:00
|
|
|
// Swallow GeneratorExit (== successful close), and re-raise any other
|
2014-03-26 15:33:23 -04:00
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
|
|
|
// ret should always be an instance of an exception class
|
2018-09-13 10:44:06 -04:00
|
|
|
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
|
2014-03-26 15:33:23 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(ret);
|
2014-03-26 15:33:23 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
// The only choice left is MP_VM_RETURN_NORMAL which is successful close
|
2014-03-23 15:48:29 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
|
2014-03-22 11:50:12 -04:00
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
#if MICROPY_PY_GENERATOR_PEND_THROW
|
2017-10-21 05:13:44 -04:00
|
|
|
STATIC mp_obj_t gen_instance_pend_throw(mp_obj_t self_in, mp_obj_t exc_in) {
|
|
|
|
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
|
2021-04-23 15:26:42 -04:00
|
|
|
if (self->pend_exc == MP_OBJ_NULL) {
|
|
|
|
mp_raise_ValueError(translate("generator already executing"));
|
2017-10-21 05:13:44 -04:00
|
|
|
}
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_obj_t prev = self->pend_exc;
|
|
|
|
self->pend_exc = exc_in;
|
2017-10-21 05:13:44 -04:00
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_pend_throw_obj, gen_instance_pend_throw);
|
2021-04-23 15:26:42 -04:00
|
|
|
#endif
|
2017-10-21 05:13:44 -04:00
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t gen_instance_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&gen_instance_close_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&gen_instance_send_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_throw), MP_ROM_PTR(&gen_instance_throw_obj) },
|
2017-10-21 05:13:44 -04:00
|
|
|
#if MICROPY_PY_GENERATOR_PEND_THROW
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pend_throw), MP_ROM_PTR(&gen_instance_pend_throw_obj) },
|
|
|
|
#endif
|
2020-08-05 22:55:40 -04:00
|
|
|
#if MICROPY_PY_ASYNC_AWAIT
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___await__), MP_ROM_PTR(&gen_instance_await_obj) },
|
|
|
|
#endif
|
2014-01-26 13:50:11 -05:00
|
|
|
};
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
|
|
|
|
|
2014-03-29 09:43:38 -04:00
|
|
|
const mp_obj_type_t mp_type_gen_instance = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_generator,
|
2014-01-07 10:58:30 -05:00
|
|
|
.print = gen_instance_print,
|
2017-07-06 21:47:38 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2016-01-09 18:14:54 -05:00
|
|
|
.getiter = mp_identity_getiter,
|
2014-01-07 10:58:30 -05:00
|
|
|
.iternext = gen_instance_iternext,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&gen_instance_locals_dict,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|