[Python-checkins] r83671 - in python/branches/release31-maint: Misc/NEWS Modules/readline.c

mark.dickinson python-checkins at python.org
Tue Aug 3 18:52:24 CEST 2010


Author: mark.dickinson
Date: Tue Aug  3 18:52:23 2010
New Revision: 83671

Log:
Merged revisions 83670 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r83670 | mark.dickinson | 2010-08-03 17:49:49 +0100 (Tue, 03 Aug 2010) | 3 lines
  
  Issue #8065:  Fix another memory leak in readline module, from failure to free
  the result of a call to history_get_history_state.
........


Modified:
   python/branches/release31-maint/   (props changed)
   python/branches/release31-maint/Misc/NEWS
   python/branches/release31-maint/Modules/readline.c

Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS	(original)
+++ python/branches/release31-maint/Misc/NEWS	Tue Aug  3 18:52:23 2010
@@ -383,6 +383,9 @@
 Extension Modules
 -----------------
 
+- Issue #8065: Fix memory leak in readline module (from failure to
+  free the result of history_get_history_state()).
+
 - Issue #9450: Fix memory leak in readline.replace_history_item and
   readline.remove_history_item for readline version >= 5.0.
 

Modified: python/branches/release31-maint/Modules/readline.c
==============================================================================
--- python/branches/release31-maint/Modules/readline.c	(original)
+++ python/branches/release31-maint/Modules/readline.c	Tue Aug  3 18:52:23 2010
@@ -490,6 +490,25 @@
 \n\
 Returns current completer function.");
 
+/* Private function to get current length of history.  XXX It may be
+ * possible to replace this with a direct use of history_length instead,
+ * but it's not clear whether BSD's libedit keeps history_length up to date.
+ * See issue #8065.*/
+
+static int
+_py_get_history_length(void)
+{
+    HISTORY_STATE *hist_st = history_get_history_state();
+    int length = hist_st->length;
+    /* the history docs don't say so, but the address of hist_st changes each
+       time history_get_history_state is called which makes me think it's
+       freshly malloc'd memory...  on the other hand, the address of the last
+       line stays the same as long as history isn't extended, so it appears to
+       be malloc'd but managed by the history package... */
+    free(hist_st);
+    return length;
+}
+
 /* Exported function to get any element of history */
 
 static PyObject *
@@ -517,10 +536,7 @@
 static PyObject *
 get_current_history_length(PyObject *self, PyObject *noarg)
 {
-    HISTORY_STATE *hist_st;
-
-    hist_st = history_get_history_state();
-    return PyLong_FromLong(hist_st ? (long) hist_st->length : (long) 0);
+    return PyLong_FromLong((long)_py_get_history_length());
 }
 
 PyDoc_STRVAR(doc_get_current_history_length,
@@ -1000,20 +1016,13 @@
     n = strlen(p);
     if (n > 0) {
         char *line;
-        HISTORY_STATE *state = history_get_history_state();
-        if (state->length > 0)
-            line = history_get(state->length)->line;
+        int length = _py_get_history_length();
+        if (length > 0)
+            line = history_get(length)->line;
         else
             line = "";
         if (strcmp(p, line))
             add_history(p);
-        /* the history docs don't say so, but the address of state
-           changes each time history_get_history_state is called
-           which makes me think it's freshly malloc'd memory...
-           on the other hand, the address of the last line stays the
-           same as long as history isn't extended, so it appears to
-           be malloc'd but managed by the history package... */
-        free(state);
     }
     /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
        release the original. */


More information about the Python-checkins mailing list