Fix convert_osc corrupting text with multiple OSC sequences - #426
Open
gaoflow wants to merge 1 commit into
Open
Conversation
The finditer loop mutated 'text' in-place on each iteration, but the
match positions came from the original string. After removing the first
OSC sequence the second match's start/end offsets pointed into the
already-shortened string, so slicing left garbage behind.
Fix: separate side effects (title calls) from text stripping; use
re.sub to remove all OSC sequences in one pass, which guarantees
correct results regardless of how many sequences appear.
Also guard the winterm.set_title() call with 'self.convert and winterm'
so that convert_osc no longer raises AttributeError on non-Windows
platforms where winterm is None, and tighten the params[0] check
from substring ('02') to tuple membership (('0', '2')).
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.
Problem
convert_oscmangles text when the input contains more than one OSC sequence.The loop uses
re.finditeron the original string, but then mutatestexton every iteration. After the first removal the second match's
start/endoffsets are stale — they point into the already-shortened string — so the slice
lands in the wrong place and leaves behind a partial OSC escape or drops
trailing content.
Minimal reproducer:
The same code path also raises
AttributeError: 'NoneType' object has no attribute 'set_title'on non-Windows whenwinterm is None, as reported in#407.
Fix
Separate the two concerns:
winterm.set_title) — iterate all matches first,guarded by
self.convert and wintermso non-Windows never toucheswinterm.re.sub('', text)once, which correctly removesall matches in a single pass.
Also tighten
params[0] in '02'(substring test, matches''and'02')to
params[0] in ('0', '2')(tuple membership).Tests
Two new tests added:
test_osc_multiple_sequences_stripped_correctly— asserts both OSC sequencesare removed and surrounding text is preserved.
test_osc_strips_without_crash_when_winterm_none— asserts no crash whenwintermisNone(non-Windows withstrip=True, convert=False).All 40 tests pass (14 skipped, Windows-only).