42
42
typedef struct _mp_obj_gen_wrap_t {
43
43
mp_obj_base_t base ;
44
44
mp_obj_t * fun ;
45
+ bool coroutine_generator ;
45
46
} mp_obj_gen_wrap_t ;
46
47
47
48
typedef struct _mp_obj_gen_instance_t {
48
49
mp_obj_base_t base ;
49
50
mp_obj_dict_t * globals ;
51
+ bool coroutine_generator ;
50
52
mp_code_state_t code_state ;
51
53
} mp_obj_gen_instance_t ;
52
54
@@ -64,6 +66,7 @@ STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons
64
66
n_state * sizeof (mp_obj_t ) + n_exc_stack * sizeof (mp_exc_stack_t ));
65
67
o -> base .type = & mp_type_gen_instance ;
66
68
69
+ o -> coroutine_generator = self -> coroutine_generator ;
67
70
o -> globals = self_fun -> globals ;
68
71
o -> code_state .fun_bc = self_fun ;
69
72
o -> code_state .ip = 0 ;
@@ -78,10 +81,11 @@ const mp_obj_type_t mp_type_gen_wrap = {
78
81
.unary_op = mp_generic_unary_op ,
79
82
};
80
83
81
- mp_obj_t mp_obj_new_gen_wrap (mp_obj_t fun ) {
84
+ mp_obj_t mp_obj_new_gen_wrap (mp_obj_t fun , bool is_coroutine ) {
82
85
mp_obj_gen_wrap_t * o = m_new_obj (mp_obj_gen_wrap_t );
83
86
o -> base .type = & mp_type_gen_wrap ;
84
87
o -> fun = MP_OBJ_TO_PTR (fun );
88
+ o -> coroutine_generator = is_coroutine ;
85
89
return MP_OBJ_FROM_PTR (o );
86
90
}
87
91
@@ -91,6 +95,12 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
91
95
STATIC void gen_instance_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
92
96
(void )kind ;
93
97
mp_obj_gen_instance_t * self = MP_OBJ_TO_PTR (self_in );
98
+ #if MICROPY_PY_ASYNC_AWAIT
99
+ if (self -> coroutine_generator ) {
100
+ mp_printf (print , "<coroutine object '%q' at %p>" , mp_obj_fun_get_name (MP_OBJ_FROM_PTR (self -> code_state .fun_bc )), self );
101
+ return ;
102
+ }
103
+ #endif
94
104
mp_printf (print , "<generator object '%q' at %p>" , mp_obj_fun_get_name (MP_OBJ_FROM_PTR (self -> code_state .fun_bc )), self );
95
105
}
96
106
@@ -194,6 +204,13 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o
194
204
}
195
205
196
206
STATIC mp_obj_t gen_instance_iternext (mp_obj_t self_in ) {
207
+ #if MICROPY_PY_ASYNC_AWAIT
208
+ // This translate is literally too much for m0 boards
209
+ mp_obj_gen_instance_t * self = MP_OBJ_TO_PTR (self_in );
210
+ if (self -> coroutine_generator ) {
211
+ mp_raise_TypeError (translate ("'coroutine' object is not an iterator" ));
212
+ }
213
+ #endif
197
214
return gen_resume_and_raise (self_in , mp_const_none , MP_OBJ_NULL );
198
215
}
199
216
0 commit comments