Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ce8d17

Browse files
committed
Merge from 3.3
2 parents 70833a8 + 9290dd1 commit 6ce8d17

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

Lib/test/test_curses.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ def test_userptr_memory_leak(stdscr):
264264
if sys.getrefcount(obj) != nrefs:
265265
raise RuntimeError("set_userptr leaked references")
266266

267+
def test_userptr_segfault(stdscr):
268+
panel = curses.panel.new_panel(stdscr)
269+
class A:
270+
def __del__(self):
271+
panel.set_userptr(None)
272+
panel.set_userptr(A())
273+
panel.set_userptr(None)
274+
267275
def test_resize_term(stdscr):
268276
if hasattr(curses, 'resizeterm'):
269277
lines, cols = curses.LINES, curses.COLS
@@ -330,6 +338,7 @@ def main(stdscr):
330338
window_funcs(stdscr)
331339
test_userptr_without_set(stdscr)
332340
test_userptr_memory_leak(stdscr)
341+
test_userptr_segfault(stdscr)
333342
test_resize_term(stdscr)
334343
test_issue6243(stdscr)
335344
test_unget_wch(stdscr)

Modules/_curses_panel.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,17 @@ static PyObject *
323323
PyCursesPanel_set_panel_userptr(PyCursesPanelObject *self, PyObject *obj)
324324
{
325325
PyObject *oldobj;
326+
int rc;
326327
PyCursesInitialised;
328+
Py_INCREF(obj);
327329
oldobj = (PyObject *) panel_userptr(self->pan);
330+
rc = set_panel_userptr(self->pan, (void*)obj);
331+
if (rc == ERR) {
332+
/* In case of an ncurses error, decref the new object again */
333+
Py_DECREF(obj);
334+
}
328335
Py_XDECREF(oldobj);
329-
Py_INCREF(obj);
330-
return PyCursesCheckERR(set_panel_userptr(self->pan, (void*)obj),
331-
"set_panel_userptr");
336+
return PyCursesCheckERR(rc, "set_panel_userptr");
332337
}
333338

334339
static PyObject *

0 commit comments

Comments
 (0)