Skip to content

bugfix infinite loop in Terminal.wrap - #275

Merged
jquast merged 16 commits into
masterfrom
jq/bugfix-issue-273
Apr 26, 2025
Merged

jquast merged 16 commits into
masterfrom
jq/bugfix-issue-273

Conversation

@jquast

@jquast jquast commented Jun 26, 2024 •

Copy link
Copy Markdown
Owner

Closes #273 by @grayjk,

The following code enters an infinite loop:

import blessed
blessed.Terminal().wrap('\u5973', 1)

This fixes by explicit check: when

  • the given individual sequence is of length '2'
  • and the wrap width is '1'
  • and the cur_len is '0'

we cannot break down this "Wide" character any further -- so it is allowed to flow outside the given cell.

  • faulthandler_timeout = 30 is added to [pytest] in tox.ini
  • Tests for East-Asian, Emoji, and ZWJ are added
  • Further noting that blessed gets ZWJ wrong

jquast added 2 commits June 26, 2024 11:59
From #273,

> The following code enters an infinite loop:

    import blessed
    blessed.Terminal().wrap('\u5973', 1)

This fixes by explicit test: when the given individual sequence is
of length '2', and the width is '1', and the cur_len is '0', we cannot
break down this "Wide" character any further -- so it is allowed to
flow outside the given cell.

- 'faulthandler_timeout = 30' is added to [pytest] in tox.ini,
- Tests for East-Asian, Emoji, and ZWJ are added
- Further noting that blessed gets ZWJ wrong
@codecov

codecov Bot commented Jun 26, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.57%. Comparing base (77fe25a) to head (edb5196).
Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #275      +/-   ##
==========================================
+ Coverage   95.56%   95.57%   +0.01%     
==========================================
  Files          10       10              
  Lines        1037     1040       +3     
  Branches      177      165      -12     
==========================================
+ Hits          991      994       +3     
  Misses         43       43              
  Partials        3        3              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jquast jquast changed the title jq/bugfix issue 273 bugfix infinite loop in Terminal.wrap Jun 26, 2024
Comment thread blessed/sequences.py
nxt += len(text)
if Sequence(chunk[:nxt], term).length() > space_left:
break
seq_length = Sequence(chunk[:nxt], term).length()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when seq_length is equal to space_left, the for loop does the next chunk even though there is no space remaining

is there another small optimization possible to call length fewer times? something like:

if seq_length == space_left:
    idx = nxt
    break
elif seq_length > space_left:
    ...

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many unicode characters that consume 0 cells, https://wcwidth.readthedocs.io/en/latest/specs.html#width-of-0, there are examples of this in the ZWJ test added, though it gets it wrong in that specific case, we would prefer to cojoin with 1,800+ possible Combining Marks that might follow, like in cafe\u0301 we would not wish to break immediately after the letter 'e', because the next character \u0301 is desired

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test in 6beb3c6 hope that makes it more clear

@grayjk

grayjk commented Apr 24, 2025

Copy link
Copy Markdown
Collaborator

poke @jquast @avylove

hoping this can get merged so I can remove a workaround for #273. thanks!

@jquast

jquast commented Apr 26, 2025

Copy link
Copy Markdown
Owner Author

yes, sure, i'll do it soon

jquast added 2 commits April 26, 2025 10:35
_line_buffered *is* defined in the __init__ method of
base Terminal class which is derived here
@jquast
jquast merged commit e4554e1 into master Apr 26, 2025
@jquast
jquast deleted the jq/bugfix-issue-273 branch April 26, 2025 14:48
@jquast

jquast commented Apr 26, 2025

Copy link
Copy Markdown
Owner Author

This is released, although the wheel has uploaded I'm having a problem with pushing the source tarball, it should get online eventually

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

infinite loop in Terminal.wrap

2 participants