fix(stl_bind): correct __delitem__ for negative-step slices and re-enable contiguous erase fast path#6088
Draft
henryiii wants to merge 1 commit into
Draft
Conversation
…able contiguous erase fast path The slice __delitem__ binding advanced the erase index by step - 1 for all steps. That correction is only valid for positive steps, where erasing shifts later elements down by one. For negative steps the visited indices are strictly decreasing and erasing never shifts them, so the extra -1 deleted the wrong elements (e.g. del v[::-2] on [0,1,2,3] yielded [1,2] instead of [0,2]) and del v[::-1] walked off the front of the vector (v.begin() - 1, observed SIGBUS). Switch to the signed slice::compute overload so negative steps stay signed, advance by step for negative steps and step - 1 for positive ones, and drop the && false that had disabled the O(n) contiguous fast path since 2016. Assisted-by: ClaudeCode:claude-fable-5
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Bug
bind_vector's__delitem__(slice)instl_bind.hadvanced the eraseindex by
step - 1for every slice, but that correction is only validfor positive steps (where erasing an element shifts the later
elements down by one). For negative steps the visited indices are
strictly decreasing, so erasing never shifts them and the
- 1iswrong:
del v[::-2]on[0, 1, 2, 3]produced[1, 2]instead of thecorrect
[0, 2](silent data corruption).del v[::-1]ended up callingv.erase(v.begin() - 1), anout-of-bounds access (observed SIGBUS).
Separately, the contiguous fast path was guarded by
step == 1 && false— a debugging artifact from 2016 (25c03ce) — so a contiguous delete
like
del v[1:1000]did 999 individualerasecalls instead of onerange erase.
Fix
slice::computeoverload so negative steps stay signedinstead of arriving as wrapped
size_tvalues.stepfor negative steps andstep - 1for positivesteps.
&& false, restoring the O(n) contiguous range erase forstep == 1.Semantics now match CPython
del l[slice]exactly for forward,strided, negative, empty, and full slices.
Tests
Added
test_vector_delitem_sliceintests/test_stl_binders.py,looping over forward / strided / negative-step / empty / full slices for
vectors of several lengths and comparing against the equivalent Python
listdel.Verification
Built a scratch opaque
VectorIntmodule against this branch's headersand compared
del v[...]to Pythonlistsemantics across all the slicecases above (and more) for lengths 0–11 — all match, no out-of-bounds
access.
Part of #6084