Commit | Line | Data |
---|---|---|
6fa6bfa1 SM |
1 | /* |
2 | * The MIT License (MIT) | |
3 | * | |
4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | /* From component-class-const.h */ | |
26 | ||
27 | typedef enum bt_component_class_status { | |
28 | BT_COMPONENT_CLASS_STATUS_OK = 0, | |
29 | BT_COMPONENT_CLASS_STATUS_NOMEM = -12, | |
30 | } bt_component_class_status; | |
31 | ||
32 | typedef enum bt_component_class_type { | |
33 | BT_COMPONENT_CLASS_TYPE_SOURCE = 0, | |
34 | BT_COMPONENT_CLASS_TYPE_FILTER = 1, | |
35 | BT_COMPONENT_CLASS_TYPE_SINK = 2, | |
36 | } bt_component_class_type; | |
37 | ||
38 | extern const char *bt_component_class_get_name( | |
39 | const bt_component_class *component_class); | |
40 | ||
41 | extern const char *bt_component_class_get_description( | |
42 | const bt_component_class *component_class); | |
43 | ||
44 | extern const char *bt_component_class_get_help( | |
45 | const bt_component_class *component_class); | |
46 | ||
47 | extern bt_component_class_type bt_component_class_get_type( | |
48 | const bt_component_class *component_class); | |
49 | ||
50 | bt_bool bt_component_class_is_source( | |
51 | const bt_component_class *component_class); | |
52 | ||
53 | bt_bool bt_component_class_is_filter( | |
54 | const bt_component_class *component_class); | |
55 | ||
56 | bt_bool bt_component_class_is_sink( | |
57 | const bt_component_class *component_class); | |
58 | ||
59 | extern void bt_component_class_get_ref( | |
60 | const bt_component_class *component_class); | |
61 | ||
62 | extern void bt_component_class_put_ref( | |
63 | const bt_component_class *component_class); | |
64 | ||
65 | /* From component-class-source-const.h */ | |
66 | ||
67 | const bt_component_class * | |
68 | bt_component_class_source_as_component_class_const( | |
69 | const bt_component_class_source *comp_cls_source); | |
70 | ||
71 | extern void bt_component_class_source_get_ref( | |
72 | const bt_component_class_source *component_class_source); | |
73 | ||
74 | extern void bt_component_class_source_put_ref( | |
75 | const bt_component_class_source *component_class_source); | |
76 | ||
77 | /* From component-class-source.h */ | |
78 | ||
79 | typedef bt_self_component_status | |
80 | (*bt_component_class_source_init_method)( | |
81 | bt_self_component_source *self_component, | |
82 | const bt_value *params, void *init_method_data); | |
83 | ||
84 | typedef void (*bt_component_class_source_finalize_method)( | |
85 | bt_self_component_source *self_component); | |
86 | ||
87 | typedef bt_self_message_iterator_status | |
88 | (*bt_component_class_source_message_iterator_init_method)( | |
89 | bt_self_message_iterator *message_iterator, | |
90 | bt_self_component_source *self_component, | |
91 | bt_self_component_port_output *port); | |
92 | ||
93 | typedef void | |
94 | (*bt_component_class_source_message_iterator_finalize_method)( | |
95 | bt_self_message_iterator *message_iterator); | |
96 | ||
97 | typedef bt_self_message_iterator_status | |
98 | (*bt_component_class_source_message_iterator_next_method)( | |
99 | bt_self_message_iterator *message_iterator, | |
100 | bt_message_array_const msgs, uint64_t capacity, | |
101 | uint64_t *count); | |
102 | ||
103 | typedef bt_self_message_iterator_status | |
104 | (*bt_component_class_source_message_iterator_seek_ns_from_origin_method)( | |
105 | bt_self_message_iterator *message_iterator, | |
106 | int64_t ns_from_origin); | |
107 | ||
108 | typedef bt_self_message_iterator_status | |
109 | (*bt_component_class_source_message_iterator_seek_beginning_method)( | |
110 | bt_self_message_iterator *message_iterator); | |
111 | ||
112 | typedef bt_bool | |
113 | (*bt_component_class_source_message_iterator_can_seek_ns_from_origin_method)( | |
114 | bt_self_message_iterator *message_iterator, | |
115 | int64_t ns_from_origin); | |
116 | ||
117 | typedef bt_bool | |
118 | (*bt_component_class_source_message_iterator_can_seek_beginning_method)( | |
119 | bt_self_message_iterator *message_iterator); | |
120 | ||
121 | typedef bt_query_status (*bt_component_class_source_query_method)( | |
122 | bt_self_component_class_source *comp_class, | |
123 | const bt_query_executor *query_executor, | |
124 | const char *object, const bt_value *params, | |
125 | const bt_value **result); | |
126 | ||
127 | typedef bt_self_component_status | |
128 | (*bt_component_class_source_accept_output_port_connection_method)( | |
129 | bt_self_component_source *self_component, | |
130 | bt_self_component_port_output *self_port, | |
131 | const bt_port_input *other_port); | |
132 | ||
133 | typedef bt_self_component_status | |
134 | (*bt_component_class_source_output_port_connected_method)( | |
135 | bt_self_component_source *self_component, | |
136 | bt_self_component_port_output *self_port, | |
137 | const bt_port_input *other_port); | |
138 | ||
139 | bt_component_class *bt_component_class_source_as_component_class( | |
140 | bt_component_class_source *comp_cls_source); | |
141 | ||
142 | extern | |
143 | bt_component_class_source *bt_component_class_source_create( | |
144 | const char *name, | |
145 | bt_component_class_source_message_iterator_next_method method); | |
146 | ||
147 | extern bt_component_class_status | |
148 | bt_component_class_source_set_init_method( | |
149 | bt_component_class_source *comp_class, | |
150 | bt_component_class_source_init_method method); | |
151 | ||
152 | extern bt_component_class_status | |
153 | bt_component_class_source_set_finalize_method( | |
154 | bt_component_class_source *comp_class, | |
155 | bt_component_class_source_finalize_method method); | |
156 | ||
157 | extern bt_component_class_status | |
158 | bt_component_class_source_set_accept_output_port_connection_method( | |
159 | bt_component_class_source *comp_class, | |
160 | bt_component_class_source_accept_output_port_connection_method method); | |
161 | ||
162 | extern bt_component_class_status | |
163 | bt_component_class_source_set_output_port_connected_method( | |
164 | bt_component_class_source *comp_class, | |
165 | bt_component_class_source_output_port_connected_method method); | |
166 | ||
167 | extern bt_component_class_status | |
168 | bt_component_class_source_set_query_method( | |
169 | bt_component_class_source *comp_class, | |
170 | bt_component_class_source_query_method method); | |
171 | ||
172 | extern bt_component_class_status | |
173 | bt_component_class_source_set_message_iterator_init_method( | |
174 | bt_component_class_source *comp_class, | |
175 | bt_component_class_source_message_iterator_init_method method); | |
176 | ||
177 | extern bt_component_class_status | |
178 | bt_component_class_source_set_message_iterator_finalize_method( | |
179 | bt_component_class_source *comp_class, | |
180 | bt_component_class_source_message_iterator_finalize_method method); | |
181 | ||
182 | extern bt_component_class_status | |
183 | bt_component_class_source_set_message_iterator_seek_ns_from_origin_method( | |
184 | bt_component_class_source *comp_class, | |
185 | bt_component_class_source_message_iterator_seek_ns_from_origin_method method); | |
186 | ||
187 | extern bt_component_class_status | |
188 | bt_component_class_source_set_message_iterator_seek_beginning_method( | |
189 | bt_component_class_source *comp_class, | |
190 | bt_component_class_source_message_iterator_seek_beginning_method method); | |
191 | ||
192 | extern bt_bool | |
193 | bt_component_class_source_set_message_iterator_can_seek_ns_from_origin_method( | |
194 | bt_component_class_source *comp_class, | |
195 | bt_component_class_source_message_iterator_can_seek_ns_from_origin_method method); | |
196 | ||
197 | extern bt_bool | |
198 | bt_component_class_source_set_message_iterator_can_seek_beginning_method( | |
199 | bt_component_class_source *comp_class, | |
200 | bt_component_class_source_message_iterator_can_seek_beginning_method method); | |
201 | ||
202 | /* From component-class-filter-const.h */ | |
203 | ||
204 | const bt_component_class * | |
205 | bt_component_class_filter_as_component_class_const( | |
206 | const bt_component_class_filter *comp_cls_filter); | |
207 | ||
208 | extern void bt_component_class_filter_get_ref( | |
209 | const bt_component_class_filter *component_class_filter); | |
210 | ||
211 | extern void bt_component_class_filter_put_ref( | |
212 | const bt_component_class_filter *component_class_filter); | |
213 | ||
214 | /* From component-class-filter.h */ | |
215 | ||
216 | typedef bt_self_component_status | |
217 | (*bt_component_class_filter_init_method)( | |
218 | bt_self_component_filter *self_component, | |
219 | const bt_value *params, void *init_method_data); | |
220 | ||
221 | typedef void (*bt_component_class_filter_finalize_method)( | |
222 | bt_self_component_filter *self_component); | |
223 | ||
224 | typedef bt_self_message_iterator_status | |
225 | (*bt_component_class_filter_message_iterator_init_method)( | |
226 | bt_self_message_iterator *message_iterator, | |
227 | bt_self_component_filter *self_component, | |
228 | bt_self_component_port_output *port); | |
229 | ||
230 | typedef void | |
231 | (*bt_component_class_filter_message_iterator_finalize_method)( | |
232 | bt_self_message_iterator *message_iterator); | |
233 | ||
234 | typedef bt_self_message_iterator_status | |
235 | (*bt_component_class_filter_message_iterator_next_method)( | |
236 | bt_self_message_iterator *message_iterator, | |
237 | bt_message_array_const msgs, uint64_t capacity, | |
238 | uint64_t *count); | |
239 | ||
240 | typedef bt_self_message_iterator_status | |
241 | (*bt_component_class_filter_message_iterator_seek_ns_from_origin_method)( | |
242 | bt_self_message_iterator *message_iterator, | |
243 | int64_t ns_from_origin); | |
244 | ||
245 | typedef bt_self_message_iterator_status | |
246 | (*bt_component_class_filter_message_iterator_seek_beginning_method)( | |
247 | bt_self_message_iterator *message_iterator); | |
248 | ||
249 | typedef bt_bool | |
250 | (*bt_component_class_filter_message_iterator_can_seek_ns_from_origin_method)( | |
251 | bt_self_message_iterator *message_iterator, | |
252 | int64_t ns_from_origin); | |
253 | ||
254 | typedef bt_bool | |
255 | (*bt_component_class_filter_message_iterator_can_seek_beginning_method)( | |
256 | bt_self_message_iterator *message_iterator); | |
257 | ||
258 | typedef bt_query_status | |
259 | (*bt_component_class_filter_query_method)( | |
260 | bt_self_component_class_filter *comp_class, | |
261 | const bt_query_executor *query_executor, | |
262 | const char *object, const bt_value *params, | |
263 | const bt_value **result); | |
264 | ||
265 | typedef bt_self_component_status | |
266 | (*bt_component_class_filter_accept_input_port_connection_method)( | |
267 | bt_self_component_filter *self_component, | |
268 | bt_self_component_port_input *self_port, | |
269 | const bt_port_output *other_port); | |
270 | ||
271 | typedef bt_self_component_status | |
272 | (*bt_component_class_filter_accept_output_port_connection_method)( | |
273 | bt_self_component_filter *self_component, | |
274 | bt_self_component_port_output *self_port, | |
275 | const bt_port_input *other_port); | |
276 | ||
277 | typedef bt_self_component_status | |
278 | (*bt_component_class_filter_input_port_connected_method)( | |
279 | bt_self_component_filter *self_component, | |
280 | bt_self_component_port_input *self_port, | |
281 | const bt_port_output *other_port); | |
282 | ||
283 | typedef bt_self_component_status | |
284 | (*bt_component_class_filter_output_port_connected_method)( | |
285 | bt_self_component_filter *self_component, | |
286 | bt_self_component_port_output *self_port, | |
287 | const bt_port_input *other_port); | |
288 | ||
289 | bt_component_class *bt_component_class_filter_as_component_class( | |
290 | bt_component_class_filter *comp_cls_filter); | |
291 | ||
292 | extern | |
293 | bt_component_class_filter *bt_component_class_filter_create( | |
294 | const char *name, | |
295 | bt_component_class_filter_message_iterator_next_method method); | |
296 | ||
297 | extern bt_component_class_status | |
298 | bt_component_class_filter_set_init_method( | |
299 | bt_component_class_filter *comp_class, | |
300 | bt_component_class_filter_init_method method); | |
301 | ||
302 | extern bt_component_class_status | |
303 | bt_component_class_filter_set_finalize_method( | |
304 | bt_component_class_filter *comp_class, | |
305 | bt_component_class_filter_finalize_method method); | |
306 | ||
307 | extern bt_component_class_status | |
308 | bt_component_class_filter_set_accept_input_port_connection_method( | |
309 | bt_component_class_filter *comp_class, | |
310 | bt_component_class_filter_accept_input_port_connection_method method); | |
311 | ||
312 | extern bt_component_class_status | |
313 | bt_component_class_filter_set_accept_output_port_connection_method( | |
314 | bt_component_class_filter *comp_class, | |
315 | bt_component_class_filter_accept_output_port_connection_method method); | |
316 | ||
317 | extern bt_component_class_status | |
318 | bt_component_class_filter_set_input_port_connected_method( | |
319 | bt_component_class_filter *comp_class, | |
320 | bt_component_class_filter_input_port_connected_method method); | |
321 | ||
322 | extern bt_component_class_status | |
323 | bt_component_class_filter_set_output_port_connected_method( | |
324 | bt_component_class_filter *comp_class, | |
325 | bt_component_class_filter_output_port_connected_method method); | |
326 | ||
327 | extern bt_component_class_status | |
328 | bt_component_class_filter_set_query_method( | |
329 | bt_component_class_filter *comp_class, | |
330 | bt_component_class_filter_query_method method); | |
331 | ||
332 | extern bt_component_class_status | |
333 | bt_component_class_filter_set_message_iterator_init_method( | |
334 | bt_component_class_filter *comp_class, | |
335 | bt_component_class_filter_message_iterator_init_method method); | |
336 | ||
337 | extern bt_component_class_status | |
338 | bt_component_class_filter_set_message_iterator_finalize_method( | |
339 | bt_component_class_filter *comp_class, | |
340 | bt_component_class_filter_message_iterator_finalize_method method); | |
341 | ||
342 | extern bt_component_class_status | |
343 | bt_component_class_filter_set_message_iterator_seek_ns_from_origin_method( | |
344 | bt_component_class_filter *comp_class, | |
345 | bt_component_class_filter_message_iterator_seek_ns_from_origin_method method); | |
346 | ||
347 | extern bt_component_class_status | |
348 | bt_component_class_filter_set_message_iterator_seek_beginning_method( | |
349 | bt_component_class_filter *comp_class, | |
350 | bt_component_class_filter_message_iterator_seek_beginning_method method); | |
351 | ||
352 | extern bt_bool | |
353 | bt_component_class_filter_set_message_iterator_can_seek_ns_from_origin_method( | |
354 | bt_component_class_filter *comp_class, | |
355 | bt_component_class_filter_message_iterator_can_seek_ns_from_origin_method method); | |
356 | ||
357 | extern bt_bool | |
358 | bt_component_class_filter_set_message_iterator_can_seek_beginning_method( | |
359 | bt_component_class_filter *comp_class, | |
360 | bt_component_class_filter_message_iterator_can_seek_beginning_method method); | |
361 | ||
362 | /* From component-class-sink-const.h */ | |
363 | ||
364 | const bt_component_class * | |
365 | bt_component_class_sink_as_component_class_const( | |
366 | const bt_component_class_sink *comp_cls_sink); | |
367 | ||
368 | extern void bt_component_class_sink_get_ref( | |
369 | const bt_component_class_sink *component_class_sink); | |
370 | ||
371 | extern void bt_component_class_sink_put_ref( | |
372 | const bt_component_class_sink *component_class_sink); | |
373 | ||
374 | /* From component-class-sink.h */ | |
375 | ||
376 | typedef bt_self_component_status (*bt_component_class_sink_init_method)( | |
377 | bt_self_component_sink *self_component, | |
378 | const bt_value *params, void *init_method_data); | |
379 | ||
380 | typedef void (*bt_component_class_sink_finalize_method)( | |
381 | bt_self_component_sink *self_component); | |
382 | ||
383 | typedef bt_query_status | |
384 | (*bt_component_class_sink_query_method)( | |
385 | bt_self_component_class_sink *comp_class, | |
386 | const bt_query_executor *query_executor, | |
387 | const char *object, const bt_value *params, | |
388 | const bt_value **result); | |
389 | ||
390 | typedef bt_self_component_status | |
391 | (*bt_component_class_sink_accept_input_port_connection_method)( | |
392 | bt_self_component_sink *self_component, | |
393 | bt_self_component_port_input *self_port, | |
394 | const bt_port_output *other_port); | |
395 | ||
396 | typedef bt_self_component_status | |
397 | (*bt_component_class_sink_input_port_connected_method)( | |
398 | bt_self_component_sink *self_component, | |
399 | bt_self_component_port_input *self_port, | |
400 | const bt_port_output *other_port); | |
401 | ||
402 | typedef bt_self_component_status | |
403 | (*bt_component_class_sink_graph_is_configured_method)( | |
404 | bt_self_component_sink *self_component); | |
405 | ||
406 | typedef bt_self_component_status (*bt_component_class_sink_consume_method)( | |
407 | bt_self_component_sink *self_component); | |
408 | ||
409 | bt_component_class *bt_component_class_sink_as_component_class( | |
410 | bt_component_class_sink *comp_cls_sink); | |
411 | ||
412 | extern | |
413 | bt_component_class_sink *bt_component_class_sink_create( | |
414 | const char *name, | |
415 | bt_component_class_sink_consume_method method); | |
416 | ||
417 | extern bt_component_class_status bt_component_class_sink_set_init_method( | |
418 | bt_component_class_sink *comp_class, | |
419 | bt_component_class_sink_init_method method); | |
420 | ||
421 | extern bt_component_class_status bt_component_class_sink_set_finalize_method( | |
422 | bt_component_class_sink *comp_class, | |
423 | bt_component_class_sink_finalize_method method); | |
424 | ||
425 | extern bt_component_class_status | |
426 | bt_component_class_sink_set_accept_input_port_connection_method( | |
427 | bt_component_class_sink *comp_class, | |
428 | bt_component_class_sink_accept_input_port_connection_method method); | |
429 | ||
430 | extern bt_component_class_status | |
431 | bt_component_class_sink_set_input_port_connected_method( | |
432 | bt_component_class_sink *comp_class, | |
433 | bt_component_class_sink_input_port_connected_method method); | |
434 | ||
435 | extern bt_component_class_status | |
436 | bt_component_class_sink_set_graph_is_configured_method( | |
437 | bt_component_class_sink *comp_class, | |
438 | bt_component_class_sink_graph_is_configured_method method); | |
439 | ||
440 | extern bt_component_class_status bt_component_class_sink_set_query_method( | |
441 | bt_component_class_sink *comp_class, | |
442 | bt_component_class_sink_query_method method); | |
443 | ||
444 | /* From self-component-class-source.h */ | |
445 | ||
446 | const bt_component_class_source * | |
447 | bt_self_component_class_source_as_component_class_source( | |
448 | bt_self_component_class_source *self_comp_cls_source); | |
449 | ||
450 | /* From self-component-class-filter.h */ | |
451 | ||
452 | const bt_component_class_filter * | |
453 | bt_self_component_class_filter_as_component_class_filter( | |
454 | bt_self_component_class_filter *self_comp_cls_filter); | |
455 | ||
456 | /* From self-component-class-sink.h */ | |
457 | ||
458 | const bt_component_class_sink * | |
459 | bt_self_component_class_sink_as_component_class_sink( | |
460 | bt_self_component_class_sink *self_comp_cls_sink); | |
461 | ||
462 | %{ | |
463 | /* | |
464 | * This hash table associates a BT component class object address to a | |
465 | * user-defined Python class (PyObject *). The keys and values are NOT | |
466 | * owned by this hash table. The Python class objects are owned by the | |
467 | * Python module, which should not be unloaded until it is not possible | |
468 | * to create a user Python component anyway. | |
469 | * | |
470 | * This hash table is written to when a user-defined Python component | |
471 | * class is created by one of the bt_py3_component_class_*_create() | |
472 | * functions. | |
473 | * | |
474 | * This function is read from when a user calls bt_component_create() | |
475 | * with a component class pointer created by one of the functions above. | |
476 | * In this case, the original Python class needs to be found to | |
477 | * instantiate it and associate the created Python component object with | |
478 | * a BT component object instance. | |
479 | */ | |
480 | ||
481 | static GHashTable *bt_cc_ptr_to_py_cls; | |
482 | ||
483 | static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, | |
484 | PyObject *py_cls) | |
485 | { | |
486 | if (!bt_cc_ptr_to_py_cls) { | |
487 | /* | |
488 | * Lazy-initializing this GHashTable because GLib | |
489 | * might not be initialized yet and it needs to be | |
490 | * before we call g_hash_table_new() | |
491 | */ | |
492 | BT_LOGD_STR("Creating native component class to Python component class hash table."); | |
493 | bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal); | |
494 | BT_ASSERT(bt_cc_ptr_to_py_cls); | |
495 | } | |
496 | ||
497 | g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc, | |
498 | (gpointer) py_cls); | |
499 | } | |
500 | ||
501 | static PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc) | |
502 | { | |
503 | if (!bt_cc_ptr_to_py_cls) { | |
504 | BT_LOGW("Cannot look up Python component class because hash table is NULL: " | |
505 | "comp-cls-addr=%p", bt_cc); | |
506 | return NULL; | |
507 | } | |
508 | ||
509 | return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls, | |
510 | (gconstpointer) bt_cc); | |
511 | } | |
512 | ||
513 | ||
514 | /* | |
515 | * Useful Python objects. | |
516 | */ | |
517 | ||
518 | static PyObject *py_mod_bt2 = NULL; | |
519 | static PyObject *py_mod_bt2_exc_error_type = NULL; | |
520 | static PyObject *py_mod_bt2_exc_try_again_type = NULL; | |
521 | static PyObject *py_mod_bt2_exc_stop_type = NULL; | |
522 | static PyObject *py_mod_bt2_exc_port_connection_refused_type = NULL; | |
523 | static PyObject *py_mod_bt2_exc_notif_iter_canceled_type = NULL; | |
524 | static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL; | |
525 | static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL; | |
526 | ||
527 | static void bt_py3_cc_init_from_bt2(void) | |
528 | { | |
529 | /* | |
530 | * This is called once the bt2 package is loaded. | |
531 | * | |
532 | * Those modules and functions are needed while the package is | |
533 | * used. Loading them here is safe because we know the bt2 | |
534 | * package is imported, and we know that the user cannot use the | |
535 | * code here without importing bt2 first. | |
536 | */ | |
537 | py_mod_bt2 = PyImport_ImportModule("bt2"); | |
538 | BT_ASSERT(py_mod_bt2); | |
539 | py_mod_bt2_exc_error_type = | |
540 | PyObject_GetAttrString(py_mod_bt2, "Error"); | |
541 | BT_ASSERT(py_mod_bt2_exc_error_type); | |
542 | py_mod_bt2_exc_try_again_type = | |
543 | PyObject_GetAttrString(py_mod_bt2, "TryAgain"); | |
544 | BT_ASSERT(py_mod_bt2_exc_try_again_type); | |
545 | py_mod_bt2_exc_stop_type = | |
546 | PyObject_GetAttrString(py_mod_bt2, "Stop"); | |
547 | BT_ASSERT(py_mod_bt2_exc_stop_type); | |
548 | py_mod_bt2_exc_port_connection_refused_type = | |
549 | PyObject_GetAttrString(py_mod_bt2, "PortConnectionRefused"); | |
550 | BT_ASSERT(py_mod_bt2_exc_port_connection_refused_type); | |
551 | py_mod_bt2_exc_invalid_query_object_type = | |
552 | PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject"); | |
553 | BT_ASSERT(py_mod_bt2_exc_invalid_query_object_type); | |
554 | py_mod_bt2_exc_invalid_query_params_type = | |
555 | PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams"); | |
556 | BT_ASSERT(py_mod_bt2_exc_invalid_query_params_type); | |
557 | } | |
558 | ||
559 | static void bt_py3_cc_exit_handler(void) | |
560 | { | |
561 | /* | |
562 | * This is an exit handler (set by the bt2 package). | |
563 | * | |
564 | * We only give back the references that we took in | |
565 | * bt_py3_cc_init_from_bt2() here. The global variables continue | |
566 | * to exist for the code of this file, but they are now borrowed | |
567 | * references. If this code is executed, it means that somehow | |
568 | * the modules are still loaded, so it should be safe to use | |
569 | * them even without a strong reference. | |
570 | * | |
571 | * We cannot do this in the library's destructor because it | |
572 | * gets executed once Python is already finalized. | |
573 | */ | |
574 | Py_XDECREF(py_mod_bt2); | |
575 | Py_XDECREF(py_mod_bt2_exc_error_type); | |
576 | Py_XDECREF(py_mod_bt2_exc_try_again_type); | |
577 | Py_XDECREF(py_mod_bt2_exc_stop_type); | |
578 | Py_XDECREF(py_mod_bt2_exc_port_connection_refused_type); | |
579 | Py_XDECREF(py_mod_bt2_exc_notif_iter_canceled_type); | |
580 | Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type); | |
581 | Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type); | |
582 | } | |
583 | ||
584 | ||
585 | /* Library destructor */ | |
586 | ||
587 | __attribute__((destructor)) | |
588 | static void bt_py3_native_comp_class_dtor(void) { | |
589 | /* Destroy component class association hash table */ | |
590 | if (bt_cc_ptr_to_py_cls) { | |
591 | BT_LOGD_STR("Destroying native component class to Python component class hash table."); | |
592 | g_hash_table_destroy(bt_cc_ptr_to_py_cls); | |
593 | } | |
594 | } | |
595 | ||
596 | ||
597 | // TODO: maybe we can wrap code in the Python methods (e.g. _query_from_native) | |
598 | // in a try catch and print the error there instead, it would be simpler. | |
599 | static | |
600 | void bt2_py_loge_exception(void) | |
601 | { | |
602 | PyObject *type = NULL; | |
603 | PyObject *value = NULL; | |
604 | PyObject *traceback = NULL; | |
605 | PyObject *traceback_module = NULL; | |
606 | PyObject *format_exception_func = NULL; | |
607 | PyObject *exc_str_list = NULL; | |
608 | GString *msg_buf = NULL; | |
609 | Py_ssize_t i; | |
610 | ||
611 | BT_ASSERT(PyErr_Occurred() != NULL); | |
612 | ||
613 | PyErr_Fetch(&type, &value, &traceback); | |
614 | ||
615 | BT_ASSERT(type != NULL); | |
616 | ||
617 | /* | |
618 | * traceback can be NULL, when we fail to call a Python function from the | |
619 | * native code (there is not Python stack at that point). E.g.: | |
620 | * | |
621 | * TypeError: _accept_port_connection_from_native() takes 3 positional arguments but 4 were given | |
622 | */ | |
623 | ||
624 | ||
625 | /* Make sure `value` is what we expected - an instance of `type`. */ | |
626 | PyErr_NormalizeException(&type, &value, &traceback); | |
627 | ||
628 | traceback_module = PyImport_ImportModule("traceback"); | |
629 | if (!traceback_module) { | |
630 | BT_LOGE_STR("Failed to log Python exception (could not import traceback module)."); | |
631 | goto end; | |
632 | } | |
633 | ||
634 | format_exception_func = PyObject_GetAttrString(traceback_module, | |
635 | traceback ? "format_exception" : "format_exception_only"); | |
636 | if (!format_exception_func) { | |
637 | BT_LOGE_STR("Failed to log Python exception (could not find format_exception)."); | |
638 | goto end; | |
639 | } | |
640 | ||
641 | if (!PyCallable_Check(format_exception_func)) { | |
642 | BT_LOGE_STR("Failed to log Python exception (format_exception is not callable)."); | |
643 | goto end; | |
644 | } | |
645 | ||
646 | exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, type, value, traceback, NULL); | |
647 | if (!exc_str_list) { | |
648 | PyErr_Print(); | |
649 | BT_LOGE_STR("Failed to log Python exception (call to format_exception failed)."); | |
650 | goto end; | |
651 | } | |
652 | ||
653 | msg_buf = g_string_new(NULL); | |
654 | ||
655 | for (i = 0; i < PyList_Size(exc_str_list); i++) { | |
656 | PyObject *exc_str = PyList_GetItem(exc_str_list, i); | |
657 | const char *str = PyUnicode_AsUTF8(exc_str); | |
658 | if (!str) { | |
659 | BT_LOGE_STR("Failed to log Python exception (failed to convert exception to string)."); | |
660 | goto end; | |
661 | } | |
662 | ||
663 | g_string_append(msg_buf, str); | |
664 | } | |
665 | ||
666 | BT_LOGE_STR(msg_buf->str); | |
667 | ||
668 | end: | |
669 | if (msg_buf) { | |
670 | g_string_free(msg_buf, TRUE); | |
671 | } | |
672 | Py_XDECREF(exc_str_list); | |
673 | Py_XDECREF(format_exception_func); | |
674 | Py_XDECREF(traceback_module); | |
675 | ||
676 | /* PyErr_Restore takes our references. */ | |
677 | PyErr_Restore(type, value, traceback); | |
678 | } | |
679 | ||
680 | static bt_self_component_status bt_py3_exc_to_self_component_status(void) | |
681 | { | |
682 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; | |
683 | PyObject *exc = PyErr_Occurred(); | |
684 | ||
685 | if (!exc) { | |
686 | goto end; | |
687 | } | |
688 | ||
689 | if (PyErr_GivenExceptionMatches(exc, | |
690 | py_mod_bt2_exc_try_again_type)) { | |
691 | status = BT_SELF_COMPONENT_STATUS_AGAIN; | |
692 | } else if (PyErr_GivenExceptionMatches(exc, | |
693 | py_mod_bt2_exc_stop_type)) { | |
694 | status = BT_SELF_COMPONENT_STATUS_END; | |
695 | } else if (PyErr_GivenExceptionMatches(exc, | |
696 | py_mod_bt2_exc_port_connection_refused_type)) { | |
697 | status = BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION; | |
698 | } else { | |
699 | bt2_py_loge_exception(); | |
700 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
701 | } | |
702 | ||
703 | end: | |
704 | PyErr_Clear(); | |
705 | return status; | |
706 | } | |
707 | ||
708 | /* Component class proxy methods (delegate to the attached Python object) */ | |
709 | ||
710 | static bt_self_message_iterator_status | |
711 | bt_py3_exc_to_self_message_iterator_status(void) | |
712 | { | |
713 | enum bt_self_message_iterator_status status = | |
714 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
715 | PyObject *exc = PyErr_Occurred(); | |
716 | ||
717 | if (!exc) { | |
718 | goto end; | |
719 | } | |
720 | ||
721 | if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) { | |
722 | status = BT_MESSAGE_ITERATOR_STATUS_END; | |
723 | } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) { | |
724 | status = BT_MESSAGE_ITERATOR_STATUS_AGAIN; | |
725 | } else { | |
726 | bt2_py_loge_exception(); | |
727 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
728 | } | |
729 | ||
730 | end: | |
731 | PyErr_Clear(); | |
732 | return status; | |
733 | } | |
734 | ||
735 | static enum bt_query_status bt_py3_exc_to_query_status(void) | |
736 | { | |
737 | enum bt_query_status status = BT_QUERY_STATUS_OK; | |
738 | PyObject *exc = PyErr_Occurred(); | |
739 | ||
740 | if (!exc) { | |
741 | goto end; | |
742 | } | |
743 | ||
744 | if (PyErr_GivenExceptionMatches(exc, | |
745 | py_mod_bt2_exc_invalid_query_object_type)) { | |
746 | status = BT_QUERY_STATUS_INVALID_OBJECT; | |
747 | } else if (PyErr_GivenExceptionMatches(exc, | |
748 | py_mod_bt2_exc_invalid_query_params_type)) { | |
749 | status = BT_QUERY_STATUS_INVALID_PARAMS; | |
750 | } else if (PyErr_GivenExceptionMatches(exc, | |
751 | py_mod_bt2_exc_try_again_type)) { | |
752 | status = BT_QUERY_STATUS_AGAIN; | |
753 | } else { | |
754 | bt2_py_loge_exception(); | |
755 | status = BT_QUERY_STATUS_ERROR; | |
756 | } | |
757 | ||
758 | end: | |
759 | PyErr_Clear(); | |
760 | return status; | |
761 | } | |
762 | ||
763 | static bt_self_component_status | |
764 | bt_py3_component_class_init( | |
765 | bt_self_component *self_component, | |
766 | void *self_component_v, | |
767 | swig_type_info *self_comp_cls_type_swig_type, | |
768 | const bt_value *params, | |
769 | void *init_method_data) | |
770 | { | |
771 | const bt_component *component = bt_self_component_as_component(self_component); | |
772 | const bt_component_class *component_class = bt_component_borrow_class_const(component); | |
773 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; | |
774 | PyObject *py_cls = NULL; | |
775 | PyObject *py_comp = NULL; | |
776 | PyObject *py_params_ptr = NULL; | |
777 | PyObject *py_comp_ptr = NULL; | |
778 | ||
779 | (void) init_method_data; | |
780 | ||
781 | BT_ASSERT(self_component); | |
782 | BT_ASSERT(self_component_v); | |
783 | BT_ASSERT(self_comp_cls_type_swig_type); | |
784 | ||
785 | /* | |
786 | * Get the user-defined Python class which created this | |
787 | * component's class in the first place (borrowed | |
788 | * reference). | |
789 | */ | |
790 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
791 | if (!py_cls) { | |
792 | BT_LOGE("Cannot find Python class associated to native component class: " | |
793 | "comp-cls-addr=%p", component_class); | |
794 | goto error; | |
795 | } | |
796 | ||
797 | /* Parameters pointer -> SWIG pointer Python object */ | |
798 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
799 | SWIGTYPE_p_bt_value, 0); | |
800 | if (!py_params_ptr) { | |
801 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
802 | goto error; | |
803 | } | |
804 | ||
805 | py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), | |
806 | self_comp_cls_type_swig_type, 0); | |
807 | if (!py_comp_ptr) { | |
808 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
809 | goto error; | |
810 | } | |
811 | ||
812 | /* | |
813 | * Do the equivalent of this: | |
814 | * | |
815 | * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr) | |
816 | * | |
817 | * _UserComponentType._init_from_native() calls the Python | |
818 | * component object's __init__() function. | |
819 | */ | |
820 | py_comp = PyObject_CallMethod(py_cls, | |
821 | "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr); | |
822 | if (!py_comp) { | |
823 | bt2_py_loge_exception(); | |
824 | BT_LOGE("Failed to call Python class's _init_from_native() method: " | |
825 | "py-cls-addr=%p", py_cls); | |
826 | ||
827 | goto error; | |
828 | } | |
829 | ||
830 | /* | |
831 | * Our user Python component object is now fully created and | |
832 | * initialized by the user. Since we just created it, this | |
833 | * native component is its only (persistent) owner. | |
834 | */ | |
835 | bt_self_component_set_data(self_component, py_comp); | |
836 | py_comp = NULL; | |
837 | goto end; | |
838 | ||
839 | error: | |
840 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
841 | ||
842 | /* | |
843 | * Clear any exception: we're returning a bad status anyway. If | |
844 | * this call originated from Python (creation from a plugin's | |
845 | * component class, for example), then the user gets an | |
846 | * appropriate creation error. | |
847 | */ | |
848 | PyErr_Clear(); | |
849 | ||
850 | end: | |
851 | Py_XDECREF(py_comp); | |
852 | Py_XDECREF(py_params_ptr); | |
853 | Py_XDECREF(py_comp_ptr); | |
854 | return status; | |
855 | } | |
856 | ||
857 | /* | |
858 | * Method of bt_component_class_source to initialize a bt_self_component_source | |
859 | * of that class. | |
860 | */ | |
861 | ||
862 | static bt_self_component_status | |
863 | bt_py3_component_class_source_init(bt_self_component_source *self_component_source, | |
864 | const bt_value *params, void *init_method_data) | |
865 | { | |
866 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
867 | return bt_py3_component_class_init( | |
868 | self_component, | |
869 | self_component_source, | |
870 | SWIGTYPE_p_bt_self_component_source, | |
871 | params, init_method_data); | |
872 | } | |
873 | ||
874 | static bt_self_component_status | |
875 | bt_py3_component_class_filter_init(bt_self_component_filter *self_component_filter, | |
876 | const bt_value *params, void *init_method_data) | |
877 | { | |
878 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
879 | return bt_py3_component_class_init( | |
880 | self_component, | |
881 | self_component_filter, | |
882 | SWIGTYPE_p_bt_self_component_filter, | |
883 | params, init_method_data); | |
884 | } | |
885 | ||
886 | static bt_self_component_status | |
887 | bt_py3_component_class_sink_init(bt_self_component_sink *self_component_sink, | |
888 | const bt_value *params, void *init_method_data) | |
889 | { | |
890 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
891 | return bt_py3_component_class_init( | |
892 | self_component, | |
893 | self_component_sink, | |
894 | SWIGTYPE_p_bt_self_component_sink, | |
895 | params, init_method_data); | |
896 | } | |
897 | ||
898 | static void bt_py3_component_class_finalize(bt_self_component *self_component) | |
899 | { | |
900 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
901 | BT_ASSERT(py_comp); | |
902 | ||
903 | /* Call user's _finalize() method */ | |
904 | PyObject *py_method_result = PyObject_CallMethod(py_comp, | |
905 | "_finalize", NULL); | |
906 | ||
907 | if (PyErr_Occurred()) { | |
908 | BT_LOGW("User's _finalize() method raised an exception: ignoring."); | |
909 | } | |
910 | ||
911 | /* | |
912 | * Ignore any exception raised by the _finalize() method because | |
913 | * it won't change anything at this point: the component is | |
914 | * being destroyed anyway. | |
915 | */ | |
916 | PyErr_Clear(); | |
917 | Py_XDECREF(py_method_result); | |
918 | Py_DECREF(py_comp); | |
919 | } | |
920 | ||
921 | static void | |
922 | bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source) | |
923 | { | |
924 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
925 | bt_py3_component_class_finalize(self_component); | |
926 | } | |
927 | ||
928 | static void | |
929 | bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter) | |
930 | { | |
931 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
932 | bt_py3_component_class_finalize(self_component); | |
933 | } | |
934 | ||
935 | static void | |
936 | bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink) | |
937 | { | |
938 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
939 | bt_py3_component_class_finalize(self_component); | |
940 | } | |
941 | ||
942 | static bt_self_component_status | |
943 | bt_py3_component_class_accept_port_connection( | |
944 | bt_self_component *self_component, | |
945 | bt_self_component_port *self_component_port, | |
946 | bt_port_type self_component_port_type, | |
947 | const bt_port *other_port) | |
948 | { | |
949 | enum bt_self_component_status status; | |
950 | PyObject *py_comp = NULL; | |
951 | PyObject *py_self_port_ptr = NULL; | |
952 | PyObject *py_other_port_ptr = NULL; | |
953 | PyObject *py_method_result = NULL; | |
954 | ||
955 | py_comp = bt_self_component_get_data(self_component); | |
956 | BT_ASSERT(py_comp); | |
957 | ||
958 | swig_type_info *self_component_port_swig_type = NULL; | |
959 | swig_type_info *other_port_swig_type = NULL; | |
960 | switch (self_component_port_type) { | |
961 | case BT_PORT_TYPE_INPUT: | |
962 | self_component_port_swig_type = SWIGTYPE_p_bt_self_component_port_input; | |
963 | other_port_swig_type = SWIGTYPE_p_bt_port_output; | |
964 | break; | |
965 | case BT_PORT_TYPE_OUTPUT: | |
966 | self_component_port_swig_type = SWIGTYPE_p_bt_self_component_port_output; | |
967 | other_port_swig_type = SWIGTYPE_p_bt_port_input; | |
968 | break; | |
969 | } | |
970 | BT_ASSERT(self_component_port_swig_type != NULL); | |
971 | BT_ASSERT(other_port_swig_type != NULL); | |
972 | ||
973 | py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), | |
974 | self_component_port_swig_type, 0); | |
975 | if (!py_self_port_ptr) { | |
976 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
977 | goto error; | |
978 | } | |
979 | ||
980 | py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), | |
981 | other_port_swig_type, 0); | |
982 | if (!py_other_port_ptr) { | |
983 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
984 | goto error; | |
985 | } | |
986 | ||
987 | py_method_result = PyObject_CallMethod(py_comp, | |
988 | "_accept_port_connection_from_native", "(OiO)", py_self_port_ptr, | |
989 | self_component_port_type, py_other_port_ptr); | |
990 | ||
991 | status = bt_py3_exc_to_self_component_status(); | |
992 | if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) { | |
993 | /* Pretty sure this should never happen, but just in case */ | |
994 | BT_LOGE("User's _accept_port_connection() method failed without raising an exception: " | |
995 | "status=%d", status); | |
996 | goto error; | |
997 | } | |
998 | ||
999 | if (status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { | |
1000 | /* | |
1001 | * Looks like the user method raised | |
1002 | * PortConnectionRefused: accept this like if it | |
1003 | * returned False. | |
1004 | */ | |
1005 | goto end; | |
1006 | } else if (status != BT_SELF_COMPONENT_STATUS_OK) { | |
1007 | BT_LOGE("User's _accept_port_connection() raised an unexpected exception: " | |
1008 | "status=%d", status); | |
1009 | goto error; | |
1010 | } | |
1011 | ||
1012 | BT_ASSERT(PyBool_Check(py_method_result)); | |
1013 | ||
1014 | if (py_method_result == Py_True) { | |
1015 | status = BT_SELF_COMPONENT_STATUS_OK; | |
1016 | } else { | |
1017 | status = BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION; | |
1018 | } | |
1019 | ||
1020 | goto end; | |
1021 | ||
1022 | error: | |
1023 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
1024 | ||
1025 | /* | |
1026 | * Clear any exception: we're returning a bad status anyway. If | |
1027 | * this call originated from Python, then the user gets an | |
1028 | * appropriate error. | |
1029 | */ | |
1030 | PyErr_Clear(); | |
1031 | ||
1032 | end: | |
1033 | Py_XDECREF(py_self_port_ptr); | |
1034 | Py_XDECREF(py_other_port_ptr); | |
1035 | Py_XDECREF(py_method_result); | |
1036 | return status; | |
1037 | } | |
1038 | ||
1039 | static bt_self_component_status | |
1040 | bt_py3_component_class_source_accept_output_port_connection(bt_self_component_source *self_component_source, | |
1041 | bt_self_component_port_output *self_component_port_output, | |
1042 | const bt_port_input *other_port_input) | |
1043 | { | |
1044 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
1045 | bt_self_component_port *self_component_port = bt_self_component_port_output_as_self_component_port(self_component_port_output); | |
1046 | const bt_port *other_port = bt_port_input_as_port_const(other_port_input); | |
1047 | return bt_py3_component_class_accept_port_connection(self_component, self_component_port, BT_PORT_TYPE_OUTPUT, other_port); | |
1048 | } | |
1049 | ||
1050 | static bt_self_component_status | |
1051 | bt_py3_component_class_filter_accept_input_port_connection(bt_self_component_filter *self_component_filter, | |
1052 | bt_self_component_port_input *self_component_port_input, | |
1053 | const bt_port_output *other_port_output) | |
1054 | { | |
1055 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
1056 | bt_self_component_port *self_component_port = bt_self_component_port_input_as_self_component_port(self_component_port_input); | |
1057 | const bt_port *other_port = bt_port_output_as_port_const(other_port_output); | |
1058 | return bt_py3_component_class_accept_port_connection(self_component, self_component_port, BT_PORT_TYPE_INPUT, other_port); | |
1059 | } | |
1060 | ||
1061 | static bt_self_component_status | |
1062 | bt_py3_component_class_filter_accept_output_port_connection(bt_self_component_filter *self_component_filter, | |
1063 | bt_self_component_port_output *self_component_port_output, | |
1064 | const bt_port_input *other_port_input) | |
1065 | { | |
1066 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
1067 | bt_self_component_port *self_component_port = bt_self_component_port_output_as_self_component_port(self_component_port_output); | |
1068 | const bt_port *other_port = bt_port_input_as_port_const(other_port_input); | |
1069 | return bt_py3_component_class_accept_port_connection(self_component, self_component_port, BT_PORT_TYPE_OUTPUT, other_port); | |
1070 | } | |
1071 | ||
1072 | static bt_self_component_status | |
1073 | bt_py3_component_class_sink_accept_input_port_connection(bt_self_component_sink *self_component_sink, | |
1074 | bt_self_component_port_input *self_component_port_input, | |
1075 | const bt_port_output *other_port_output) | |
1076 | { | |
1077 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
1078 | bt_self_component_port *self_component_port = bt_self_component_port_input_as_self_component_port(self_component_port_input); | |
1079 | const bt_port *other_port = bt_port_output_as_port_const(other_port_output); | |
1080 | return bt_py3_component_class_accept_port_connection(self_component, self_component_port, BT_PORT_TYPE_INPUT, other_port); | |
1081 | } | |
1082 | ||
1083 | static bt_self_component_status | |
1084 | bt_py3_component_class_port_connected( | |
1085 | bt_self_component *self_component, | |
1086 | void *self_component_port, | |
1087 | swig_type_info *self_component_port_swig_type, | |
1088 | bt_port_type self_component_port_type, | |
1089 | const void *other_port, | |
1090 | swig_type_info *other_port_swig_type) | |
1091 | { | |
1092 | bt_self_component_status status; | |
1093 | PyObject *py_comp = NULL; | |
1094 | PyObject *py_self_port_ptr = NULL; | |
1095 | PyObject *py_other_port_ptr = NULL; | |
1096 | PyObject *py_method_result = NULL; | |
1097 | ||
1098 | py_comp = bt_self_component_get_data(self_component); | |
1099 | BT_ASSERT(py_comp); | |
1100 | ||
1101 | py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), | |
1102 | self_component_port_swig_type, 0); | |
1103 | if (!py_self_port_ptr) { | |
1104 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
1105 | status = BT_SELF_COMPONENT_STATUS_NOMEM; | |
1106 | goto end; | |
1107 | } | |
1108 | ||
1109 | py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), | |
1110 | other_port_swig_type, 0); | |
1111 | if (!py_other_port_ptr) { | |
1112 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
1113 | status = BT_SELF_COMPONENT_STATUS_NOMEM; | |
1114 | goto end; } | |
1115 | ||
1116 | py_method_result = PyObject_CallMethod(py_comp, | |
1117 | "_port_connected_from_native", "(OiO)", py_self_port_ptr, | |
1118 | self_component_port_type, py_other_port_ptr); | |
1119 | ||
1120 | BT_ASSERT(!py_method_result || py_method_result == Py_None); | |
1121 | ||
1122 | status = bt_py3_exc_to_self_component_status(); | |
1123 | ||
1124 | end: | |
1125 | Py_XDECREF(py_self_port_ptr); | |
1126 | Py_XDECREF(py_other_port_ptr); | |
1127 | Py_XDECREF(py_method_result); | |
1128 | ||
1129 | return status; | |
1130 | } | |
1131 | ||
1132 | static bt_self_component_status | |
1133 | bt_py3_component_class_source_output_port_connected( | |
1134 | bt_self_component_source *self_component_source, | |
1135 | bt_self_component_port_output *self_component_port_output, | |
1136 | const bt_port_input *other_port_input) | |
1137 | { | |
1138 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
1139 | ||
1140 | return bt_py3_component_class_port_connected( | |
1141 | self_component, | |
1142 | self_component_port_output, | |
1143 | SWIGTYPE_p_bt_self_component_port_output, | |
1144 | BT_PORT_TYPE_OUTPUT, | |
1145 | other_port_input, | |
1146 | SWIGTYPE_p_bt_port_input); | |
1147 | } | |
1148 | ||
1149 | static bt_self_component_status | |
1150 | bt_py3_component_class_filter_input_port_connected( | |
1151 | bt_self_component_filter *self_component_filter, | |
1152 | bt_self_component_port_input *self_component_port_input, | |
1153 | const bt_port_output *other_port_output) | |
1154 | { | |
1155 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
1156 | ||
1157 | return bt_py3_component_class_port_connected( | |
1158 | self_component, | |
1159 | self_component_port_input, | |
1160 | SWIGTYPE_p_bt_self_component_port_input, | |
1161 | BT_PORT_TYPE_INPUT, | |
1162 | other_port_output, | |
1163 | SWIGTYPE_p_bt_port_output); | |
1164 | } | |
1165 | ||
1166 | static bt_self_component_status | |
1167 | bt_py3_component_class_filter_output_port_connected( | |
1168 | bt_self_component_filter *self_component_filter, | |
1169 | bt_self_component_port_output *self_component_port_output, | |
1170 | const bt_port_input *other_port_input) | |
1171 | { | |
1172 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
1173 | ||
1174 | return bt_py3_component_class_port_connected( | |
1175 | self_component, | |
1176 | self_component_port_output, | |
1177 | SWIGTYPE_p_bt_self_component_port_output, | |
1178 | BT_PORT_TYPE_OUTPUT, | |
1179 | other_port_input, | |
1180 | SWIGTYPE_p_bt_port_input); | |
1181 | } | |
1182 | ||
1183 | static bt_self_component_status | |
1184 | bt_py3_component_class_sink_input_port_connected( | |
1185 | bt_self_component_sink *self_component_sink, | |
1186 | bt_self_component_port_input *self_component_port_input, | |
1187 | const bt_port_output *other_port_output) | |
1188 | { | |
1189 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
1190 | ||
1191 | return bt_py3_component_class_port_connected( | |
1192 | self_component, | |
1193 | self_component_port_input, | |
1194 | SWIGTYPE_p_bt_self_component_port_input, | |
1195 | BT_PORT_TYPE_INPUT, | |
1196 | other_port_output, | |
1197 | SWIGTYPE_p_bt_port_output); | |
1198 | } | |
1199 | ||
1200 | static bt_self_component_status | |
1201 | bt_py3_component_class_sink_graph_is_configured(bt_self_component_sink *self_component_sink) | |
1202 | { | |
1203 | PyObject *py_comp = NULL; | |
1204 | PyObject *py_method_result = NULL; | |
1205 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; | |
1206 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
1207 | ||
1208 | py_comp = bt_self_component_get_data(self_component); | |
1209 | py_method_result = PyObject_CallMethod(py_comp, | |
1210 | "_graph_is_configured_from_native", NULL); | |
1211 | ||
1212 | BT_ASSERT(!py_method_result || py_method_result == Py_None); | |
1213 | ||
1214 | status = bt_py3_exc_to_self_component_status(); | |
1215 | ||
1216 | Py_XDECREF(py_method_result); | |
1217 | ||
1218 | return status; | |
1219 | } | |
1220 | ||
1221 | static bt_query_status | |
1222 | bt_py3_component_class_query( | |
1223 | const bt_component_class *component_class, | |
1224 | const bt_query_executor *query_executor, | |
1225 | const char *object, const bt_value *params, | |
1226 | const bt_value **result) | |
1227 | { | |
1228 | PyObject *py_cls = NULL; | |
1229 | PyObject *py_params_ptr = NULL; | |
1230 | PyObject *py_query_exec_ptr = NULL; | |
1231 | PyObject *py_query_func = NULL; | |
1232 | PyObject *py_object = NULL; | |
1233 | PyObject *py_results_addr = NULL; | |
1234 | bt_query_status status = BT_QUERY_STATUS_OK; | |
1235 | ||
1236 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
1237 | if (!py_cls) { | |
1238 | BT_LOGE("Cannot find Python class associated to native component class: " | |
1239 | "comp-cls-addr=%p", component_class); | |
1240 | goto error; | |
1241 | } | |
1242 | ||
1243 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
1244 | SWIGTYPE_p_bt_value, 0); | |
1245 | if (!py_params_ptr) { | |
1246 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
1247 | goto error; | |
1248 | } | |
1249 | ||
1250 | py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor), | |
1251 | SWIGTYPE_p_bt_query_executor, 0); | |
1252 | if (!py_query_exec_ptr) { | |
1253 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
1254 | goto error; | |
1255 | } | |
1256 | ||
1257 | py_object = SWIG_FromCharPtr(object); | |
1258 | if (!py_object) { | |
1259 | BT_LOGE_STR("Failed to create a Python string."); | |
1260 | goto error; | |
1261 | } | |
1262 | ||
1263 | py_results_addr = PyObject_CallMethod(py_cls, | |
1264 | "_query_from_native", "(OOO)", py_query_exec_ptr, | |
1265 | py_object, py_params_ptr); | |
1266 | ||
1267 | if (!py_results_addr) { | |
1268 | BT_LOGE("Failed to call Python class's _query_from_native() method: " | |
1269 | "py-cls-addr=%p", py_cls); | |
1270 | status = bt_py3_exc_to_query_status(); | |
1271 | goto end; | |
1272 | } | |
1273 | ||
1274 | /* | |
1275 | * The returned object, on success, is an integer object | |
1276 | * (PyLong) containing the address of a BT value object (new | |
1277 | * reference). | |
1278 | */ | |
1279 | *result = (void *) PyLong_AsUnsignedLongLong(py_results_addr); | |
1280 | BT_ASSERT(!PyErr_Occurred()); | |
1281 | BT_ASSERT(*result); | |
1282 | goto end; | |
1283 | ||
1284 | error: | |
1285 | PyErr_Clear(); | |
1286 | status = BT_QUERY_STATUS_ERROR; | |
1287 | ||
1288 | end: | |
1289 | Py_XDECREF(py_params_ptr); | |
1290 | Py_XDECREF(py_query_exec_ptr); | |
1291 | Py_XDECREF(py_query_func); | |
1292 | Py_XDECREF(py_object); | |
1293 | Py_XDECREF(py_results_addr); | |
1294 | return status; | |
1295 | } | |
1296 | ||
1297 | static bt_query_status | |
1298 | bt_py3_component_class_source_query( | |
1299 | bt_self_component_class_source *self_component_class_source, | |
1300 | const bt_query_executor *query_executor, | |
1301 | const char *object, const bt_value *params, | |
1302 | const bt_value **result) | |
1303 | { | |
1304 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); | |
1305 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); | |
1306 | return bt_py3_component_class_query(component_class, query_executor, object, params, result); | |
1307 | } | |
1308 | ||
1309 | static bt_query_status | |
1310 | bt_py3_component_class_filter_query( | |
1311 | bt_self_component_class_filter *self_component_class_filter, | |
1312 | const bt_query_executor *query_executor, | |
1313 | const char *object, const bt_value *params, | |
1314 | const bt_value **result) | |
1315 | { | |
1316 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); | |
1317 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); | |
1318 | return bt_py3_component_class_query(component_class, query_executor, object, params, result); | |
1319 | } | |
1320 | ||
1321 | static bt_query_status | |
1322 | bt_py3_component_class_sink_query( | |
1323 | bt_self_component_class_sink *self_component_class_sink, | |
1324 | const bt_query_executor *query_executor, | |
1325 | const char *object, const bt_value *params, | |
1326 | const bt_value **result) | |
1327 | { | |
1328 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); | |
1329 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); | |
1330 | return bt_py3_component_class_query(component_class, query_executor, object, params, result); | |
1331 | } | |
1332 | ||
1333 | static bt_self_message_iterator_status | |
1334 | bt_py3_component_class_message_iterator_init( | |
1335 | bt_self_message_iterator *self_message_iterator, | |
1336 | bt_self_component *self_component, | |
1337 | bt_self_component_port_output *self_component_port_output) | |
1338 | { | |
1339 | bt_self_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK; | |
1340 | PyObject *py_comp_cls = NULL; | |
1341 | PyObject *py_iter_cls = NULL; | |
1342 | PyObject *py_iter_ptr = NULL; | |
1343 | PyObject *py_init_method_result = NULL; | |
1344 | PyObject *py_iter = NULL; | |
1345 | PyObject *py_comp; | |
1346 | ||
1347 | py_comp = bt_self_component_get_data(self_component); | |
1348 | ||
1349 | /* Find user's Python notification iterator class */ | |
1350 | py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); | |
1351 | if (!py_comp_cls) { | |
1352 | BT_LOGE_STR("Cannot get Python object's `__class__` attribute."); | |
1353 | goto error; | |
1354 | } | |
1355 | ||
1356 | py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); | |
1357 | if (!py_iter_cls) { | |
1358 | BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute."); | |
1359 | goto error; | |
1360 | } | |
1361 | ||
1362 | py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), | |
1363 | SWIGTYPE_p_bt_self_message_iterator, 0); | |
1364 | if (!py_iter_ptr) { | |
1365 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
1366 | goto error; | |
1367 | } | |
1368 | ||
1369 | /* | |
1370 | * Create object with borrowed native notification iterator | |
1371 | * reference: | |
1372 | * | |
1373 | * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) | |
1374 | */ | |
1375 | py_iter = PyObject_CallMethod(py_iter_cls, "__new__", | |
1376 | "(OO)", py_iter_cls, py_iter_ptr); | |
1377 | if (!py_iter) { | |
1378 | BT_LOGE("Failed to call Python class's __new__() method: " | |
1379 | "py-cls-addr=%p", py_iter_cls); | |
1380 | bt2_py_loge_exception(); | |
1381 | goto error; | |
1382 | } | |
1383 | ||
1384 | /* | |
1385 | * Initialize object: | |
1386 | * | |
1387 | * py_iter.__init__() | |
1388 | * | |
1389 | * At this point, py_iter._ptr is set, so this initialization | |
1390 | * function has access to self._component (which gives it the | |
1391 | * user Python component object from which the iterator was | |
1392 | * created). | |
1393 | */ | |
1394 | py_init_method_result = PyObject_CallMethod(py_iter, "__init__", NULL); | |
1395 | if (!py_init_method_result) { | |
1396 | BT_LOGE_STR("User's __init__() method failed."); | |
1397 | bt2_py_loge_exception(); | |
1398 | goto error; | |
1399 | } | |
1400 | ||
1401 | /* | |
1402 | * Since the Python code can never instantiate a user-defined | |
1403 | * notification iterator class, the native notification iterator | |
1404 | * object does NOT belong to a user Python notification iterator | |
1405 | * object (borrowed reference). However this Python object is | |
1406 | * owned by this native notification iterator object. | |
1407 | * | |
1408 | * In the Python world, the lifetime of the native notification | |
1409 | * iterator is managed by a _GenericNotificationIterator | |
1410 | * instance: | |
1411 | * | |
1412 | * _GenericNotificationIterator instance: | |
1413 | * owns a native bt_notification_iterator object (iter) | |
1414 | * owns a _UserMessageIterator instance (py_iter) | |
1415 | * self._ptr is a borrowed reference to the | |
1416 | * native bt_private_connection_private_notification_iterator | |
1417 | * object (iter) | |
1418 | */ | |
1419 | bt_self_message_iterator_set_data(self_message_iterator, py_iter); | |
1420 | py_iter = NULL; | |
1421 | goto end; | |
1422 | ||
1423 | error: | |
1424 | status = bt_py3_exc_to_self_message_iterator_status(); | |
1425 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
1426 | /* | |
1427 | * Looks like there wasn't any exception from the Python | |
1428 | * side, but we're still in an error state here. | |
1429 | */ | |
1430 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
1431 | } | |
1432 | ||
1433 | /* | |
1434 | * Clear any exception: we're returning a bad status anyway. If | |
1435 | * this call originated from Python, then the user gets an | |
1436 | * appropriate creation error. | |
1437 | */ | |
1438 | PyErr_Clear(); | |
1439 | ||
1440 | end: | |
1441 | Py_XDECREF(py_comp_cls); | |
1442 | Py_XDECREF(py_iter_cls); | |
1443 | Py_XDECREF(py_iter_ptr); | |
1444 | Py_XDECREF(py_init_method_result); | |
1445 | Py_XDECREF(py_iter); | |
1446 | return status; | |
1447 | } | |
1448 | ||
1449 | static bt_self_message_iterator_status | |
1450 | bt_py3_component_class_source_message_iterator_init( | |
1451 | bt_self_message_iterator *self_message_iterator, | |
1452 | bt_self_component_source *self_component_source, | |
1453 | bt_self_component_port_output *self_component_port_output) | |
1454 | { | |
1455 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
1456 | return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); | |
1457 | } | |
1458 | ||
1459 | static bt_self_message_iterator_status | |
1460 | bt_py3_component_class_filter_message_iterator_init( | |
1461 | bt_self_message_iterator *self_message_iterator, | |
1462 | bt_self_component_filter *self_component_filter, | |
1463 | bt_self_component_port_output *self_component_port_output) | |
1464 | { | |
1465 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
1466 | return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); | |
1467 | } | |
1468 | ||
1469 | static void | |
1470 | bt_py3_component_class_message_iterator_finalize( | |
1471 | bt_self_message_iterator *message_iterator) | |
1472 | { | |
1473 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); | |
1474 | PyObject *py_method_result = NULL; | |
1475 | ||
1476 | BT_ASSERT(py_message_iter); | |
1477 | ||
1478 | /* Call user's _finalize() method */ | |
1479 | py_method_result = PyObject_CallMethod(py_message_iter, | |
1480 | "_finalize", NULL); | |
1481 | ||
1482 | if (PyErr_Occurred()) { | |
1483 | BT_LOGW("User's _finalize() method raised an exception: ignoring."); | |
1484 | } | |
1485 | ||
1486 | /* | |
1487 | * Ignore any exception raised by the _finalize() method because | |
1488 | * it won't change anything at this point: the component is | |
1489 | * being destroyed anyway. | |
1490 | */ | |
1491 | PyErr_Clear(); | |
1492 | Py_XDECREF(py_method_result); | |
1493 | Py_DECREF(py_message_iter); | |
1494 | } | |
1495 | ||
1496 | /* Valid for both sources and filters. */ | |
1497 | ||
1498 | static bt_self_message_iterator_status | |
1499 | bt_py3_component_class_message_iterator_next( | |
1500 | bt_self_message_iterator *message_iterator, | |
1501 | bt_message_array_const msgs, uint64_t capacity, | |
1502 | uint64_t *count) | |
1503 | { | |
1504 | bt_self_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK; | |
1505 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); | |
1506 | PyObject *py_method_result = NULL; | |
1507 | ||
1508 | BT_ASSERT(py_message_iter); | |
1509 | py_method_result = PyObject_CallMethod(py_message_iter, | |
1510 | "_next_from_native", NULL); | |
1511 | if (!py_method_result) { | |
1512 | status = bt_py3_exc_to_self_message_iterator_status(); | |
1513 | BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK); | |
1514 | goto end; | |
1515 | } | |
1516 | ||
1517 | /* | |
1518 | * The returned object, on success, is an integer object | |
1519 | * (PyLong) containing the address of a native notification | |
1520 | * object (which is now ours). | |
1521 | */ | |
1522 | msgs[0] = | |
1523 | (const bt_message *) PyLong_AsUnsignedLongLong( | |
1524 | py_method_result); | |
1525 | *count = 1; | |
1526 | ||
1527 | /* Clear potential overflow error; should never happen */ | |
1528 | BT_ASSERT(!PyErr_Occurred()); | |
1529 | goto end; | |
1530 | ||
1531 | end: | |
1532 | Py_XDECREF(py_method_result); | |
1533 | return status; | |
1534 | } | |
1535 | ||
1536 | static bt_self_component_status | |
1537 | bt_py3_component_class_sink_consume(bt_self_component_sink *self_component_sink) | |
1538 | { | |
1539 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
1540 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
1541 | PyObject *py_method_result = NULL; | |
1542 | bt_self_component_status status; | |
1543 | ||
1544 | BT_ASSERT(py_comp); | |
1545 | py_method_result = PyObject_CallMethod(py_comp, | |
1546 | "_consume", NULL); | |
1547 | ||
1548 | status = bt_py3_exc_to_self_component_status(); | |
1549 | if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) { | |
1550 | /* Pretty sure this should never happen, but just in case */ | |
1551 | BT_LOGE("User's _consume() method failed without raising an exception: " | |
1552 | "status=%d", status); | |
1553 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
1554 | } | |
1555 | ||
1556 | Py_XDECREF(py_method_result); | |
1557 | return status; | |
1558 | } | |
1559 | ||
1560 | static | |
1561 | int bt_py3_component_class_set_help_and_desc( | |
1562 | bt_component_class *component_class, | |
1563 | const char *description, const char *help) | |
1564 | { | |
1565 | int ret; | |
1566 | ||
1567 | if (description) { | |
1568 | ret = bt_component_class_set_description(component_class, description); | |
1569 | if (ret) { | |
1570 | BT_LOGE("Cannot set component class's description: " | |
1571 | "comp-cls-addr=%p", component_class); | |
1572 | goto end; | |
1573 | } | |
1574 | } | |
1575 | ||
1576 | if (help) { | |
1577 | ret = bt_component_class_set_help(component_class, help); | |
1578 | if (ret) { | |
1579 | BT_LOGE("Cannot set component class's help text: " | |
1580 | "comp-cls-addr=%p", component_class); | |
1581 | goto end; | |
1582 | } | |
1583 | } | |
1584 | ||
1585 | ret = 0; | |
1586 | ||
1587 | end: | |
1588 | return ret; | |
1589 | } | |
1590 | ||
1591 | static | |
1592 | bt_component_class_source *bt_py3_component_class_source_create( | |
1593 | PyObject *py_cls, const char *name, const char *description, | |
1594 | const char *help) | |
1595 | { | |
1596 | bt_component_class_source *component_class_source; | |
1597 | bt_component_class *component_class; | |
1598 | int ret; | |
1599 | ||
1600 | BT_ASSERT(py_cls); | |
1601 | ||
1602 | component_class_source = bt_component_class_source_create(name, | |
1603 | bt_py3_component_class_message_iterator_next); | |
1604 | if (!component_class_source) { | |
1605 | BT_LOGE_STR("Cannot create source component class."); | |
1606 | goto end; | |
1607 | } | |
1608 | ||
1609 | component_class = bt_component_class_source_as_component_class(component_class_source); | |
1610 | ||
1611 | if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) { | |
1612 | goto end; | |
1613 | } | |
1614 | ||
1615 | ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init); | |
1616 | BT_ASSERT(ret == 0); | |
1617 | ret = bt_component_class_source_set_finalize_method (component_class_source, bt_py3_component_class_source_finalize); | |
1618 | BT_ASSERT(ret == 0); | |
1619 | ret = bt_component_class_source_set_accept_output_port_connection_method(component_class_source, | |
1620 | bt_py3_component_class_source_accept_output_port_connection); | |
1621 | BT_ASSERT(ret == 0); | |
1622 | ret = bt_component_class_source_set_output_port_connected_method(component_class_source, | |
1623 | bt_py3_component_class_source_output_port_connected); | |
1624 | BT_ASSERT(ret == 0); | |
1625 | ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query); | |
1626 | BT_ASSERT(ret == 0); | |
1627 | ret = bt_component_class_source_set_message_iterator_init_method( | |
1628 | component_class_source, bt_py3_component_class_source_message_iterator_init); | |
1629 | BT_ASSERT(ret == 0); | |
1630 | ret = bt_component_class_source_set_message_iterator_finalize_method( | |
1631 | component_class_source, bt_py3_component_class_message_iterator_finalize); | |
1632 | BT_ASSERT(ret == 0); | |
1633 | ||
1634 | register_cc_ptr_to_py_cls(component_class, py_cls); | |
1635 | ||
1636 | end: | |
1637 | return component_class_source; | |
1638 | } | |
1639 | ||
1640 | static | |
1641 | bt_component_class_filter *bt_py3_component_class_filter_create( | |
1642 | PyObject *py_cls, const char *name, const char *description, | |
1643 | const char *help) | |
1644 | { | |
1645 | bt_component_class *component_class; | |
1646 | bt_component_class_filter *component_class_filter; | |
1647 | int ret; | |
1648 | ||
1649 | BT_ASSERT(py_cls); | |
1650 | ||
1651 | component_class_filter = bt_component_class_filter_create(name, | |
1652 | bt_py3_component_class_message_iterator_next); | |
1653 | if (!component_class_filter) { | |
1654 | BT_LOGE_STR("Cannot create filter component class."); | |
1655 | goto end; | |
1656 | } | |
1657 | ||
1658 | component_class = bt_component_class_filter_as_component_class(component_class_filter); | |
1659 | ||
1660 | if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) { | |
1661 | goto end; | |
1662 | } | |
1663 | ||
1664 | ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init); | |
1665 | BT_ASSERT(ret == 0); | |
1666 | ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize); | |
1667 | BT_ASSERT(ret == 0); | |
1668 | ret = bt_component_class_filter_set_accept_input_port_connection_method(component_class_filter, | |
1669 | bt_py3_component_class_filter_accept_input_port_connection); | |
1670 | BT_ASSERT(ret == 0); | |
1671 | ret = bt_component_class_filter_set_accept_output_port_connection_method(component_class_filter, | |
1672 | bt_py3_component_class_filter_accept_output_port_connection); | |
1673 | BT_ASSERT(ret == 0); | |
1674 | ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, | |
1675 | bt_py3_component_class_filter_input_port_connected); | |
1676 | BT_ASSERT(ret == 0); | |
1677 | ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter, | |
1678 | bt_py3_component_class_filter_output_port_connected); | |
1679 | BT_ASSERT(ret == 0); | |
1680 | ret = bt_component_class_filter_set_query_method(component_class_filter, bt_py3_component_class_filter_query); | |
1681 | BT_ASSERT(ret == 0); | |
1682 | ret = bt_component_class_filter_set_message_iterator_init_method( | |
1683 | component_class_filter, bt_py3_component_class_filter_message_iterator_init); | |
1684 | BT_ASSERT(ret == 0); | |
1685 | ret = bt_component_class_filter_set_message_iterator_finalize_method( | |
1686 | component_class_filter, bt_py3_component_class_message_iterator_finalize); | |
1687 | BT_ASSERT(ret == 0); | |
1688 | ||
1689 | register_cc_ptr_to_py_cls(component_class, py_cls); | |
1690 | ||
1691 | end: | |
1692 | return component_class_filter; | |
1693 | } | |
1694 | ||
1695 | static | |
1696 | bt_component_class_sink *bt_py3_component_class_sink_create( | |
1697 | PyObject *py_cls, const char *name, const char *description, | |
1698 | const char *help) | |
1699 | { | |
1700 | bt_component_class_sink *component_class_sink; | |
1701 | bt_component_class *component_class; | |
1702 | int ret; | |
1703 | ||
1704 | BT_ASSERT(py_cls); | |
1705 | ||
1706 | component_class_sink = bt_component_class_sink_create(name, bt_py3_component_class_sink_consume); | |
1707 | ||
1708 | if (!component_class_sink) { | |
1709 | BT_LOGE_STR("Cannot create sink component class."); | |
1710 | goto end; | |
1711 | } | |
1712 | ||
1713 | component_class = bt_component_class_sink_as_component_class(component_class_sink); | |
1714 | ||
1715 | if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) { | |
1716 | goto end; | |
1717 | } | |
1718 | ||
1719 | ret = bt_component_class_sink_set_init_method(component_class_sink, bt_py3_component_class_sink_init); | |
1720 | BT_ASSERT(ret == 0); | |
1721 | ret = bt_component_class_sink_set_finalize_method(component_class_sink, bt_py3_component_class_sink_finalize); | |
1722 | BT_ASSERT(ret == 0); | |
1723 | ret = bt_component_class_sink_set_accept_input_port_connection_method(component_class_sink, | |
1724 | bt_py3_component_class_sink_accept_input_port_connection); | |
1725 | BT_ASSERT(ret == 0); | |
1726 | ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink, | |
1727 | bt_py3_component_class_sink_input_port_connected); | |
1728 | BT_ASSERT(ret == 0); | |
1729 | ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink, | |
1730 | bt_py3_component_class_sink_graph_is_configured); | |
1731 | BT_ASSERT(ret == 0); | |
1732 | ret = bt_component_class_sink_set_query_method(component_class_sink, bt_py3_component_class_sink_query); | |
1733 | BT_ASSERT(ret == 0); | |
1734 | ||
1735 | register_cc_ptr_to_py_cls(component_class, py_cls); | |
1736 | ||
1737 | end: | |
1738 | return component_class_sink; | |
1739 | } | |
1740 | %} | |
1741 | ||
1742 | struct bt_component_class_source *bt_py3_component_class_source_create( | |
1743 | PyObject *py_cls, const char *name, const char *description, | |
1744 | const char *help); | |
1745 | struct bt_component_class_filter *bt_py3_component_class_filter_create( | |
1746 | PyObject *py_cls, const char *name, const char *description, | |
1747 | const char *help); | |
1748 | struct bt_component_class_sink *bt_py3_component_class_sink_create( | |
1749 | PyObject *py_cls, const char *name, const char *description, | |
1750 | const char *help); | |
1751 | void bt_py3_cc_init_from_bt2(void); | |
1752 | void bt_py3_cc_exit_handler(void); |