]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-function.c
Remove more uses of explicit reference counting in Python
[thirdparty/binutils-gdb.git] / gdb / python / py-function.c
CommitLineData
bc3b79fd
TJB
1/* Convenience functions implemented in Python.
2
42a4f53d 3 Copyright (C) 2008-2019 Free Software Foundation, Inc.
bc3b79fd
TJB
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
21#include "defs.h"
22#include "value.h"
bc3b79fd
TJB
23#include "python-internal.h"
24#include "charset.h"
25#include "gdbcmd.h"
26#include "cli/cli-decode.h"
27#include "completer.h"
28#include "expression.h"
d452c4bc 29#include "language.h"
80bd970a 30#include "py-ref.h"
bc3b79fd 31
e36122e9 32extern PyTypeObject fnpy_object_type
62eec1a5 33 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject");
bc3b79fd
TJB
34
35\f
36
b352ceb6
AB
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. */
39
40static gdbpy_ref<>
bc3b79fd
TJB
41convert_values_to_python (int argc, struct value **argv)
42{
43 int i;
7780f186 44 gdbpy_ref<> result (PyTuple_New (argc));
256458bc 45
80bd970a 46 if (result == NULL)
f77b9a5d 47 return NULL;
d59b6f6c 48
bc3b79fd
TJB
49 for (i = 0; i < argc; ++i)
50 {
7780f186 51 gdbpy_ref<> elt (value_to_value_object (argv[i]));
80bd970a
TT
52 if (elt == NULL)
53 return NULL;
54 PyTuple_SetItem (result.get (), i, elt.release ());
bc3b79fd 55 }
b352ceb6 56 return result;
bc3b79fd
TJB
57}
58
59/* Call a Python function object's invoke method. */
60
61static struct value *
d452c4bc
UW
62fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
63 void *cookie, int argc, struct value **argv)
bc3b79fd 64{
b1ce6568
SM
65 /* The gdbpy_enter object needs to be placed first, so that it's the last to
66 be destroyed. */
0e9dcc75 67 gdbpy_enter enter_py (gdbarch, language);
b1ce6568 68 struct value *value;
7780f186 69 gdbpy_ref<> result;
b352ceb6 70 gdbpy_ref<> args = convert_values_to_python (argc, argv);
b1ce6568 71
f77b9a5d
PM
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
76 have arguments. */
bc3b79fd 77
0e9dcc75 78 if (args != NULL)
bc3b79fd 79 {
7780f186
TT
80 gdbpy_ref<> callable (PyObject_GetAttrString ((PyObject *) cookie,
81 "invoke"));
0e9dcc75
TT
82 if (callable == NULL)
83 error (_("No method named 'invoke' in object."));
f77b9a5d 84
0e9dcc75 85 result.reset (PyObject_Call (callable.get (), args.get (), NULL));
bc3b79fd
TJB
86 }
87
0e9dcc75 88 if (result == NULL)
2b4ad2fe 89 gdbpy_handle_exception ();
bc3b79fd 90
0e9dcc75 91 value = convert_value_from_python (result.get ());
bc3b79fd
TJB
92 if (value == NULL)
93 {
bc3b79fd
TJB
94 gdbpy_print_stack ();
95 error (_("Error while executing Python code."));
96 }
97
bc3b79fd
TJB
98 return value;
99}
100
101/* Initializer for a Function object. It takes one argument, the name
102 of the function. */
103
104static int
105fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
106{
ddd49eee 107 const char *name;
9b972014 108 gdb::unique_xmalloc_ptr<char> docstring;
d59b6f6c 109
bc3b79fd
TJB
110 if (! PyArg_ParseTuple (args, "s", &name))
111 return -1;
2a3c71d6
TT
112
113 gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self);
bc3b79fd
TJB
114
115 if (PyObject_HasAttrString (self, "__doc__"))
116 {
7780f186 117 gdbpy_ref<> ds_obj (PyObject_GetAttrString (self, "__doc__"));
764123e4 118 if (ds_obj != NULL)
8dc78533 119 {
80bd970a 120 if (gdbpy_is_string (ds_obj.get ()))
8dc78533 121 {
80bd970a 122 docstring = python_string_to_host_string (ds_obj.get ());
764123e4 123 if (docstring == NULL)
2a3c71d6 124 return -1;
8dc78533
JK
125 }
126 }
bc3b79fd
TJB
127 }
128 if (! docstring)
9b972014 129 docstring.reset (xstrdup (_("This function is not documented.")));
bc3b79fd 130
2a3c71d6
TT
131 add_internal_function (name, docstring.release (), fnpy_call,
132 self_ref.release ());
bc3b79fd
TJB
133 return 0;
134}
135
136/* Initialize internal function support. */
137
999633ed 138int
bc3b79fd
TJB
139gdbpy_initialize_functions (void)
140{
6a1b1664 141 fnpy_object_type.tp_new = PyType_GenericNew;
bc3b79fd 142 if (PyType_Ready (&fnpy_object_type) < 0)
999633ed 143 return -1;
bc3b79fd 144
aa36459a
TT
145 return gdb_pymodule_addobject (gdb_module, "Function",
146 (PyObject *) &fnpy_object_type);
bc3b79fd
TJB
147}
148
149\f
150
e36122e9 151PyTypeObject fnpy_object_type =
bc3b79fd 152{
9a27f2c6 153 PyVarObject_HEAD_INIT (NULL, 0)
bc3b79fd
TJB
154 "gdb.Function", /*tp_name*/
155 sizeof (PyObject), /*tp_basicsize*/
156 0, /*tp_itemsize*/
157 0, /*tp_dealloc*/
158 0, /*tp_print*/
159 0, /*tp_getattr*/
160 0, /*tp_setattr*/
161 0, /*tp_compare*/
162 0, /*tp_repr*/
163 0, /*tp_as_number*/
164 0, /*tp_as_sequence*/
165 0, /*tp_as_mapping*/
166 0, /*tp_hash */
167 0, /*tp_call*/
168 0, /*tp_str*/
169 0, /*tp_getattro*/
170 0, /*tp_setattro*/
171 0, /*tp_as_buffer*/
172 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
173 "GDB function object", /* tp_doc */
174 0, /* tp_traverse */
175 0, /* tp_clear */
176 0, /* tp_richcompare */
177 0, /* tp_weaklistoffset */
178 0, /* tp_iter */
179 0, /* tp_iternext */
180 0, /* tp_methods */
181 0, /* tp_members */
182 0, /* tp_getset */
183 0, /* tp_base */
184 0, /* tp_dict */
185 0, /* tp_descr_get */
186 0, /* tp_descr_set */
187 0, /* tp_dictoffset */
188 fnpy_init, /* tp_init */
189 0, /* tp_alloc */
bc3b79fd 190};