Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
33f29c19
Commit
33f29c19
authored
Aug 08, 2000
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Barry's patch to implement the new setdefault() method.
parent
53b75023
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
0 deletions
+40
-0
Lib/UserDict.py
Lib/UserDict.py
+4
-0
Objects/dictobject.c
Objects/dictobject.c
+36
-0
No files found.
Lib/UserDict.py
View file @
33f29c19
...
...
@@ -34,3 +34,7 @@ class UserDict:
self
.
data
[
k
]
=
v
def
get
(
self
,
key
,
failobj
=
None
):
return
self
.
data
.
get
(
key
,
failobj
)
def
setdefault
(
self
,
key
,
failobj
=
None
):
if
not
self
.
data
.
has_key
(
key
):
self
.
data
[
key
]
=
failobj
return
self
.
data
[
key
]
Objects/dictobject.c
View file @
33f29c19
...
...
@@ -949,6 +949,41 @@ dict_get(register dictobject *mp, PyObject *args)
}
static
PyObject
*
dict_setdefault
(
register
dictobject
*
mp
,
PyObject
*
args
)
{
PyObject
*
key
;
PyObject
*
failobj
=
Py_None
;
PyObject
*
val
=
NULL
;
long
hash
;
if
(
!
PyArg_ParseTuple
(
args
,
"O|O:setdefault"
,
&
key
,
&
failobj
))
return
NULL
;
if
(
mp
->
ma_table
==
NULL
)
goto
finally
;
#ifdef CACHE_HASH
if
(
!
PyString_Check
(
key
)
||
(
hash
=
((
PyStringObject
*
)
key
)
->
ob_shash
)
==
-
1
)
#endif
{
hash
=
PyObject_Hash
(
key
);
if
(
hash
==
-
1
)
return
NULL
;
}
val
=
lookdict
(
mp
,
key
,
hash
)
->
me_value
;
finally:
if
(
val
==
NULL
)
{
val
=
failobj
;
if
(
PyDict_SetItem
((
PyObject
*
)
mp
,
key
,
failobj
))
val
=
NULL
;
}
Py_XINCREF
(
val
);
return
val
;
}
static
PyObject
*
dict_clear
(
register
dictobject
*
mp
,
PyObject
*
args
)
{
...
...
@@ -993,6 +1028,7 @@ static PyMethodDef mapp_methods[] = {
{
"clear"
,
(
PyCFunction
)
dict_clear
},
{
"copy"
,
(
PyCFunction
)
dict_copy
},
{
"get"
,
(
PyCFunction
)
dict_get
,
METH_VARARGS
},
{
"setdefault"
,
(
PyCFunction
)
dict_setdefault
,
METH_VARARGS
},
{
NULL
,
NULL
}
/* sentinel */
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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