summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXiang Zhang <angwerzx@126.com>2017-03-08 03:04:24 (GMT)
committerGitHub <noreply@github.com>2017-03-08 03:04:24 (GMT)
commitd5d3249e8a37936d32266fa06ac20017307a1f70 (patch)
tree93cc04b3e6be8772aa9afec42ed4765505f5ebd8
parent8f6b344d368c15c3fe56c65c2f2776e7766fef55 (diff)
downloadcpython-d5d3249e8a37936d32266fa06ac20017307a1f70.zip
cpython-d5d3249e8a37936d32266fa06ac20017307a1f70.tar.gz
cpython-d5d3249e8a37936d32266fa06ac20017307a1f70.tar.bz2
bpo-26915: Test identity first in membership operation in index() and count() methods of collections.abc.Sequence (GH-503)
-rw-r--r--Lib/_collections_abc.py5
-rw-r--r--Lib/test/test_collections.py17
-rw-r--r--Misc/NEWS3
3 files changed, 19 insertions, 6 deletions
diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py
index b172f3f..005d884 100644
--- a/Lib/_collections_abc.py
+++ b/Lib/_collections_abc.py
@@ -908,7 +908,8 @@ class Sequence(Reversible, Collection):
i = start
while stop is None or i < stop:
try:
- if self[i] == value:
+ v = self[i]
+ if v is value or v == value:
return i
except IndexError:
break
@@ -917,7 +918,7 @@ class Sequence(Reversible, Collection):
def count(self, value):
'S.count(value) -> integer -- return number of occurrences of value'
- return sum(1 for v in self if v == value)
+ return sum(1 for v in self if v is value or v == value)
Sequence.register(tuple)
Sequence.register(str)
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 76c7139..87c6978 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -1309,20 +1309,29 @@ class TestCollectionABCs(ABCTestCase):
class CustomEqualObject:
def __eq__(self, other):
return False
- class CustomSequence(list):
- def __contains__(self, value):
- return Sequence.__contains__(self, value)
+ class CustomSequence(Sequence):
+ def __init__(self, seq):
+ self._seq = seq
+ def __getitem__(self, index):
+ return self._seq[index]
+ def __len__(self):
+ return len(self._seq)
nan = float('nan')
obj = CustomEqualObject()
+ seq = CustomSequence([nan, obj, nan])
containers = [
- CustomSequence([nan, obj]),
+ seq,
ItemsView({1: nan, 2: obj}),
ValuesView({1: nan, 2: obj})
]
for container in containers:
for elem in container:
self.assertIn(elem, container)
+ self.assertEqual(seq.index(nan), 0)
+ self.assertEqual(seq.index(obj), 1)
+ self.assertEqual(seq.count(nan), 2)
+ self.assertEqual(seq.count(obj), 1)
def assertSameSet(self, s1, s2):
# coerce both to a real set then check equality
diff --git a/Misc/NEWS b/Misc/NEWS
index 6f998e8..b5437e5 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -265,6 +265,9 @@ Extension Modules
Library
-------
+- bpo-26915: index() and count() methods of collections.abc.Sequence now
+ check identity before checking equality when do comparisons.
+
- bpo-28682: Added support for bytes paths in os.fwalk().
- bpo-29623: Allow use of path-like object as a single argument in