GDB (API)
/home/stan/gdb/src/gdb/python/py-event.c
Go to the documentation of this file.
00001 /* Python interface to inferior events.
00002 
00003    Copyright (C) 2009-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "py-event.h"
00022 
00023 void
00024 evpy_dealloc (PyObject *self)
00025 {
00026   Py_XDECREF (((event_object *) self)->dict);
00027   Py_TYPE (self)->tp_free (self);
00028 }
00029 
00030 PyObject *
00031 create_event_object (PyTypeObject *py_type)
00032 {
00033   event_object *event_obj;
00034 
00035   event_obj = PyObject_New (event_object, py_type);
00036   if (!event_obj)
00037     goto fail;
00038 
00039   event_obj->dict = PyDict_New ();
00040   if (!event_obj->dict)
00041     goto fail;
00042 
00043   return (PyObject*) event_obj;
00044 
00045  fail:
00046   Py_XDECREF (event_obj);
00047   return NULL;
00048 }
00049 
00050 /* Add the attribute ATTR to the event object EVENT.  In
00051    python this attribute will be accessible by the name NAME.
00052    returns 0 if the operation succeeds and -1 otherwise.  This
00053    function acquires a new reference to ATTR.  */
00054 
00055 int
00056 evpy_add_attribute (PyObject *event, char *name, PyObject *attr)
00057 {
00058   return PyObject_SetAttrString (event, name, attr);
00059 }
00060 
00061 /* Initialize the Python event code.  */
00062 
00063 int
00064 gdbpy_initialize_event (void)
00065 {
00066   return gdbpy_initialize_event_generic (&event_object_type,
00067                                          "Event");
00068 }
00069 
00070 /* Initialize the given event type.  If BASE is not NULL it will
00071   be set as the types base.
00072   Returns 0 if initialization was successful -1 otherwise.  */
00073 
00074 int
00075 gdbpy_initialize_event_generic (PyTypeObject *type,
00076                                 char *name)
00077 {
00078   if (PyType_Ready (type) < 0)
00079     return -1;
00080 
00081   return gdb_pymodule_addobject (gdb_module, name, (PyObject *) type);
00082 }
00083 
00084 
00085 /* Notify the list of listens that the given EVENT has occurred.
00086    returns 0 if emit is successful -1 otherwise.  */
00087 
00088 int
00089 evpy_emit_event (PyObject *event,
00090                  eventregistry_object *registry)
00091 {
00092   PyObject *callback_list_copy = NULL;
00093   Py_ssize_t i;
00094 
00095   /* Create a copy of call back list and use that for
00096      notifying listeners to avoid skipping callbacks
00097      in the case of a callback being disconnected during
00098      a notification.  */
00099   callback_list_copy = PySequence_List (registry->callbacks);
00100   if (!callback_list_copy)
00101     goto fail;
00102 
00103   for (i = 0; i < PyList_Size (callback_list_copy); i++)
00104     {
00105       PyObject *func = PyList_GetItem (callback_list_copy, i);
00106       PyObject *func_result;
00107 
00108       if (func == NULL)
00109         goto fail;
00110 
00111       func_result = PyObject_CallFunctionObjArgs (func, event, NULL);
00112 
00113       if (func_result == NULL)
00114         {
00115           /* Print the trace here, but keep going -- we want to try to
00116              call all of the callbacks even if one is broken.  */
00117           gdbpy_print_stack ();
00118         }
00119       else
00120         {
00121           Py_DECREF (func_result);
00122         }
00123     }
00124 
00125   Py_XDECREF (callback_list_copy);
00126   Py_XDECREF (event);
00127   return 0;
00128 
00129  fail:
00130   gdbpy_print_stack ();
00131   Py_XDECREF (callback_list_copy);
00132   Py_XDECREF (event);
00133   return -1;
00134 }
00135 
00136 static PyGetSetDef event_object_getset[] =
00137 {
00138   { "__dict__", gdb_py_generic_dict, NULL,
00139     "The __dict__ for this event.", &event_object_type },
00140   { NULL }
00141 };
00142 
00143 PyTypeObject event_object_type =
00144 {
00145   PyVarObject_HEAD_INIT (NULL, 0)
00146   "gdb.Event",                                /* tp_name */
00147   sizeof (event_object),                      /* tp_basicsize */
00148   0,                                          /* tp_itemsize */
00149   evpy_dealloc,                               /* tp_dealloc */
00150   0,                                          /* tp_print */
00151   0,                                          /* tp_getattr */
00152   0,                                          /* tp_setattr */
00153   0,                                          /* tp_compare */
00154   0,                                          /* tp_repr */
00155   0,                                          /* tp_as_number */
00156   0,                                          /* tp_as_sequence */
00157   0,                                          /* tp_as_mapping */
00158   0,                                          /* tp_hash  */
00159   0,                                          /* tp_call */
00160   0,                                          /* tp_str */
00161   0,                                          /* tp_getattro */
00162   0,                                          /* tp_setattro */
00163   0,                                          /* tp_as_buffer */
00164   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
00165   "GDB event object",                         /* tp_doc */
00166   0,                                          /* tp_traverse */
00167   0,                                          /* tp_clear */
00168   0,                                          /* tp_richcompare */
00169   0,                                          /* tp_weaklistoffset */
00170   0,                                          /* tp_iter */
00171   0,                                          /* tp_iternext */
00172   0,                                          /* tp_methods */
00173   0,                                          /* tp_members */
00174   event_object_getset,                        /* tp_getset */
00175   0,                                          /* tp_base */
00176   0,                                          /* tp_dict */
00177   0,                                          /* tp_descr_get */
00178   0,                                          /* tp_descr_set */
00179   offsetof (event_object, dict),              /* tp_dictoffset */
00180   0,                                          /* tp_init */
00181   0                                           /* tp_alloc */
00182 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines