Skip to content

gh-154786: Fix crashes on a screen without a terminal - #154787

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:fix-prescr-terminal
Jul 28, 2026
Merged

gh-154786: Fix crashes on a screen without a terminal#154787
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:fix-prescr-terminal

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

screen.use() makes its screen current for the callback, by calling use_screen(), so a screen made by new_prescr() can be current without going through set_term(). It has no terminal, and operations that need one crashed inside curses.

Eleven window methods crashed -- refresh(), noutrefresh(), getch(), getkey(), get_wch(), getstr(), get_wstr(), echochar(), keypad(), idlok(), idcok() -- and newwin() and newpad() returned a window whose delwin() failed. They were found by calling every window method and every module-level function inside such a callback; some crash only for some arguments, idlok(0) is harmless while idlok(True) is not.

The check is that stdscr exists, which is true whichever way the screen became current. It is added to PyCursesStatefulInitialised(), so every module-level function that already required initscr() requires a terminal too, and to the window methods that need one. Window methods that only touch the window's own memory, such as addstr() or clearok(), are unchanged.

pre.use(lambda s: curses.use_env(False)) keeps working: affecting the state before initscr() is what such a screen is for, and those functions are not gated.

screen.use() makes its screen current for the callback, so a new_prescr()
screen, which has no terminal, can be current without set_term().
Operations that need a terminal then crashed inside curses.

Raise curses.error instead, checking that stdscr exists.
@serhiy-storchaka
serhiy-storchaka merged commit c8eb79d into python:main Jul 28, 2026
54 checks passed
@serhiy-storchaka
serhiy-storchaka deleted the fix-prescr-terminal branch July 28, 2026 05:00
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.

1 participant