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
GitLab 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
686afc5c
Commit
686afc5c
authored
11 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Check that sequence has 2 elements for dict iterable constructor.
parent
be019ce0
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/obj.c
+1
-1
1 addition, 1 deletion
py/obj.c
py/objdict.c
+4
-2
4 additions, 2 deletions
py/objdict.c
tests/basics/dict-from-iter.py
+10
-0
10 additions, 0 deletions
tests/basics/dict-from-iter.py
with
15 additions
and
3 deletions
py/obj.c
+
1
−
1
View file @
686afc5c
...
...
@@ -280,7 +280,7 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items) {
mp_obj_list_get
(
o
,
&
seq_len
,
items
);
}
if
(
seq_len
!=
len
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_
Index
Error
,
"requested length %d but object has length %d"
,
len
,
seq_len
));
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_
Value
Error
,
"requested length %d but object has length %d"
,
len
,
seq_len
));
}
}
else
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"object '%s' is not a tuple or list"
,
mp_obj_get_type_str
(
o
)));
...
...
This diff is collapsed.
Click to expand it.
py/objdict.c
+
4
−
2
View file @
686afc5c
...
...
@@ -50,9 +50,11 @@ STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
mp_obj_t
iterable
=
mp_getiter
(
args
[
0
]);
mp_obj_t
dict
=
mp_obj_new_dict
(
0
);
// TODO: support arbitrary seq as a pair
mp_obj_t
uple_t
*
item
;
mp_obj_t
item
;
while
((
item
=
mp_iternext
(
iterable
))
!=
MP_OBJ_NULL
)
{
mp_obj_dict_store
(
dict
,
item
->
items
[
0
],
item
->
items
[
1
]);
mp_obj_t
*
sub_items
;
mp_obj_get_array_fixed_n
(
item
,
2
,
&
sub_items
);
mp_obj_dict_store
(
dict
,
sub_items
[
0
],
sub_items
[
1
]);
}
return
dict
;
}
...
...
This diff is collapsed.
Click to expand it.
tests/basics/dict-from-iter.py
+
10
−
0
View file @
686afc5c
...
...
@@ -2,3 +2,13 @@ print(dict([(1, "foo")]))
d
=
dict
([(
"
foo
"
,
"
foo2
"
),
(
"
bar
"
,
"
baz
"
)])
print
(
sorted
(
d
.
keys
()))
print
(
sorted
(
d
.
values
()))
try
:
dict
(((
1
,),))
except
ValueError
:
print
(
"
ValueError
"
)
try
:
dict
(((
1
,
2
,
3
),))
except
ValueError
:
print
(
"
ValueError
"
)
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