python versions of bisect_right and bisect_left can fail guarantees
Ouverte
@rhettinger y travaille déjà.
Depuis le 25/10/2024.
3.13
3.14
extension-modules
stdlib
type-bug
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 35.9k
- Métriques de merge des PR
- Métriques de PR en attente
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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Évaluation
Cette issue n'a pas encore été évaluée.