Skip to content
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

bisect index out of bounds issue #66873

Closed
PaulIanas mannequin opened this issue Oct 21, 2014 · 4 comments
Closed

bisect index out of bounds issue #66873

PaulIanas mannequin opened this issue Oct 21, 2014 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@PaulIanas
Copy link
Mannequin

PaulIanas mannequin commented Oct 21, 2014

BPO 22683
Nosy @tim-one, @rhettinger

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/rhettinger'
closed_at = <Date 2014-10-31.21:22:48.978>
created_at = <Date 2014-10-21.07:23:12.716>
labels = ['type-feature', 'library']
title = 'bisect index out of bounds issue'
updated_at = <Date 2014-10-31.21:22:48.977>
user = 'https://bugs.python.org/PaulIanas'

bugs.python.org fields:

activity = <Date 2014-10-31.21:22:48.977>
actor = 'rhettinger'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2014-10-31.21:22:48.978>
closer = 'rhettinger'
components = ['Library (Lib)']
creation = <Date 2014-10-21.07:23:12.716>
creator = 'Paul.Ianas'
dependencies = []
files = []
hgrepos = []
issue_num = 22683
keywords = []
message_count = 4.0
messages = ['229750', '229855', '229857', '230390']
nosy_count = 3.0
nosy_names = ['tim.peters', 'rhettinger', 'Paul.Ianas']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue22683'
versions = ['Python 3.5']

@PaulIanas
Copy link
Mannequin Author

PaulIanas mannequin commented Oct 21, 2014

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)

@PaulIanas PaulIanas mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Oct 21, 2014
@rhettinger rhettinger self-assigned this Oct 23, 2014
@rhettinger
Copy link
Contributor

These functions are very old and the API is unlikely to change, particularly if the goal is to change one kind of exception to another (making bad inputs fail in a different way than they do now). As far as I can tell, the current arrangement has never been a actual problem in practice.

@PaulIanas
Copy link
Mannequin Author

PaulIanas mannequin commented Oct 23, 2014

Sure, it is your call. As said, this is rather an enhancement.

Still, if I were to decide, I would chose:

  1. not to break the API <=> raise IndexError instead of ValueError in case hi is invalid.
  2. to protect against illegal values: as said, if hi < 0 bisect_* always returns 0 (whatever the searched value).
  3. I would implement a single _range_check(_len, lo, hi) method to do this logic (DRY).

That being said, from my point of view this ticket can be closed.

@rhettinger
Copy link
Contributor

treat the hi in the precondition in the same way as the lo parameter

Originally, there was no special test for lo. The test was added only because negative lo value failed in an unfortunate way (with an infinite loop). No change to hi was made because (it succeeded in some cases and existing code might be relying on that or it failed in clear-cut way by raising an IndexError).

That being said, from my point of view this ticket can be closed.

I concur. When there isn't a clear case for change, it is usually better to favor stability over potential disruption.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant