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.panel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ Panel objects
.. method:: panel.replace(win)

Change the window associated with the panel to the window *win*.
Raise :exc:`curses.panel.error` if *win* has been detached from its
screen by :meth:`screen.close() <curses.screen.close>`.


.. method:: panel.set_userptr(obj)
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3079,6 +3079,22 @@ def test_close(self):
# close() is idempotent.
screen.close()

@requires_curses_func('panel')
def test_close_then_panel_replace(self):
# A detached window has no underlying curses window, so replace()
# must reject it. It used to be accepted, and the panel then
# crashed inside curses on its next use.
s = self.make_pty()
screen = curses.newterm('xterm', s, s)
win = screen.stdscr
panel = curses.panel.new_panel(curses.newwin(3, 6, 0, 0))
# Drop the panel from the global stack before later tests inspect it.
self.addCleanup(gc_collect)
screen.close()
self.assertRaises(curses.panel.error, panel.replace, win)
# The panel kept its own window, so it still works.
panel.move(1, 1)

@unittest.skipUnless(hasattr(curses, 'new_prescr'),
'requires curses.new_prescr()')
def test_new_prescr(self):
Expand Down
6 changes: 6 additions & 0 deletions Modules/_curses_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,12 @@ _curses_panel_panel_replace_impl(PyCursesPanelObject *self,
return NULL;
}

if (win->win == NULL) {
_curses_panel_state *state = get_curses_panel_state_by_panel(self);
PyErr_SetString(state->error, "the window has been detached");
return NULL;
}

int rtn = replace_panel(self->pan, win->win);
if (rtn == ERR) {
curses_panel_panel_set_error(self, "replace_panel", "replace");
Expand Down
Loading