This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Paul.Ianas
Recipients Paul.Ianas
Date 2014-10-21.07:23:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1413876192.76.0.889942882253.issue22683@psf.upfronthosting.co.za>
In-reply-to
Content
The precondition for all the bisect functions is implemented like this:

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)

Now, of course, if hi is given, and hi >= 2 * len(a), then we get an IndexError. In case hi < 0, we always get 0 as a result (even if the element is there).

I think it would be better to treat the hi in the precondition in the same way as the lo parameter: that means, raise a ValueError in case hi has an illegal value.

Disclaimer: of course, it makes no sense to give an illegal argument to that function; still, since lo is treated against illegal values, maybe it's better to do the same for hi.

At the same time, maybe moving the precondition code in a separate function (which raises a ValueError in case precondition is not met) makes more sense, for not repeating the same code in all bisect functions.

A small snippet which reproduces this:

from bisect import bisect_left

a = [1, 2, 3, 4]
idx = bisect_left(a, 2, 0, 10)  # 10 > 2 * 4
print(idx)
History
Date User Action Args
2014-10-21 07:23:12Paul.Ianassetrecipients: + Paul.Ianas
2014-10-21 07:23:12Paul.Ianassetmessageid: <1413876192.76.0.889942882253.issue22683@psf.upfronthosting.co.za>
2014-10-21 07:23:12Paul.Ianaslinkissue22683 messages
2014-10-21 07:23:11Paul.Ianascreate