]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #17289: The readline module now plays nicer with external modules or applicatio...
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 6 May 2013 19:51:03 +0000 (21:51 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 6 May 2013 19:51:03 +0000 (21:51 +0200)
Initial patch by Bradley Froehle.

Misc/NEWS
Modules/readline.c

index 4c3f833398601e86ae4a1c25bc2330195b769360..7989eb323be35731807b6004bcf612d972507b16 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #17289: The readline module now plays nicer with external modules
+  or applications changing the rl_completer_word_break_characters global
+  variable.  Initial patch by Bradley Froehle.
+
 - Issue #12181: select module: Fix struct kevent definition on OpenBSD 64-bit
   platforms. Patch by Federico Schwindt.
 
index b5e258db67e41e524f61c1bc9991f578117d1f24..233ebf95ad1b40ee55eda6a5e7f50310c9faa260 100644 (file)
@@ -69,6 +69,10 @@ on_completion_display_matches_hook(char **matches,
                                    int num_matches, int max_length);
 
 
+/* Memory allocated for rl_completer_word_break_characters
+   (see issue #17289 for the motivation). */
+static char *completer_word_break_characters;
+
 /* Exported function to send one line to readline's init file parser */
 
 static PyObject *
@@ -344,12 +348,20 @@ set_completer_delims(PyObject *self, PyObject *args)
 {
     char *break_chars;
 
-    if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
+    if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
         return NULL;
     }
-    free((void*)rl_completer_word_break_characters);
-    rl_completer_word_break_characters = strdup(break_chars);
-    Py_RETURN_NONE;
+    /* Keep a reference to the allocated memory in the module state in case
+       some other module modifies rl_completer_word_break_characters
+       (see issue #17289). */
+    free(completer_word_break_characters);
+    completer_word_break_characters = strdup(break_chars);
+    if (completer_word_break_characters) {
+        rl_completer_word_break_characters = completer_word_break_characters;
+        Py_RETURN_NONE;
+    }
+    else
+        return PyErr_NoMemory();
 }
 
 PyDoc_STRVAR(doc_set_completer_delims,
@@ -893,7 +905,8 @@ setup_readline(void)
     /* Set our completion function */
     rl_attempted_completion_function = (CPPFunction *)flex_complete;
     /* Set Python word break characters */
-    rl_completer_word_break_characters =
+    completer_word_break_characters =
+        rl_completer_word_break_characters =
         strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
         /* All nonalphanums except '.' */
 
@@ -906,7 +919,7 @@ setup_readline(void)
      */
 #ifdef __APPLE__
     if (using_libedit_emulation)
-       rl_read_init_file(NULL);
+        rl_read_init_file(NULL);
     else
 #endif /* __APPLE__ */
         rl_initialize();
@@ -1137,8 +1150,6 @@ initreadline(void)
     if (m == NULL)
         return;
 
-
-
     PyOS_ReadlineFunctionPointer = call_readline;
     setup_readline();
 }