1 /* Convenience functions implemented in Python.
3 Copyright (C) 2008-2019 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"
32 extern PyTypeObject fnpy_object_type
33 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject");
37 /* Return a reference to a tuple ARGC elements long. Each element of the
38 tuple is a PyObject converted from the corresponding element of ARGV. */
41 convert_values_to_python (int argc
, struct value
**argv
)
44 gdbpy_ref
<> result (PyTuple_New (argc
));
49 for (i
= 0; i
< argc
; ++i
)
51 gdbpy_ref
<> elt (value_to_value_object (argv
[i
]));
54 PyTuple_SetItem (result
.get (), i
, elt
.release ());
59 /* Call a Python function object's invoke method. */
62 fnpy_call (struct gdbarch
*gdbarch
, const struct language_defn
*language
,
63 void *cookie
, int argc
, struct value
**argv
)
65 /* The gdbpy_enter object needs to be placed first, so that it's the last to
67 gdbpy_enter
enter_py (gdbarch
, language
);
70 gdbpy_ref
<> args
= convert_values_to_python (argc
, argv
);
72 /* convert_values_to_python can return NULL on error. If we
73 encounter this, do not call the function, but allow the Python ->
74 error code conversion below to deal with the Python exception.
75 Note, that this is different if the function simply does not
80 gdbpy_ref
<> callable (PyObject_GetAttrString ((PyObject
*) cookie
,
83 error (_("No method named 'invoke' in object."));
85 result
.reset (PyObject_Call (callable
.get (), args
.get (), NULL
));
89 gdbpy_handle_exception ();
91 value
= convert_value_from_python (result
.get ());
95 error (_("Error while executing Python code."));
101 /* Initializer for a Function object. It takes one argument, the name
105 fnpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
108 gdb::unique_xmalloc_ptr
<char> docstring
;
110 if (! PyArg_ParseTuple (args
, "s", &name
))
114 if (PyObject_HasAttrString (self
, "__doc__"))
116 gdbpy_ref
<> ds_obj (PyObject_GetAttrString (self
, "__doc__"));
119 if (gdbpy_is_string (ds_obj
.get ()))
121 docstring
= python_string_to_host_string (ds_obj
.get ());
122 if (docstring
== NULL
)
131 docstring
.reset (xstrdup (_("This function is not documented.")));
133 add_internal_function (name
, docstring
.release (), fnpy_call
, self
);
137 /* Initialize internal function support. */
140 gdbpy_initialize_functions (void)
142 fnpy_object_type
.tp_new
= PyType_GenericNew
;
143 if (PyType_Ready (&fnpy_object_type
) < 0)
146 return gdb_pymodule_addobject (gdb_module
, "Function",
147 (PyObject
*) &fnpy_object_type
);
152 PyTypeObject fnpy_object_type
=
154 PyVarObject_HEAD_INIT (NULL
, 0)
155 "gdb.Function", /*tp_name*/
156 sizeof (PyObject
), /*tp_basicsize*/
165 0, /*tp_as_sequence*/
173 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
174 "GDB function object", /* tp_doc */
177 0, /* tp_richcompare */
178 0, /* tp_weaklistoffset */
186 0, /* tp_descr_get */
187 0, /* tp_descr_set */
188 0, /* tp_dictoffset */
189 fnpy_init
, /* tp_init */