Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions Lib/test/test_bisect.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ def test_negative_lo(self):
self.assertRaises(ValueError, mod.insort_left, [1, 2, 3], 5, -1, 3)
self.assertRaises(ValueError, mod.insort_right, [1, 2, 3], 5, -1, 3)

def test_negative_hi(self):
# Issue 125889
def assertNoRiases(f, *arg, **kwargs):
try:
f(*arg, **kwargs)
except Exception:
self.fail('Unable to work negative hi argument')
mod = self.module
# It can work negative values
assertNoRiases(mod.bisect_left, [1, 2, 3], 5, 3, -2)
assertNoRiases(mod.bisect_right, [1, 2, 3], 5, 3, -2)
assertNoRiases(mod.insort_left, [1, 2, 3], 5, 3, -2)
assertNoRiases(mod.insort_right, [1, 2, 3], 5, 3, -2)

def test_large_range(self):
# Issue 13496
mod = self.module
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Modify the check of the ``hi`` argument in ``internal_bisect_left`` and ``internal_bisect_right``
so that when ``hi`` is negative, it's calculated based on the list length.
4 changes: 2 additions & 2 deletions Modules/_bisectmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t
PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
return -1;
}
if (hi == -1) {
if (hi < 0) {
hi = PySequence_Size(list);
if (hi < 0)
return -1;
Expand Down Expand Up @@ -245,7 +245,7 @@ internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t h
PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
return -1;
}
if (hi == -1) {
if (hi < 0) {
hi = PySequence_Size(list);
if (hi < 0)
return -1;
Expand Down
Loading