Skip to content

Commit f50b64c

Browse files
committed
py/runtime: Be sure that non-intercepted thrown object is an exception.
The VM expects that, if mp_resume() returns MP_VM_RETURN_EXCEPTION, then the returned value is an exception instance (eg to add a traceback to it). It's possible that a value passed to a generator's throw() is not an exception so must be explicitly checked for if the thrown value is not intercepted by the generator. Thanks to @jepler for finding the bug.
1 parent 3280788 commit f50b64c

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

py/runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t th
12821282
// will be propagated up. This behavior is approved by test_pep380.py
12831283
// test_delegation_of_close_to_non_generator(),
12841284
// test_delegating_throw_to_non_generator()
1285-
*ret_val = throw_value;
1285+
*ret_val = mp_make_raise_obj(throw_value);
12861286
return MP_VM_RETURN_EXCEPTION;
12871287
}
12881288
}

tests/basics/gen_yield_from_throw3.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,30 @@ def gen():
2828
g = gen()
2929
print(next(g))
3030
print(g.throw(ZeroDivisionError))
31+
32+
# this user-defined generator doesn't have a throw() method
33+
class Iter2:
34+
def __iter__(self):
35+
return self
36+
37+
def __next__(self):
38+
return 1
39+
40+
def gen2():
41+
yield from Iter2()
42+
43+
# the thrown ValueError is not intercepted by the user class
44+
g = gen2()
45+
print(next(g))
46+
try:
47+
g.throw(ValueError)
48+
except:
49+
print('ValueError')
50+
51+
# the thrown 123 is not an exception so raises a TypeError
52+
g = gen2()
53+
print(next(g))
54+
try:
55+
g.throw(123)
56+
except TypeError:
57+
print('TypeError')

0 commit comments

Comments
 (0)