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
be8fe5be
Commit
be8fe5be
authored
11 years ago
by
John R. Lenton
Browse files
Options
Downloads
Patches
Plain Diff
Added dict.setdefault
parent
f77dce8a
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
py/map.c
+1
-0
1 addition, 0 deletions
py/map.c
py/objdict.c
+26
-10
26 additions, 10 deletions
py/objdict.c
tests/basics/tests/dict_setdefault.py
+13
-0
13 additions, 0 deletions
tests/basics/tests/dict_setdefault.py
with
40 additions
and
10 deletions
py/map.c
+
1
−
0
View file @
be8fe5be
...
@@ -112,6 +112,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
...
@@ -112,6 +112,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
retval
->
key
=
elem
->
key
;
retval
->
key
=
elem
->
key
;
retval
->
value
=
elem
->
value
;
retval
->
value
=
elem
->
value
;
elem
->
key
=
NULL
;
elem
->
key
=
NULL
;
elem
->
value
=
NULL
;
return
retval
;
return
retval
;
}
}
return
elem
;
return
elem
;
...
...
This diff is collapsed.
Click to expand it.
py/objdict.c
+
26
−
10
View file @
be8fe5be
...
@@ -140,27 +140,30 @@ static mp_obj_t dict_copy(mp_obj_t self_in) {
...
@@ -140,27 +140,30 @@ static mp_obj_t dict_copy(mp_obj_t self_in) {
}
}
static
MP_DEFINE_CONST_FUN_OBJ_1
(
dict_copy_obj
,
dict_copy
);
static
MP_DEFINE_CONST_FUN_OBJ_1
(
dict_copy_obj
,
dict_copy
);
static
mp_obj_t
dict_get_helper
(
mp_map_t
*
self
,
mp_obj_t
key
,
mp_obj_t
deflt
,
bool
pop
)
{
static
mp_obj_t
dict_get_helper
(
mp_map_t
*
self
,
mp_obj_t
key
,
mp_obj_t
deflt
,
bool
pop
,
bool
set
)
{
mp_map_elem_t
*
elem
=
mp_map_lookup_helper
(
self
,
key
,
false
,
pop
);
mp_map_elem_t
*
elem
=
mp_map_lookup_helper
(
self
,
key
,
set
,
pop
);
if
(
elem
==
NULL
)
{
mp_obj_t
value
;
if
(
elem
==
NULL
||
elem
->
value
==
NULL
)
{
if
(
deflt
==
NULL
)
{
if
(
deflt
==
NULL
)
{
if
(
pop
)
{
if
(
pop
)
{
nlr_jump
(
mp_obj_new_exception_msg
(
MP_QSTR_KeyError
,
"<value>"
));
nlr_jump
(
mp_obj_new_exception_msg
(
MP_QSTR_KeyError
,
"<value>"
));
}
else
{
}
else
{
return
mp_const_none
;
value
=
mp_const_none
;
}
}
}
else
{
}
else
{
return
deflt
;
value
=
deflt
;
}
}
}
else
{
}
else
{
mp_obj_t
value
=
elem
->
value
;
value
=
elem
->
value
;
if
(
pop
)
{
if
(
pop
)
{
/* catch the leak (from mp_map_lookup_helper) */
/* catch the leak (from mp_map_lookup_helper) */
m_free
(
elem
,
2
*
sizeof
(
mp_obj_t
));
m_free
(
elem
,
2
*
sizeof
(
mp_obj_t
));
}
}
return
value
;
}
}
if
(
set
)
{
elem
->
value
=
value
;
}
return
value
;
}
}
static
mp_obj_t
dict_get
(
int
n_args
,
const
mp_obj_t
*
args
)
{
static
mp_obj_t
dict_get
(
int
n_args
,
const
mp_obj_t
*
args
)
{
...
@@ -170,7 +173,7 @@ static mp_obj_t dict_get(int n_args, const mp_obj_t *args) {
...
@@ -170,7 +173,7 @@ static mp_obj_t dict_get(int n_args, const mp_obj_t *args) {
return
dict_get_helper
(
&
((
mp_obj_dict_t
*
)
args
[
0
])
->
map
,
return
dict_get_helper
(
&
((
mp_obj_dict_t
*
)
args
[
0
])
->
map
,
args
[
1
],
args
[
1
],
n_args
==
3
?
args
[
2
]
:
NULL
,
n_args
==
3
?
args
[
2
]
:
NULL
,
false
);
false
,
false
);
}
}
static
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
dict_get_obj
,
2
,
3
,
dict_get
);
static
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
dict_get_obj
,
2
,
3
,
dict_get
);
...
@@ -181,11 +184,22 @@ static mp_obj_t dict_pop(int n_args, const mp_obj_t *args) {
...
@@ -181,11 +184,22 @@ static mp_obj_t dict_pop(int n_args, const mp_obj_t *args) {
return
dict_get_helper
(
&
((
mp_obj_dict_t
*
)
args
[
0
])
->
map
,
return
dict_get_helper
(
&
((
mp_obj_dict_t
*
)
args
[
0
])
->
map
,
args
[
1
],
args
[
1
],
n_args
==
3
?
args
[
2
]
:
NULL
,
n_args
==
3
?
args
[
2
]
:
NULL
,
true
);
true
,
false
);
}
}
static
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
dict_pop_obj
,
2
,
3
,
dict_pop
);
static
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
dict_pop_obj
,
2
,
3
,
dict_pop
);
static
mp_obj_t
dict_setdefault
(
int
n_args
,
const
mp_obj_t
*
args
)
{
assert
(
2
<=
n_args
&&
n_args
<=
3
);
assert
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
dict_type
));
return
dict_get_helper
(
&
((
mp_obj_dict_t
*
)
args
[
0
])
->
map
,
args
[
1
],
n_args
==
3
?
args
[
2
]
:
NULL
,
false
,
true
);
}
static
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
dict_setdefault_obj
,
2
,
3
,
dict_setdefault
);
static
mp_obj_t
dict_popitem
(
mp_obj_t
self_in
)
{
static
mp_obj_t
dict_popitem
(
mp_obj_t
self_in
)
{
assert
(
MP_OBJ_IS_TYPE
(
self_in
,
&
dict_type
));
assert
(
MP_OBJ_IS_TYPE
(
self_in
,
&
dict_type
));
...
@@ -199,6 +213,7 @@ static mp_obj_t dict_popitem(mp_obj_t self_in) {
...
@@ -199,6 +213,7 @@ static mp_obj_t dict_popitem(mp_obj_t self_in) {
self
->
map
.
used
--
;
self
->
map
.
used
--
;
mp_obj_t
items
[]
=
{
next
->
key
,
next
->
value
};
mp_obj_t
items
[]
=
{
next
->
key
,
next
->
value
};
next
->
key
=
NULL
;
next
->
key
=
NULL
;
next
->
value
=
NULL
;
mp_obj_t
tuple
=
mp_obj_new_tuple
(
2
,
items
);
mp_obj_t
tuple
=
mp_obj_new_tuple
(
2
,
items
);
return
tuple
;
return
tuple
;
...
@@ -222,6 +237,7 @@ const mp_obj_type_t dict_type = {
...
@@ -222,6 +237,7 @@ const mp_obj_type_t dict_type = {
{
"get"
,
&
dict_get_obj
},
{
"get"
,
&
dict_get_obj
},
{
"pop"
,
&
dict_pop_obj
},
{
"pop"
,
&
dict_pop_obj
},
{
"popitem"
,
&
dict_popitem_obj
},
{
"popitem"
,
&
dict_popitem_obj
},
{
"setdefault"
,
&
dict_setdefault_obj
},
{
NULL
,
NULL
},
// end-of-list sentinel
{
NULL
,
NULL
},
// end-of-list sentinel
},
},
};
};
...
...
This diff is collapsed.
Click to expand it.
tests/basics/tests/dict_setdefault.py
0 → 100644
+
13
−
0
View file @
be8fe5be
d
=
{}
print
(
d
.
setdefault
(
1
))
print
(
d
.
setdefault
(
1
))
print
(
d
.
setdefault
(
5
,
42
))
print
(
d
.
setdefault
(
5
,
1
))
print
(
d
[
1
])
print
(
d
[
5
])
d
.
pop
(
5
)
print
(
d
.
setdefault
(
5
,
1
))
print
(
d
[
1
])
print
(
d
[
5
])
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