Skip to content

Commit 12e1560

Browse files
committed
Don't crash when assigning attributes of the GeneratorExit const singleton
1 parent 8077e5b commit 12e1560

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

py/objexcept.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ 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 (attr == MP_QSTR___traceback__) {
221+
if (self == &mp_const_GeneratorExit_obj) {
222+
// Can't set attributes, but don't want to crash...
223+
dest[0] = MP_OBJ_NULL; // indicate success
224+
} else if (attr == MP_QSTR___traceback__) {
222225
if (dest[1] == mp_const_none) {
223226
self->traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;
224227
} else {

tests/basics/gen_yield_from_close.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ 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+
e.__traceback__ = None
4243
raise
4344
yield 10
4445
yield 11

0 commit comments

Comments
 (0)