mirror of
https://github.com/python/cpython.git
synced 2025-10-03 21:55:41 +00:00
bpo-26915: Test identity first in index() and count() of collections.abc.Sequence (GH-553)
This commit is contained in:
parent
23b26c4e20
commit
78ad039bcf
3 changed files with 19 additions and 6 deletions
|
@ -908,7 +908,8 @@ class Sequence(Reversible, Collection):
|
||||||
i = start
|
i = start
|
||||||
while stop is None or i < stop:
|
while stop is None or i < stop:
|
||||||
try:
|
try:
|
||||||
if self[i] == value:
|
v = self[i]
|
||||||
|
if v is value or v == value:
|
||||||
return i
|
return i
|
||||||
except IndexError:
|
except IndexError:
|
||||||
break
|
break
|
||||||
|
@ -917,7 +918,7 @@ class Sequence(Reversible, Collection):
|
||||||
|
|
||||||
def count(self, value):
|
def count(self, value):
|
||||||
'S.count(value) -> integer -- return number of occurrences of 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(tuple)
|
||||||
Sequence.register(str)
|
Sequence.register(str)
|
||||||
|
|
|
@ -1310,20 +1310,29 @@ class TestCollectionABCs(ABCTestCase):
|
||||||
class CustomEqualObject:
|
class CustomEqualObject:
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return False
|
return False
|
||||||
class CustomSequence(list):
|
class CustomSequence(Sequence):
|
||||||
def __contains__(self, value):
|
def __init__(self, seq):
|
||||||
return Sequence.__contains__(self, value)
|
self._seq = seq
|
||||||
|
def __getitem__(self, index):
|
||||||
|
return self._seq[index]
|
||||||
|
def __len__(self):
|
||||||
|
return len(self._seq)
|
||||||
|
|
||||||
nan = float('nan')
|
nan = float('nan')
|
||||||
obj = CustomEqualObject()
|
obj = CustomEqualObject()
|
||||||
|
seq = CustomSequence([nan, obj, nan])
|
||||||
containers = [
|
containers = [
|
||||||
CustomSequence([nan, obj]),
|
seq,
|
||||||
ItemsView({1: nan, 2: obj}),
|
ItemsView({1: nan, 2: obj}),
|
||||||
ValuesView({1: nan, 2: obj})
|
ValuesView({1: nan, 2: obj})
|
||||||
]
|
]
|
||||||
for container in containers:
|
for container in containers:
|
||||||
for elem in container:
|
for elem in container:
|
||||||
self.assertIn(elem, 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):
|
def assertSameSet(self, s1, s2):
|
||||||
# coerce both to a real set then check equality
|
# coerce both to a real set then check equality
|
||||||
|
|
|
@ -16,6 +16,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- bpo-26915: index() and count() methods of collections.abc.Sequence now
|
||||||
|
check identity before checking equality when do comparisons.
|
||||||
|
|
||||||
- bpo-29615: SimpleXMLRPCDispatcher no longer chains KeyError (or any other
|
- bpo-29615: SimpleXMLRPCDispatcher no longer chains KeyError (or any other
|
||||||
exception) to exception(s) raised in the dispatched methods.
|
exception) to exception(s) raised in the dispatched methods.
|
||||||
Patch by Petr Motejlek.
|
Patch by Petr Motejlek.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue