RISC-V: Don't report warnings when linking different privileged spec objects.
[binutils-gdb.git] / gdb / python / py-function.c
blob2b65d8bc87d634672d20370f6288eb4a5f112d57
1 /* Convenience functions implemented in Python.
3 Copyright (C) 2008-2024 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/>. */
21 #include "value.h"
22 #include "python-internal.h"
23 #include "charset.h"
24 #include "cli/cli-cmds.h"
25 #include "cli/cli-decode.h"
26 #include "completer.h"
27 #include "expression.h"
28 #include "language.h"
30 extern PyTypeObject fnpy_object_type
31 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject");
35 /* Return a reference to a tuple ARGC elements long. Each element of the
36 tuple is a PyObject converted from the corresponding element of ARGV. */
38 static gdbpy_ref<>
39 convert_values_to_python (int argc, struct value **argv)
41 int i;
42 gdbpy_ref<> result (PyTuple_New (argc));
44 if (result == NULL)
45 return NULL;
47 for (i = 0; i < argc; ++i)
49 gdbpy_ref<> elt (value_to_value_object (argv[i]));
50 if (elt == NULL)
51 return NULL;
52 PyTuple_SetItem (result.get (), i, elt.release ());
54 return result;
57 /* Call a Python function object's invoke method. */
59 static struct value *
60 fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
61 void *cookie, int argc, struct value **argv)
63 /* The gdbpy_enter object needs to be placed first, so that it's the last to
64 be destroyed. */
65 gdbpy_enter enter_py (gdbarch, language);
66 struct value *value;
67 gdbpy_ref<> result;
68 gdbpy_ref<> args = convert_values_to_python (argc, argv);
70 /* convert_values_to_python can return NULL on error. If we
71 encounter this, do not call the function, but allow the Python ->
72 error code conversion below to deal with the Python exception.
73 Note, that this is different if the function simply does not
74 have arguments. */
76 if (args != NULL)
78 gdbpy_ref<> callable (PyObject_GetAttrString ((PyObject *) cookie,
79 "invoke"));
80 if (callable == NULL)
81 error (_("No method named 'invoke' in object."));
83 result.reset (PyObject_Call (callable.get (), args.get (), NULL));
86 if (result == NULL)
87 gdbpy_handle_exception ();
89 value = convert_value_from_python (result.get ());
90 if (value == NULL)
92 gdbpy_print_stack ();
93 error (_("Error while executing Python code."));
96 return value;
99 /* Initializer for a Function object. It takes one argument, the name
100 of the function. */
102 static int
103 fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
105 const char *name;
106 gdb::unique_xmalloc_ptr<char> docstring;
108 if (! PyArg_ParseTuple (args, "s", &name))
109 return -1;
111 gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self);
113 if (PyObject_HasAttrString (self, "__doc__"))
115 gdbpy_ref<> ds_obj (PyObject_GetAttrString (self, "__doc__"));
116 if (ds_obj != NULL)
118 if (gdbpy_is_string (ds_obj.get ()))
120 docstring = python_string_to_host_string (ds_obj.get ());
121 if (docstring == NULL)
122 return -1;
123 docstring
124 = gdbpy_fix_doc_string_indentation (std::move (docstring));
128 if (! docstring)
129 docstring.reset (xstrdup (_("This function is not documented.")));
131 add_internal_function (make_unique_xstrdup (name), std::move (docstring),
132 fnpy_call, self_ref.release ());
133 return 0;
136 /* Initialize internal function support. */
138 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
139 gdbpy_initialize_functions (void)
141 fnpy_object_type.tp_new = PyType_GenericNew;
142 return gdbpy_type_ready (&fnpy_object_type);
145 GDBPY_INITIALIZE_FILE (gdbpy_initialize_functions);
149 PyTypeObject fnpy_object_type =
151 PyVarObject_HEAD_INIT (NULL, 0)
152 "gdb.Function", /*tp_name*/
153 sizeof (PyObject), /*tp_basicsize*/
154 0, /*tp_itemsize*/
155 0, /*tp_dealloc*/
156 0, /*tp_print*/
157 0, /*tp_getattr*/
158 0, /*tp_setattr*/
159 0, /*tp_compare*/
160 0, /*tp_repr*/
161 0, /*tp_as_number*/
162 0, /*tp_as_sequence*/
163 0, /*tp_as_mapping*/
164 0, /*tp_hash */
165 0, /*tp_call*/
166 0, /*tp_str*/
167 0, /*tp_getattro*/
168 0, /*tp_setattro*/
169 0, /*tp_as_buffer*/
170 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
171 "GDB function object", /* tp_doc */
172 0, /* tp_traverse */
173 0, /* tp_clear */
174 0, /* tp_richcompare */
175 0, /* tp_weaklistoffset */
176 0, /* tp_iter */
177 0, /* tp_iternext */
178 0, /* tp_methods */
179 0, /* tp_members */
180 0, /* tp_getset */
181 0, /* tp_base */
182 0, /* tp_dict */
183 0, /* tp_descr_get */
184 0, /* tp_descr_set */
185 0, /* tp_dictoffset */
186 fnpy_init, /* tp_init */
187 0, /* tp_alloc */