[Lldb-commits] [PATCH] D13268: Simple readline functionality for interactive python on linux.

Ryan Brown via lldb-commits lldb-commits at lists.llvm.org
Thu Oct 1 14:00:25 PDT 2015


ribrdb updated this revision to Diff 36295.
ribrdb marked an inline comment as done.
ribrdb added a comment.

Updated comments.


Repository:
  rL LLVM

http://reviews.llvm.org/D13268

Files:
  scripts/Python/modules/readline/CMakeLists.txt
  scripts/Python/modules/readline/readline.cpp

Index: scripts/Python/modules/readline/readline.cpp
===================================================================
--- scripts/Python/modules/readline/readline.cpp
+++ scripts/Python/modules/readline/readline.cpp
@@ -1,10 +1,12 @@
 #include <stdio.h>
 #include "Python.h"
+#include <editline/readline.h>
 
-// Python readline module intentionally built to not implement the
-// readline module interface. This is meant to work around llvm
-// pr18841 to avoid seg faults in the stock Python readline.so linked
-// against GNU readline.
+// Simple implementation of the Python readline module using libedit.
+// Currently it only installs a PyOS_ReadlineFunctionPointer, without
+// implementing any of the readline module methods. This is meant to
+// work around LLVM pr18841 to avoid seg faults in the stock Python
+// readline.so linked against GNU readline.
 
 static struct PyMethodDef moduleMethods[] =
 {
@@ -13,11 +15,36 @@
 
 PyDoc_STRVAR(
     moduleDocumentation,
-    "Stub module meant to effectively disable readline support.");
+    "Stub module meant to avoid linking GNU readline.");
+
+static char*
+simple_readline(FILE *stdin, FILE *stdout, char *prompt)
+{
+    rl_instream = stdin;
+    rl_outstream = stdout;
+    char* line = readline(prompt);
+    if (!line)
+    {
+        char* ret = (char*)PyMem_Malloc(1);
+        if (ret != NULL)
+            *ret = '\0';
+        return ret;
+    }
+    if (*line)
+        add_history(line);
+    int n = strlen(line);
+    char* ret = (char*)PyMem_Malloc(n + 2);
+    strncpy(ret, line, n);
+    free(line);
+    ret[n] = '\n';
+    ret[n+1] = '\0';
+    return ret;
+}
 
 PyMODINIT_FUNC
 initreadline(void)
 {
+    PyOS_ReadlineFunctionPointer = simple_readline;
     Py_InitModule4(
         "readline",
         moduleMethods,
Index: scripts/Python/modules/readline/CMakeLists.txt
===================================================================
--- scripts/Python/modules/readline/CMakeLists.txt
+++ scripts/Python/modules/readline/CMakeLists.txt
@@ -7,7 +7,7 @@
 include_directories(${PYTHON_INCLUDE_DIR})
 add_library(readline SHARED readline.cpp)
 
-target_link_libraries(readline ${PYTHON_LIBRARY})
+target_link_libraries(readline ${PYTHON_LIBRARY} edit)
 
 # FIXME: the LIBRARY_OUTPUT_PATH seems to be ignored - this is not a
 # functional issue for the build dir, though, since the shared lib dir


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13268.36295.patch
Type: text/x-patch
Size: 2384 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20151001/88540b72/attachment.bin>


More information about the lldb-commits mailing list