[Python-checkins] gh-96538: Fix refleak in _bisectmodule.c (gh-96619)

sweeneyde webhook-mailer at python.org
Tue Sep 6 19:37:27 EDT 2022


https://github.com/python/cpython/commit/56d9cf7fc87d2cd7b8231c44f1d710ee77fbb998
commit: 56d9cf7fc87d2cd7b8231c44f1d710ee77fbb998
branch: main
author: Dennis Sweeney <36520290+sweeneyde at users.noreply.github.com>
committer: sweeneyde <36520290+sweeneyde at users.noreply.github.com>
date: 2022-09-06T19:37:18-04:00
summary:

gh-96538: Fix refleak in _bisectmodule.c (gh-96619)

files:
M Lib/test/test_bisect.py
M Modules/_bisectmodule.c

diff --git a/Lib/test/test_bisect.py b/Lib/test/test_bisect.py
index ba108221ebf..97204d4cad3 100644
--- a/Lib/test/test_bisect.py
+++ b/Lib/test/test_bisect.py
@@ -263,6 +263,34 @@ def test_insort_keynotNone(self):
         for f in (self.module.insort_left, self.module.insort_right):
             self.assertRaises(TypeError, f, x, y, key = "b")
 
+    def test_lt_returns_non_bool(self):
+        class A:
+            def __init__(self, val):
+                self.val = val
+            def __lt__(self, other):
+                return "nonempty" if self.val < other.val else ""
+
+        data = [A(i) for i in range(100)]
+        i1 = self.module.bisect_left(data, A(33))
+        i2 = self.module.bisect_right(data, A(33))
+        self.assertEqual(i1, 33)
+        self.assertEqual(i2, 34)
+
+    def test_lt_returns_notimplemented(self):
+        class A:
+            def __init__(self, val):
+                self.val = val
+            def __lt__(self, other):
+                return NotImplemented
+            def __gt__(self, other):
+                return self.val > other.val
+
+        data = [A(i) for i in range(100)]
+        i1 = self.module.bisect_left(data, A(40))
+        i2 = self.module.bisect_right(data, A(40))
+        self.assertEqual(i1, 40)
+        self.assertEqual(i2, 41)
+
 class TestBisectPython(TestBisect, unittest.TestCase):
     module = py_bisect
 
diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c
index 09f8e5a6f07..9ceb3ae46fe 100644
--- a/Modules/_bisectmodule.c
+++ b/Modules/_bisectmodule.c
@@ -120,6 +120,7 @@ internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t
             }
             else {
                 res = PyObject_IsTrue(res_obj);
+                Py_DECREF(res_obj);
             }
         }
         else {
@@ -299,6 +300,7 @@ internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t h
             }
             else {
                 res = PyObject_IsTrue(res_obj);
+                Py_DECREF(res_obj);
             }
         }
         else {



More information about the Python-checkins mailing list