Skip to content

gh-161973: Fix SequenceMatcher.get_grouped_opcodes() mutating the cached opcodes - #161985

Open
tanveer-arch wants to merge 2 commits into
python:mainfrom
tanveer-arch:fix-difflib-grouped-opcodes-mutates-cache
Open

tanveer-arch wants to merge 2 commits into
python:mainfrom
tanveer-arch:fix-difflib-grouped-opcodes-mutates-cache

Conversation

@tanveer-arch

@tanveer-arch tanveer-arch commented Nov 14, 2026 •

Copy link
Copy Markdown

Fixes #161973.

SequenceMatcher.get_opcodes() caches its result and returns the same list object on every call. get_grouped_opcodes() took that list and overwrote its first and last entries while trimming context, so after one call the cached opcodes no longer covered the whole input and a later get_opcodes() — or a wider n — returned the trimmed list.

Fix: copy the list in get_grouped_opcodes() before trimming, so the cached opcodes are never mutated. Adds a regression test that consumes get_grouped_opcodes(1) and asserts get_opcodes() still returns the full list, and that a later get_grouped_opcodes(3) matches a fresh matcher.

AI assistance disclosure: I used an AI tool to help investigate and draft this fix; I reviewed the change and the test in detail and verified them by building CPython from main and running the difflib test suite.

@python-cla-bot

python-cla-bot Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SequenceMatcher.get_grouped_opcodes() changes the result of later get_opcodes() calls

1 participant