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
0e708a1b
Commit
0e708a1b
authored
Aug 22, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #9214: Fix set operations on KeysView and ItemsView.
parent
d5c190d2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
1 deletion
+37
-1
Lib/_abcoll.py
Lib/_abcoll.py
+8
-0
Lib/test/test_collections.py
Lib/test/test_collections.py
+26
-1
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/_abcoll.py
View file @
0e708a1b
...
...
@@ -393,6 +393,10 @@ class MappingView(Sized):
class
KeysView
(
MappingView
,
Set
):
@
classmethod
def
_from_iterable
(
self
,
it
):
return
set
(
it
)
def
__contains__
(
self
,
key
):
return
key
in
self
.
_mapping
...
...
@@ -405,6 +409,10 @@ KeysView.register(dict_keys)
class
ItemsView
(
MappingView
,
Set
):
@
classmethod
def
_from_iterable
(
self
,
it
):
return
set
(
it
)
def
__contains__
(
self
,
item
):
key
,
value
=
item
try
:
...
...
Lib/test/test_collections.py
View file @
0e708a1b
...
...
@@ -13,7 +13,7 @@ import re
from
collections
import
Hashable
,
Iterable
,
Iterator
from
collections
import
Sized
,
Container
,
Callable
from
collections
import
Set
,
MutableSet
from
collections
import
Mapping
,
MutableMapping
from
collections
import
Mapping
,
MutableMapping
,
KeysView
,
ItemsView
,
UserDict
from
collections
import
Sequence
,
MutableSequence
from
collections
import
ByteString
...
...
@@ -516,6 +516,31 @@ class TestCollectionABCs(ABCTestCase):
self
.
validate_abstract_methods
(
MutableMapping
,
'__contains__'
,
'__iter__'
,
'__len__'
,
'__getitem__'
,
'__setitem__'
,
'__delitem__'
)
def
test_MutableMapping_subclass
(
self
):
# Test issue 9214
mymap
=
UserDict
()
mymap
[
'red'
]
=
5
self
.
assert_
(
isinstance
(
mymap
.
keys
(),
Set
))
self
.
assert_
(
isinstance
(
mymap
.
keys
(),
KeysView
))
self
.
assert_
(
isinstance
(
mymap
.
items
(),
Set
))
self
.
assert_
(
isinstance
(
mymap
.
items
(),
ItemsView
))
mymap
=
UserDict
()
mymap
[
'red'
]
=
5
z
=
mymap
.
keys
()
|
{
'orange'
}
self
.
assertEqual
(
type
(
z
),
set
)
list
(
z
)
mymap
[
'blue'
]
=
7
# Shouldn't affect 'z'
self
.
assertEqual
(
sorted
(
z
),
[
'orange'
,
'red'
])
mymap
=
UserDict
()
mymap
[
'red'
]
=
5
z
=
mymap
.
items
()
|
{(
'orange'
,
3
)}
self
.
assertEqual
(
type
(
z
),
set
)
list
(
z
)
mymap
[
'blue'
]
=
7
# Shouldn't affect 'z'
self
.
assertEqual
(
sorted
(
z
),
[(
'orange'
,
3
),
(
'red'
,
5
)])
def
test_Sequence
(
self
):
for
sample
in
[
tuple
,
list
,
bytes
,
str
]:
self
.
assertTrue
(
isinstance
(
sample
(),
Sequence
))
...
...
Misc/NEWS
View file @
0e708a1b
...
...
@@ -95,6 +95,9 @@ C-API
Library
-------
- Issue #9214: Set operations on KeysView or ItemsView in the collections
module now correctly return a set. (Patch by Eli Bendersky.)
- Issue #9617: Signals received during a low-level write operation aren't
ignored by the buffered IO layer anymore.
...
...
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