Skip to content

Commit 17bda55

Browse files
authored
Merge pull request #7103 from jepler/crash-assign-generatorexit
Don't crash when assigning attributes of the GeneratorExit const singleton
2 parents 5a6fffc + 05252c8 commit 17bda55

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

py/objexcept.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
218218
mp_obj_exception_t *self = MP_OBJ_TO_PTR(self_in);
219219
if (dest[0] != MP_OBJ_NULL) {
220220
// store/delete attribute
221+
if (self == &mp_const_GeneratorExit_obj) {
222+
mp_raise_AttributeError(MP_ERROR_TEXT("can't set attribute"));
223+
}
221224
if (attr == MP_QSTR___traceback__) {
222225
if (dest[1] == mp_const_none) {
223226
self->traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;

tests/basics/gen_yield_from_close.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ def gen4():
3737
yield -1
3838
try:
3939
print((yield from gen3()))
40-
except GeneratorExit:
40+
except GeneratorExit as e:
4141
print("delegating caught GeneratorExit")
42+
try:
43+
e.__traceback__ = None
44+
except AttributeError:
45+
pass # generated in micropython, not in python3
4246
raise
4347
yield 10
4448
yield 11
@@ -121,3 +125,20 @@ def gen9():
121125
g = gen9()
122126
print(next(g))
123127
g.close()
128+
129+
# Test that, when chaining to a GeneratorExit exception generated internally,
130+
# no exception or crash occurs
131+
def gen10():
132+
try:
133+
yield 1/0
134+
except Exception as e:
135+
yield 1
136+
yield 2
137+
yield 3
138+
g = gen10()
139+
print(next(g))
140+
g.close()
141+
try:
142+
print(next(g))
143+
except StopIteration:
144+
print("StopIteration")

0 commit comments

Comments
 (0)