daily update
[deliverable/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 } objfile_object;
36
37 static PyTypeObject objfile_object_type;
38
39 static const struct objfile_data *objfpy_objfile_data_key;
40
41 \f
42
43 /* An Objfile method which returns the objfile's file name, or None. */
44 static PyObject *
45 objfpy_get_filename (PyObject *self, void *closure)
46 {
47 objfile_object *obj = (objfile_object *) self;
48
49 if (obj->objfile)
50 return PyString_Decode (obj->objfile->name, strlen (obj->objfile->name),
51 host_charset (), NULL);
52 Py_RETURN_NONE;
53 }
54
55 static void
56 objfpy_dealloc (PyObject *o)
57 {
58 objfile_object *self = (objfile_object *) o;
59
60 Py_XDECREF (self->printers);
61 self->ob_type->tp_free ((PyObject *) self);
62 }
63
64 static PyObject *
65 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
66 {
67 objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
68
69 if (self)
70 {
71 self->objfile = NULL;
72
73 self->printers = PyList_New (0);
74 if (!self->printers)
75 {
76 Py_DECREF (self);
77 return NULL;
78 }
79 }
80 return (PyObject *) self;
81 }
82
83 PyObject *
84 objfpy_get_printers (PyObject *o, void *ignore)
85 {
86 objfile_object *self = (objfile_object *) o;
87
88 Py_INCREF (self->printers);
89 return self->printers;
90 }
91
92 static int
93 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
94 {
95 PyObject *tmp;
96 objfile_object *self = (objfile_object *) o;
97
98 if (! value)
99 {
100 PyErr_SetString (PyExc_TypeError,
101 _("Cannot delete the pretty_printers attribute."));
102 return -1;
103 }
104
105 if (! PyList_Check (value))
106 {
107 PyErr_SetString (PyExc_TypeError,
108 _("The pretty_printers attribute must be a list."));
109 return -1;
110 }
111
112 /* Take care in case the LHS and RHS are related somehow. */
113 tmp = self->printers;
114 Py_INCREF (value);
115 self->printers = value;
116 Py_XDECREF (tmp);
117
118 return 0;
119 }
120
121 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
122 Returns True if this object file still exists in GDB. */
123
124 static PyObject *
125 objfpy_is_valid (PyObject *self, PyObject *args)
126 {
127 objfile_object *obj = (objfile_object *) self;
128
129 if (! obj->objfile)
130 Py_RETURN_FALSE;
131
132 Py_RETURN_TRUE;
133 }
134
135 \f
136
137 /* Clear the OBJFILE pointer in an Objfile object and remove the
138 reference. */
139 static void
140 py_free_objfile (struct objfile *objfile, void *datum)
141 {
142 struct cleanup *cleanup;
143 objfile_object *object = datum;
144
145 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
146 object->objfile = NULL;
147 Py_DECREF ((PyObject *) object);
148 do_cleanups (cleanup);
149 }
150
151 /* Return a borrowed reference to the Python object of type Objfile
152 representing OBJFILE. If the object has already been created,
153 return it. Otherwise, create it. Return NULL and set the Python
154 error on failure. */
155 PyObject *
156 objfile_to_objfile_object (struct objfile *objfile)
157 {
158 objfile_object *object;
159
160 object = objfile_data (objfile, objfpy_objfile_data_key);
161 if (!object)
162 {
163 object = PyObject_New (objfile_object, &objfile_object_type);
164 if (object)
165 {
166 object->objfile = objfile;
167
168 object->printers = PyList_New (0);
169 if (!object->printers)
170 {
171 Py_DECREF (object);
172 return NULL;
173 }
174
175 set_objfile_data (objfile, objfpy_objfile_data_key, object);
176 }
177 }
178
179 return (PyObject *) object;
180 }
181
182 void
183 gdbpy_initialize_objfile (void)
184 {
185 objfpy_objfile_data_key
186 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
187
188 if (PyType_Ready (&objfile_object_type) < 0)
189 return;
190
191 Py_INCREF (&objfile_object_type);
192 PyModule_AddObject (gdb_module, "Objfile",
193 (PyObject *) &objfile_object_type);
194 }
195
196 \f
197
198 static PyMethodDef objfile_object_methods[] =
199 {
200 { "is_valid", objfpy_is_valid, METH_NOARGS,
201 "is_valid () -> Boolean.\n\
202 Return true if this object file is valid, false if not." },
203
204 { NULL }
205 };
206
207 static PyGetSetDef objfile_getset[] =
208 {
209 { "filename", objfpy_get_filename, NULL,
210 "The objfile's filename, or None.", NULL },
211 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
212 "Pretty printers.", NULL },
213 { NULL }
214 };
215
216 static PyTypeObject objfile_object_type =
217 {
218 PyObject_HEAD_INIT (NULL)
219 0, /*ob_size*/
220 "gdb.Objfile", /*tp_name*/
221 sizeof (objfile_object), /*tp_basicsize*/
222 0, /*tp_itemsize*/
223 objfpy_dealloc, /*tp_dealloc*/
224 0, /*tp_print*/
225 0, /*tp_getattr*/
226 0, /*tp_setattr*/
227 0, /*tp_compare*/
228 0, /*tp_repr*/
229 0, /*tp_as_number*/
230 0, /*tp_as_sequence*/
231 0, /*tp_as_mapping*/
232 0, /*tp_hash */
233 0, /*tp_call*/
234 0, /*tp_str*/
235 0, /*tp_getattro*/
236 0, /*tp_setattro*/
237 0, /*tp_as_buffer*/
238 Py_TPFLAGS_DEFAULT, /*tp_flags*/
239 "GDB objfile object", /* tp_doc */
240 0, /* tp_traverse */
241 0, /* tp_clear */
242 0, /* tp_richcompare */
243 0, /* tp_weaklistoffset */
244 0, /* tp_iter */
245 0, /* tp_iternext */
246 objfile_object_methods, /* tp_methods */
247 0, /* tp_members */
248 objfile_getset, /* tp_getset */
249 0, /* tp_base */
250 0, /* tp_dict */
251 0, /* tp_descr_get */
252 0, /* tp_descr_set */
253 0, /* tp_dictoffset */
254 0, /* tp_init */
255 0, /* tp_alloc */
256 objfpy_new, /* tp_new */
257 };
This page took 0.036105 seconds and 4 git commands to generate.