Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
e5d371b5
Commit
e5d371b5
authored
11 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Pass keyword arguments to byte code.
parent
2e482cdb
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
py/objfun.c
+25
-2
25 additions, 2 deletions
py/objfun.c
tests/basics/fun-kwargs.py
+19
-0
19 additions, 0 deletions
tests/basics/fun-kwargs.py
with
44 additions
and
2 deletions
py/objfun.c
+
25
−
2
View file @
e5d371b5
...
...
@@ -141,15 +141,18 @@ typedef struct _mp_obj_fun_bc_t {
};
uint
n_state
;
// total state size for the executing function (incl args, locals, stack)
const
byte
*
bytecode
;
// bytecode for the function
mp_obj_t
extra_args
[];
// values of default args (if any), plus a slot at the end for var args (if it takes them)
mp_obj_t
extra_args
[];
// values of default args (if any), plus a slot at the end for var args
and/or kw args
(if it takes them)
}
mp_obj_fun_bc_t
;
STATIC
mp_obj_t
fun_bc_call
(
mp_obj_t
self_in
,
uint
n_args
,
uint
n_kw
,
const
mp_obj_t
*
args
)
{
mp_obj_fun_bc_t
*
self
=
self_in
;
const
mp_obj_t
*
kwargs
=
args
+
n_args
;
mp_obj_t
*
extra_args
=
self
->
extra_args
+
self
->
n_def_args
;
uint
n_extra_args
=
0
;
// check positional arguments
if
(
n_args
>
self
->
n_args
)
{
// given more than enough arguments
if
(
!
self
->
takes_var_args
)
{
...
...
@@ -171,8 +174,25 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_o
goto
arg_error
;
}
// check keyword arguments
if
(
n_kw
!=
0
)
{
nlr_jump
(
mp_obj_new_exception_msg
(
&
mp_type_TypeError
,
"function does not take keyword arguments"
));
// keyword arguments given
if
(
!
self
->
takes_kw_args
)
{
nlr_jump
(
mp_obj_new_exception_msg
(
&
mp_type_TypeError
,
"function does not take keyword arguments"
));
}
mp_obj_t
dict
=
mp_obj_new_dict
(
n_kw
);
for
(
uint
i
=
0
;
i
<
n_kw
;
i
++
)
{
mp_obj_dict_store
(
dict
,
kwargs
[
2
*
i
],
kwargs
[
2
*
i
+
1
]);
}
extra_args
[
n_extra_args
]
=
dict
;
n_extra_args
+=
1
;
}
else
{
// no keyword arguments given
if
(
self
->
takes_kw_args
)
{
extra_args
[
n_extra_args
]
=
mp_obj_new_dict
(
0
);
n_extra_args
+=
1
;
}
}
mp_map_t
*
old_globals
=
rt_globals_get
();
...
...
@@ -208,6 +228,9 @@ mp_obj_t mp_obj_new_fun_bc(uint scope_flags, uint n_args, mp_obj_t def_args_in,
if
((
scope_flags
&
MP_SCOPE_FLAG_VARARGS
)
!=
0
)
{
n_extra_args
+=
1
;
}
if
((
scope_flags
&
MP_SCOPE_FLAG_VARKEYWORDS
)
!=
0
)
{
n_extra_args
+=
1
;
}
mp_obj_fun_bc_t
*
o
=
m_new_obj_var
(
mp_obj_fun_bc_t
,
mp_obj_t
,
n_extra_args
);
o
->
base
.
type
=
&
fun_bc_type
;
o
->
globals
=
rt_globals_get
();
...
...
This diff is collapsed.
Click to expand it.
tests/basics/fun-kwargs.py
0 → 100644
+
19
−
0
View file @
e5d371b5
def
f1
(
**
kwargs
):
print
(
kwargs
)
f1
()
f1
(
a
=
1
)
def
f2
(
a
,
**
kwargs
):
print
(
a
,
kwargs
)
f2
(
1
)
f2
(
1
,
b
=
2
)
def
f3
(
a
,
*
vargs
,
**
kwargs
):
print
(
a
,
vargs
,
kwargs
)
f3
(
1
)
f3
(
1
,
2
)
f3
(
1
,
b
=
2
)
f3
(
1
,
2
,
b
=
3
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment