1 // NOTE: Since Python may define some pre-processor definitions which affect the
2 // standard headers on some systems, you must include Python.h before any
3 // standard headers are included.
8 #ifndef LLDB_DISABLE_LIBEDIT
9 #include <editline/readline.h>
12 // Simple implementation of the Python readline module using libedit.
13 // In the event that libedit is excluded from the build, this turns
14 // back into a null implementation that blocks the module from pulling
15 // in the GNU readline shared lib, which causes linkage confusion when
16 // both readline and libedit's readline compatibility symbols collide.
18 // Currently it only installs a PyOS_ReadlineFunctionPointer, without
19 // implementing any of the readline module methods. This is meant to
20 // work around LLVM pr18841 to avoid seg faults in the stock Python
21 // readline.so linked against GNU readline.
23 #ifndef LLDB_DISABLE_LIBEDIT
26 "Simple readline module implementation based on libedit.");
30 "Stub module meant to avoid linking GNU readline.");
33 #if PY_MAJOR_VERSION >= 3
34 static struct PyModuleDef readline_module =
36 PyModuleDef_HEAD_INIT, // m_base
38 moduleDocumentation, // m_doc
42 nullptr, // m_traverse
47 static struct PyMethodDef moduleMethods[] =
49 {nullptr, nullptr, 0, nullptr}
53 #ifndef LLDB_DISABLE_LIBEDIT
55 #if PY_MAJOR_VERSION >= 3
56 simple_readline(FILE *stdin, FILE *stdout, const char *prompt)
58 simple_readline(FILE *stdin, FILE *stdout, char *prompt)
62 rl_outstream = stdout;
63 char* line = readline(prompt);
66 char* ret = (char*)PyMem_Malloc(1);
74 char* ret = (char*)PyMem_Malloc(n + 2);
75 strncpy(ret, line, n);
86 #ifndef LLDB_DISABLE_LIBEDIT
87 PyOS_ReadlineFunctionPointer = simple_readline;
90 #if PY_MAJOR_VERSION >= 3
91 return PyModule_Create(&readline_module);
97 static_cast<PyObject *>(NULL),