1 /* Python interface to inferior thread event registries.
3 Copyright (C) 2009-2017 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/>. */
22 #include "py-events.h"
24 events_object gdb_py_events
;
26 extern PyTypeObject eventregistry_object_type
27 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("eventregistry_object");
29 /* Implementation of EventRegistry.connect () -> NULL.
30 Add FUNCTION to the list of listeners. */
33 evregpy_connect (PyObject
*self
, PyObject
*function
)
36 PyObject
*callback_list
= (((eventregistry_object
*) self
)->callbacks
);
38 if (!PyArg_ParseTuple (function
, "O", &func
))
41 if (!PyCallable_Check (func
))
43 PyErr_SetString (PyExc_RuntimeError
, "Function is not callable");
47 if (PyList_Append (callback_list
, func
) < 0)
53 /* Implementation of EventRegistry.disconnect () -> NULL.
54 Remove FUNCTION from the list of listeners. */
57 evregpy_disconnect (PyObject
*self
, PyObject
*function
)
61 PyObject
*callback_list
= (((eventregistry_object
*) self
)->callbacks
);
63 if (!PyArg_ParseTuple (function
, "O", &func
))
66 index
= PySequence_Index (callback_list
, func
);
70 if (PySequence_DelItem (callback_list
, index
) < 0)
76 /* Create a new event registry. This function uses PyObject_New
77 and therefore returns a new reference that callers must handle. */
79 eventregistry_object
*
80 create_eventregistry_object (void)
82 eventregistry_object
*eventregistry_obj
;
84 eventregistry_obj
= PyObject_New (eventregistry_object
,
85 &eventregistry_object_type
);
87 if (!eventregistry_obj
)
90 eventregistry_obj
->callbacks
= PyList_New (0);
91 if (!eventregistry_obj
->callbacks
)
93 Py_DECREF (eventregistry_obj
);
97 return eventregistry_obj
;
101 evregpy_dealloc (PyObject
*self
)
103 Py_XDECREF (((eventregistry_object
*) self
)->callbacks
);
104 Py_TYPE (self
)->tp_free (self
);
107 /* Initialize the Python event registry code. */
110 gdbpy_initialize_eventregistry (void)
112 if (PyType_Ready (&eventregistry_object_type
) < 0)
115 return gdb_pymodule_addobject (gdb_module
, "EventRegistry",
116 (PyObject
*) &eventregistry_object_type
);
119 /* Retern the number of listeners currently connected to this
123 evregpy_no_listeners_p (eventregistry_object
*registry
)
125 return PyList_Size (registry
->callbacks
) == 0;
128 static PyMethodDef eventregistry_object_methods
[] =
130 { "connect", evregpy_connect
, METH_VARARGS
, "Add function" },
131 { "disconnect", evregpy_disconnect
, METH_VARARGS
, "Remove function" },
132 { NULL
} /* Sentinel. */
135 PyTypeObject eventregistry_object_type
=
137 PyVarObject_HEAD_INIT (NULL
, 0)
138 "gdb.EventRegistry", /* tp_name */
139 sizeof (eventregistry_object
), /* tp_basicsize */
141 evregpy_dealloc
, /* tp_dealloc */
147 0, /* tp_as_number */
148 0, /* tp_as_sequence */
149 0, /* tp_as_mapping */
155 0, /* tp_as_buffer */
156 Py_TPFLAGS_DEFAULT
, /* tp_flags */
157 "GDB event registry object", /* tp_doc */
160 0, /* tp_richcompare */
161 0, /* tp_weaklistoffset */
164 eventregistry_object_methods
, /* tp_methods */
169 0, /* tp_descr_get */
170 0, /* tp_descr_set */
171 0, /* tp_dictoffset */