swap: fix list element order on revert#197
Open
gaoflow wants to merge 1 commit into
Open
Conversation
* Reverses the changes produced by swap's remove handler so that reverting a diff which removed three or more list elements restores their original order. diff emits list removals in descending index order, but revert replays them as inserts (which need ascending order); the add handler already reverses its changes, the remove handler did not. Signed-off-by: Vincent Gao <gaobing1230@gmail.com>
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.
Summary
revert()restores list elements in the wrong order when three or more elements are removed from a list, silently corrupting data and breaking the documentedrevert(diff(a, b), b) == around-trip:It only manifests with ≥3 removed elements (with ≤2 the order happens to coincide), which is why the existing
test_revert(2 elements) doesn't catch it.Cause
diffemits list removals in descending index order so thatpatch'sdel dest[key]stays valid as the list shrinks.swapturns thoseremoveentries intoaddentries, whichrevert/patchreplay withdest.insert(key, value)— but inserting at descending indices into a growing list scrambles the order; insert-replay needs ascending order.swap'saddhandler already reverses its changes (list(reversed(changes))); theremovehandler omitted the symmetric reversal. That asymmetry is the bug.Fix
Make
swap'sremovehandler symmetric withadd:Harmless for dict removals (assignment order is irrelevant) and sets (single entry); only list inserts depend on order.
Verified: the minimal case and a 2000-case
revert/patchround-trip fuzz now report 0 mismatches; dict and set reverts unchanged. Added a regression test (full removal and partial removal of ≥3 elements) that fails before the fix. Full unit suite: 88 passed, 2 skipped; theswapdoctest passes. The changed line ispycodestyle-clean.This pull request was prepared with the assistance of AI, under my direction and review.