python versions of bisect_right and bisect_left can fail guarantees
Open
@rhettinger is already working on this.
Since Oct 25, 2024.
3.13
3.14
extension-modules
stdlib
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
The documentation for bisect.bisect_right and bisect.bisect_left states in both cases that
The returned insertion point ip partitions the array a into two slices such that
all(elem <= x for elem in a[lo : ip]) is true for the left slice and
all(elem > x for elem in a[ip : hi]) is true for the right slice.
But in the python version of bisect, that guarantee can fail:
import sys
import _bisect as c_bisect
sys.modules['_bisect'] = None
import bisect as py_bisect
def check_guarantees(bisect_func, a, x, lo, hi):
ip = bisect_func(a, x, lo, hi)
check_pass = True
if not all(elem <= x for elem in a[lo : ip]):
print("\tFAIL. Elements of",a[lo:ip]," are not <=",x)
check_pass = False
if not all(elem > x for elem in a[ip : hi]):
print("\tFAIL. Elements of",a[ip:hi]," are not >",x)
check_pass = False
if check_pass: print("\tOk")
#print(c_bisect.bisect_right) # <built-in function bisect_right>
#print(py_bisect.bisect_right) # <function bisect_right at 0x0000023164AE7C40>
a = [0,1,2,3,4]
x = 8
lo = 3
hi = -1
print("bisect_left from C")
check_guarantees(c_bisect.bisect_left, a, x, lo, hi)
print("bisect_left from python")
check_guarantees(py_bisect.bisect_left, a, x, lo, hi)
print("bisect_right from C")
check_guarantees(c_bisect.bisect_right, a, x, lo, hi)
print("bisect_right from python")
check_guarantees(py_bisect.bisect_right, a, x, lo, hi)
Attempt this online
Outputs :
bisect_left from C
Ok
bisect_left from python
FAIL. Elements of [3] are not > 8
bisect_right from C
Ok
bisect_right from python
FAIL. Elements of [3] are not > 8
I think there is an extra check in the C version for hi < 0.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
- gh-125915
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.