GDB (xrefs)
Loading...
Searching...
No Matches
py-gdb-readline.c
Go to the documentation of this file.
1/* Readline support for Python.
2
3 Copyright (C) 2012-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "python-internal.h"
22#include "top.h"
23#include "cli/cli-utils.h"
24
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. */
30
31static char *
32gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
33#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4
34 const char *prompt)
35#else
36 char *prompt)
37#endif
38{
39 int n;
40 const char *p = NULL;
41 std::string buffer;
42 char *q;
43
44 try
45 {
46 p = command_line_input (buffer, prompt, "python");
47 }
48 /* Handle errors by raising Python exceptions. */
49 catch (const gdb_exception_forced_quit &e)
50 {
51 quit_force (NULL, 0);
52 }
53 catch (const gdb_exception &except)
54 {
55 /* Detect user interrupt (Ctrl-C). */
56 if (except.reason == RETURN_QUIT)
57 return NULL;
58
59 /* The thread state is nulled during gdbpy_readline_wrapper,
60 with the original value saved in the following undocumented
61 variable (see Python's Parser/myreadline.c and
62 Modules/readline.c). */
63 PyEval_RestoreThread (_PyOS_ReadlineTState);
65 PyEval_SaveThread ();
66 return NULL;
67 }
68
69 /* Detect EOF (Ctrl-D). */
70 if (p == NULL)
71 {
72 q = (char *) PyMem_RawMalloc (1);
73 if (q != NULL)
74 q[0] = '\0';
75 return q;
76 }
77
78 n = strlen (p);
79
80 /* Copy the line to Python and return. */
81 q = (char *) PyMem_RawMalloc (n + 2);
82 if (q != NULL)
83 {
84 strcpy (q, p);
85 q[n] = '\n';
86 q[n + 1] = '\0';
87 }
88 return q;
89}
90
91/* Initialize Python readline support. */
92
95{
96 /* Python's readline module conflicts with GDB's use of readline
97 since readline is not reentrant. Ideally, a reentrant wrapper to
98 GDB's readline should be implemented to replace Python's readline
99 and prevent conflicts. For now, this file implements a
100 sys.meta_path finder that simply fails to import the readline
101 module. */
102 if (PyRun_SimpleString ("\
103import sys\n\
104\n\
105class GdbRemoveReadlineFinder:\n\
106 def find_module(self, fullname, path=None):\n\
107 if fullname == 'readline' and path is None:\n\
108 return self\n\
109 return None\n\
110\n\
111 def load_module(self, fullname):\n\
112 raise ImportError('readline module disabled under GDB')\n\
113\n\
114sys.meta_path.append(GdbRemoveReadlineFinder())\n\
115") == 0)
116 PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
117
118 return 0;
119}
120
const char * command_line_input(std::string &cmd_line_buffer, const char *, const char *)
Definition top.c:1226
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION gdbpy_initialize_gdb_readline(void)
static char * gdbpy_readline_wrapper(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
void gdbpy_convert_exception(const struct gdb_exception &exception)
Definition py-utils.c:217
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
#define GDBPY_INITIALIZE_FILE(INIT,...)
#define PyMem_RawMalloc
void quit_force(int *exit_arg, int from_tty)
Definition top.c:1732