← Winnitude! Search  ·  Reader Mode (760 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 25 LINKS

Don't crash when the current line contains control characters by imeerkhurram · Pull Request #1068 · bpython/bpython

Repository navigation

Don't crash when the current line contains control characters - #1068


Open
imeerkhurram wants to merge 1 commit into bpython:main from imeerkhurram:fix/paint-cursor-control-chars
Open

Don't crash when the current line contains control characters #1068 imeerkhurram wants to merge 1 commit into bpython:main from imeerkhurram:fix/paint-cursor-control-chars

Conversation

imeerkhurram commented Dec 6, 2026



Copy link Copy Markdown
Contributor

Fixes #900
Fixes #985

print('', end='\r'), print('\033[91m', end='') and sys.stdout.write('\033[1;1H') still end bpython on current main (Python 3.15, 80 columns):

  File ".../bpython/curtsiesfrontend/repl.py", line 1768, in paint
    assert cursor_row >= 0 and cursor_column >= 0, (
AssertionError: (-1, 79, '\r>>> ', '', 0)

Cause

paint() measures the current line with wcswidth, which returns -1 for control characters such as \r, \t or \x1b. Output without a trailing newline stays on the current line, so the cursor position comes out as -1. While input() is waiting, the output part goes through FmtStr.width instead, which raises a ValueError (for example 1/0 followed by input('\r> ')), and a soft hyphen typed into input() hits the same assertion.

Fix

display_linize, which paints that line, already splits it by length when its width can't be computed. paint() now does the same for the cursor: when wcswidth returns -1 for the line, the position is counted in characters. Lines without control characters are measured exactly as before.

The control characters are still not interpreted: \x1b[1;1H doesn't move the cursor to the top, but bpython keeps running.

Tests

  • Four tests in TestStdOutErr: the inputs from both issues plus a tab, wide characters typed after \r, input() after a traceback, and a soft hyphen typed into input(). All fail on main and pass with the change.
  • Ran bpython in a pseudo-terminal with the issue inputs: on main each one exits with status 1; with the change, 1+41 on the next line prints 42.
  • pytest --cov=bpython -v: 356 passed, 5 skipped, 3 xfailed (main: 352 passed, same skips and xfails). ruff format --check, ruff check, codespell and mypy pass.
  • Added a line to CHANGELOG.rst under 0.27 fixes.
Don't crash when the current line contains control characters 51dcd35
wcswidth returns -1 for control characters such as "\r" or "\x1b", so
after print('', end='\r') or sys.stdout.write('\033[1;1H') paint()
computed cursor row -1 and failed an assertion, which ended bpython.
While input() was waiting, FmtStr.width raised a ValueError instead,
e.g. for 1/0 followed by input('\r> ').

display_linize already splits such a line by its length when its width
is unknown, so compute the cursor position from the length as well in
that case. Lines without control characters are measured as before.

Fixes bpython#900
Fixes bpython#985

codecov
Bot
commented Dec 6, 2026



Copy link Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.36%. Comparing base (1a919d3 ) to head (51dcd35 ).
⚠️ Report is 88 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1068      +/-   ##
==========================================
- Coverage   76.51%   76.36%   -0.16%     
==========================================
  Files          62       60       -2     
  Lines        8308     8613     +305     
==========================================
+ Hits         6357     6577     +220     
- Misses       1951     2036      +85     

☔ View full report in Codecov by Harness .
📢 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.
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment

Labels

None yet

1 participant