objgenerator.c 9.55 KB
Newer Older
1
2
3
4
5
6
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
7
 * Copyright (c) 2014 Paul Sokolovsky
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 * 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.
 */

28
29
30
#include <stdlib.h>
#include <assert.h>

31
32
33
34
35
36
#include "py/nlr.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "py/bc.h"
#include "py/objgenerator.h"
#include "py/objfun.h"
37
38
39
40
41
42
43
44
45

/******************************************************************************/
/* generator wrapper                                                          */

typedef struct _mp_obj_gen_wrap_t {
    mp_obj_base_t base;
    mp_obj_t *fun;
} mp_obj_gen_wrap_t;

46
47
48
49
50
typedef struct _mp_obj_gen_instance_t {
    mp_obj_base_t base;
    mp_obj_dict_t *globals;
    mp_code_state code_state;
} mp_obj_gen_instance_t;
51

52
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) {
53
    mp_obj_gen_wrap_t *self = MP_OBJ_TO_PTR(self_in);
54
    mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
55
    assert(self_fun->base.type == &mp_type_fun_bc);
Damien George's avatar
Damien George committed
56

57
58
    // get start of bytecode
    const byte *ip = self_fun->bytecode;
59

60
    // bytecode prelude: get state size and exception stack size
61
62
    mp_uint_t n_state = mp_decode_uint(&ip);
    mp_uint_t n_exc_stack = mp_decode_uint(&ip);
63
64
65
66
67
68
69
70

    // 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;
71
    o->code_state.ip = (byte*)(ip - self_fun->bytecode); // offset to prelude
72
    mp_setup_code_state(&o->code_state, self_fun, n_args, n_kw, args);
73
    return MP_OBJ_FROM_PTR(o);
74
75
}

76
const mp_obj_type_t mp_type_gen_wrap = {
77
    { &mp_type_type },
78
    .name = MP_QSTR_generator,
79
    .call = gen_wrap_call,
80
81
};

82
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
83
    mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
84
    o->base.type = &mp_type_gen_wrap;
85
86
    o->fun = MP_OBJ_TO_PTR(fun);
    return MP_OBJ_FROM_PTR(o);
87
88
89
90
91
}

/******************************************************************************/
/* generator instance                                                         */

92
STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
93
    (void)kind;
94
95
    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);
96
97
}

98
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) {
99
    mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
100
    mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
101
    if (self->code_state.ip == 0) {
102
        // Trying to resume already stopped generator
103
        *ret_val = MP_OBJ_STOP_ITERATION;
104
        return MP_VM_RETURN_NORMAL;
105
    }
106
    if (self->code_state.sp == self->code_state.state - 1) {
107
        if (send_value != mp_const_none) {
108
            nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
109
110
        }
    } else {
111
        *self->code_state.sp = send_value;
112
    }
113
114
    mp_obj_dict_t *old_globals = mp_globals_get();
    mp_globals_set(self->globals);
115
    mp_vm_return_kind_t ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
116
    mp_globals_set(old_globals);
117

118
    switch (ret_kind) {
119
        case MP_VM_RETURN_NORMAL:
120
        default:
121
122
123
124
125
            // 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.
126
127
            self->code_state.ip = 0;
            *ret_val = *self->code_state.sp;
128
            break;
129
130

        case MP_VM_RETURN_YIELD:
131
            *ret_val = *self->code_state.sp;
132
133
134
            if (*ret_val == MP_OBJ_STOP_ITERATION) {
                self->code_state.ip = 0;
            }
135
            break;
136
137

        case MP_VM_RETURN_EXCEPTION:
138
139
            self->code_state.ip = 0;
            *ret_val = self->code_state.state[self->code_state.n_state - 1];
140
            break;
141
    }
142
143

    return ret_kind;
144
145
146
}

STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) {
147
148
    mp_obj_t ret;
    switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
149
        case MP_VM_RETURN_NORMAL:
150
        default:
151
            // Optimize return w/o value in case generator is used in for loop
152
153
            if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
                return MP_OBJ_STOP_ITERATION;
154
            } else {
155
                nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
156
157
158
            }

        case MP_VM_RETURN_YIELD:
159
            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))) {
160
                nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
161
            }
162
            return ret;
163
164

        case MP_VM_RETURN_EXCEPTION:
165
            // TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
166
            // of mp_iternext() protocol, but this function is called by other methods
167
            // too, which may not handled MP_OBJ_STOP_ITERATION.
168
            if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
169
170
171
172
                mp_obj_t val = mp_obj_exception_get_value(ret);
                if (val == mp_const_none) {
                    return MP_OBJ_STOP_ITERATION;
                }
173
            }
174
            nlr_raise(ret);
175
176
    }
}
177

178
STATIC mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
179
    return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
180
181
}

182
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
183
    mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
184
    if (ret == MP_OBJ_STOP_ITERATION) {
185
        nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
186
187
    } else {
        return ret;
188
    }
189
}
190

191
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
192

193
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
194
STATIC mp_obj_t gen_instance_throw(size_t n_args, const mp_obj_t *args) {
195
    mp_obj_t exc = (n_args == 2) ? args[1] : args[2];
Damien George's avatar
Damien George committed
196
    exc = mp_make_raise_obj(exc);
197
198

    mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
199
    if (ret == MP_OBJ_STOP_ITERATION) {
200
        nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
201
202
203
204
205
206
207
    } else {
        return ret;
    }
}

STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);

208
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
209
    mp_obj_t ret;
210
    switch (mp_obj_gen_resume(self_in, mp_const_none, MP_OBJ_FROM_PTR(&mp_const_GeneratorExit_obj), &ret)) {
211
        case MP_VM_RETURN_YIELD:
212
            nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
213

214
215
216
        // 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
217
218
            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))) {
219
220
                return mp_const_none;
            }
221
            nlr_raise(ret);
222
223
224

        default:
            // The only choice left is MP_VM_RETURN_NORMAL which is successful close
225
226
227
228
229
            return mp_const_none;
    }
}

STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
230

231
232
233
234
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) },
235
};
236

237
238
STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);

239
const mp_obj_type_t mp_type_gen_instance = {
240
    { &mp_type_type },
241
    .name = MP_QSTR_generator,
242
    .print = gen_instance_print,
Paul Sokolovsky's avatar
Paul Sokolovsky committed
243
    .getiter = mp_identity,
244
    .iternext = gen_instance_iternext,
245
    .locals_dict = (mp_obj_dict_t*)&gen_instance_locals_dict,
246
};