]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/py-objfile.c
Add support for Python 3.
[thirdparty/binutils-gdb.git] / gdb / python / py-objfile.c
1 /* Python interface to objfiles.
2
3 Copyright (C) 2008-2012 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 #include "defs.h"
21 #include "python-internal.h"
22 #include "charset.h"
23 #include "objfiles.h"
24 #include "language.h"
25
26 typedef struct
27 {
28 PyObject_HEAD
29
30 /* The corresponding objfile. */
31 struct objfile *objfile;
32
33 /* The pretty-printer list of functions. */
34 PyObject *printers;
35
36 /* The type-printer list. */
37 PyObject *type_printers;
38 } objfile_object;
39
40 static PyTypeObject objfile_object_type;
41
42 static const struct objfile_data *objfpy_objfile_data_key;
43
44 \f
45
46 /* An Objfile method which returns the objfile's file name, or None. */
47 static PyObject *
48 objfpy_get_filename (PyObject *self, void *closure)
49 {
50 objfile_object *obj = (objfile_object *) self;
51
52 if (obj->objfile)
53 return PyString_Decode (obj->objfile->name, strlen (obj->objfile->name),
54 host_charset (), NULL);
55 Py_RETURN_NONE;
56 }
57
58 static void
59 objfpy_dealloc (PyObject *o)
60 {
61 objfile_object *self = (objfile_object *) o;
62
63 Py_XDECREF (self->printers);
64 Py_XDECREF (self->type_printers);
65 Py_TYPE (self)->tp_free (self);
66 }
67
68 static PyObject *
69 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
70 {
71 objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
72
73 if (self)
74 {
75 self->objfile = NULL;
76
77 self->printers = PyList_New (0);
78 if (!self->printers)
79 {
80 Py_DECREF (self);
81 return NULL;
82 }
83
84 self->type_printers = PyList_New (0);
85 if (!self->type_printers)
86 {
87 Py_DECREF (self);
88 return NULL;
89 }
90 }
91 return (PyObject *) self;
92 }
93
94 PyObject *
95 objfpy_get_printers (PyObject *o, void *ignore)
96 {
97 objfile_object *self = (objfile_object *) o;
98
99 Py_INCREF (self->printers);
100 return self->printers;
101 }
102
103 static int
104 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
105 {
106 PyObject *tmp;
107 objfile_object *self = (objfile_object *) o;
108
109 if (! value)
110 {
111 PyErr_SetString (PyExc_TypeError,
112 _("Cannot delete the pretty_printers attribute."));
113 return -1;
114 }
115
116 if (! PyList_Check (value))
117 {
118 PyErr_SetString (PyExc_TypeError,
119 _("The pretty_printers attribute must be a list."));
120 return -1;
121 }
122
123 /* Take care in case the LHS and RHS are related somehow. */
124 tmp = self->printers;
125 Py_INCREF (value);
126 self->printers = value;
127 Py_XDECREF (tmp);
128
129 return 0;
130 }
131
132 /* Get the 'type_printers' attribute. */
133
134 static PyObject *
135 objfpy_get_type_printers (PyObject *o, void *ignore)
136 {
137 objfile_object *self = (objfile_object *) o;
138
139 Py_INCREF (self->type_printers);
140 return self->type_printers;
141 }
142
143 /* Set the 'type_printers' attribute. */
144
145 static int
146 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
147 {
148 PyObject *tmp;
149 objfile_object *self = (objfile_object *) o;
150
151 if (! value)
152 {
153 PyErr_SetString (PyExc_TypeError,
154 _("Cannot delete the type_printers attribute."));
155 return -1;
156 }
157
158 if (! PyList_Check (value))
159 {
160 PyErr_SetString (PyExc_TypeError,
161 _("The type_printers attribute must be a list."));
162 return -1;
163 }
164
165 /* Take care in case the LHS and RHS are related somehow. */
166 tmp = self->type_printers;
167 Py_INCREF (value);
168 self->type_printers = value;
169 Py_XDECREF (tmp);
170
171 return 0;
172 }
173
174 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
175 Returns True if this object file still exists in GDB. */
176
177 static PyObject *
178 objfpy_is_valid (PyObject *self, PyObject *args)
179 {
180 objfile_object *obj = (objfile_object *) self;
181
182 if (! obj->objfile)
183 Py_RETURN_FALSE;
184
185 Py_RETURN_TRUE;
186 }
187
188 \f
189
190 /* Clear the OBJFILE pointer in an Objfile object and remove the
191 reference. */
192 static void
193 py_free_objfile (struct objfile *objfile, void *datum)
194 {
195 struct cleanup *cleanup;
196 objfile_object *object = datum;
197
198 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
199 object->objfile = NULL;
200 Py_DECREF ((PyObject *) object);
201 do_cleanups (cleanup);
202 }
203
204 /* Return a borrowed reference to the Python object of type Objfile
205 representing OBJFILE. If the object has already been created,
206 return it. Otherwise, create it. Return NULL and set the Python
207 error on failure. */
208 PyObject *
209 objfile_to_objfile_object (struct objfile *objfile)
210 {
211 objfile_object *object;
212
213 object = objfile_data (objfile, objfpy_objfile_data_key);
214 if (!object)
215 {
216 object = PyObject_New (objfile_object, &objfile_object_type);
217 if (object)
218 {
219 object->objfile = objfile;
220
221 object->printers = PyList_New (0);
222 if (!object->printers)
223 {
224 Py_DECREF (object);
225 return NULL;
226 }
227
228 object->type_printers = PyList_New (0);
229 if (!object->type_printers)
230 {
231 Py_DECREF (object);
232 return NULL;
233 }
234
235 set_objfile_data (objfile, objfpy_objfile_data_key, object);
236 }
237 }
238
239 return (PyObject *) object;
240 }
241
242 void
243 gdbpy_initialize_objfile (void)
244 {
245 objfpy_objfile_data_key
246 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
247
248 if (PyType_Ready (&objfile_object_type) < 0)
249 return;
250
251 Py_INCREF (&objfile_object_type);
252 PyModule_AddObject (gdb_module, "Objfile",
253 (PyObject *) &objfile_object_type);
254 }
255
256 \f
257
258 static PyMethodDef objfile_object_methods[] =
259 {
260 { "is_valid", objfpy_is_valid, METH_NOARGS,
261 "is_valid () -> Boolean.\n\
262 Return true if this object file is valid, false if not." },
263
264 { NULL }
265 };
266
267 static PyGetSetDef objfile_getset[] =
268 {
269 { "filename", objfpy_get_filename, NULL,
270 "The objfile's filename, or None.", NULL },
271 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
272 "Pretty printers.", NULL },
273 { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
274 "Type printers.", NULL },
275 { NULL }
276 };
277
278 static PyTypeObject objfile_object_type =
279 {
280 PyVarObject_HEAD_INIT (NULL, 0)
281 "gdb.Objfile", /*tp_name*/
282 sizeof (objfile_object), /*tp_basicsize*/
283 0, /*tp_itemsize*/
284 objfpy_dealloc, /*tp_dealloc*/
285 0, /*tp_print*/
286 0, /*tp_getattr*/
287 0, /*tp_setattr*/
288 0, /*tp_compare*/
289 0, /*tp_repr*/
290 0, /*tp_as_number*/
291 0, /*tp_as_sequence*/
292 0, /*tp_as_mapping*/
293 0, /*tp_hash */
294 0, /*tp_call*/
295 0, /*tp_str*/
296 0, /*tp_getattro*/
297 0, /*tp_setattro*/
298 0, /*tp_as_buffer*/
299 Py_TPFLAGS_DEFAULT, /*tp_flags*/
300 "GDB objfile object", /* tp_doc */
301 0, /* tp_traverse */
302 0, /* tp_clear */
303 0, /* tp_richcompare */
304 0, /* tp_weaklistoffset */
305 0, /* tp_iter */
306 0, /* tp_iternext */
307 objfile_object_methods, /* tp_methods */
308 0, /* tp_members */
309 objfile_getset, /* tp_getset */
310 0, /* tp_base */
311 0, /* tp_dict */
312 0, /* tp_descr_get */
313 0, /* tp_descr_set */
314 0, /* tp_dictoffset */
315 0, /* tp_init */
316 0, /* tp_alloc */
317 objfpy_new, /* tp_new */
318 };