changeset: 83656:0f65426009e2 parent: 83653:557599a32821 parent: 83655:df0afd3ebb70 user: Antoine Pitrou date: Mon May 06 21:54:07 2013 +0200 files: Misc/NEWS Modules/readline.c description: 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. diff -r 557599a32821 -r 0f65426009e2 Misc/NEWS --- a/Misc/NEWS Mon May 06 21:26:05 2013 +0200 +++ b/Misc/NEWS Mon May 06 21:54:07 2013 +0200 @@ -77,6 +77,10 @@ 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. diff -r 557599a32821 -r 0f65426009e2 Modules/readline.c --- a/Modules/readline.c Mon May 06 21:26:05 2013 +0200 +++ b/Modules/readline.c Mon May 06 21:54:07 2013 +0200 @@ -70,6 +70,10 @@ int num_matches, int max_length); #endif +/* 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 * @@ -368,12 +372,20 @@ { 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, @@ -914,7 +926,8 @@ /* 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 '.' */ @@ -1170,8 +1183,6 @@ if (m == NULL) return NULL; - - PyOS_ReadlineFunctionPointer = call_readline; setup_readline(); return m;