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
a673b1fd
Commit
a673b1fd
authored
Dec 31, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix OrderedDict.setdefault() to work for subclasses that define __missing__().
parent
ed13853e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
1 deletion
+16
-1
Lib/collections.py
Lib/collections.py
+7
-1
Lib/test/test_collections.py
Lib/test/test_collections.py
+6
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/collections.py
View file @
a673b1fd
...
...
@@ -171,7 +171,6 @@ class OrderedDict(dict, MutableMapping):
size
+=
sizeof
(
self
.
__root
)
*
n
# proxy objects
return
size
setdefault
=
MutableMapping
.
setdefault
update
=
MutableMapping
.
update
pop
=
MutableMapping
.
pop
keys
=
MutableMapping
.
keys
...
...
@@ -179,6 +178,13 @@ class OrderedDict(dict, MutableMapping):
items
=
MutableMapping
.
items
__ne__
=
MutableMapping
.
__ne__
def
setdefault
(
self
,
key
,
default
=
None
):
'OD.setdefault(k[,d]) -> OD.get(k,d), also set OD[k]=d if k not in OD'
if
key
in
self
:
return
self
[
key
]
self
[
key
]
=
default
return
default
@
_recursive_repr
()
def
__repr__
(
self
):
'od.__repr__() <==> repr(od)'
...
...
Lib/test/test_collections.py
View file @
a673b1fd
...
...
@@ -976,6 +976,12 @@ class TestOrderedDict(unittest.TestCase):
# make sure 'x' is added to the end
self
.
assertEqual
(
list
(
od
.
items
())[
-
1
],
(
'x'
,
10
))
# make sure setdefault still works when __missing__ is defined
class
Missing
(
OrderedDict
):
def
__missing__
(
self
,
key
):
return
0
self
.
assertEqual
(
Missing
().
setdefault
(
5
,
9
),
9
)
def
test_reinsert
(
self
):
# Given insert a, insert b, delete a, re-insert a,
# verify that a is now later than b.
...
...
Misc/NEWS
View file @
a673b1fd
...
...
@@ -20,6 +20,9 @@ Core and Builtins
Library
-------
- Fix collections.OrderedDict.setdefault() so that it works in
subclasses that define __missing__().
- Issue 10786: unittest.TextTestRunner default stream no longer bound at
import time. `sys.stderr` now looked up at instantiation time. Fix contributed
by Mark Roddy.
...
...
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