2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2014-05-13 06:44:45 +01:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
2014-05-03 23:27:38 +01: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 18:17:45 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/bc.h"
|
|
|
|
#include "py/objgenerator.h"
|
|
|
|
#include "py/objfun.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* generator wrapper */
|
|
|
|
|
|
|
|
typedef struct _mp_obj_gen_wrap_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t *fun;
|
|
|
|
} mp_obj_gen_wrap_t;
|
|
|
|
|
2014-06-11 18:17:38 +01:00
|
|
|
typedef struct _mp_obj_gen_instance_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_dict_t *globals;
|
2016-08-27 14:21:00 +01:00
|
|
|
mp_code_state_t code_state;
|
2014-06-11 18:17:38 +01:00
|
|
|
} mp_obj_gen_instance_t;
|
2014-03-30 18:21:28 +01:00
|
|
|
|
2016-01-03 09:59:18 +00: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) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_gen_wrap_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-17 03:49:47 +01:00
|
|
|
mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
|
2015-11-27 17:01:44 +00:00
|
|
|
assert(self_fun->base.type == &mp_type_fun_bc);
|
2013-12-30 22:32:17 +00:00
|
|
|
|
2015-03-18 17:47:47 +00:00
|
|
|
// get start of bytecode
|
|
|
|
const byte *ip = self_fun->bytecode;
|
2014-10-25 15:07:02 +01:00
|
|
|
|
2014-06-11 18:17:38 +01:00
|
|
|
// bytecode prelude: get state size and exception stack size
|
2014-09-04 14:44:01 +01:00
|
|
|
mp_uint_t n_state = mp_decode_uint(&ip);
|
|
|
|
mp_uint_t n_exc_stack = mp_decode_uint(&ip);
|
2014-06-11 18:17:38 +01: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;
|
|
|
|
|
|
|
|
o->globals = self_fun->globals;
|
|
|
|
o->code_state.n_state = n_state;
|
2015-04-06 22:38:53 +01:00
|
|
|
o->code_state.ip = (byte*)(ip - self_fun->bytecode); // offset to prelude
|
2014-06-11 18:17:38 +01:00
|
|
|
mp_setup_code_state(&o->code_state, self_fun, n_args, n_kw, args);
|
2015-11-27 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 13:43:38 +00:00
|
|
|
const mp_obj_type_t mp_type_gen_wrap = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_generator,
|
2014-01-18 14:10:48 +00:00
|
|
|
.call = gen_wrap_call,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
2014-01-29 20:30:52 +00:00
|
|
|
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
|
2014-03-29 13:43:38 +00:00
|
|
|
o->base.type = &mp_type_gen_wrap;
|
2015-11-27 17:01:44 +00:00
|
|
|
o->fun = MP_OBJ_TO_PTR(fun);
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* generator instance */
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_printf(print, "<generator object '%q' at %p>", mp_obj_code_get_name(self->code_state.code_info), self);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 19:33:23 +00: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) {
|
2016-08-12 20:06:47 +01:00
|
|
|
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-05-31 14:50:46 +01:00
|
|
|
if (self->code_state.ip == 0) {
|
2016-04-27 23:54:23 +01:00
|
|
|
// Trying to resume already stopped generator
|
2014-04-17 23:19:36 +01:00
|
|
|
*ret_val = MP_OBJ_STOP_ITERATION;
|
2014-03-26 19:33:23 +00:00
|
|
|
return MP_VM_RETURN_NORMAL;
|
2014-01-26 23:01:37 +00:00
|
|
|
}
|
2014-05-31 14:50:46 +01:00
|
|
|
if (self->code_state.sp == self->code_state.state - 1) {
|
2014-01-26 18:50:11 +00:00
|
|
|
if (send_value != mp_const_none) {
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
|
2014-01-26 18:50:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-31 14:50:46 +01:00
|
|
|
*self->code_state.sp = send_value;
|
2014-01-26 18:50:11 +00:00
|
|
|
}
|
2014-04-17 03:49:47 +01:00
|
|
|
mp_obj_dict_t *old_globals = mp_globals_get();
|
|
|
|
mp_globals_set(self->globals);
|
2014-06-07 14:16:08 +01:00
|
|
|
mp_vm_return_kind_t ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
|
2014-04-17 03:49:47 +01:00
|
|
|
mp_globals_set(old_globals);
|
2014-03-23 19:48:29 +00:00
|
|
|
|
2014-03-26 19:33:23 +00:00
|
|
|
switch (ret_kind) {
|
2014-02-15 22:55:00 +00:00
|
|
|
case MP_VM_RETURN_NORMAL:
|
2015-01-14 21:32:42 +00:00
|
|
|
default:
|
2014-02-15 22:55:00 +00: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.
|
|
|
|
// TODO: check how return with value behaves under such conditions
|
|
|
|
// in CPython.
|
2014-05-31 14:50:46 +01:00
|
|
|
self->code_state.ip = 0;
|
|
|
|
*ret_val = *self->code_state.sp;
|
2014-03-26 19:33:23 +00:00
|
|
|
break;
|
2014-03-23 19:48:29 +00:00
|
|
|
|
|
|
|
case MP_VM_RETURN_YIELD:
|
2014-05-31 14:50:46 +01:00
|
|
|
*ret_val = *self->code_state.sp;
|
2015-05-11 00:59:25 +01:00
|
|
|
if (*ret_val == MP_OBJ_STOP_ITERATION) {
|
|
|
|
self->code_state.ip = 0;
|
|
|
|
}
|
2014-03-26 19:33:23 +00:00
|
|
|
break;
|
2014-03-23 19:48:29 +00:00
|
|
|
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
2014-05-31 14:50:46 +01:00
|
|
|
self->code_state.ip = 0;
|
|
|
|
*ret_val = self->code_state.state[self->code_state.n_state - 1];
|
2014-03-26 19:33:23 +00:00
|
|
|
break;
|
2014-03-23 19:48:29 +00:00
|
|
|
}
|
2014-03-26 19:33:23 +00:00
|
|
|
|
|
|
|
return ret_kind;
|
2014-03-23 19:48:29 +00: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 19:33:23 +00:00
|
|
|
mp_obj_t ret;
|
|
|
|
switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
|
2014-03-23 19:48:29 +00:00
|
|
|
case MP_VM_RETURN_NORMAL:
|
2015-01-14 21:32:42 +00:00
|
|
|
default:
|
2014-03-23 19:48:29 +00:00
|
|
|
// Optimize return w/o value in case generator is used in for loop
|
2014-04-17 23:19:36 +01:00
|
|
|
if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
|
|
|
|
return MP_OBJ_STOP_ITERATION;
|
2014-02-15 22:55:00 +00:00
|
|
|
} else {
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
|
2014-02-15 22:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case MP_VM_RETURN_YIELD:
|
2015-11-27 17:01:44 +00:00
|
|
|
if (throw_value != MP_OBJ_NULL && mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(throw_value)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
|
2014-03-31 02:09:53 +01:00
|
|
|
}
|
2014-03-23 19:48:29 +00:00
|
|
|
return ret;
|
2014-02-15 22:55:00 +00:00
|
|
|
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
2014-04-17 23:19:36 +01:00
|
|
|
// TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
|
2014-03-30 21:14:55 +01:00
|
|
|
// of mp_iternext() protocol, but this function is called by other methods
|
2014-04-17 23:19:36 +01:00
|
|
|
// too, which may not handled MP_OBJ_STOP_ITERATION.
|
2015-11-27 17:01:44 +00:00
|
|
|
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
|
2015-05-11 00:59:25 +01:00
|
|
|
mp_obj_t val = mp_obj_exception_get_value(ret);
|
|
|
|
if (val == mp_const_none) {
|
|
|
|
return MP_OBJ_STOP_ITERATION;
|
|
|
|
}
|
2014-03-30 21:14:55 +01:00
|
|
|
}
|
2015-05-11 00:59:25 +01:00
|
|
|
nlr_raise(ret);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-26 18:50:11 +00:00
|
|
|
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
|
2014-03-23 19:48:29 +00:00
|
|
|
return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
|
2014-01-26 23:01:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
|
2014-03-23 19:48:29 +00:00
|
|
|
mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
|
2014-04-17 23:19:36 +01:00
|
|
|
if (ret == MP_OBJ_STOP_ITERATION) {
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
|
2014-02-15 16:10:44 +00:00
|
|
|
} else {
|
|
|
|
return ret;
|
2014-01-26 23:01:37 +00:00
|
|
|
}
|
2014-01-26 18:50:11 +00:00
|
|
|
}
|
2014-02-15 16:10:44 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
|
2014-01-26 18:50:11 +00:00
|
|
|
|
2014-03-30 11:13:12 +01:00
|
|
|
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t gen_instance_throw(size_t n_args, const mp_obj_t *args) {
|
2014-03-30 11:13:12 +01:00
|
|
|
mp_obj_t exc = (n_args == 2) ? args[1] : args[2];
|
2014-03-30 13:35:08 +01:00
|
|
|
exc = mp_make_raise_obj(exc);
|
2014-03-30 11:13:12 +01:00
|
|
|
|
|
|
|
mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
|
2014-04-17 23:19:36 +01:00
|
|
|
if (ret == MP_OBJ_STOP_ITERATION) {
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
|
2014-03-22 15:50:12 +00:00
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
|
|
|
|
|
2014-03-23 19:48:29 +00:00
|
|
|
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
|
2014-03-26 19:33:23 +00:00
|
|
|
mp_obj_t ret;
|
2015-11-27 17:01:44 +00:00
|
|
|
switch (mp_obj_gen_resume(self_in, mp_const_none, MP_OBJ_FROM_PTR(&mp_const_GeneratorExit_obj), &ret)) {
|
2014-03-26 19:33:23 +00:00
|
|
|
case MP_VM_RETURN_YIELD:
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
|
2014-03-23 19:48:29 +00:00
|
|
|
|
2014-03-26 19:33:23 +00:00
|
|
|
// Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
|
|
|
// ret should always be an instance of an exception class
|
2015-11-27 17:01:44 +00:00
|
|
|
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))) {
|
2014-03-26 19:33:23 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(ret);
|
2014-03-26 19:33:23 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// The only choice left is MP_VM_RETURN_NORMAL which is successful close
|
2014-03-23 19:48:29 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
|
2014-03-22 15:50:12 +00:00
|
|
|
|
2015-11-27 13:38:15 +00: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) },
|
2014-01-26 18:50:11 +00:00
|
|
|
};
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
|
|
|
|
|
2014-03-29 13:43:38 +00:00
|
|
|
const mp_obj_type_t mp_type_gen_instance = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_generator,
|
2014-01-07 15:58:30 +00:00
|
|
|
.print = gen_instance_print,
|
2014-05-17 09:18:23 +01:00
|
|
|
.getiter = mp_identity,
|
2014-01-07 15:58:30 +00:00
|
|
|
.iternext = gen_instance_iternext,
|
2015-11-27 17:01:44 +00:00
|
|
|
.locals_dict = (mp_obj_dict_t*)&gen_instance_locals_dict,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|