Skip to content

Commit 1f8486f

Browse files
bpo-44558: Make the implementation consistency of operator.indexOf (GH-27012)
(cherry picked from commit 0930240) Co-authored-by: Dong-hee Na <[email protected]>
1 parent 22bcc07 commit 1f8486f

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

Lib/operator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def getitem(a, b):
173173
def indexOf(a, b):
174174
"Return the first index of b in a."
175175
for i, j in enumerate(a):
176-
if j == b:
176+
if j is b or j == b:
177177
return i
178178
else:
179179
raise ValueError('sequence.index(x): x not in sequence')

Lib/test/test_operator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ def test_indexOf(self):
188188
self.assertRaises(ZeroDivisionError, operator.indexOf, BadIterable(), 1)
189189
self.assertEqual(operator.indexOf([4, 3, 2, 1], 3), 1)
190190
self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
191+
nan = float("nan")
192+
self.assertEqual(operator.indexOf([nan, nan, 21], nan), 0)
193+
self.assertEqual(operator.indexOf([{}, 1, {}, 2], {}), 0)
191194

192195
def test_invert(self):
193196
operator = self.module
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make the implementation consistency of :func:`~operator.indexOf` between
2+
C and Python versions. Patch by Dong-hee Na.

0 commit comments

Comments
 (0)