1 /* Python interface to objfiles.
3 Copyright (C) 2008-2013 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 "python-internal.h"
30 /* The corresponding objfile. */
31 struct objfile *objfile;
33 /* The pretty-printer list of functions. */
36 /* The type-printer list. */
37 PyObject *type_printers;
40 static PyTypeObject objfile_object_type;
42 static const struct objfile_data *objfpy_objfile_data_key;
46 /* An Objfile method which returns the objfile's file name, or None. */
48 objfpy_get_filename (PyObject *self, void *closure)
50 objfile_object *obj = (objfile_object *) self;
53 return PyString_Decode (obj->objfile->name, strlen (obj->objfile->name),
54 host_charset (), NULL);
59 objfpy_dealloc (PyObject *o)
61 objfile_object *self = (objfile_object *) o;
63 Py_XDECREF (self->printers);
64 Py_XDECREF (self->type_printers);
65 Py_TYPE (self)->tp_free (self);
69 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
71 objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
77 self->printers = PyList_New (0);
84 self->type_printers = PyList_New (0);
85 if (!self->type_printers)
91 return (PyObject *) self;
95 objfpy_get_printers (PyObject *o, void *ignore)
97 objfile_object *self = (objfile_object *) o;
99 Py_INCREF (self->printers);
100 return self->printers;
104 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
107 objfile_object *self = (objfile_object *) o;
111 PyErr_SetString (PyExc_TypeError,
112 _("Cannot delete the pretty_printers attribute."));
116 if (! PyList_Check (value))
118 PyErr_SetString (PyExc_TypeError,
119 _("The pretty_printers attribute must be a list."));
123 /* Take care in case the LHS and RHS are related somehow. */
124 tmp = self->printers;
126 self->printers = value;
132 /* Get the 'type_printers' attribute. */
135 objfpy_get_type_printers (PyObject *o, void *ignore)
137 objfile_object *self = (objfile_object *) o;
139 Py_INCREF (self->type_printers);
140 return self->type_printers;
143 /* Set the 'type_printers' attribute. */
146 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
149 objfile_object *self = (objfile_object *) o;
153 PyErr_SetString (PyExc_TypeError,
154 _("Cannot delete the type_printers attribute."));
158 if (! PyList_Check (value))
160 PyErr_SetString (PyExc_TypeError,
161 _("The type_printers attribute must be a list."));
165 /* Take care in case the LHS and RHS are related somehow. */
166 tmp = self->type_printers;
168 self->type_printers = value;
174 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
175 Returns True if this object file still exists in GDB. */
178 objfpy_is_valid (PyObject *self, PyObject *args)
180 objfile_object *obj = (objfile_object *) self;
190 /* Clear the OBJFILE pointer in an Objfile object and remove the
193 py_free_objfile (struct objfile *objfile, void *datum)
195 struct cleanup *cleanup;
196 objfile_object *object = datum;
198 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
199 object->objfile = NULL;
200 Py_DECREF ((PyObject *) object);
201 do_cleanups (cleanup);
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
209 objfile_to_objfile_object (struct objfile *objfile)
211 objfile_object *object;
213 object = objfile_data (objfile, objfpy_objfile_data_key);
216 object = PyObject_New (objfile_object, &objfile_object_type);
219 object->objfile = objfile;
221 object->printers = PyList_New (0);
222 if (!object->printers)
228 object->type_printers = PyList_New (0);
229 if (!object->type_printers)
235 set_objfile_data (objfile, objfpy_objfile_data_key, object);
239 return (PyObject *) object;
243 gdbpy_initialize_objfile (void)
245 objfpy_objfile_data_key
246 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
248 if (PyType_Ready (&objfile_object_type) < 0)
251 Py_INCREF (&objfile_object_type);
252 PyModule_AddObject (gdb_module, "Objfile",
253 (PyObject *) &objfile_object_type);
258 static PyMethodDef objfile_object_methods[] =
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." },
267 static PyGetSetDef objfile_getset[] =
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 },
278 static PyTypeObject objfile_object_type =
280 PyVarObject_HEAD_INIT (NULL, 0)
281 "gdb.Objfile", /*tp_name*/
282 sizeof (objfile_object), /*tp_basicsize*/
284 objfpy_dealloc, /*tp_dealloc*/
291 0, /*tp_as_sequence*/
299 Py_TPFLAGS_DEFAULT, /*tp_flags*/
300 "GDB objfile object", /* tp_doc */
303 0, /* tp_richcompare */
304 0, /* tp_weaklistoffset */
307 objfile_object_methods, /* tp_methods */
309 objfile_getset, /* tp_getset */
312 0, /* tp_descr_get */
313 0, /* tp_descr_set */
314 0, /* tp_dictoffset */
317 objfpy_new, /* tp_new */