Browse Source

objclosure: Fix printing of generator closures.

The code previously assumed that only functions can be closed over.
pull/913/head
Paul Sokolovsky 10 years ago
parent
commit
067ae1269d
  1. 4
      py/objclosure.c
  2. 6
      tests/basics/generator_closure.py

4
py/objclosure.c

@ -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
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
STATIC void closure_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_closure_t *o = o_in;
print(env, "<closure %s at %p, n_closed=%u ", mp_obj_fun_get_name(o->fun), o, o->n_closed);
print(env, "<closure ");
mp_obj_print_helper(print, env, o->fun, PRINT_REPR);
print(env, " at %p, n_closed=%u ", o, o->n_closed);
for (mp_uint_t i = 0; i < o->n_closed; i++) {
if (o->closed[i] == MP_OBJ_NULL) {
print(env, "(nil)");

6
tests/basics/generator_closure.py

@ -24,3 +24,9 @@ generator_of_generators = (((x, y) for x in range(2)) for y in range(3))
for i in generator_of_generators:
for j in i:
print(j)
# test that printing of closed-over generators doesn't lead to segfaults
def genc():
foo = 1
repr(lambda: (yield foo))
genc()

Loading…
Cancel
Save