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
22d85ec5
Commit
22d85ec5
authored
9 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Use new code pattern for parsing kw args with mp_arg_parse_all.
Makes code easier to read and more maintainable.
parent
8bb4931f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
py/objenumerate.c
+12
-10
12 additions, 10 deletions
py/objenumerate.c
py/objlist.c
+7
-4
7 additions, 4 deletions
py/objlist.c
py/objstr.c
+6
-3
6 additions, 3 deletions
py/objstr.c
with
25 additions
and
17 deletions
py/objenumerate.c
+
12
−
10
View file @
22d85ec5
...
...
@@ -39,23 +39,25 @@ typedef struct _mp_obj_enumerate_t {
STATIC
mp_obj_t
enumerate_iternext
(
mp_obj_t
self_in
);
STATIC
const
mp_arg_t
enumerate_make_new_args
[]
=
{
STATIC
mp_obj_t
enumerate_make_new
(
const
mp_obj_type_t
*
type
,
size_t
n_args
,
size_t
n_kw
,
const
mp_obj_t
*
args
)
{
#if MICROPY_CPYTHON_COMPAT
static
const
mp_arg_t
allowed_args
[]
=
{
{
MP_QSTR_iterable
,
MP_ARG_REQUIRED
|
MP_ARG_OBJ
,
{.
u_obj
=
MP_OBJ_NULL
}
},
{
MP_QSTR_start
,
MP_ARG_INT
,
{.
u_int
=
0
}
},
};
#define ENUMERATE_MAKE_NEW_NUM_ARGS MP_ARRAY_SIZE(enumerate_make_new_args)
STATIC
mp_obj_t
enumerate_make_new
(
const
mp_obj_type_t
*
type
,
size_t
n_args
,
size_t
n_kw
,
const
mp_obj_t
*
args
)
{
#if MICROPY_CPYTHON_COMPAT
// parse args
mp_arg_val_t
vals
[
ENUMERATE_MAKE_NEW_NUM_ARGS
];
mp_arg_parse_all_kw_array
(
n_args
,
n_kw
,
args
,
ENUMERATE_MAKE_NEW_NUM_ARGS
,
enumerate_make_new_args
,
vals
);
struct
{
mp_arg_val_t
iterable
,
start
;
}
arg_vals
;
mp_arg_parse_all_kw_array
(
n_args
,
n_kw
,
args
,
MP_ARRAY_SIZE
(
allowed_args
),
allowed_args
,
(
mp_arg_val_t
*
)
&
arg_vals
);
// create enumerate object
mp_obj_enumerate_t
*
o
=
m_new_obj
(
mp_obj_enumerate_t
);
o
->
base
.
type
=
type
;
o
->
iter
=
mp_getiter
(
vals
[
0
]
.
u_obj
);
o
->
cur
=
vals
[
1
]
.
u_int
;
o
->
iter
=
mp_getiter
(
arg_vals
.
iterable
.
u_obj
);
o
->
cur
=
arg_vals
.
start
.
u_int
;
#else
(
void
)
n_kw
;
mp_obj_enumerate_t
*
o
=
m_new_obj
(
mp_obj_enumerate_t
);
...
...
This diff is collapsed.
Click to expand it.
py/objlist.c
+
7
−
4
View file @
22d85ec5
...
...
@@ -318,16 +318,19 @@ mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
};
// parse args
mp_arg_val_t
args
[
MP_ARRAY_SIZE
(
allowed_args
)];
mp_arg_parse_all
(
n_args
-
1
,
pos_args
+
1
,
kw_args
,
MP_ARRAY_SIZE
(
allowed_args
),
allowed_args
,
args
);
struct
{
mp_arg_val_t
key
,
reverse
;
}
args
;
mp_arg_parse_all
(
n_args
-
1
,
pos_args
+
1
,
kw_args
,
MP_ARRAY_SIZE
(
allowed_args
),
allowed_args
,
(
mp_arg_val_t
*
)
&
args
);
assert
(
MP_OBJ_IS_TYPE
(
pos_args
[
0
],
&
mp_type_list
));
mp_obj_list_t
*
self
=
MP_OBJ_TO_PTR
(
pos_args
[
0
]);
if
(
self
->
len
>
1
)
{
mp_quicksort
(
self
->
items
,
self
->
items
+
self
->
len
-
1
,
args
[
0
]
.
u_obj
==
mp_const_none
?
MP_OBJ_NULL
:
args
[
0
]
.
u_obj
,
args
[
1
]
.
u_bool
?
mp_const_false
:
mp_const_true
);
args
.
key
.
u_obj
==
mp_const_none
?
MP_OBJ_NULL
:
args
.
key
.
u_obj
,
args
.
reverse
.
u_bool
?
mp_const_false
:
mp_const_true
);
}
return
mp_const_none
;
...
...
This diff is collapsed.
Click to expand it.
py/objstr.c
+
6
−
3
View file @
22d85ec5
...
...
@@ -557,11 +557,14 @@ STATIC mp_obj_t str_splitlines(size_t n_args, const mp_obj_t *pos_args, mp_map_t
};
// parse args
mp_arg_val_t
args
[
MP_ARRAY_SIZE
(
allowed_args
)];
mp_arg_parse_all
(
n_args
-
1
,
pos_args
+
1
,
kw_args
,
MP_ARRAY_SIZE
(
allowed_args
),
allowed_args
,
args
);
struct
{
mp_arg_val_t
keepends
;
}
args
;
mp_arg_parse_all
(
n_args
-
1
,
pos_args
+
1
,
kw_args
,
MP_ARRAY_SIZE
(
allowed_args
),
allowed_args
,
(
mp_arg_val_t
*
)
&
args
);
mp_obj_t
new_args
[
2
]
=
{
pos_args
[
0
],
MP_OBJ_NEW_QSTR
(
MP_QSTR__backslash_n
)};
return
str_split_internal
(
2
,
new_args
,
SPLITLINES
|
(
args
[
0
]
.
u_bool
?
KEEP
:
0
));
return
str_split_internal
(
2
,
new_args
,
SPLITLINES
|
(
args
.
keepends
.
u_bool
?
KEEP
:
0
));
}
#endif
...
...
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