[Python-checkins] bpo-44558: Match countOf `is`/`==` treatment to c (GH-27007)

miss-islington webhook-mailer at python.org
Wed Jul 7 09:50:51 EDT 2021


https://github.com/python/cpython/commit/9f431dd0a59c9ac2de46772a4dbd7db2199ef26a
commit: 9f431dd0a59c9ac2de46772a4dbd7db2199ef26a
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-07-07T06:50:41-07:00
summary:

bpo-44558: Match countOf `is`/`==` treatment to c (GH-27007)

(cherry picked from commit 6bd3ecfc272b122b55a6adec50dd7a7c868f262f)

Co-authored-by: Rupert Tombs <rupert.tombs at gmail.com>

files:
A Misc/NEWS.d/next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst
M Lib/operator.py
M Lib/test/test_operator.py
M Modules/_operator.c
M Modules/clinic/_operator.c.h

diff --git a/Lib/operator.py b/Lib/operator.py
index 6782703476edee..241fdbb679e7c1 100644
--- a/Lib/operator.py
+++ b/Lib/operator.py
@@ -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
 
diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py
index d50306b7f1ecb8..b9b8f155826708 100644
--- a/Lib/test/test_operator.py
+++ b/Lib/test/test_operator.py
@@ -153,6 +153,11 @@ def test_countOf(self):
         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
index 00000000000000..a12a49ccd80cc2
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst
@@ -0,0 +1,2 @@
+Match the docstring and python implementation of :func:`~operator.countOf` to the behavior
+of its c implementation.
diff --git a/Modules/_operator.c b/Modules/_operator.c
index d5e092e2f82f00..f051513fc793a0 100644
--- a/Modules/_operator.c
+++ b/Modules/_operator.c
@@ -507,12 +507,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);
 }
diff --git a/Modules/clinic/_operator.c.h b/Modules/clinic/_operator.c.h
index 34b6fdadfb7309..bda2eba6d12a21 100644
--- a/Modules/clinic/_operator.c.h
+++ b/Modules/clinic/_operator.c.h
@@ -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__},
@@ -1486,4 +1486,4 @@ _operator__compare_digest(PyObject *module, PyObject *const *args, Py_ssize_t na
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=eae5d08f971a65fd input=a9049054013a1b77]*/
+/*[clinic end generated code: output=16749e11fda51785 input=a9049054013a1b77]*/



More information about the Python-checkins mailing list