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