]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-44558: Match countOf `is`/`==` treatment to c (GH-27007). (GH-27055)
authorDong-hee Na <donghee.na@python.org>
Wed, 7 Jul 2021 14:55:22 +0000 (23:55 +0900)
committerGitHub <noreply@github.com>
Wed, 7 Jul 2021 14:55:22 +0000 (23:55 +0900)
Lib/operator.py
Lib/test/test_operator.py
Misc/NEWS.d/next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst [new file with mode: 0644]
Modules/_operator.c
Modules/clinic/_operator.c.h

index 6782703476edee4d3b0605efc3e47db834d5f6c9..241fdbb679e7c136808336539cd97a458543662d 100644 (file)
@@ -155,10 +155,10 @@ def contains(a, b):
     return b in a
 
 def countOf(a, b):
-    "Return the number of times b occurs in a."
+    "Return the number of items in a which are, or which equal, b."
     count = 0
     for i in a:
-        if i == b:
+        if i is b or i == b:
             count += 1
     return count
 
index 3093a7d8f7ec33a4b7c50b580c897486b8ffaca1..82a1ddc29968b868d41976bfb4db8488db724f98 100644 (file)
@@ -149,6 +149,11 @@ class OperatorTestCase:
         self.assertRaises(ZeroDivisionError, operator.countOf, BadIterable(), 1)
         self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 3), 1)
         self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 5), 0)
+        # is but not ==
+        nan = float("nan")
+        self.assertEqual(operator.countOf([nan, nan, 21], nan), 2)
+        # == but not is
+        self.assertEqual(operator.countOf([{}, 1, {}, 2], {}), 2)
 
     def test_delitem(self):
         operator = self.module
diff --git a/Misc/NEWS.d/next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst b/Misc/NEWS.d/next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst
new file mode 100644 (file)
index 0000000..a12a49c
--- /dev/null
@@ -0,0 +1,2 @@
+Match the docstring and python implementation of :func:`~operator.countOf` to the behavior\r
+of its c implementation.\r
index 6f8f68f4599e4f2d69665aa3294c7da528926386..0136e380d2abe153527e780353290ae4a0739de5 100644 (file)
@@ -494,12 +494,12 @@ _operator_indexOf_impl(PyObject *module, PyObject *a, PyObject *b)
 /*[clinic input]
 _operator.countOf = _operator.indexOf
 
-Return the number of times b occurs in a.
+Return the number of items in a which are, or which equal, b.
 [clinic start generated code]*/
 
 static Py_ssize_t
 _operator_countOf_impl(PyObject *module, PyObject *a, PyObject *b)
-/*[clinic end generated code: output=9e1623197daf3382 input=0c3a2656add252db]*/
+/*[clinic end generated code: output=9e1623197daf3382 input=93ea57f170f3f0bb]*/
 {
     return PySequence_Count(a, b);
 }
index f9e353d86b4961d8b5990c3e95162ac88a5c0045..0e39f3b77adfdc139e1de5b4c9c24f185f954a61 100644 (file)
@@ -957,7 +957,7 @@ PyDoc_STRVAR(_operator_countOf__doc__,
 "countOf($module, a, b, /)\n"
 "--\n"
 "\n"
-"Return the number of times b occurs in a.");
+"Return the number of items in a which are, or which equal, b.");
 
 #define _OPERATOR_COUNTOF_METHODDEF    \
     {"countOf", (PyCFunction)(void(*)(void))_operator_countOf, METH_FASTCALL, _operator_countOf__doc__},
@@ -1491,4 +1491,4 @@ _operator__compare_digest(PyObject *module, PyObject *const *args, Py_ssize_t na
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=e7ed71a8c475a901 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=ed2bc172e42320d8 input=a9049054013a1b77]*/