Skip to content

Commit bdcbf0f

Browse files
committed
py: Restore CPython compatibility in compiler for closures with def args.
1 parent d6f9434 commit bdcbf0f

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

py/compile.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,13 @@ void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
766766

767767
// stuff for lambda and comprehensions and generators
768768
void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
769+
#if !MICROPY_EMIT_CPYTHON
770+
// in Micro Python we put the default params into a tuple using the bytecode
769771
if (n_default_params) {
770772
EMIT_ARG(build_tuple, n_default_params);
771773
}
774+
#endif
775+
772776
// make closed over variables, if any
773777
// ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
774778
int nfree = 0;
@@ -791,14 +795,12 @@ void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_
791795
}
792796
}
793797
}
794-
if (nfree > 0) {
795-
EMIT_ARG(build_tuple, nfree);
796-
}
797798

798799
// make the function/closure
799800
if (nfree == 0) {
800801
EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
801802
} else {
803+
EMIT_ARG(build_tuple, nfree);
802804
EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
803805
}
804806
}

0 commit comments

Comments
 (0)