From cc0694cf9653642c79ff6114560e66e7651f1a5b Mon Sep 17 00:00:00 2001 From: Rupert Tombs Date: Sat, 3 Jul 2021 18:27:23 +0100 Subject: [PATCH 1/6] bpo-44558: Match countOf `is`/`==` treatment to c Python impl and docstring treated these comparisons differently. Update both docstrings to reflect the behavior. This preserves the existing behaviour of the c implementation. --- Lib/operator.py | 4 ++-- Lib/test/test_operator.py | 5 +++++ Modules/_operator.c | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Lib/operator.py b/Lib/operator.py index fb58851fa6ef673..277a4915d8d6ac0 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 1ecae85f62f2c9a..2187ec25cdc1ef8 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/Modules/_operator.c b/Modules/_operator.c index d5e092e2f82f00c..fdaf164e784cb6f 100644 --- a/Modules/_operator.c +++ b/Modules/_operator.c @@ -507,7 +507,7 @@ _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 From 505bb1c27a5b14eb8b63b8e5fbd75971b2f1964c Mon Sep 17 00:00:00 2001 From: Rupert Tombs Date: Sat, 3 Jul 2021 19:11:26 +0100 Subject: [PATCH 2/6] Update with clinic --- Modules/_operator.c | 2 +- Modules/clinic/_operator.c.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_operator.c b/Modules/_operator.c index fdaf164e784cb6f..f051513fc793a01 100644 --- a/Modules/_operator.c +++ b/Modules/_operator.c @@ -512,7 +512,7 @@ Return the number of items in a which are, or which equal, b. 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 34b6fdadfb7309f..bda2eba6d12a214 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]*/ From d9e673f29a19c3697d5ef27e0deaaa214052a451 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 3 Jul 2021 18:25:17 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst 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 000000000000000..594bc03d3d8ca09 --- /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:countOf to the behaviour +of its c implementation. \ No newline at end of file From 3debd3f95c4b5cc679d86965b9fec880c3786104 Mon Sep 17 00:00:00 2001 From: Rupert Tombs Date: Sat, 3 Jul 2021 19:45:49 +0100 Subject: [PATCH 4/6] Remove blurb_it trailing whitespace --- .../Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 594bc03d3d8ca09..478e27b1c872ae3 100644 --- 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 @@ -1,2 +1,2 @@ -Match the docstring and python implementation of :func:countOf to the behaviour -of its c implementation. \ No newline at end of file +Match the docstring and python implementation of :func:countOf to the behaviour +of its c implementation. From 44ccf3ab1a5993b19d4732722c277f0622ed93bd Mon Sep 17 00:00:00 2001 From: Rupert Tombs Date: Sun, 4 Jul 2021 15:38:11 +0100 Subject: [PATCH 5/6] Update Misc/NEWS.d/next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst Co-authored-by: Dong-hee Na --- .../Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 478e27b1c872ae3..359f768ed3bd342 100644 --- 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 @@ -1,2 +1,3 @@ -Match the docstring and python implementation of :func:countOf to the behaviour +Match the docstring and python implementation of :func:`~operator.countOf` to the behavior + of its c implementation. From 0015da8252b6657420a139bbbc77409e1406aa05 Mon Sep 17 00:00:00 2001 From: Rupert Tombs Date: Sun, 4 Jul 2021 17:42:03 +0100 Subject: [PATCH 6/6] Update Misc/NEWS.d/next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst Co-authored-by: Dong-hee Na --- .../next/Documentation/2021-07-03-18-25-17.bpo-44558.0pTknl.rst | 1 - 1 file changed, 1 deletion(-) 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 index 359f768ed3bd342..a12a49ccd80cc22 100644 --- 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 @@ -1,3 +1,2 @@ Match the docstring and python implementation of :func:`~operator.countOf` to the behavior - of its c implementation.