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