Repository navigation
Bugfix clip() can hang on OSC 8 hyperlinks - #252
Merged
Merged
Conversation
clip() could loop forever, appending to its output until the process was
killed by the OOM killer:
clip('\x1b]8;;http://x\x07\x1b[5Ga\x1b]8;;\x07', 0, 4)
The painter reaches _process_hyperlink() with an OSC 8 unit whose inner
text is visible somewhere, so the unit is neither EMPTY nor OUTSIDE, but
whose visible text lands outside the requested columns once clipped. The
clipped inner text is then the empty string of width zero, and
_write_cells() stored it as a cell.
That single zero-width cell caused two failures:
- _reconstruct_painter() advances its walk by the cell's width, so it
revisited the same column forever, appending the cell's text each
time. The walk now always advances at least one column.
- Storing the cell also replaced whatever the column already held, so a
hyperlink that clipped away silently erased a character underneath it.
Painting nothing is now a no-op.
The input is unusual but well-formed. The OSC 8 specification restricts
bytes to 32-126 only for the parameters and the URI; it places no such
restriction on the text between the opening and closing sequences, and
describes a hyperlink as "yet another attribute that character cells
have", where "all subsequent cells that are painted are hyperlinks to
this target". Cursor movement between those cells is exactly how the
specification's own example of an anchor split across two editor panes
would be emitted.
The suite never executed doctests, so the examples in the public docstrings
had drifted from what the code returns. Enabling --doctest-modules turned up
eight stale examples:
- ljust(), rjust(), center() and clip() wrote their examples as
'wcwidth.ljust(...)', but a doctest runs in its own module's namespace,
where the package name is not bound.
- iter_graphemes() spelled its expected output with the same escapes as its
input, but repr() escapes only what is unprintable: the ZWJ stays escaped
while the emoji and the combining acute do not.
- HyperlinkParams.parse() doubled the backslash of its terminator inside a
raw docstring.
- TextSizingParams and TextSizing gained a repr that omits fields left at
their defaults, which no example had been updated for.
- TextSizing.from_match() imported TEXT_SIZING_PATTERN on a plain indented
line rather than a '>>>' line, so the name was never bound and the
example raised NameError.
testpaths is set so that a bare 'pytest' does not walk bin/, docs/ and
setup.py, which --doctest-modules would otherwise import. The legacy
wcwidth/wcwidth.py re-export shim is skipped: it defines nothing of its own,
and importing it rebinds the package attribute 'wcwidth.wcwidth' from the
function to the module, which breaks every later 'from wcwidth import
wcwidth'.
Also, add module doctests to pylint and make them match
jquast
force-pushed
the
jq/clip-hyperlink-hang
branch
from
September 17, 2026 00:14
6e60aae to
416d584
Compare
jquast
marked this pull request as ready for review
September 17, 2026 00:14
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #252 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 1980 1982 +2
Branches 462 463 +1
=========================================
+ Hits 1980 1982 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
wcwidth.clip()could loop forever, appending to its output and reaching OOM:Strange enough, sequences are allowed within hyperlink text, and in this case cursor movement is used, even allowed. But it results in an otherwise empty hyperlink (move 5 columns, then letter 'a', but clip from 0-4). This put the code in a loop that this bugfix returns or breaks early for.
Also, module doctests are enabled and repaired