Skip to content

Commit

Permalink
vm: Properly handle StopIteration raised in user instance iterator.
Browse files Browse the repository at this point in the history
I.e. in bytecode Python functions.
  • Loading branch information
pfalcon authored and dpgeorge committed May 11, 2015
1 parent d5e629a commit 6738c1d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions py/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,12 @@ unwind_jump:;
code_state->ip = ip + ulab; // jump to after for-block
code_state->sp -= 1; // pop the exhausted iterator
goto outer_dispatch_loop; // continue with dispatch loop
} else if (*code_state->ip == MP_BC_YIELD_FROM) {
// StopIteration inside yield from call means return a value of
// yield from, so inject exception's value as yield from's result
*++code_state->sp = mp_obj_exception_get_value(nlr.ret_val);
code_state->ip++; // yield from is over, move to next instruction
goto outer_dispatch_loop; // continue with dispatch loop
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/basics/gen_yield_from_ducktype.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,25 @@ def gen3():
print(next(g))
print(g.send(5))
print(g.send(100))


#
# Test proper handling of StopIteration vs other exceptions
#
class MyIter:
def __iter__(self):
return self
def __next__(self):
raise StopIteration(42)

def gen4():
global ret
ret = yield from MyIter()
1//0

ret = None
try:
print(list(gen4()))
except ZeroDivisionError:
print("ZeroDivisionError")
print(ret)

0 comments on commit 6738c1d

Please sign in to comment.