Skip to content

Commit 1d75533

Browse files
committed
Merge pull request adafruit#382 from pfalcon/genexit-inst
objgenerator: close(): Throw instance of GeneratorExit (not type).
2 parents 440f041 + c4d589e commit 1d75533

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ extern const mp_obj_t mp_const_false;
231231
extern const mp_obj_t mp_const_true;
232232
extern const mp_obj_t mp_const_empty_tuple;
233233
extern const mp_obj_t mp_const_ellipsis;
234+
extern const mp_obj_t mp_const_GeneratorExit;
234235

235236
// General API for objects
236237

py/objexcept.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ typedef struct mp_obj_exception_t {
2121
mp_obj_tuple_t args;
2222
} mp_obj_exception_t;
2323

24+
// Instance of GeneratorExit exception - needed by generator.close()
25+
// This would belong to objgenerator.c, but to keep mp_obj_exception_t
26+
// definition module-private so far, have it here.
27+
STATIC mp_obj_exception_t GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, NULL, {{&tuple_type}, 0}};
28+
const mp_obj_t mp_const_GeneratorExit = (mp_obj_t)&GeneratorExit_obj;
29+
2430
STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
2531
mp_obj_exception_t *o = o_in;
2632
if (o->msg != NULL) {

py/objgenerator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_ins
171171

172172
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
173173
mp_obj_t ret;
174-
switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_type_GeneratorExit, &ret)) {
174+
switch (mp_obj_gen_resume(self_in, mp_const_none, mp_const_GeneratorExit, &ret)) {
175175
case MP_VM_RETURN_YIELD:
176176
nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
177177

0 commit comments

Comments
 (0)