1 /* Python interface to objfiles.
3 Copyright (C) 2008, 2009, 2010 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. */
37 static PyTypeObject objfile_object_type
;
39 static const struct objfile_data
*objfpy_objfile_data_key
;
43 /* An Objfile method which returns the objfile's file name, or None. */
45 objfpy_get_filename (PyObject
*self
, void *closure
)
47 objfile_object
*obj
= (objfile_object
*) self
;
48 if (obj
->objfile
&& obj
->objfile
->name
)
49 return PyString_Decode (obj
->objfile
->name
, strlen (obj
->objfile
->name
),
50 host_charset (), NULL
);
55 objfpy_dealloc (PyObject
*o
)
57 objfile_object
*self
= (objfile_object
*) o
;
58 Py_XDECREF (self
->printers
);
59 self
->ob_type
->tp_free ((PyObject
*) self
);
63 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
65 objfile_object
*self
= (objfile_object
*) type
->tp_alloc (type
, 0);
70 self
->printers
= PyList_New (0);
77 return (PyObject
*) self
;
81 objfpy_get_printers (PyObject
*o
, void *ignore
)
83 objfile_object
*self
= (objfile_object
*) o
;
84 Py_INCREF (self
->printers
);
85 return self
->printers
;
89 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
92 objfile_object
*self
= (objfile_object
*) o
;
95 PyErr_SetString (PyExc_TypeError
,
96 _("Cannot delete the pretty_printers attribute."));
100 if (! PyList_Check (value
))
102 PyErr_SetString (PyExc_TypeError
,
103 _("The pretty_printers attribute must be a list."));
107 /* Take care in case the LHS and RHS are related somehow. */
108 tmp
= self
->printers
;
110 self
->printers
= value
;
118 /* Clear the OBJFILE pointer in an Objfile object and remove the
121 py_free_objfile (struct objfile
*objfile
, void *datum
)
123 struct cleanup
*cleanup
;
124 objfile_object
*object
= datum
;
126 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
127 object
->objfile
= NULL
;
128 Py_DECREF ((PyObject
*) object
);
129 do_cleanups (cleanup
);
132 /* Return a borrowed reference to the Python object of type Objfile
133 representing OBJFILE. If the object has already been created,
134 return it. Otherwise, create it. Return NULL and set the Python
137 objfile_to_objfile_object (struct objfile
*objfile
)
139 objfile_object
*object
;
141 object
= objfile_data (objfile
, objfpy_objfile_data_key
);
144 object
= PyObject_New (objfile_object
, &objfile_object_type
);
147 object
->objfile
= objfile
;
149 object
->printers
= PyList_New (0);
150 if (!object
->printers
)
156 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
);
160 return (PyObject
*) object
;
164 gdbpy_initialize_objfile (void)
166 objfpy_objfile_data_key
167 = register_objfile_data_with_cleanup (NULL
, py_free_objfile
);
169 if (PyType_Ready (&objfile_object_type
) < 0)
172 Py_INCREF (&objfile_object_type
);
173 PyModule_AddObject (gdb_module
, "Objfile", (PyObject
*) &objfile_object_type
);
178 static PyGetSetDef objfile_getset
[] =
180 { "filename", objfpy_get_filename
, NULL
,
181 "The objfile's filename, or None.", NULL
},
182 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
183 "Pretty printers.", NULL
},
187 static PyTypeObject objfile_object_type
=
189 PyObject_HEAD_INIT (NULL
)
191 "gdb.Objfile", /*tp_name*/
192 sizeof (objfile_object
), /*tp_basicsize*/
194 objfpy_dealloc
, /*tp_dealloc*/
201 0, /*tp_as_sequence*/
209 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
210 "GDB objfile object", /* tp_doc */
213 0, /* tp_richcompare */
214 0, /* tp_weaklistoffset */
219 objfile_getset
, /* tp_getset */
222 0, /* tp_descr_get */
223 0, /* tp_descr_set */
224 0, /* tp_dictoffset */
227 objfpy_new
, /* tp_new */