1 /* Readline support for Python.
3 Copyright (C) 2012-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "python-internal.h"
23 #include "cli/cli-utils.h"
25 /* Readline function suitable for PyOS_ReadlineFunctionPointer, which
26 is used for Python's interactive parser and raw_input. In both
27 cases, sys_stdin and sys_stdout are always stdin and stdout
28 respectively, as far as I can tell; they are ignored and
29 command_line_input is used instead. */
32 gdbpy_readline_wrapper (FILE *sys_stdin
, FILE *sys_stdout
,
33 #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4
46 p
= command_line_input (buffer
, prompt
, "python");
48 /* Handle errors by raising Python exceptions. */
49 catch (const gdb_exception_forced_quit
&e
)
53 catch (const gdb_exception
&except
)
55 /* Detect user interrupt (Ctrl-C). */
56 if (except
.reason
== RETURN_QUIT
)
60 /* This readline callback is called without the GIL held. */
63 gdbpy_convert_exception (except
);
67 /* Detect EOF (Ctrl-D). */
70 q
= (char *) PyMem_RawMalloc (1);
78 /* Copy the line to Python and return. */
79 q
= (char *) PyMem_RawMalloc (n
+ 2);
89 /* Initialize Python readline support. */
91 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
92 gdbpy_initialize_gdb_readline (void)
94 /* Python's readline module conflicts with GDB's use of readline
95 since readline is not reentrant. Ideally, a reentrant wrapper to
96 GDB's readline should be implemented to replace Python's readline
97 and prevent conflicts. For now, this file implements a
98 sys.meta_path finder that simply fails to import the readline
100 if (PyRun_SimpleString ("\
103 class GdbRemoveReadlineFinder:\n\
104 def find_module(self, fullname, path=None):\n\
105 if fullname == 'readline' and path is None:\n\
109 def load_module(self, fullname):\n\
110 raise ImportError('readline module disabled under GDB')\n\
112 sys.meta_path.append(GdbRemoveReadlineFinder())\n\
114 PyOS_ReadlineFunctionPointer
= gdbpy_readline_wrapper
;
119 GDBPY_INITIALIZE_FILE (gdbpy_initialize_gdb_readline
);