Commit | Line | Data |
---|---|---|
c17a9e46 HZ |
1 | /* Python interface to inferior events. |
2 | ||
0b302171 | 3 | Copyright (C) 2009-2012 Free Software Foundation, Inc. |
c17a9e46 HZ |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
20 | #include "py-event.h" | |
21 | ||
22 | void | |
23 | evpy_dealloc (PyObject *self) | |
24 | { | |
25 | Py_XDECREF (((event_object *) self)->dict); | |
26 | self->ob_type->tp_free (self); | |
27 | } | |
28 | ||
29 | PyObject * | |
30 | create_event_object (PyTypeObject *py_type) | |
31 | { | |
32 | event_object *event_obj; | |
33 | ||
34 | event_obj = PyObject_New (event_object, py_type); | |
35 | if (!event_obj) | |
36 | goto fail; | |
37 | ||
38 | event_obj->dict = PyDict_New (); | |
39 | if (!event_obj->dict) | |
40 | goto fail; | |
41 | ||
42 | return (PyObject*) event_obj; | |
43 | ||
44 | fail: | |
45 | Py_XDECREF (event_obj); | |
46 | return NULL; | |
47 | } | |
48 | ||
49 | /* Add the attribute ATTR to the event object EVENT. In | |
50 | python this attribute will be accessible by the name NAME. | |
db6573d6 TT |
51 | returns 0 if the operation succeeds and -1 otherwise. This |
52 | function acquires a new reference to ATTR. */ | |
c17a9e46 HZ |
53 | |
54 | int | |
55 | evpy_add_attribute (PyObject *event, char *name, PyObject *attr) | |
56 | { | |
57 | return PyObject_SetAttrString (event, name, attr); | |
58 | } | |
59 | ||
60 | /* Initialize the Python event code. */ | |
61 | ||
62 | void | |
63 | gdbpy_initialize_event (void) | |
64 | { | |
65 | gdbpy_initialize_event_generic (&event_object_type, | |
66 | "Event"); | |
67 | } | |
68 | ||
69 | /* Initialize the given event type. If BASE is not NULL it will | |
70 | be set as the types base. | |
71 | Returns 0 if initialization was successful -1 otherwise. */ | |
72 | ||
73 | int | |
74 | gdbpy_initialize_event_generic (PyTypeObject *type, | |
75 | char *name) | |
76 | { | |
77 | if (PyType_Ready (type) < 0) | |
78 | goto fail; | |
79 | ||
80 | Py_INCREF (type); | |
81 | if (PyModule_AddObject (gdb_module, name, (PyObject *) type) < 0) | |
82 | goto fail; | |
83 | ||
84 | return 0; | |
85 | ||
86 | fail: | |
87 | Py_XDECREF (type); | |
88 | return -1; | |
89 | } | |
90 | ||
91 | ||
92 | /* Notify the list of listens that the given EVENT has occurred. | |
93 | returns 0 if emit is successful -1 otherwise. */ | |
94 | ||
95 | int | |
96 | evpy_emit_event (PyObject *event, | |
97 | eventregistry_object *registry) | |
98 | { | |
99 | PyObject *callback_list_copy = NULL; | |
100 | Py_ssize_t i; | |
101 | ||
102 | /* Create a copy of call back list and use that for | |
103 | notifying listeners to avoid skipping callbacks | |
104 | in the case of a callback being disconnected during | |
105 | a notification. */ | |
106 | callback_list_copy = PySequence_List (registry->callbacks); | |
107 | if (!callback_list_copy) | |
108 | goto fail; | |
109 | ||
110 | for (i = 0; i < PyList_Size (callback_list_copy); i++) | |
111 | { | |
112 | PyObject *func = PyList_GetItem (callback_list_copy, i); | |
113 | ||
114 | if (func == NULL) | |
115 | goto fail; | |
116 | ||
117 | if (!PyObject_CallFunctionObjArgs (func, event, NULL)) | |
118 | { | |
119 | /* Print the trace here, but keep going -- we want to try to | |
120 | call all of the callbacks even if one is broken. */ | |
121 | gdbpy_print_stack (); | |
122 | } | |
123 | } | |
124 | ||
125 | Py_XDECREF (callback_list_copy); | |
126 | Py_XDECREF (event); | |
127 | return 0; | |
128 | ||
129 | fail: | |
130 | gdbpy_print_stack (); | |
131 | Py_XDECREF (callback_list_copy); | |
132 | Py_XDECREF (event); | |
133 | return -1; | |
134 | } | |
135 | ||
2e8265fd TT |
136 | static PyGetSetDef event_object_getset[] = |
137 | { | |
138 | { "__dict__", gdb_py_generic_dict, NULL, | |
139 | "The __dict__ for this event.", &event_object_type }, | |
140 | { NULL } | |
141 | }; | |
142 | ||
c17a9e46 HZ |
143 | PyTypeObject event_object_type = |
144 | { | |
145 | PyObject_HEAD_INIT (NULL) | |
146 | 0, /* ob_size */ | |
147 | "gdb.Event", /* tp_name */ | |
148 | sizeof (event_object), /* tp_basicsize */ | |
149 | 0, /* tp_itemsize */ | |
150 | evpy_dealloc, /* tp_dealloc */ | |
151 | 0, /* tp_print */ | |
152 | 0, /* tp_getattr */ | |
153 | 0, /* tp_setattr */ | |
154 | 0, /* tp_compare */ | |
155 | 0, /* tp_repr */ | |
156 | 0, /* tp_as_number */ | |
157 | 0, /* tp_as_sequence */ | |
158 | 0, /* tp_as_mapping */ | |
159 | 0, /* tp_hash */ | |
160 | 0, /* tp_call */ | |
161 | 0, /* tp_str */ | |
162 | 0, /* tp_getattro */ | |
163 | 0, /* tp_setattro */ | |
164 | 0, /* tp_as_buffer */ | |
165 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ | |
166 | "GDB event object", /* tp_doc */ | |
167 | 0, /* tp_traverse */ | |
168 | 0, /* tp_clear */ | |
169 | 0, /* tp_richcompare */ | |
170 | 0, /* tp_weaklistoffset */ | |
171 | 0, /* tp_iter */ | |
172 | 0, /* tp_iternext */ | |
173 | 0, /* tp_methods */ | |
174 | 0, /* tp_members */ | |
2e8265fd | 175 | event_object_getset, /* tp_getset */ |
c17a9e46 HZ |
176 | 0, /* tp_base */ |
177 | 0, /* tp_dict */ | |
178 | 0, /* tp_descr_get */ | |
179 | 0, /* tp_descr_set */ | |
180 | offsetof (event_object, dict), /* tp_dictoffset */ | |
181 | 0, /* tp_init */ | |
182 | 0 /* tp_alloc */ | |
183 | }; |