Skip to content

gh-96538: Fix refleak in _bisectmodule.c #96619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Lib/test/test_bisect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Modules/_bisectmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down