1 /* Convenience functions implemented in Python.
3 Copyright (C) 2008, 2009, 2010, 2011 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 "exceptions.h"
24 #include "python-internal.h"
27 #include "cli/cli-decode.h"
28 #include "completer.h"
29 #include "expression.h"
32 static PyTypeObject fnpy_object_type;
37 convert_values_to_python (int argc, struct value **argv)
40 PyObject *result = PyTuple_New (argc);
42 for (i = 0; i < argc; ++i)
44 PyObject *elt = value_to_value_object (argv[i]);
48 error (_("Could not convert value to Python object."));
50 PyTuple_SetItem (result, i, elt);
55 /* Call a Python function object's invoke method. */
58 fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
59 void *cookie, int argc, struct value **argv)
61 struct value *value = NULL;
62 PyObject *result, *callable, *args;
63 struct cleanup *cleanup;
65 cleanup = ensure_python_env (gdbarch, language);
67 args = convert_values_to_python (argc, argv);
69 callable = PyObject_GetAttrString ((PyObject *) cookie, "invoke");
73 error (_("No method named 'invoke' in object."));
76 result = PyObject_Call (callable, args, NULL);
83 error (_("Error while executing Python code."));
86 value = convert_value_from_python (result);
91 error (_("Error while executing Python code."));
95 do_cleanups (cleanup);
100 /* Initializer for a Function object. It takes one argument, the name
104 fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
106 char *name, *docstring = NULL;
108 if (! PyArg_ParseTuple (args, "s", &name))
112 if (PyObject_HasAttrString (self, "__doc__"))
114 PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
115 if (ds_obj && gdbpy_is_string (ds_obj))
117 docstring = python_string_to_host_string (ds_obj);
118 if (docstring == NULL)
126 docstring = xstrdup (_("This function is not documented."));
128 add_internal_function (name, docstring, fnpy_call, self);
132 /* Initialize internal function support. */
135 gdbpy_initialize_functions (void)
137 if (PyType_Ready (&fnpy_object_type) < 0)
140 Py_INCREF (&fnpy_object_type);
141 PyModule_AddObject (gdb_module, "Function", (PyObject *) &fnpy_object_type);
146 static PyTypeObject fnpy_object_type =
148 PyObject_HEAD_INIT (NULL)
150 "gdb.Function", /*tp_name*/
151 sizeof (PyObject), /*tp_basicsize*/
160 0, /*tp_as_sequence*/
168 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
169 "GDB function object", /* tp_doc */
172 0, /* tp_richcompare */
173 0, /* tp_weaklistoffset */
181 0, /* tp_descr_get */
182 0, /* tp_descr_set */
183 0, /* tp_dictoffset */
184 fnpy_init, /* tp_init */
186 PyType_GenericNew /* tp_new */