Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonReadline.cpp
39638 views
#include "PythonReadline.h"12#ifdef LLDB_USE_LIBEDIT_READLINE_COMPAT_MODULE34#include <cstdio>56#include <editline/readline.h>78// Simple implementation of the Python readline module using libedit.9// In the event that libedit is excluded from the build, this turns10// back into a null implementation that blocks the module from pulling11// in the GNU readline shared lib, which causes linkage confusion when12// both readline and libedit's readline compatibility symbols collide.13//14// Currently it only installs a PyOS_ReadlineFunctionPointer, without15// implementing any of the readline module methods. This is meant to16// work around LLVM pr18841 to avoid seg faults in the stock Python17// readline.so linked against GNU readline.18//19// Bug on the cpython side: https://bugs.python.org/issue386342021PyDoc_STRVAR(moduleDocumentation,22"Simple readline module implementation based on libedit.");2324static struct PyModuleDef readline_module = {25PyModuleDef_HEAD_INIT, // m_base26"lldb_editline", // m_name27moduleDocumentation, // m_doc28-1, // m_size29nullptr, // m_methods30nullptr, // m_reload31nullptr, // m_traverse32nullptr, // m_clear33nullptr, // m_free34};3536static char *simple_readline(FILE *stdin, FILE *stdout, const char *prompt) {37rl_instream = stdin;38rl_outstream = stdout;39char *line = readline(prompt);40if (!line) {41char *ret = (char *)PyMem_RawMalloc(1);42if (ret != nullptr)43*ret = '\0';44return ret;45}46if (*line)47add_history(line);48int n = strlen(line);49char *ret = (char *)PyMem_RawMalloc(n + 2);50if (ret) {51memcpy(ret, line, n);52free(line);53ret[n] = '\n';54ret[n + 1] = '\0';55}56return ret;57}5859PyMODINIT_FUNC initlldb_readline(void) {60PyOS_ReadlineFunctionPointer = simple_readline;6162return PyModule_Create(&readline_module);63}64#endif656667