Automatic date update in version.in
[binutils-gdb.git] / gdb / python / py-event.c
bloba918136cccaaf1f3a7e28caf4c86aecb0bfdacf7
1 /* Python interface to inferior events.
3 Copyright (C) 2009-2024 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/>. */
20 #include "py-event.h"
22 void
23 evpy_dealloc (PyObject *self)
25 Py_XDECREF (((event_object *) self)->dict);
26 Py_TYPE (self)->tp_free (self);
29 gdbpy_ref<>
30 create_event_object (PyTypeObject *py_type)
32 gdbpy_ref<event_object> event_obj (PyObject_New (event_object, py_type));
33 if (event_obj == NULL)
34 return NULL;
36 event_obj->dict = PyDict_New ();
37 if (!event_obj->dict)
38 return NULL;
40 return gdbpy_ref<> ((PyObject *) event_obj.release ());
43 /* Add the attribute ATTR to the event object EVENT. In
44 python this attribute will be accessible by the name NAME.
45 returns 0 if the operation succeeds and -1 otherwise. This
46 function acquires a new reference to ATTR. */
48 int
49 evpy_add_attribute (PyObject *event, const char *name, PyObject *attr)
51 return PyObject_SetAttrString (event, name, attr);
54 /* Initialize the Python event code. */
56 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
57 gdbpy_initialize_event (void)
59 return gdbpy_type_ready (&event_object_type);
62 /* Notify the list of listens that the given EVENT has occurred.
63 returns 0 if emit is successful -1 otherwise. */
65 int
66 evpy_emit_event (PyObject *event,
67 eventregistry_object *registry)
69 Py_ssize_t i;
71 /* Create a copy of call back list and use that for
72 notifying listeners to avoid skipping callbacks
73 in the case of a callback being disconnected during
74 a notification. */
75 gdbpy_ref<> callback_list_copy (PySequence_List (registry->callbacks));
76 if (callback_list_copy == NULL)
77 return -1;
79 for (i = 0; i < PyList_Size (callback_list_copy.get ()); i++)
81 PyObject *func = PyList_GetItem (callback_list_copy.get (), i);
83 if (func == NULL)
84 return -1;
86 gdbpy_ref<> func_result (PyObject_CallFunctionObjArgs (func, event,
87 NULL));
89 if (func_result == NULL)
91 /* Print the trace here, but keep going -- we want to try to
92 call all of the callbacks even if one is broken. */
93 gdbpy_print_stack ();
97 return 0;
100 GDBPY_INITIALIZE_FILE (gdbpy_initialize_event);
102 static gdb_PyGetSetDef event_object_getset[] =
104 { "__dict__", gdb_py_generic_dict, NULL,
105 "The __dict__ for this event.", &event_object_type },
106 { NULL }
109 PyTypeObject event_object_type =
111 PyVarObject_HEAD_INIT (NULL, 0)
112 "gdb.Event", /* tp_name */
113 sizeof (event_object), /* tp_basicsize */
114 0, /* tp_itemsize */
115 evpy_dealloc, /* tp_dealloc */
116 0, /* tp_print */
117 0, /* tp_getattr */
118 0, /* tp_setattr */
119 0, /* tp_compare */
120 0, /* tp_repr */
121 0, /* tp_as_number */
122 0, /* tp_as_sequence */
123 0, /* tp_as_mapping */
124 0, /* tp_hash */
125 0, /* tp_call */
126 0, /* tp_str */
127 0, /* tp_getattro */
128 0, /* tp_setattro */
129 0, /* tp_as_buffer */
130 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
131 "GDB event object", /* tp_doc */
132 0, /* tp_traverse */
133 0, /* tp_clear */
134 0, /* tp_richcompare */
135 0, /* tp_weaklistoffset */
136 0, /* tp_iter */
137 0, /* tp_iternext */
138 0, /* tp_methods */
139 0, /* tp_members */
140 event_object_getset, /* tp_getset */
141 0, /* tp_base */
142 0, /* tp_dict */
143 0, /* tp_descr_get */
144 0, /* tp_descr_set */
145 offsetof (event_object, dict), /* tp_dictoffset */
146 0, /* tp_init */
147 0 /* tp_alloc */