summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-03-29 12:18:14 +0000
committerDamien George <damien.p.george@gmail.com>2014-03-29 12:18:14 +0000
commitda51a399cf628699546f77f320ef0388a268b49f (patch)
tree44e842786517ed39a8738e24da605160c46edcf3
parent75f71584a67b3aa8ad03762547ac6441551c63ca (diff)
parent3c2b2acd8c21294d046f4d2ef9195a976a5ef999 (diff)
downloadmicropython-da51a399cf628699546f77f320ef0388a268b49f.tar.gz
micropython-da51a399cf628699546f77f320ef0388a268b49f.zip
Merge pull request #383 from pfalcon/yield-from
Implement "yield from"
-rw-r--r--py/vm.c58
-rw-r--r--tests/basics/gen-yield-from-close.py87
-rw-r--r--tests/basics/gen-yield-from-exc.py13
-rw-r--r--tests/basics/gen-yield-from-send.py14
-rw-r--r--tests/basics/gen-yield-from-throw.py19
-rw-r--r--tests/basics/gen-yield-from.py42
6 files changed, 232 insertions, 1 deletions
diff --git a/py/vm.c b/py/vm.c
index 12cea7f26d..1a1d345b2a 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -9,6 +9,7 @@
#include "runtime.h"
#include "bc0.h"
#include "bc.h"
+#include "objgenerator.h"
// Value stack grows up (this makes it incompatible with native C stack, but
// makes sure that arguments to functions are in natural order arg1..argN
@@ -146,7 +147,9 @@ outer_dispatch_loop:
// If we have exception to inject, now that we finish setting up
// execution context, raise it. This works as if RAISE_VARARGS
// bytecode was executed.
- if (inject_exc != MP_OBJ_NULL) {
+ // Injecting exc into yield from generator is a special case,
+ // handled by MP_BC_YIELD_FROM itself
+ if (inject_exc != MP_OBJ_NULL && *ip != MP_BC_YIELD_FROM) {
mp_obj_t t = inject_exc;
inject_exc = MP_OBJ_NULL;
nlr_jump(rt_make_raise_obj(t));
@@ -634,12 +637,65 @@ unwind_return:
nlr_jump(rt_make_raise_obj(obj1));
case MP_BC_YIELD_VALUE:
+yield:
nlr_pop();
*ip_in_out = ip;
*sp_in_out = sp;
*exc_sp_in_out = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
return MP_VM_RETURN_YIELD;
+ case MP_BC_YIELD_FROM:
+ {
+//#define EXC_MATCH(exc, type) MP_OBJ_IS_TYPE(exc, type)
+#define EXC_MATCH(exc, type) mp_obj_exception_match(exc, type)
+#define GENERATOR_EXIT_IF_NEEDED(t) if (t != MP_OBJ_NULL && EXC_MATCH(t, &mp_type_GeneratorExit)) { nlr_jump(t); }
+ mp_vm_return_kind_t ret_kind;
+ obj1 = POP();
+ mp_obj_t t_exc = MP_OBJ_NULL;
+ if (inject_exc != MP_OBJ_NULL) {
+ t_exc = inject_exc;
+ inject_exc = MP_OBJ_NULL;
+ ret_kind = mp_obj_gen_resume(TOP(), mp_const_none, t_exc, &obj2);
+ } else {
+ ret_kind = mp_obj_gen_resume(TOP(), obj1, MP_OBJ_NULL, &obj2);
+ }
+
+ if (ret_kind == MP_VM_RETURN_YIELD) {
+ ip--;
+ PUSH(obj2);
+ goto yield;
+ }
+ if (ret_kind == MP_VM_RETURN_NORMAL) {
+ // Pop exhausted gen
+ sp--;
+ if (obj2 == MP_OBJ_NULL) {
+ // Optimize StopIteration
+ // TODO: get StopIteration's value
+ PUSH(mp_const_none);
+ } else {
+ PUSH(obj2);
+ }
+
+ // If we injected GeneratorExit downstream, then even
+ // if it was swallowed, we re-raise GeneratorExit
+ GENERATOR_EXIT_IF_NEEDED(t_exc);
+ break;
+ }
+ if (ret_kind == MP_VM_RETURN_EXCEPTION) {
+ // Pop exhausted gen
+ sp--;
+ if (EXC_MATCH(obj2, &mp_type_StopIteration)) {
+ PUSH(mp_obj_exception_get_value(obj2));
+ // If we injected GeneratorExit downstream, then even
+ // if it was swallowed, we re-raise GeneratorExit
+ GENERATOR_EXIT_IF_NEEDED(t_exc);
+ break;
+ } else {
+ nlr_jump(obj2);
+ }
+ }
+ }
+
case MP_BC_IMPORT_NAME:
DECODE_QSTR;
obj1 = POP();
diff --git a/tests/basics/gen-yield-from-close.py b/tests/basics/gen-yield-from-close.py
new file mode 100644
index 0000000000..7982d5c990
--- /dev/null
+++ b/tests/basics/gen-yield-from-close.py
@@ -0,0 +1,87 @@
+def gen():
+ yield 1
+ yield 2
+ yield 3
+ yield 4
+
+def gen2():
+ yield -1
+ print((yield from gen()))
+ yield 10
+ yield 11
+
+g = gen2()
+print(next(g))
+print(next(g))
+g.close()
+try:
+ print(next(g))
+except StopIteration:
+ print("StopIteration")
+
+
+# Now variation of same test, but with leaf generator
+# swallowing GeneratorExit exception - its upstream gen
+# generator should still receive one.
+def gen3():
+ yield 1
+ try:
+ yield 2
+ except GeneratorExit:
+ print("leaf caught GeneratorExit and swallowed it")
+ return
+ yield 3
+ yield 4
+
+def gen4():
+ yield -1
+ try:
+ print((yield from gen3()))
+ except GeneratorExit:
+ print("delegating caught GeneratorExit")
+ raise
+ yield 10
+ yield 11
+
+g = gen4()
+print(next(g))
+print(next(g))
+print(next(g))
+g.close()
+try:
+ print(next(g))
+except StopIteration:
+ print("StopIteration")
+
+
+# Yet another variation - leaf generator gets GeneratorExit,
+# but raises StopIteration instead. This still should close chain properly.
+def gen5():
+ yield 1
+ try:
+ yield 2
+ except GeneratorExit:
+ print("leaf caught GeneratorExit and raised StopIteration instead")
+ raise StopIteration(123)
+ yield 3
+ yield 4
+
+def gen6():
+ yield -1
+ try:
+ print((yield from gen5()))
+ except GeneratorExit:
+ print("delegating caught GeneratorExit")
+ raise
+ yield 10
+ yield 11
+
+g = gen6()
+print(next(g))
+print(next(g))
+print(next(g))
+g.close()
+try:
+ print(next(g))
+except StopIteration:
+ print("StopIteration")
diff --git a/tests/basics/gen-yield-from-exc.py b/tests/basics/gen-yield-from-exc.py
new file mode 100644
index 0000000000..01c954f5b7
--- /dev/null
+++ b/tests/basics/gen-yield-from-exc.py
@@ -0,0 +1,13 @@
+def gen():
+ yield 1
+ yield 2
+ raise ValueError
+
+def gen2():
+ try:
+ print((yield from gen()))
+ except ValueError:
+ print("caught ValueError from downstream")
+
+g = gen2()
+print(list(g))
diff --git a/tests/basics/gen-yield-from-send.py b/tests/basics/gen-yield-from-send.py
new file mode 100644
index 0000000000..c2e94b5ad8
--- /dev/null
+++ b/tests/basics/gen-yield-from-send.py
@@ -0,0 +1,14 @@
+def gen():
+ print("sent:", (yield 1))
+ yield 2
+
+def gen2():
+ print((yield from gen()))
+
+g = gen2()
+next(g)
+print("yielded:", g.send("val"))
+try:
+ next(g)
+except StopIteration:
+ print("StopIteration")
diff --git a/tests/basics/gen-yield-from-throw.py b/tests/basics/gen-yield-from-throw.py
new file mode 100644
index 0000000000..30960fb9c1
--- /dev/null
+++ b/tests/basics/gen-yield-from-throw.py
@@ -0,0 +1,19 @@
+def gen():
+ try:
+ yield 1
+ except ValueError:
+ print("got ValueError from upstream!")
+ yield "str1"
+ raise TypeError
+
+def gen2():
+ print((yield from gen()))
+
+g = gen2()
+print(next(g))
+print(g.throw(ValueError))
+try:
+ print(next(g))
+except TypeError:
+ print("got TypeError from downstream!")
+
diff --git a/tests/basics/gen-yield-from.py b/tests/basics/gen-yield-from.py
new file mode 100644
index 0000000000..5196b48d2b
--- /dev/null
+++ b/tests/basics/gen-yield-from.py
@@ -0,0 +1,42 @@
+# Case of terminating subgen using return with value
+def gen():
+ yield 1
+ yield 2
+ return 3
+
+def gen2():
+ print("here1")
+ print((yield from gen()))
+ print("here2")
+
+g = gen2()
+print(list(g))
+
+
+# Like above, but terminate subgen using StopIteration
+def gen3():
+ yield 1
+ yield 2
+ raise StopIteration
+
+def gen4():
+ print("here1")
+ print((yield from gen3()))
+ print("here2")
+
+g = gen4()
+print(list(g))
+
+# Like above, but terminate subgen using StopIteration with value
+def gen5():
+ yield 1
+ yield 2
+ raise StopIteration(123)
+
+def gen6():
+ print("here1")
+ print((yield from gen5()))
+ print("here2")
+
+g = gen6()
+print(list(g))