Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
d6f94340
Commit
d6f94340
authored
Mar 26, 2014
by
Damien George
Browse files
Merge pull request #381 from pfalcon/closure-defargs
py: Support closures with default args.
parents
eed6f26b
2447a5b5
Changes
8
Hide whitespace changes
Inline
Side-by-side
py/bc0.h
View file @
d6f94340
...
...
@@ -95,6 +95,7 @@
#define MP_BC_CALL_METHOD_KW (0x98) // uint
#define MP_BC_CALL_METHOD_VAR_KW (0x99) // uint
#define MP_BC_MAKE_FUNCTION_DEFARGS (0x9a) // uint
#define MP_BC_MAKE_CLOSURE_DEFARGS (0x9b) // uint
#define MP_BC_IMPORT_NAME (0xe0) // qstr
#define MP_BC_IMPORT_FROM (0xe1) // qstr
...
...
py/compile.c
View file @
d6f94340
...
...
@@ -766,6 +766,9 @@ void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
// stuff for lambda and comprehensions and generators
void
close_over_variables_etc
(
compiler_t
*
comp
,
scope_t
*
this_scope
,
int
n_dict_params
,
int
n_default_params
)
{
if
(
n_default_params
)
{
EMIT_ARG
(
build_tuple
,
n_default_params
);
}
// make closed over variables, if any
// ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
int
nfree
=
0
;
...
...
py/emitbc.c
View file @
d6f94340
...
...
@@ -722,16 +722,20 @@ STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, int n_dict_param
emit_pre
(
emit
,
1
);
emit_write_byte_code_byte_uint
(
emit
,
MP_BC_MAKE_FUNCTION
,
scope
->
unique_code_id
);
}
else
{
emit_bc_build_tuple
(
emit
,
n_default_params
);
emit_pre
(
emit
,
0
);
emit_write_byte_code_byte_uint
(
emit
,
MP_BC_MAKE_FUNCTION_DEFARGS
,
scope
->
unique_code_id
);
}
}
STATIC
void
emit_bc_make_closure
(
emit_t
*
emit
,
scope_t
*
scope
,
int
n_dict_params
,
int
n_default_params
)
{
assert
(
n_default_params
==
0
&&
n_dict_params
==
0
);
emit_pre
(
emit
,
0
);
emit_write_byte_code_byte_uint
(
emit
,
MP_BC_MAKE_CLOSURE
,
scope
->
unique_code_id
);
assert
(
n_dict_params
==
0
);
if
(
n_default_params
==
0
)
{
emit_pre
(
emit
,
0
);
emit_write_byte_code_byte_uint
(
emit
,
MP_BC_MAKE_CLOSURE
,
scope
->
unique_code_id
);
}
else
{
emit_pre
(
emit
,
-
1
);
emit_write_byte_code_byte_uint
(
emit
,
MP_BC_MAKE_CLOSURE_DEFARGS
,
scope
->
unique_code_id
);
}
}
STATIC
void
emit_bc_call_function
(
emit_t
*
emit
,
int
n_positional
,
int
n_keyword
,
bool
have_star_arg
,
bool
have_dbl_star_arg
)
{
...
...
py/runtime.c
View file @
d6f94340
...
...
@@ -683,10 +683,10 @@ mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
return
fun
;
}
mp_obj_t
rt_make_closure_from_id
(
int
unique_code_id
,
mp_obj_t
closure_tuple
)
{
mp_obj_t
rt_make_closure_from_id
(
int
unique_code_id
,
mp_obj_t
closure_tuple
,
mp_obj_t
def_args
)
{
DEBUG_OP_printf
(
"make_closure_from_id %d
\n
"
,
unique_code_id
);
// make function object
mp_obj_t
ffun
=
rt_make_function_from_id
(
unique_code_id
,
MP_OBJ_NULL
);
mp_obj_t
ffun
=
rt_make_function_from_id
(
unique_code_id
,
def_args
);
// wrap function in closure object
return
mp_obj_new_closure
(
ffun
,
closure_tuple
);
}
...
...
py/runtime.h
View file @
d6f94340
...
...
@@ -19,7 +19,7 @@ mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args);
mp_obj_t
rt_make_function_n
(
int
n_args
,
void
*
fun
);
// fun must have the correct signature for n_args fixed arguments
mp_obj_t
rt_make_function_var
(
int
n_args_min
,
mp_fun_var_t
fun
);
mp_obj_t
rt_make_function_var_between
(
int
n_args_min
,
int
n_args_max
,
mp_fun_var_t
fun
);
// min and max are inclusive
mp_obj_t
rt_make_closure_from_id
(
int
unique_code_id
,
mp_obj_t
closure_tuple
);
mp_obj_t
rt_make_closure_from_id
(
int
unique_code_id
,
mp_obj_t
closure_tuple
,
mp_obj_t
def_args
);
mp_obj_t
rt_call_function_0
(
mp_obj_t
fun
);
mp_obj_t
rt_call_function_1
(
mp_obj_t
fun
,
mp_obj_t
arg
);
mp_obj_t
rt_call_function_2
(
mp_obj_t
fun
,
mp_obj_t
arg1
,
mp_obj_t
arg2
);
...
...
py/showbc.c
View file @
d6f94340
...
...
@@ -371,6 +371,11 @@ void mp_byte_code_print(const byte *ip, int len) {
printf
(
"MAKE_CLOSURE "
UINT_FMT
,
unum
);
break
;
case
MP_BC_MAKE_CLOSURE_DEFARGS
:
DECODE_UINT
;
printf
(
"MAKE_CLOSURE_DEFARGS "
UINT_FMT
,
unum
);
break
;
case
MP_BC_CALL_FUNCTION
:
DECODE_UINT
;
printf
(
"CALL_FUNCTION n="
UINT_FMT
" nkw="
UINT_FMT
,
unum
&
0xff
,
(
unum
>>
8
)
&
0xff
);
...
...
py/vm.c
View file @
d6f94340
...
...
@@ -561,7 +561,13 @@ unwind_jump:
case
MP_BC_MAKE_CLOSURE
:
DECODE_UINT
;
SET_TOP
(
rt_make_closure_from_id
(
unum
,
TOP
()));
SET_TOP
(
rt_make_closure_from_id
(
unum
,
TOP
(),
MP_OBJ_NULL
));
break
;
case
MP_BC_MAKE_CLOSURE_DEFARGS
:
DECODE_UINT
;
obj1
=
POP
();
SET_TOP
(
rt_make_closure_from_id
(
unum
,
obj1
,
TOP
()));
break
;
case
MP_BC_CALL_FUNCTION
:
...
...
tests/basics/closure-defargs.py
0 → 100644
View file @
d6f94340
def
f
():
a
=
1
def
bar
(
b
=
10
,
c
=
20
):
print
(
a
+
b
+
c
)
bar
()
print
(
f
())
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment