Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/library/curses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ Initialization and termination
and return the previously current screen.
Returns ``None`` if the previous screen was the one created by
:func:`initscr`.
Raises :exc:`error` if *screen* has no terminal,
as is the case for a screen returned by :func:`new_prescr`.

.. versionadded:: next

Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,17 @@ def test_new_prescr(self):
del screen
gc_collect()

@requires_curses_func('new_prescr')
def test_set_term_prescr_screen(self):
# A new_prescr() screen has no terminal, so it cannot become the
# current one. It used to be accepted, and the next refresh then
# crashed inside curses.
s = self.make_pty()
screen = curses.newterm('xterm', s, s)
self.assertRaises(curses.error, curses.set_term, curses.new_prescr())
# The current screen is unchanged, so refreshing it still works.
screen.stdscr.refresh()

def test_initscr_after_newterm_keeps_screen_alive(self):
# initscr() called while a newterm() screen is current returns that
# screen's own standard window, so the window keeps the screen alive.
Expand Down
8 changes: 7 additions & 1 deletion Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6850,11 +6850,17 @@ _curses_set_term(PyObject *module, PyObject *screen)
if (so == NULL) {
return NULL;
}
cursesmodule_state *state = get_cursesmodule_state(module);
if (so->stdscr_win == NULL) {
/* A screen from new_prescr() has no terminal, so it cannot become the
current one: a later refresh would dereference NULL in curses. */
PyErr_SetString(state->error, "the screen has no terminal");
return NULL;
}
set_term(so->screen);
if (!update_lines_cols(module)) {
return NULL;
}
cursesmodule_state *state = get_cursesmodule_state(module);
PyObject *prev = state->topscreen; /* steal the owned reference */
state->topscreen = Py_NewRef(screen);
return prev != NULL ? prev : Py_NewRef(Py_None);
Expand Down
Loading