Fix build with Python 3.4 (PR python/16784)
[deliverable/binutils-gdb.git] / gdb / python / py-gdb-readline.c
CommitLineData
037bbc8e
YPK
1/* Readline support for Python.
2
ecd75fc8 3 Copyright (C) 2012-2014 Free Software Foundation, Inc.
037bbc8e
YPK
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"
037bbc8e
YPK
22#include "top.h"
23#include "cli/cli-utils.h"
037bbc8e
YPK
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
30static char *
31gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
38bcc89d
SM
32#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4
33 const char *prompt)
34#else
037bbc8e 35 char *prompt)
38bcc89d 36#endif
037bbc8e
YPK
37{
38 int n;
5799c0b9 39 char *p = NULL, *q;
037bbc8e
YPK
40 volatile struct gdb_exception except;
41
42 TRY_CATCH (except, RETURN_MASK_ALL)
43 p = command_line_input (prompt, 0, "python");
44
45 /* Detect user interrupt (Ctrl-C). */
46 if (except.reason == RETURN_QUIT)
47 return NULL;
48
49 /* Handle errors by raising Python exceptions. */
50 if (except.reason < 0)
51 {
52 /* The thread state is nulled during gdbpy_readline_wrapper,
53 with the original value saved in the following undocumented
54 variable (see Python's Parser/myreadline.c and
55 Modules/readline.c). */
56 PyEval_RestoreThread (_PyOS_ReadlineTState);
57 gdbpy_convert_exception (except);
58 PyEval_SaveThread ();
59 return NULL;
60 }
61
62 /* Detect EOF (Ctrl-D). */
63 if (p == NULL)
64 {
65 q = PyMem_Malloc (1);
66 if (q != NULL)
67 q[0] = '\0';
68 return q;
69 }
70
71 n = strlen (p);
72
73 /* Copy the line to Python and return. */
74 q = PyMem_Malloc (n + 2);
75 if (q != NULL)
76 {
77 strncpy (q, p, n);
78 q[n] = '\n';
79 q[n + 1] = '\0';
80 }
81 return q;
82}
83
84/* Initialize Python readline support. */
85
86void
87gdbpy_initialize_gdb_readline (void)
88{
89 /* Python's readline module conflicts with GDB's use of readline
90 since readline is not reentrant. Ideally, a reentrant wrapper to
91 GDB's readline should be implemented to replace Python's readline
92 and prevent conflicts. For now, this file implements a
93 sys.meta_path finder that simply fails to import the readline
94 module. */
999633ed 95 if (PyRun_SimpleString ("\
037bbc8e
YPK
96import sys\n\
97\n\
98class GdbRemoveReadlineFinder:\n\
99 def find_module(self, fullname, path=None):\n\
100 if fullname == 'readline' and path is None:\n\
101 return self\n\
102 return None\n\
103\n\
104 def load_module(self, fullname):\n\
105 raise ImportError('readline module disabled under GDB')\n\
106\n\
107sys.meta_path.append(GdbRemoveReadlineFinder())\n\
999633ed
TT
108") == 0)
109 PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
037bbc8e
YPK
110}
111
This page took 0.262218 seconds and 4 git commands to generate.