Remove spurious exceptions.h inclusions
[deliverable/binutils-gdb.git] / gdb / python / py-gdb-readline.c
1 /* Readline support for Python.
2
3 Copyright (C) 2012-2014 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 /* Readline function suitable for PyOS_ReadlineFunctionPointer, which
25 is used for Python's interactive parser and raw_input. In both
26 cases, sys_stdin and sys_stdout are always stdin and stdout
27 respectively, as far as I can tell; they are ignored and
28 command_line_input is used instead. */
29
30 static char *
31 gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
32 char *prompt)
33 {
34 int n;
35 char *p = NULL, *q;
36 volatile struct gdb_exception except;
37
38 TRY_CATCH (except, RETURN_MASK_ALL)
39 p = command_line_input (prompt, 0, "python");
40
41 /* Detect user interrupt (Ctrl-C). */
42 if (except.reason == RETURN_QUIT)
43 return NULL;
44
45 /* Handle errors by raising Python exceptions. */
46 if (except.reason < 0)
47 {
48 /* The thread state is nulled during gdbpy_readline_wrapper,
49 with the original value saved in the following undocumented
50 variable (see Python's Parser/myreadline.c and
51 Modules/readline.c). */
52 PyEval_RestoreThread (_PyOS_ReadlineTState);
53 gdbpy_convert_exception (except);
54 PyEval_SaveThread ();
55 return NULL;
56 }
57
58 /* Detect EOF (Ctrl-D). */
59 if (p == NULL)
60 {
61 q = PyMem_Malloc (1);
62 if (q != NULL)
63 q[0] = '\0';
64 return q;
65 }
66
67 n = strlen (p);
68
69 /* Copy the line to Python and return. */
70 q = PyMem_Malloc (n + 2);
71 if (q != NULL)
72 {
73 strncpy (q, p, n);
74 q[n] = '\n';
75 q[n + 1] = '\0';
76 }
77 return q;
78 }
79
80 /* Initialize Python readline support. */
81
82 void
83 gdbpy_initialize_gdb_readline (void)
84 {
85 /* Python's readline module conflicts with GDB's use of readline
86 since readline is not reentrant. Ideally, a reentrant wrapper to
87 GDB's readline should be implemented to replace Python's readline
88 and prevent conflicts. For now, this file implements a
89 sys.meta_path finder that simply fails to import the readline
90 module. */
91 if (PyRun_SimpleString ("\
92 import sys\n\
93 \n\
94 class GdbRemoveReadlineFinder:\n\
95 def find_module(self, fullname, path=None):\n\
96 if fullname == 'readline' and path is None:\n\
97 return self\n\
98 return None\n\
99 \n\
100 def load_module(self, fullname):\n\
101 raise ImportError('readline module disabled under GDB')\n\
102 \n\
103 sys.meta_path.append(GdbRemoveReadlineFinder())\n\
104 ") == 0)
105 PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
106 }
107
This page took 0.035275 seconds and 5 git commands to generate.