Skip to content

feat: bisect, finding by halving where an item belongs - #269

Merged
jonruttan merged 1 commit into
mainfrom
feat/stdlib-bisect
Nov 28, 2026
Merged

jonruttan merged 1 commit into
mainfrom
feat/stdlib-bisect

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

What

Adds bisect to the standard library as stdlib/bisect.py. It has bisect_left, bisect_right, insort_left and insort_right, with bisect and insort as other names for the right-hand pair. Each takes lo, hi and key. A README row is added.

The code is x-python's own (MIT-0), not CPython's. Items are compared only with <. Arguments are handled as in CPython's C module:

  • lo must be a non-negative index.
  • hi is an index or None, and hi == -1 also means the whole sequence.
  • key is applied to the list's items, and insort applies it to x as well.

The checks run in the C module's order:

  1. Convert lo and hi.
  2. Apply key(x), for insort only.
  3. Check lo < 0.
  4. Take len(a), only when hi is left open.
  5. Check that a can be indexed.
  6. Search.
  7. Call a.insert, for insort only.

So a bad call fails with the same error as in CPython, for example 'int' object does not support indexing or dict is not a sequence.

Checking

  • Fuzz: a fuzzer compared this module with CPython 3.14's bisect under CPython. It covered random sorted lists with ties, positional and keyword lo/hi (ints, None, bools, __index__ objects and floats), key, and bad targets (None, int, tuple, str, dict, a dict subclass and range). Seeds 1–6 ran 5,000 rounds each with 0 differences.
  • D14-bisect: 4 cases, with expected output recorded from CPython 3.14. They pass on x-python.
  • Full suite: 1284 tests, 0 failed.
  • Conformance: 644 tests, 11 failed, all from main's known 12. complex1_micropython now passes, likely because of changes merged into main since that list was taken. basics-builtin-10 gave no totals; it hits the 6 GB guard on main too.

No runtime changes were needed.

Gaps

  • Calling with the wrong number of arguments gives Python's message for a def (takes from 2 to 4 positional arguments), not the C module's (takes at most 4 positional arguments).

🤖 Generated with Claude Code

bisect_left, bisect_right, insort_left and insort_right, with bisect and
insort as the right-hand pair's other names, written in Python under
stdlib/bisect.py.  lo, hi and key are taken as CPython's C module takes
them, and its checks run in the same order, so a bad call fails with the
same error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonruttan added a commit that referenced this pull request Nov 27, 2026
D14 is taken by bisect's spec in #269.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonruttan
jonruttan merged commit 2a8f499 into main Nov 28, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant