Branch data Line data Source code
1 : : /*
2 : : * This file is part of the MicroPython project, http://micropython.org/
3 : : *
4 : : * The MIT License (MIT)
5 : : *
6 : : * Copyright (c) 2013-2019 Damien P. George
7 : : * Copyright (c) 2014-2017 Paul Sokolovsky
8 : : *
9 : : * Permission is hereby granted, free of charge, to any person obtaining a copy
10 : : * of this software and associated documentation files (the "Software"), to deal
11 : : * in the Software without restriction, including without limitation the rights
12 : : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 : : * copies of the Software, and to permit persons to whom the Software is
14 : : * furnished to do so, subject to the following conditions:
15 : : *
16 : : * The above copyright notice and this permission notice shall be included in
17 : : * all copies or substantial portions of the Software.
18 : : *
19 : : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 : : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 : : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 : : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 : : * THE SOFTWARE.
26 : : */
27 : :
28 : : #include <stdlib.h>
29 : : #include <assert.h>
30 : :
31 : : #include "py/runtime.h"
32 : : #include "py/bc.h"
33 : : #include "py/objstr.h"
34 : : #include "py/objgenerator.h"
35 : : #include "py/objfun.h"
36 : : #include "py/stackctrl.h"
37 : :
38 : : // Instance of GeneratorExit exception - needed by generator.close()
39 : : const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, 0, 0, NULL, (mp_obj_tuple_t *)&mp_const_empty_tuple_obj};
40 : :
41 : : /******************************************************************************/
42 : : /* generator wrapper */
43 : :
44 : : typedef struct _mp_obj_gen_instance_t {
45 : : mp_obj_base_t base;
46 : : // mp_const_none: Not-running, no exception.
47 : : // MP_OBJ_NULL: Running, no exception.
48 : : // other: Not running, pending exception.
49 : : mp_obj_t pend_exc;
50 : : mp_code_state_t code_state;
51 : : } mp_obj_gen_instance_t;
52 : :
53 : 1264 : 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) {
54 : : // A generating function is just a bytecode function with type mp_type_gen_wrap
55 : 1264 : mp_obj_fun_bc_t *self_fun = MP_OBJ_TO_PTR(self_in);
56 : :
57 : : // bytecode prelude: get state size and exception stack size
58 : 1264 : const uint8_t *ip = self_fun->bytecode;
59 [ + + ]: 2568 : MP_BC_PRELUDE_SIG_DECODE(ip);
60 : :
61 : : // allocate the generator object, with room for local stack and exception stack
62 : 1264 : mp_obj_gen_instance_t *o = mp_obj_malloc_var(mp_obj_gen_instance_t, byte,
63 : : n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t),
64 : : &mp_type_gen_instance);
65 : :
66 : 1264 : o->pend_exc = mp_const_none;
67 : 1264 : o->code_state.fun_bc = self_fun;
68 : 1264 : o->code_state.n_state = n_state;
69 : 1264 : mp_setup_code_state(&o->code_state, n_args, n_kw, args);
70 : 1264 : return MP_OBJ_FROM_PTR(o);
71 : : }
72 : :
73 : : #if MICROPY_PY_FUNCTION_ATTRS
74 : : #define GEN_WRAP_TYPE_ATTR attr, mp_obj_fun_bc_attr,
75 : : #else
76 : : #define GEN_WRAP_TYPE_ATTR
77 : : #endif
78 : :
79 : : MP_DEFINE_CONST_OBJ_TYPE(
80 : : mp_type_gen_wrap,
81 : : MP_QSTR_generator,
82 : : MP_TYPE_FLAG_BINDS_SELF,
83 : : GEN_WRAP_TYPE_ATTR
84 : : call, gen_wrap_call,
85 : : unary_op, mp_generic_unary_op
86 : : );
87 : :
88 : : /******************************************************************************/
89 : : // native generator wrapper
90 : :
91 : : #if MICROPY_EMIT_NATIVE
92 : :
93 : : // Based on mp_obj_gen_instance_t.
94 : : typedef struct _mp_obj_gen_instance_native_t {
95 : : mp_obj_base_t base;
96 : : mp_obj_t pend_exc;
97 : : mp_code_state_native_t code_state;
98 : : } mp_obj_gen_instance_native_t;
99 : :
100 : 1049 : 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) {
101 : : // The state for a native generating function is held in the same struct as a bytecode function
102 : 1049 : mp_obj_fun_bc_t *self_fun = MP_OBJ_TO_PTR(self_in);
103 : :
104 : : // Determine start of prelude.
105 : 1049 : uintptr_t prelude_ptr_index = ((uintptr_t *)self_fun->bytecode)[0];
106 : 1049 : const uint8_t *prelude_ptr;
107 [ + + ]: 1049 : if (prelude_ptr_index == 0) {
108 : 1029 : prelude_ptr = (void *)self_fun->child_table;
109 : : } else {
110 : 20 : prelude_ptr = (void *)self_fun->child_table[prelude_ptr_index];
111 : : }
112 : :
113 : : // Extract n_state from the prelude.
114 : 1049 : const uint8_t *ip = prelude_ptr;
115 [ + + ]: 2138 : MP_BC_PRELUDE_SIG_DECODE(ip);
116 : :
117 : : // Allocate the generator object, with room for local stack (exception stack not needed).
118 : 1049 : mp_obj_gen_instance_native_t *o = mp_obj_malloc_var(mp_obj_gen_instance_native_t, byte, n_state * sizeof(mp_obj_t), &mp_type_gen_instance);
119 : :
120 : : // Parse the input arguments and set up the code state
121 : 1049 : o->pend_exc = mp_const_none;
122 : 1049 : o->code_state.fun_bc = self_fun;
123 : 1049 : o->code_state.ip = prelude_ptr;
124 : 1049 : o->code_state.n_state = n_state;
125 : 1049 : o->code_state.sp = &o->code_state.state[0] - 1;
126 : 1049 : mp_setup_code_state_native(&o->code_state, n_args, n_kw, args);
127 : :
128 : : // Indicate we are a native function, which doesn't use this variable
129 : 1049 : o->code_state.exc_sp_idx = MP_CODE_STATE_EXC_SP_IDX_SENTINEL;
130 : :
131 : : // Prepare the generator instance for execution
132 : 1049 : uintptr_t start_offset = ((uintptr_t *)self_fun->bytecode)[1];
133 : 1049 : o->code_state.ip = MICROPY_MAKE_POINTER_CALLABLE((void *)(self_fun->bytecode + start_offset));
134 : :
135 : 1049 : return MP_OBJ_FROM_PTR(o);
136 : : }
137 : :
138 : : #if MICROPY_PY_FUNCTION_ATTRS
139 : : #define NATIVE_GEN_WRAP_TYPE_ATTR attr, mp_obj_fun_bc_attr,
140 : : #else
141 : : #define NATIVE_GEN_WRAP_TYPE_ATTR
142 : : #endif
143 : :
144 : : MP_DEFINE_CONST_OBJ_TYPE(
145 : : mp_type_native_gen_wrap,
146 : : MP_QSTR_generator,
147 : : MP_TYPE_FLAG_BINDS_SELF,
148 : : call, native_gen_wrap_call,
149 : : NATIVE_GEN_WRAP_TYPE_ATTR
150 : : unary_op, mp_generic_unary_op
151 : : );
152 : :
153 : : #endif // MICROPY_EMIT_NATIVE
154 : :
155 : : /******************************************************************************/
156 : : /* generator instance */
157 : :
158 : 4 : STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
159 : 4 : (void)kind;
160 : 4 : mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
161 : 4 : mp_printf(print, "<generator object '%q' at %p>", mp_obj_fun_get_name(MP_OBJ_FROM_PTR(self->code_state.fun_bc)), self);
162 : 4 : }
163 : :
164 : 180929 : 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) {
165 : 180929 : MP_STACK_CHECK();
166 : 180929 : mp_check_self(mp_obj_is_type(self_in, &mp_type_gen_instance));
167 : 180929 : mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
168 [ + + ]: 180929 : if (self->code_state.ip == 0) {
169 : : // Trying to resume an already stopped generator.
170 : : // This is an optimised "raise StopIteration(None)".
171 : 65 : *ret_val = mp_const_none;
172 : 65 : return MP_VM_RETURN_NORMAL;
173 : : }
174 : :
175 : : // Ensure the generator cannot be reentered during execution
176 [ + + ]: 180864 : if (self->pend_exc == MP_OBJ_NULL) {
177 : 8 : mp_raise_ValueError(MP_ERROR_TEXT("generator already executing"));
178 : : }
179 : :
180 : : #if MICROPY_PY_GENERATOR_PEND_THROW
181 : : // If exception is pending (set using .pend_throw()), process it now.
182 [ + + ]: 180856 : if (self->pend_exc != mp_const_none) {
183 : 16 : throw_value = self->pend_exc;
184 : : }
185 : : #endif
186 : :
187 : : // If the generator is started, allow sending a value.
188 : 180856 : void *state_start = self->code_state.state - 1;
189 : : #if MICROPY_EMIT_NATIVE
190 : 180856 : if (self->code_state.exc_sp_idx == MP_CODE_STATE_EXC_SP_IDX_SENTINEL) {
191 : : state_start = ((mp_obj_gen_instance_native_t *)self)->code_state.state - 1;
192 : : }
193 : : #endif
194 [ + + ]: 180856 : if (self->code_state.sp == state_start) {
195 [ + + ]: 2287 : if (send_value != mp_const_none) {
196 : 4 : mp_raise_TypeError(MP_ERROR_TEXT("can't send non-None value to a just-started generator"));
197 : : }
198 : : } else {
199 : 178569 : *self->code_state.sp = send_value;
200 : : }
201 : :
202 : : // Mark as running
203 : 180852 : self->pend_exc = MP_OBJ_NULL;
204 : :
205 : : // Set up the correct globals context for the generator and execute it
206 : 180852 : self->code_state.old_globals = mp_globals_get();
207 : 180846 : mp_globals_set(self->code_state.fun_bc->context->module.globals);
208 : :
209 : 180852 : mp_vm_return_kind_t ret_kind;
210 : :
211 : : #if MICROPY_EMIT_NATIVE
212 [ + + ]: 180852 : if (self->code_state.exc_sp_idx == MP_CODE_STATE_EXC_SP_IDX_SENTINEL) {
213 : : // A native generator, with entry point 2 words into the "bytecode" pointer
214 : 101715 : typedef uintptr_t (*mp_fun_native_gen_t)(void *, mp_obj_t);
215 : 101715 : mp_fun_native_gen_t fun = MICROPY_MAKE_POINTER_CALLABLE((const void *)(self->code_state.fun_bc->bytecode + 2 * sizeof(uintptr_t)));
216 : 101715 : ret_kind = fun((void *)&self->code_state, throw_value);
217 : : } else
218 : : #endif
219 : : {
220 : : // A bytecode generator
221 : 79137 : ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
222 : : }
223 : :
224 : 180853 : mp_globals_set(self->code_state.old_globals);
225 : :
226 : : // Mark as not running
227 : 180849 : self->pend_exc = mp_const_none;
228 : :
229 [ + + + ]: 180849 : switch (ret_kind) {
230 : 1063 : case MP_VM_RETURN_NORMAL:
231 : : default:
232 : : // Explicitly mark generator as completed. If we don't do this,
233 : : // subsequent next() may re-execute statements after last yield
234 : : // again and again, leading to side effects.
235 : 1063 : self->code_state.ip = 0;
236 : : // This is an optimised "raise StopIteration(*ret_val)".
237 : 1063 : *ret_val = *self->code_state.sp;
238 : 1063 : break;
239 : :
240 : 178703 : case MP_VM_RETURN_YIELD:
241 : 178703 : *ret_val = *self->code_state.sp;
242 : : #if MICROPY_PY_GENERATOR_PEND_THROW
243 : 178703 : *self->code_state.sp = mp_const_none;
244 : : #endif
245 : 178703 : break;
246 : :
247 : 1083 : case MP_VM_RETURN_EXCEPTION: {
248 : 1083 : self->code_state.ip = 0;
249 : : #if MICROPY_EMIT_NATIVE
250 [ + + ]: 1083 : if (self->code_state.exc_sp_idx == MP_CODE_STATE_EXC_SP_IDX_SENTINEL) {
251 : 522 : *ret_val = ((mp_obj_gen_instance_native_t *)self)->code_state.state[0];
252 : : } else
253 : : #endif
254 : : {
255 : 561 : *ret_val = self->code_state.state[0];
256 : : }
257 : : // PEP479: if StopIteration is raised inside a generator it is replaced with RuntimeError
258 [ + + ]: 1083 : if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(*ret_val)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
259 : 8 : *ret_val = mp_obj_new_exception_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("generator raised StopIteration"));
260 : : }
261 : : break;
262 : : }
263 : : }
264 : :
265 : : return ret_kind;
266 : : }
267 : :
268 : 179702 : STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, bool raise_stop_iteration) {
269 : 179702 : mp_obj_t ret;
270 [ + + + ]: 179702 : switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
271 : 760 : case MP_VM_RETURN_NORMAL:
272 : : default:
273 : : // A normal return is a StopIteration, either raise it or return
274 : : // MP_OBJ_STOP_ITERATION as an optimisation.
275 [ + + ]: 760 : if (ret == mp_const_none) {
276 : 691 : ret = MP_OBJ_NULL;
277 : : }
278 [ + + ]: 760 : if (raise_stop_iteration) {
279 : 284 : mp_raise_StopIteration(ret);
280 : : } else {
281 : 476 : return mp_make_stop_iteration(ret);
282 : : }
283 : :
284 : 178361 : case MP_VM_RETURN_YIELD:
285 : 178361 : return ret;
286 : :
287 : 573 : case MP_VM_RETURN_EXCEPTION:
288 : 573 : nlr_raise(ret);
289 : : }
290 : : }
291 : :
292 : 29569 : STATIC mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
293 : 29569 : return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL, false);
294 : : }
295 : :
296 : 149915 : STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
297 : 149915 : return gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL, true);
298 : : }
299 : : STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
300 : :
301 : 220 : STATIC mp_obj_t gen_instance_throw(size_t n_args, const mp_obj_t *args) {
302 : : // The signature of this function is: throw(type[, value[, traceback]])
303 : : // CPython will pass all given arguments through the call chain and process them
304 : : // at the point they are used (native generators will handle them differently to
305 : : // user-defined generators with a throw() method). To save passing multiple
306 : : // values, MicroPython instead does partial processing here to reduce it down to
307 : : // one argument and passes that through:
308 : : // - if only args[1] is given, or args[2] is given but is None, args[1] is
309 : : // passed through (in the standard case it is an exception class or instance)
310 : : // - if args[2] is given and not None it is passed through (in the standard
311 : : // case it would be an exception instance and args[1] its corresponding class)
312 : : // - args[3] is always ignored
313 : :
314 : 220 : mp_obj_t exc = args[1];
315 [ + + + + ]: 220 : if (n_args > 2 && args[2] != mp_const_none) {
316 : 8 : exc = args[2];
317 : : }
318 : :
319 : 220 : return gen_resume_and_raise(args[0], mp_const_none, exc, true);
320 : : }
321 : : STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
322 : :
323 : 40 : STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
324 : 40 : mp_obj_t ret;
325 [ + + + ]: 40 : switch (mp_obj_gen_resume(self_in, mp_const_none, MP_OBJ_FROM_PTR(&mp_const_GeneratorExit_obj), &ret)) {
326 : : case MP_VM_RETURN_YIELD:
327 : 2 : mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("generator ignored GeneratorExit"));
328 : :
329 : : // Swallow GeneratorExit (== successful close), and re-raise any other
330 : 34 : case MP_VM_RETURN_EXCEPTION:
331 : : // ret should always be an instance of an exception class
332 [ + + ]: 34 : if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
333 : : return mp_const_none;
334 : : }
335 : 4 : nlr_raise(ret);
336 : :
337 : : default:
338 : : // The only choice left is MP_VM_RETURN_NORMAL which is successful close
339 : : return mp_const_none;
340 : : }
341 : : }
342 : : STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
343 : :
344 : : #if MICROPY_PY_GENERATOR_PEND_THROW
345 : 36 : STATIC mp_obj_t gen_instance_pend_throw(mp_obj_t self_in, mp_obj_t exc_in) {
346 : 36 : mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
347 [ + + ]: 36 : if (self->pend_exc == MP_OBJ_NULL) {
348 : 4 : mp_raise_ValueError(MP_ERROR_TEXT("generator already executing"));
349 : : }
350 : 32 : mp_obj_t prev = self->pend_exc;
351 : 32 : self->pend_exc = exc_in;
352 : 32 : return prev;
353 : : }
354 : : STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_pend_throw_obj, gen_instance_pend_throw);
355 : : #endif
356 : :
357 : : STATIC const mp_rom_map_elem_t gen_instance_locals_dict_table[] = {
358 : : { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&gen_instance_close_obj) },
359 : : { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&gen_instance_send_obj) },
360 : : { MP_ROM_QSTR(MP_QSTR_throw), MP_ROM_PTR(&gen_instance_throw_obj) },
361 : : #if MICROPY_PY_GENERATOR_PEND_THROW
362 : : { MP_ROM_QSTR(MP_QSTR_pend_throw), MP_ROM_PTR(&gen_instance_pend_throw_obj) },
363 : : #endif
364 : : };
365 : :
366 : : STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
367 : :
368 : : MP_DEFINE_CONST_OBJ_TYPE(
369 : : mp_type_gen_instance,
370 : : MP_QSTR_generator,
371 : : MP_TYPE_FLAG_ITER_IS_ITERNEXT,
372 : : print, gen_instance_print,
373 : : unary_op, mp_generic_unary_op,
374 : : iter, gen_instance_iternext,
375 : : locals_dict, &gen_instance_locals_dict
376 : : );
|