fix(stl_bind): correct __delitem__ for negative-step slices and re-enable contiguous erase fast path - #6088
Conversation
e5c1c6d to
ee2b050
Compare
…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
Use static_cast instead of a C-style cast for the slice.compute() size argument, and convert the __delitem__ slice test to pytest.mark.parametrize over the slice cases. Assisted-by: ClaudeCode:claude-fable-5
ee2b050 to
68f02bd
Compare
The control flow handles all relevant boundaries: - slicelength == 0: excluded by the outer guard. - slicelength == 1: erases once, decrements to zero, and breaks without touching start. - Larger slices: updates start exactly when another erase remains. - slicelength cannot underflow because the loop exits when it reaches zero. - Mutating slicelength is harmless because it is not used afterward. - The potentially dangerous final start += step remains eliminated. It also removes the separate loop counter. The compiler would probably optimize the former i + 1, ++i mechanics away, but the new source expresses the real state more directly: "number of erasures remaining." The unconditional while (true) is safe because entry is strictly guarded by slicelength > 0, and the decrement guarantees eventual termination.
Background on the additional commitsI pushed three follow-up commits after another close review of the slice-deletion changes. Codex originally found a signed-overflow edge case in the forward-erasure loop. The loop updated Looking at that finding manually led to a more fundamental question: for a positive stride, why erase the selected elements from the front? If a slice selects indices This is simpler than compensating for forward erasure with The additional commits are:
|
🤖 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. — Revised: fix(stl_bind): correct __delitem__ for negative-step slices and re-enable contiguous erase fast path #6088 (comment)
&& 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
📚 Documentation preview 📚: https://pybind11--6088.org.readthedocs.build/