1 /* Python interface to program spaces.
3 Copyright (C) 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"
23 #include "progspace.h"
31 /* The corresponding pspace. */
32 struct program_space
*pspace
;
34 /* The pretty-printer list of functions. */
38 static PyTypeObject pspace_object_type
;
40 static const struct program_space_data
*pspy_pspace_data_key
;
44 /* An Objfile method which returns the objfile's file name, or None. */
47 pspy_get_filename (PyObject
*self
, void *closure
)
49 pspace_object
*obj
= (pspace_object
*) self
;
53 struct objfile
*objfile
= obj
->pspace
->symfile_object_file
;
55 if (objfile
&& objfile
->name
)
56 return PyString_Decode (objfile
->name
, strlen (objfile
->name
),
57 host_charset (), NULL
);
63 pspy_dealloc (PyObject
*self
)
65 pspace_object
*ps_self
= (pspace_object
*) self
;
67 Py_XDECREF (ps_self
->printers
);
68 self
->ob_type
->tp_free (self
);
72 pspy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
74 pspace_object
*self
= (pspace_object
*) type
->tp_alloc (type
, 0);
80 self
->printers
= PyList_New (0);
87 return (PyObject
*) self
;
91 pspy_get_printers (PyObject
*o
, void *ignore
)
93 pspace_object
*self
= (pspace_object
*) o
;
95 Py_INCREF (self
->printers
);
96 return self
->printers
;
100 pspy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
103 pspace_object
*self
= (pspace_object
*) o
;
107 PyErr_SetString (PyExc_TypeError
,
108 "cannot delete the pretty_printers attribute");
112 if (! PyList_Check (value
))
114 PyErr_SetString (PyExc_TypeError
,
115 "the pretty_printers attribute must be a list");
119 /* Take care in case the LHS and RHS are related somehow. */
120 tmp
= self
->printers
;
122 self
->printers
= value
;
130 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
133 py_free_pspace (struct program_space
*pspace
, void *datum
)
135 struct cleanup
*cleanup
;
136 pspace_object
*object
= datum
;
137 /* FIXME: What's the right way to get a program space's arch?
138 There may be multiple. */
139 struct gdbarch
*arch
= get_objfile_arch (pspace
->symfile_object_file
);
141 cleanup
= ensure_python_env (arch
, current_language
);
142 object
->pspace
= NULL
;
143 Py_DECREF ((PyObject
*) object
);
144 do_cleanups (cleanup
);
147 /* Return a borrowed reference to the Python object of type Pspace
148 representing PSPACE. If the object has already been created,
149 return it. Otherwise, create it. Return NULL and set the Python
153 pspace_to_pspace_object (struct program_space
*pspace
)
155 pspace_object
*object
;
157 object
= program_space_data (pspace
, pspy_pspace_data_key
);
160 object
= PyObject_New (pspace_object
, &pspace_object_type
);
163 object
->pspace
= pspace
;
165 object
->printers
= PyList_New (0);
166 if (!object
->printers
)
172 set_program_space_data (pspace
, pspy_pspace_data_key
, object
);
176 return (PyObject
*) object
;
180 gdbpy_initialize_pspace (void)
183 = register_program_space_data_with_cleanup (py_free_pspace
);
185 if (PyType_Ready (&pspace_object_type
) < 0)
188 Py_INCREF (&pspace_object_type
);
189 PyModule_AddObject (gdb_module
, "Progspace", (PyObject
*) &pspace_object_type
);
194 static PyGetSetDef pspace_getset
[] =
196 { "filename", pspy_get_filename
, NULL
,
197 "The progspace's main filename, or None.", NULL
},
198 { "pretty_printers", pspy_get_printers
, pspy_set_printers
,
199 "Pretty printers.", NULL
},
203 static PyTypeObject pspace_object_type
=
205 PyObject_HEAD_INIT (NULL
)
207 "gdb.Progspace", /*tp_name*/
208 sizeof (pspace_object
), /*tp_basicsize*/
210 pspy_dealloc
, /*tp_dealloc*/
217 0, /*tp_as_sequence*/
225 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
226 "GDB progspace object", /* tp_doc */
229 0, /* tp_richcompare */
230 0, /* tp_weaklistoffset */
235 pspace_getset
, /* tp_getset */
238 0, /* tp_descr_get */
239 0, /* tp_descr_set */
240 0, /* tp_dictoffset */
243 pspy_new
, /* tp_new */