1 /* Python interface to objfiles.
3 Copyright (C) 2008, 2009 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"
29 /* The corresponding objfile. */
30 struct objfile
*objfile
;
32 /* The pretty-printer list of functions. */
36 static PyTypeObject objfile_object_type
;
38 static const struct objfile_data
*objfpy_objfile_data_key
;
42 /* An Objfile method which returns the objfile's file name, or None. */
44 objfpy_get_filename (PyObject
*self
, void *closure
)
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
);
54 objfpy_dealloc (PyObject
*o
)
56 objfile_object
*self
= (objfile_object
*) o
;
57 Py_XDECREF (self
->printers
);
58 self
->ob_type
->tp_free ((PyObject
*) self
);
62 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
64 objfile_object
*self
= (objfile_object
*) type
->tp_alloc (type
, 0);
69 self
->printers
= PyList_New (0);
76 return (PyObject
*) self
;
80 objfpy_get_printers (PyObject
*o
, void *ignore
)
82 objfile_object
*self
= (objfile_object
*) o
;
83 Py_INCREF (self
->printers
);
84 return self
->printers
;
88 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
91 objfile_object
*self
= (objfile_object
*) o
;
94 PyErr_SetString (PyExc_TypeError
,
95 "cannot delete the pretty_printers attribute");
99 if (! PyList_Check (value
))
101 PyErr_SetString (PyExc_TypeError
,
102 "the pretty_printers attribute must be a list");
106 /* Take care in case the LHS and RHS are related somehow. */
107 tmp
= self
->printers
;
109 self
->printers
= value
;
117 /* Clear the OBJFILE pointer in an Objfile object and remove the
120 clean_up_objfile (struct objfile
*objfile
, void *datum
)
122 PyGILState_STATE state
;
123 objfile_object
*object
= datum
;
125 state
= PyGILState_Ensure ();
126 object
->objfile
= NULL
;
127 Py_DECREF ((PyObject
*) object
);
128 PyGILState_Release (state
);
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
136 objfile_to_objfile_object (struct objfile
*objfile
)
138 objfile_object
*object
;
140 object
= objfile_data (objfile
, objfpy_objfile_data_key
);
143 object
= PyObject_New (objfile_object
, &objfile_object_type
);
148 object
->objfile
= objfile
;
150 object
->printers
= PyList_New (0);
151 if (!object
->printers
)
157 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
);
161 return (PyObject
*) object
;
165 gdbpy_initialize_objfile (void)
167 objfpy_objfile_data_key
168 = register_objfile_data_with_cleanup (clean_up_objfile
);
170 if (PyType_Ready (&objfile_object_type
) < 0)
173 Py_INCREF (&objfile_object_type
);
174 PyModule_AddObject (gdb_module
, "Objfile", (PyObject
*) &objfile_object_type
);
179 static PyGetSetDef objfile_getset
[] =
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
},
188 static PyTypeObject objfile_object_type
=
190 PyObject_HEAD_INIT (NULL
)
192 "gdb.Objfile", /*tp_name*/
193 sizeof (objfile_object
), /*tp_basicsize*/
195 objfpy_dealloc
, /*tp_dealloc*/
202 0, /*tp_as_sequence*/
210 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
211 "GDB objfile object", /* tp_doc */
214 0, /* tp_richcompare */
215 0, /* tp_weaklistoffset */
220 objfile_getset
, /* tp_getset */
223 0, /* tp_descr_get */
224 0, /* tp_descr_set */
225 0, /* tp_dictoffset */
228 objfpy_new
, /* tp_new */