Skip to content

Commit a6e0486

Browse files
authored
Merge pull request #3178 from WarriorOfWire/async_def_coroutine_sim
add coroutine behavior for generators
2 parents a572594 + e9b4e0b commit a6e0486

File tree

10 files changed

+43
-6
lines changed

10 files changed

+43
-6
lines changed

locale/circuitpython.pot

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-07-17 18:03-0700\n"
11+
"POT-Creation-Date: 2020-07-21 18:43-0700\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -234,6 +234,10 @@ msgstr ""
234234
msgid "'continue' outside loop"
235235
msgstr ""
236236

237+
#: py/objgenerator.c
238+
msgid "'coroutine' object is not an iterator"
239+
msgstr ""
240+
237241
#: py/compile.c
238242
msgid "'data' requires at least 2 arguments"
239243
msgstr ""

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: 18 additions & 1 deletion
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,6 +95,12 @@ 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);
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
94104
mp_printf(print, "<generator object '%q' at %p>", mp_obj_fun_get_name(MP_OBJ_FROM_PTR(self->code_state.fun_bc)), self);
95105
}
96106

@@ -194,6 +204,13 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o
194204
}
195205

196206
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
197214
return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
198215
}
199216

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

tests/run-tests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def run_tests(pyb, tests, args, base_path=".", num_threads=1):
362362
if args.emit == 'native':
363363
skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from gen_yield_from_close gen_yield_from_ducktype gen_yield_from_exc gen_yield_from_executing gen_yield_from_iter gen_yield_from_send gen_yield_from_stopped gen_yield_from_throw gen_yield_from_throw2 gen_yield_from_throw3 generator1 generator2 generator_args generator_close generator_closure generator_exc generator_pend_throw generator_return generator_send'.split()}) # require yield
364364
skip_tests.update({'basics/%s.py' % t for t in 'bytes_gen class_store_class globals_del string_join gen_stack_overflow'.split()}) # require yield
365-
skip_tests.update({'basics/async_%s.py' % t for t in 'def await await2 for for2 with with2'.split()}) # require yield
365+
skip_tests.update({'basics/async_%s.py' % t for t in 'def await await2 for for2 with with2 coroutine'.split()}) # require yield
366366
skip_tests.update({'basics/%s.py' % t for t in 'try_reraise try_reraise2'.split()}) # require raise_varargs
367367
skip_tests.update({'basics/%s.py' % t for t in 'with_break with_continue with_return'.split()}) # require complete with support
368368
skip_tests.add('basics/array_construct2.py') # requires generators

0 commit comments

Comments
 (0)