Remove commented-out code in jv-lang.c
[binutils-gdb.git] / gdb / python / py-function.c
1 /* Convenience functions implemented in Python.
2
3 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
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"
23 #include "exceptions.h"
24 #include "python-internal.h"
25 #include "charset.h"
26 #include "gdbcmd.h"
27 #include "cli/cli-decode.h"
28 #include "completer.h"
29 #include "expression.h"
30 #include "language.h"
31
32 static PyTypeObject fnpy_object_type;
33
34 \f
35
36 static PyObject *
37 convert_values_to_python (int argc, struct value **argv)
38 {
39 int i;
40 PyObject *result = PyTuple_New (argc);
41 for (i = 0; i < argc; ++i)
42 {
43 PyObject *elt = value_to_value_object (argv[i]);
44 if (! elt)
45 {
46 Py_DECREF (result);
47 error (_("Could not convert value to Python object."));
48 }
49 PyTuple_SetItem (result, i, elt);
50 }
51 return result;
52 }
53
54 /* Call a Python function object's invoke method. */
55
56 static struct value *
57 fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
58 void *cookie, int argc, struct value **argv)
59 {
60 struct value *value = NULL;
61 PyObject *result, *callable, *args;
62 struct cleanup *cleanup;
63
64 cleanup = ensure_python_env (gdbarch, language);
65
66 args = convert_values_to_python (argc, argv);
67
68 callable = PyObject_GetAttrString ((PyObject *) cookie, "invoke");
69 if (! callable)
70 {
71 Py_DECREF (args);
72 error (_("No method named 'invoke' in object."));
73 }
74
75 result = PyObject_Call (callable, args, NULL);
76 Py_DECREF (callable);
77 Py_DECREF (args);
78
79 if (!result)
80 {
81 gdbpy_print_stack ();
82 error (_("Error while executing Python code."));
83 }
84
85 value = convert_value_from_python (result);
86 if (value == NULL)
87 {
88 Py_DECREF (result);
89 gdbpy_print_stack ();
90 error (_("Error while executing Python code."));
91 }
92
93 Py_DECREF (result);
94 do_cleanups (cleanup);
95
96 return value;
97 }
98
99 /* Initializer for a Function object. It takes one argument, the name
100 of the function. */
101
102 static int
103 fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
104 {
105 char *name, *docstring = NULL;
106 if (! PyArg_ParseTuple (args, "s", &name))
107 return -1;
108 Py_INCREF (self);
109
110 if (PyObject_HasAttrString (self, "__doc__"))
111 {
112 PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
113 if (ds_obj && gdbpy_is_string (ds_obj))
114 docstring = python_string_to_host_string (ds_obj);
115 }
116 if (! docstring)
117 docstring = xstrdup (_("This function is not documented."));
118
119 add_internal_function (name, docstring, fnpy_call, self);
120 return 0;
121 }
122
123 /* Initialize internal function support. */
124
125 void
126 gdbpy_initialize_functions (void)
127 {
128 if (PyType_Ready (&fnpy_object_type) < 0)
129 return;
130
131 Py_INCREF (&fnpy_object_type);
132 PyModule_AddObject (gdb_module, "Function", (PyObject *) &fnpy_object_type);
133 }
134
135 \f
136
137 static PyTypeObject fnpy_object_type =
138 {
139 PyObject_HEAD_INIT (NULL)
140 0, /*ob_size*/
141 "gdb.Function", /*tp_name*/
142 sizeof (PyObject), /*tp_basicsize*/
143 0, /*tp_itemsize*/
144 0, /*tp_dealloc*/
145 0, /*tp_print*/
146 0, /*tp_getattr*/
147 0, /*tp_setattr*/
148 0, /*tp_compare*/
149 0, /*tp_repr*/
150 0, /*tp_as_number*/
151 0, /*tp_as_sequence*/
152 0, /*tp_as_mapping*/
153 0, /*tp_hash */
154 0, /*tp_call*/
155 0, /*tp_str*/
156 0, /*tp_getattro*/
157 0, /*tp_setattro*/
158 0, /*tp_as_buffer*/
159 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
160 "GDB function object", /* tp_doc */
161 0, /* tp_traverse */
162 0, /* tp_clear */
163 0, /* tp_richcompare */
164 0, /* tp_weaklistoffset */
165 0, /* tp_iter */
166 0, /* tp_iternext */
167 0, /* tp_methods */
168 0, /* tp_members */
169 0, /* tp_getset */
170 0, /* tp_base */
171 0, /* tp_dict */
172 0, /* tp_descr_get */
173 0, /* tp_descr_set */
174 0, /* tp_dictoffset */
175 fnpy_init, /* tp_init */
176 0, /* tp_alloc */
177 PyType_GenericNew /* tp_new */
178 };