]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - scripts/Python/modules/readline/readline.cpp
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / scripts / Python / modules / readline / readline.cpp
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.
4 #include "Python.h"
5
6 #include <stdio.h>
7
8 #ifndef LLDB_DISABLE_LIBEDIT
9 #include <editline/readline.h>
10 #endif
11
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.
17 //
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.
22
23 #ifndef LLDB_DISABLE_LIBEDIT
24 PyDoc_STRVAR(
25     moduleDocumentation,
26     "Simple readline module implementation based on libedit.");
27 #else
28 PyDoc_STRVAR(
29     moduleDocumentation,
30     "Stub module meant to avoid linking GNU readline.");
31 #endif
32
33 #if PY_MAJOR_VERSION >= 3
34 static struct PyModuleDef readline_module =
35 {
36     PyModuleDef_HEAD_INIT, // m_base
37     "readline",            // m_name
38     moduleDocumentation,   // m_doc
39     -1,                    // m_size
40     nullptr,               // m_methods
41     nullptr,               // m_reload
42     nullptr,               // m_traverse
43     nullptr,               // m_clear
44     nullptr,               // m_free
45 };
46 #else
47 static struct PyMethodDef moduleMethods[] =
48 {
49     {nullptr, nullptr, 0, nullptr}
50 };
51 #endif
52
53 #ifndef LLDB_DISABLE_LIBEDIT
54 static char*
55 #if PY_MAJOR_VERSION >= 3
56 simple_readline(FILE *stdin, FILE *stdout, const char *prompt)
57 #else
58 simple_readline(FILE *stdin, FILE *stdout, char *prompt)
59 #endif
60 {
61     rl_instream = stdin;
62     rl_outstream = stdout;
63     char* line = readline(prompt);
64     if (!line)
65     {
66         char* ret = (char*)PyMem_Malloc(1);
67         if (ret != NULL)
68             *ret = '\0';
69         return ret;
70     }
71     if (*line)
72         add_history(line);
73     int n = strlen(line);
74     char* ret = (char*)PyMem_Malloc(n + 2);
75     strncpy(ret, line, n);
76     free(line);
77     ret[n] = '\n';
78     ret[n+1] = '\0';
79     return ret;
80 }
81 #endif
82
83 PyMODINIT_FUNC
84 initreadline(void)
85 {
86 #ifndef LLDB_DISABLE_LIBEDIT
87     PyOS_ReadlineFunctionPointer = simple_readline;
88 #endif
89
90 #if PY_MAJOR_VERSION >= 3
91     return PyModule_Create(&readline_module);
92 #else
93     Py_InitModule4(
94         "readline",
95         moduleMethods,
96         moduleDocumentation,
97         static_cast<PyObject *>(NULL),
98         PYTHON_API_VERSION);
99 #endif
100 }