2011-01-05 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / python / py-objfile.c
CommitLineData
89c73ade
TT
1/* Python interface to objfiles.
2
7b6bb8da 3 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
89c73ade
TT
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"
d452c4bc 24#include "language.h"
89c73ade
TT
25
26typedef 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
37static PyTypeObject objfile_object_type;
38
39static 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. */
44static PyObject *
45objfpy_get_filename (PyObject *self, void *closure)
46{
47 objfile_object *obj = (objfile_object *) self;
d59b6f6c 48
d31d2fc3 49 if (obj->objfile)
89c73ade
TT
50 return PyString_Decode (obj->objfile->name, strlen (obj->objfile->name),
51 host_charset (), NULL);
52 Py_RETURN_NONE;
53}
54
55static void
56objfpy_dealloc (PyObject *o)
57{
58 objfile_object *self = (objfile_object *) o;
d59b6f6c 59
89c73ade
TT
60 Py_XDECREF (self->printers);
61 self->ob_type->tp_free ((PyObject *) self);
62}
63
64static PyObject *
65objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
66{
67 objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
d59b6f6c 68
89c73ade
TT
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
83PyObject *
84objfpy_get_printers (PyObject *o, void *ignore)
85{
86 objfile_object *self = (objfile_object *) o;
d59b6f6c 87
89c73ade
TT
88 Py_INCREF (self->printers);
89 return self->printers;
90}
91
92static int
93objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
94{
95 PyObject *tmp;
96 objfile_object *self = (objfile_object *) o;
d59b6f6c 97
89c73ade
TT
98 if (! value)
99 {
100 PyErr_SetString (PyExc_TypeError,
044c0f87 101 _("Cannot delete the pretty_printers attribute."));
89c73ade
TT
102 return -1;
103 }
104
105 if (! PyList_Check (value))
106 {
107 PyErr_SetString (PyExc_TypeError,
044c0f87 108 _("The pretty_printers attribute must be a list."));
89c73ade
TT
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\f
122
123/* Clear the OBJFILE pointer in an Objfile object and remove the
124 reference. */
125static void
c1bd65d0 126py_free_objfile (struct objfile *objfile, void *datum)
89c73ade 127{
d452c4bc 128 struct cleanup *cleanup;
89c73ade
TT
129 objfile_object *object = datum;
130
d452c4bc 131 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
89c73ade
TT
132 object->objfile = NULL;
133 Py_DECREF ((PyObject *) object);
d452c4bc 134 do_cleanups (cleanup);
89c73ade
TT
135}
136
137/* Return a borrowed reference to the Python object of type Objfile
138 representing OBJFILE. If the object has already been created,
139 return it. Otherwise, create it. Return NULL and set the Python
140 error on failure. */
141PyObject *
142objfile_to_objfile_object (struct objfile *objfile)
143{
144 objfile_object *object;
145
146 object = objfile_data (objfile, objfpy_objfile_data_key);
147 if (!object)
148 {
149 object = PyObject_New (objfile_object, &objfile_object_type);
150 if (object)
151 {
89c73ade
TT
152 object->objfile = objfile;
153
154 object->printers = PyList_New (0);
155 if (!object->printers)
156 {
157 Py_DECREF (object);
158 return NULL;
159 }
160
161 set_objfile_data (objfile, objfpy_objfile_data_key, object);
162 }
163 }
164
165 return (PyObject *) object;
166}
167
168void
169gdbpy_initialize_objfile (void)
170{
171 objfpy_objfile_data_key
c1bd65d0 172 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
89c73ade
TT
173
174 if (PyType_Ready (&objfile_object_type) < 0)
175 return;
176
177 Py_INCREF (&objfile_object_type);
9a2b4c1b
MS
178 PyModule_AddObject (gdb_module, "Objfile",
179 (PyObject *) &objfile_object_type);
89c73ade
TT
180}
181
182\f
183
184static PyGetSetDef objfile_getset[] =
185{
186 { "filename", objfpy_get_filename, NULL,
187 "The objfile's filename, or None.", NULL },
188 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
189 "Pretty printers.", NULL },
190 { NULL }
191};
192
193static PyTypeObject objfile_object_type =
194{
195 PyObject_HEAD_INIT (NULL)
196 0, /*ob_size*/
197 "gdb.Objfile", /*tp_name*/
198 sizeof (objfile_object), /*tp_basicsize*/
199 0, /*tp_itemsize*/
200 objfpy_dealloc, /*tp_dealloc*/
201 0, /*tp_print*/
202 0, /*tp_getattr*/
203 0, /*tp_setattr*/
204 0, /*tp_compare*/
205 0, /*tp_repr*/
206 0, /*tp_as_number*/
207 0, /*tp_as_sequence*/
208 0, /*tp_as_mapping*/
209 0, /*tp_hash */
210 0, /*tp_call*/
211 0, /*tp_str*/
212 0, /*tp_getattro*/
213 0, /*tp_setattro*/
214 0, /*tp_as_buffer*/
215 Py_TPFLAGS_DEFAULT, /*tp_flags*/
216 "GDB objfile object", /* tp_doc */
217 0, /* tp_traverse */
218 0, /* tp_clear */
219 0, /* tp_richcompare */
220 0, /* tp_weaklistoffset */
221 0, /* tp_iter */
222 0, /* tp_iternext */
223 0, /* tp_methods */
224 0, /* tp_members */
225 objfile_getset, /* tp_getset */
226 0, /* tp_base */
227 0, /* tp_dict */
228 0, /* tp_descr_get */
229 0, /* tp_descr_set */
230 0, /* tp_dictoffset */
231 0, /* tp_init */
232 0, /* tp_alloc */
233 objfpy_new, /* tp_new */
234};
This page took 0.20394 seconds and 4 git commands to generate.