]> granicus.if.org Git - python/commitdiff
#18113: Objects associated to a curses.panel object with set_userptr() were leaked.
authorAndrew Kuchling <amk@amk.ca>
Sat, 15 Jun 2013 18:04:04 +0000 (14:04 -0400)
committerAndrew Kuchling <amk@amk.ca>
Sat, 15 Jun 2013 18:04:04 +0000 (14:04 -0400)
Reported by Atsuo Ishimoto.

Lib/test/test_curses.py
Misc/NEWS
Modules/_curses_panel.c

index e959622c7332feef9e8ca5b8c553ce0cd8aef4f5..76812b71ba3ba31c1c741920841d93e3c7898765 100644 (file)
@@ -252,6 +252,18 @@ def test_userptr_without_set(stdscr):
     except curses.panel.error:
         pass
 
+def test_userptr_memory_leak(stdscr):
+    w = curses.newwin(10, 10)
+    p = curses.panel.new_panel(w)
+    obj = object()
+    nrefs = sys.getrefcount(obj)
+    for i in range(100):
+        p.set_userptr(obj)
+
+    p.set_userptr(None)
+    if sys.getrefcount(obj) != nrefs:
+        raise RuntimeError("set_userptr leaked references")
+
 def test_resize_term(stdscr):
     if hasattr(curses, 'resizeterm'):
         lines, cols = curses.LINES, curses.COLS
@@ -317,6 +329,7 @@ def main(stdscr):
         module_funcs(stdscr)
         window_funcs(stdscr)
         test_userptr_without_set(stdscr)
+        test_userptr_memory_leak(stdscr)
         test_resize_term(stdscr)
         test_issue6243(stdscr)
         test_unget_wch(stdscr)
index 70b183f793147d0c3f71196c274a6f8541f8075a..aacf0e62a938ee5d539507528a6127f5a8bc6c56 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -78,6 +78,9 @@ Library
   the default for linking if LDSHARED is not also overriden.  This restores
   Distutils behavior introduced in 3.2.3 and inadvertently dropped in 3.3.0.
 
+- Issue #18113: Fixed a refcount leak in the curses.panel module's
+  set_userptr() method.  Reported by Atsuo Ishimoto.
+
 IDLE
 ----
 
index 1c7d0842e84ab5400fa65d696e7f2ff25fe746fb..c11f3d8476802636a50b9f09e14735aeb33925a8 100644 (file)
@@ -322,6 +322,10 @@ PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args)
 static PyObject *
 PyCursesPanel_set_panel_userptr(PyCursesPanelObject *self, PyObject *obj)
 {
+    PyObject *oldobj;
+    PyCursesInitialised;
+    oldobj = (PyObject *) panel_userptr(self->pan);
+    Py_XDECREF(oldobj);
     Py_INCREF(obj);
     return PyCursesCheckERR(set_panel_userptr(self->pan, (void*)obj),
                             "set_panel_userptr");