1 /* Convenience functions implemented in Python.
3 Copyright (C) 2008-2016 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/>. */
23 #include "python-internal.h"
26 #include "cli/cli-decode.h"
27 #include "completer.h"
28 #include "expression.h"
31 extern PyTypeObject fnpy_object_type
32 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject");
37 convert_values_to_python (int argc
, struct value
**argv
)
40 PyObject
*result
= PyTuple_New (argc
);
45 for (i
= 0; i
< argc
; ++i
)
47 PyObject
*elt
= value_to_value_object (argv
[i
]);
53 PyTuple_SetItem (result
, i
, elt
);
58 /* Call a Python function object's invoke method. */
61 fnpy_call (struct gdbarch
*gdbarch
, const struct language_defn
*language
,
62 void *cookie
, int argc
, struct value
**argv
)
64 struct value
*value
= NULL
;
65 /* 'result' must be set to NULL, this initially indicates whether
66 the function was called, or not. */
67 PyObject
*result
= NULL
;
68 PyObject
*callable
, *args
;
69 struct cleanup
*cleanup
;
71 cleanup
= ensure_python_env (gdbarch
, language
);
73 args
= convert_values_to_python (argc
, argv
);
74 /* convert_values_to_python can return NULL on error. If we
75 encounter this, do not call the function, but allow the Python ->
76 error code conversion below to deal with the Python exception.
77 Note, that this is different if the function simply does not
82 callable
= PyObject_GetAttrString ((PyObject
*) cookie
, "invoke");
86 error (_("No method named 'invoke' in object."));
89 result
= PyObject_Call (callable
, args
, NULL
);
96 PyObject
*ptype
, *pvalue
, *ptraceback
;
98 PyErr_Fetch (&ptype
, &pvalue
, &ptraceback
);
100 /* Try to fetch an error message contained within ptype, pvalue.
101 When fetching the error message we need to make our own copy,
102 we no longer own ptype, pvalue after the call to PyErr_Restore. */
104 gdb::unique_xmalloc_ptr
<char>
105 msg (gdbpy_exception_to_string (ptype
, pvalue
));
109 /* An error occurred computing the string representation of the
110 error message. This is rare, but we should inform the user. */
112 printf_filtered (_("An error occurred in a Python "
113 "convenience function\n"
114 "and then another occurred computing the "
115 "error message.\n"));
116 gdbpy_print_stack ();
119 /* Don't print the stack for gdb.GdbError exceptions.
120 It is generally used to flag user errors.
122 We also don't want to print "Error occurred in Python command"
123 for user errors. However, a missing message for gdb.GdbError
124 exceptions is arguably a bug, so we flag it as such. */
126 if (!PyErr_GivenExceptionMatches (ptype
, gdbpy_gdberror_exc
)
127 || msg
== NULL
|| *msg
== '\0')
129 PyErr_Restore (ptype
, pvalue
, ptraceback
);
130 gdbpy_print_stack ();
131 if (msg
!= NULL
&& *msg
!= '\0')
132 error (_("Error occurred in Python convenience function: %s"),
135 error (_("Error occurred in Python convenience function."));
141 Py_XDECREF (ptraceback
);
142 error ("%s", msg
.get ());
146 value
= convert_value_from_python (result
);
150 gdbpy_print_stack ();
151 error (_("Error while executing Python code."));
155 do_cleanups (cleanup
);
160 /* Initializer for a Function object. It takes one argument, the name
164 fnpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
167 gdb::unique_xmalloc_ptr
<char> docstring
;
169 if (! PyArg_ParseTuple (args
, "s", &name
))
173 if (PyObject_HasAttrString (self
, "__doc__"))
175 PyObject
*ds_obj
= PyObject_GetAttrString (self
, "__doc__");
178 if (gdbpy_is_string (ds_obj
))
180 docstring
= python_string_to_host_string (ds_obj
);
181 if (docstring
== NULL
)
193 docstring
.reset (xstrdup (_("This function is not documented.")));
195 add_internal_function (name
, docstring
.release (), fnpy_call
, self
);
199 /* Initialize internal function support. */
202 gdbpy_initialize_functions (void)
204 fnpy_object_type
.tp_new
= PyType_GenericNew
;
205 if (PyType_Ready (&fnpy_object_type
) < 0)
208 return gdb_pymodule_addobject (gdb_module
, "Function",
209 (PyObject
*) &fnpy_object_type
);
214 PyTypeObject fnpy_object_type
=
216 PyVarObject_HEAD_INIT (NULL
, 0)
217 "gdb.Function", /*tp_name*/
218 sizeof (PyObject
), /*tp_basicsize*/
227 0, /*tp_as_sequence*/
235 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
236 "GDB function object", /* tp_doc */
239 0, /* tp_richcompare */
240 0, /* tp_weaklistoffset */
248 0, /* tp_descr_get */
249 0, /* tp_descr_set */
250 0, /* tp_dictoffset */
251 fnpy_init
, /* tp_init */