Skip to content

Commit 067ae12

Browse files
committed
objclosure: Fix printing of generator closures.
The code previously assumed that only functions can be closed over.
1 parent 9b0b373 commit 067ae12

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

py/objclosure.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ mp_obj_t closure_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const
6767
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
6868
STATIC void closure_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
6969
mp_obj_closure_t *o = o_in;
70-
print(env, "<closure %s at %p, n_closed=%u ", mp_obj_fun_get_name(o->fun), o, o->n_closed);
70+
print(env, "<closure ");
71+
mp_obj_print_helper(print, env, o->fun, PRINT_REPR);
72+
print(env, " at %p, n_closed=%u ", o, o->n_closed);
7173
for (mp_uint_t i = 0; i < o->n_closed; i++) {
7274
if (o->closed[i] == MP_OBJ_NULL) {
7375
print(env, "(nil)");

tests/basics/generator_closure.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ def g():
2424
for i in generator_of_generators:
2525
for j in i:
2626
print(j)
27+
28+
# test that printing of closed-over generators doesn't lead to segfaults
29+
def genc():
30+
foo = 1
31+
repr(lambda: (yield foo))
32+
genc()

0 commit comments

Comments
 (0)