Skip to content
Open
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
12 changes: 12 additions & 0 deletions Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,18 @@ def test_new_prescr(self):
del screen
gc_collect()

@unittest.skipUnless(hasattr(curses, 'new_prescr'),
'requires curses.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()

@cpython_only
def test_disallow_instantiation(self):
# The screen type cannot be instantiated directly (bpo-43916).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`curses.set_term` now raises :exc:`curses.error` when given a screen with
no terminal, such as one returned by :func:`curses.new_prescr`. Such a screen
was accepted before, and the next window refresh crashed the interpreter.
8 changes: 7 additions & 1 deletion Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6834,11 +6834,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