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

Ryan Brown via lldb-commits lldb-commits at lists.llvm.org
Tue Sep 29 13:28:34 PDT 2015


ribrdb created this revision.
ribrdb added a reviewer: tfiala.
ribrdb added a subscriber: lldb-commits.
ribrdb set the repository for this revision to rL LLVM.

This implements basic line editing for the interactive python interpreter on linux.
No tab completion, but at least backspace works.

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,5 +1,6 @@
 #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
@@ -15,9 +16,34 @@
     moduleDocumentation,
     "Stub module meant to effectively disable readline support.");
 
+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.36030.patch
Type: text/x-patch
Size: 1833 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20150929/2f54bb67/attachment.bin>


More information about the lldb-commits mailing list