Skip to content

Commit fdb6fc4

Browse files
authored
Merge pull request RustPython#2722 from corona10/operator
Lib: Update operator.py to follow CPython's implementation
2 parents fff68dc + c554689 commit fdb6fc4

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

Lib/operator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ def contains(a, b):
155155
return b in a
156156

157157
def countOf(a, b):
158-
"Return the number of times b occurs in a."
158+
"Return the number of items in a which are, or which equal, b."
159159
count = 0
160160
for i in a:
161-
if i == b:
161+
if i is b or i == b:
162162
count += 1
163163
return count
164164

@@ -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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ def test_countOf(self):
144144
self.assertRaises(TypeError, operator.countOf, None, None)
145145
self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 3), 1)
146146
self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 5), 0)
147+
# is but not ==
148+
nan = float("nan")
149+
self.assertEqual(operator.countOf([nan, nan, 21], nan), 2)
150+
# == but not is
151+
self.assertEqual(operator.countOf([{}, 1, {}, 2], {}), 2)
147152

148153
def test_delitem(self):
149154
operator = self.module
@@ -178,6 +183,9 @@ def test_indexOf(self):
178183
self.assertRaises(TypeError, operator.indexOf, None, None)
179184
self.assertEqual(operator.indexOf([4, 3, 2, 1], 3), 1)
180185
self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
186+
nan = float("nan")
187+
self.assertEqual(operator.indexOf([nan, nan, 21], nan), 0)
188+
self.assertEqual(operator.indexOf([{}, 1, {}, 2], {}), 0)
181189

182190
def test_invert(self):
183191
operator = self.module

0 commit comments

Comments
 (0)