Skip to content

Commit 51a79b1

Browse files
committed
add coroutine behavior for generators
coroutines don't have __next__; they also call themselves coroutines. This does not change the fact that `async def` methods are generators, but it does make them behave more like CPython.
1 parent 5437084 commit 51a79b1

File tree

8 files changed

+33
-5
lines changed

8 files changed

+33
-5
lines changed

mpy-cross/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
4242
#define MICROPY_CPYTHON_COMPAT (1)
43+
#define MICROPY_PY_ASYNC_AWAIT (1)
4344
#define MICROPY_USE_INTERNAL_PRINTF (0)
4445

4546
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)

py/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
18901890
// async def
18911891
compile_funcdef(comp, pns0);
18921892
scope_t *fscope = (scope_t*)pns0->nodes[4];
1893-
fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1893+
fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR | MP_SCOPE_FLAG_ASYNC;
18941894
} else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
18951895
// async for
18961896
compile_async_for_stmt(comp, pns0);

py/emitglue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, mp_obj_t def_ar
152152

153153
// check for generator functions and if so wrap in generator object
154154
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
155-
fun = mp_obj_new_gen_wrap(fun);
155+
fun = mp_obj_new_gen_wrap(fun, (rc->scope_flags & MP_SCOPE_FLAG_ASYNC) != 0);
156156
}
157157

158158
return fun;

py/obj.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ mp_obj_t mp_obj_new_fun_bc(mp_obj_t def_args, mp_obj_t def_kw_args, const byte *
665665
mp_obj_t mp_obj_new_fun_native(mp_obj_t def_args_in, mp_obj_t def_kw_args, const void *fun_data, const mp_uint_t *const_table);
666666
mp_obj_t mp_obj_new_fun_viper(size_t n_args, void *fun_data, mp_uint_t type_sig);
667667
mp_obj_t mp_obj_new_fun_asm(size_t n_args, void *fun_data, mp_uint_t type_sig);
668-
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
668+
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun, bool is_coroutine);
669669
mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed, const mp_obj_t *closed);
670670
mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
671671
mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);

py/objgenerator.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@
4242
typedef struct _mp_obj_gen_wrap_t {
4343
mp_obj_base_t base;
4444
mp_obj_t *fun;
45+
bool coroutine_generator;
4546
} mp_obj_gen_wrap_t;
4647

4748
typedef struct _mp_obj_gen_instance_t {
4849
mp_obj_base_t base;
4950
mp_obj_dict_t *globals;
51+
bool coroutine_generator;
5052
mp_code_state_t code_state;
5153
} mp_obj_gen_instance_t;
5254

@@ -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
6466
n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
6567
o->base.type = &mp_type_gen_instance;
6668

69+
o->coroutine_generator = self->coroutine_generator;
6770
o->globals = self_fun->globals;
6871
o->code_state.fun_bc = self_fun;
6972
o->code_state.ip = 0;
@@ -78,10 +81,11 @@ const mp_obj_type_t mp_type_gen_wrap = {
7881
.unary_op = mp_generic_unary_op,
7982
};
8083

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) {
8285
mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
8386
o->base.type = &mp_type_gen_wrap;
8487
o->fun = MP_OBJ_TO_PTR(fun);
88+
o->coroutine_generator = is_coroutine;
8589
return MP_OBJ_FROM_PTR(o);
8690
}
8791

@@ -91,7 +95,11 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
9195
STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
9296
(void)kind;
9397
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
94-
mp_printf(print, "<generator object '%q' at %p>", mp_obj_fun_get_name(MP_OBJ_FROM_PTR(self->code_state.fun_bc)), self);
98+
if (self->coroutine_generator) {
99+
mp_printf(print, "<coroutine object '%q' at %p>", mp_obj_fun_get_name(MP_OBJ_FROM_PTR(self->code_state.fun_bc)), self);
100+
} else {
101+
mp_printf(print, "<generator object '%q' at %p>", mp_obj_fun_get_name(MP_OBJ_FROM_PTR(self->code_state.fun_bc)), self);
102+
}
95103
}
96104

97105
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) {
@@ -194,6 +202,10 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o
194202
}
195203

196204
STATIC mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
205+
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
206+
if (self->coroutine_generator) {
207+
mp_raise_TypeError(translate("'coroutine' object is not an iterator"));
208+
}
197209
return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
198210
}
199211

py/runtime0.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define MP_SCOPE_FLAG_VARKEYWORDS (0x02)
3434
#define MP_SCOPE_FLAG_GENERATOR (0x04)
3535
#define MP_SCOPE_FLAG_DEFKWARGS (0x08)
36+
#define MP_SCOPE_FLAG_ASYNC (0x10)
3637

3738
// types for native (viper) function signature
3839
#define MP_NATIVE_TYPE_OBJ (0x00)

tests/basics/async_coroutine.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
async def f():
2+
pass
3+
4+
try:
5+
f() # Should not crash
6+
except Exception as e:
7+
print('failed to invoke')
8+
9+
try:
10+
next(f())
11+
print('This should fail because async def returns a coroutine, and next() is not allowed')
12+
except Exception as e:
13+
print('pass')

tests/basics/async_coroutine.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pass

0 commit comments

Comments
 (0)