2 * The MIT License (MIT)
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 /* From component-class-const.h */
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;
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;
38 extern const char *bt_component_class_get_name(
39 const bt_component_class *component_class);
41 extern const char *bt_component_class_get_description(
42 const bt_component_class *component_class);
44 extern const char *bt_component_class_get_help(
45 const bt_component_class *component_class);
47 extern bt_component_class_type bt_component_class_get_type(
48 const bt_component_class *component_class);
50 bt_bool bt_component_class_is_source(
51 const bt_component_class *component_class);
53 bt_bool bt_component_class_is_filter(
54 const bt_component_class *component_class);
56 bt_bool bt_component_class_is_sink(
57 const bt_component_class *component_class);
59 extern void bt_component_class_get_ref(
60 const bt_component_class *component_class);
62 extern void bt_component_class_put_ref(
63 const bt_component_class *component_class);
65 /* From component-class-source-const.h */
67 const bt_component_class *
68 bt_component_class_source_as_component_class_const(
69 const bt_component_class_source *comp_cls_source);
71 extern void bt_component_class_source_get_ref(
72 const bt_component_class_source *component_class_source);
74 extern void bt_component_class_source_put_ref(
75 const bt_component_class_source *component_class_source);
77 /* From component-class-source.h */
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);
84 typedef void (*bt_component_class_source_finalize_method)(
85 bt_self_component_source *self_component);
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);
94 (*bt_component_class_source_message_iterator_finalize_method)(
95 bt_self_message_iterator *message_iterator);
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,
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);
108 typedef bt_self_message_iterator_status
109 (*bt_component_class_source_message_iterator_seek_beginning_method)(
110 bt_self_message_iterator *message_iterator);
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);
118 (*bt_component_class_source_message_iterator_can_seek_beginning_method)(
119 bt_self_message_iterator *message_iterator);
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);
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);
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);
139 bt_component_class *bt_component_class_source_as_component_class(
140 bt_component_class_source *comp_cls_source);
143 bt_component_class_source *bt_component_class_source_create(
145 bt_component_class_source_message_iterator_next_method method);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
202 /* From component-class-filter-const.h */
204 const bt_component_class *
205 bt_component_class_filter_as_component_class_const(
206 const bt_component_class_filter *comp_cls_filter);
208 extern void bt_component_class_filter_get_ref(
209 const bt_component_class_filter *component_class_filter);
211 extern void bt_component_class_filter_put_ref(
212 const bt_component_class_filter *component_class_filter);
214 /* From component-class-filter.h */
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);
221 typedef void (*bt_component_class_filter_finalize_method)(
222 bt_self_component_filter *self_component);
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);
231 (*bt_component_class_filter_message_iterator_finalize_method)(
232 bt_self_message_iterator *message_iterator);
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,
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);
245 typedef bt_self_message_iterator_status
246 (*bt_component_class_filter_message_iterator_seek_beginning_method)(
247 bt_self_message_iterator *message_iterator);
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);
255 (*bt_component_class_filter_message_iterator_can_seek_beginning_method)(
256 bt_self_message_iterator *message_iterator);
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);
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);
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);
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);
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);
289 bt_component_class *bt_component_class_filter_as_component_class(
290 bt_component_class_filter *comp_cls_filter);
293 bt_component_class_filter *bt_component_class_filter_create(
295 bt_component_class_filter_message_iterator_next_method method);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
362 /* From component-class-sink-const.h */
364 const bt_component_class *
365 bt_component_class_sink_as_component_class_const(
366 const bt_component_class_sink *comp_cls_sink);
368 extern void bt_component_class_sink_get_ref(
369 const bt_component_class_sink *component_class_sink);
371 extern void bt_component_class_sink_put_ref(
372 const bt_component_class_sink *component_class_sink);
374 /* From component-class-sink.h */
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);
380 typedef void (*bt_component_class_sink_finalize_method)(
381 bt_self_component_sink *self_component);
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);
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);
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);
402 typedef bt_self_component_status
403 (*bt_component_class_sink_graph_is_configured_method)(
404 bt_self_component_sink *self_component);
406 typedef bt_self_component_status (*bt_component_class_sink_consume_method)(
407 bt_self_component_sink *self_component);
409 bt_component_class *bt_component_class_sink_as_component_class(
410 bt_component_class_sink *comp_cls_sink);
413 bt_component_class_sink *bt_component_class_sink_create(
415 bt_component_class_sink_consume_method method);
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);
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);
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);
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);
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);
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);
444 /* From self-component-class-source.h */
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);
450 /* From self-component-class-filter.h */
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);
456 /* From self-component-class-sink.h */
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);
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.
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()
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.
481 static GHashTable *bt_cc_ptr_to_py_cls;
483 static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
486 if (!bt_cc_ptr_to_py_cls) {
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()
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);
497 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
501 static PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
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);
509 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
510 (gconstpointer) bt_cc);
515 * Useful Python objects.
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_msg_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;
527 static void bt_py3_cc_init_from_bt2(void)
530 * This is called once the bt2 package is loaded.
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.
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);
559 static void bt_py3_cc_exit_handler(void)
562 * This is an exit handler (set by the bt2 package).
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.
571 * We cannot do this in the library's destructor because it
572 * gets executed once Python is already finalized.
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_msg_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);
585 /* Library destructor */
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);
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.
600 void bt2_py_loge_exception(void)
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;
611 BT_ASSERT(PyErr_Occurred() != NULL);
613 PyErr_Fetch(&type, &value, &traceback);
615 BT_ASSERT(type != NULL);
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.:
621 * TypeError: _accept_port_connection_from_native() takes 3 positional arguments but 4 were given
625 /* Make sure `value` is what we expected - an instance of `type`. */
626 PyErr_NormalizeException(&type, &value, &traceback);
628 traceback_module = PyImport_ImportModule("traceback");
629 if (!traceback_module) {
630 BT_LOGE_STR("Failed to log Python exception (could not import traceback module).");
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).");
641 if (!PyCallable_Check(format_exception_func)) {
642 BT_LOGE_STR("Failed to log Python exception (format_exception is not callable).");
646 exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, type, value, traceback, NULL);
649 BT_LOGE_STR("Failed to log Python exception (call to format_exception failed).");
653 msg_buf = g_string_new(NULL);
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);
659 BT_LOGE_STR("Failed to log Python exception (failed to convert exception to string).");
663 g_string_append(msg_buf, str);
666 BT_LOGE_STR(msg_buf->str);
670 g_string_free(msg_buf, TRUE);
672 Py_XDECREF(exc_str_list);
673 Py_XDECREF(format_exception_func);
674 Py_XDECREF(traceback_module);
676 /* PyErr_Restore takes our references. */
677 PyErr_Restore(type, value, traceback);
680 static bt_self_component_status bt_py3_exc_to_self_component_status(void)
682 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
683 PyObject *exc = PyErr_Occurred();
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;
699 bt2_py_loge_exception();
700 status = BT_SELF_COMPONENT_STATUS_ERROR;
708 /* Component class proxy methods (delegate to the attached Python object) */
710 static bt_self_message_iterator_status
711 bt_py3_exc_to_self_message_iterator_status(void)
713 enum bt_self_message_iterator_status status =
714 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
715 PyObject *exc = PyErr_Occurred();
721 if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) {
722 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
723 } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) {
724 status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN;
726 bt2_py_loge_exception();
727 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
735 static enum bt_query_status bt_py3_exc_to_query_status(void)
737 enum bt_query_status status = BT_QUERY_STATUS_OK;
738 PyObject *exc = PyErr_Occurred();
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;
754 bt2_py_loge_exception();
755 status = BT_QUERY_STATUS_ERROR;
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)
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;
779 (void) init_method_data;
781 BT_ASSERT(self_component);
782 BT_ASSERT(self_component_v);
783 BT_ASSERT(self_comp_cls_type_swig_type);
786 * Get the user-defined Python class which created this
787 * component's class in the first place (borrowed
790 py_cls = lookup_cc_ptr_to_py_cls(component_class);
792 BT_LOGE("Cannot find Python class associated to native component class: "
793 "comp-cls-addr=%p", component_class);
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.");
805 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
806 self_comp_cls_type_swig_type, 0);
808 BT_LOGE_STR("Failed to create a SWIG pointer object.");
813 * Do the equivalent of this:
815 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
817 * _UserComponentType._init_from_native() calls the Python
818 * component object's __init__() function.
820 py_comp = PyObject_CallMethod(py_cls,
821 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
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);
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.
835 bt_self_component_set_data(self_component, py_comp);
840 status = BT_SELF_COMPONENT_STATUS_ERROR;
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.
852 Py_XDECREF(py_params_ptr);
853 Py_XDECREF(py_comp_ptr);
858 * Method of bt_component_class_source to initialize a bt_self_component_source
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)
866 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
867 return bt_py3_component_class_init(
869 self_component_source,
870 SWIGTYPE_p_bt_self_component_source,
871 params, init_method_data);
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)
878 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
879 return bt_py3_component_class_init(
881 self_component_filter,
882 SWIGTYPE_p_bt_self_component_filter,
883 params, init_method_data);
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)
890 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
891 return bt_py3_component_class_init(
894 SWIGTYPE_p_bt_self_component_sink,
895 params, init_method_data);
898 static void bt_py3_component_class_finalize(bt_self_component *self_component)
900 PyObject *py_comp = bt_self_component_get_data(self_component);
903 /* Call user's _finalize() method */
904 PyObject *py_method_result = PyObject_CallMethod(py_comp,
907 if (PyErr_Occurred()) {
908 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
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.
917 Py_XDECREF(py_method_result);
922 bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source)
924 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
925 bt_py3_component_class_finalize(self_component);
929 bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter)
931 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
932 bt_py3_component_class_finalize(self_component);
936 bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink)
938 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
939 bt_py3_component_class_finalize(self_component);
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)
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;
955 py_comp = bt_self_component_get_data(self_component);
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;
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;
970 BT_ASSERT(self_component_port_swig_type != NULL);
971 BT_ASSERT(other_port_swig_type != NULL);
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.");
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.");
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);
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);
999 if (status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
1001 * Looks like the user method raised
1002 * PortConnectionRefused: accept this like if it
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);
1012 BT_ASSERT(PyBool_Check(py_method_result));
1014 if (py_method_result == Py_True) {
1015 status = BT_SELF_COMPONENT_STATUS_OK;
1017 status = BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION;
1023 status = BT_SELF_COMPONENT_STATUS_ERROR;
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.
1033 Py_XDECREF(py_self_port_ptr);
1034 Py_XDECREF(py_other_port_ptr);
1035 Py_XDECREF(py_method_result);
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)
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);
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)
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);
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)
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);
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)
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);
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)
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;
1098 py_comp = bt_self_component_get_data(self_component);
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;
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;
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);
1120 BT_ASSERT(!py_method_result || py_method_result == Py_None);
1122 status = bt_py3_exc_to_self_component_status();
1125 Py_XDECREF(py_self_port_ptr);
1126 Py_XDECREF(py_other_port_ptr);
1127 Py_XDECREF(py_method_result);
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)
1138 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
1140 return bt_py3_component_class_port_connected(
1142 self_component_port_output,
1143 SWIGTYPE_p_bt_self_component_port_output,
1144 BT_PORT_TYPE_OUTPUT,
1146 SWIGTYPE_p_bt_port_input);
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)
1155 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
1157 return bt_py3_component_class_port_connected(
1159 self_component_port_input,
1160 SWIGTYPE_p_bt_self_component_port_input,
1163 SWIGTYPE_p_bt_port_output);
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)
1172 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
1174 return bt_py3_component_class_port_connected(
1176 self_component_port_output,
1177 SWIGTYPE_p_bt_self_component_port_output,
1178 BT_PORT_TYPE_OUTPUT,
1180 SWIGTYPE_p_bt_port_input);
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)
1189 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1191 return bt_py3_component_class_port_connected(
1193 self_component_port_input,
1194 SWIGTYPE_p_bt_self_component_port_input,
1197 SWIGTYPE_p_bt_port_output);
1200 static bt_self_component_status
1201 bt_py3_component_class_sink_graph_is_configured(bt_self_component_sink *self_component_sink)
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);
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);
1212 BT_ASSERT(!py_method_result || py_method_result == Py_None);
1214 status = bt_py3_exc_to_self_component_status();
1216 Py_XDECREF(py_method_result);
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)
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;
1236 py_cls = lookup_cc_ptr_to_py_cls(component_class);
1238 BT_LOGE("Cannot find Python class associated to native component class: "
1239 "comp-cls-addr=%p", component_class);
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.");
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.");
1257 py_object = SWIG_FromCharPtr(object);
1259 BT_LOGE_STR("Failed to create a Python string.");
1263 py_results_addr = PyObject_CallMethod(py_cls,
1264 "_query_from_native", "(OOO)", py_query_exec_ptr,
1265 py_object, py_params_ptr);
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();
1275 * The returned object, on success, is an integer object
1276 * (PyLong) containing the address of a BT value object (new
1279 *result = PyLong_AsVoidPtr(py_results_addr);
1280 BT_ASSERT(!PyErr_Occurred());
1286 status = BT_QUERY_STATUS_ERROR;
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);
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)
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);
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)
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);
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)
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);
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)
1339 bt_self_message_iterator_status status = BT_SELF_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_component_port_output_ptr = NULL;
1344 PyObject *py_init_method_result = NULL;
1345 PyObject *py_iter = NULL;
1348 py_comp = bt_self_component_get_data(self_component);
1350 /* Find user's Python message iterator class */
1351 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
1353 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
1357 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
1359 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
1363 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
1364 SWIGTYPE_p_bt_self_message_iterator, 0);
1366 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1371 * Create object with borrowed native message iterator
1374 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
1376 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
1377 "(OO)", py_iter_cls, py_iter_ptr);
1379 BT_LOGE("Failed to call Python class's __new__() method: "
1380 "py-cls-addr=%p", py_iter_cls);
1381 bt2_py_loge_exception();
1386 * Initialize object:
1388 * py_iter.__init__(self_output_port)
1390 * through the _init_for_native helper static method.
1392 * At this point, py_iter._ptr is set, so this initialization
1393 * function has access to self._component (which gives it the
1394 * user Python component object from which the iterator was
1397 py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output),
1398 SWIGTYPE_p_bt_self_component_port_output, 0);
1399 if (!py_component_port_output_ptr) {
1400 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1404 py_init_method_result = PyObject_CallMethod(py_iter, "_init_from_native", "O", py_component_port_output_ptr);
1405 if (!py_init_method_result) {
1406 BT_LOGE_STR("User's __init__() method failed.");
1407 bt2_py_loge_exception();
1412 * Since the Python code can never instantiate a user-defined
1413 * message iterator class, the native message iterator
1414 * object does NOT belong to a user Python message iterator
1415 * object (borrowed reference). However this Python object is
1416 * owned by this native message iterator object.
1418 * In the Python world, the lifetime of the native message
1419 * iterator is managed by a _GenericMessageIterator
1422 * _GenericMessageIterator instance:
1423 * owns a native bt_message_iterator object (iter)
1424 * owns a _UserMessageIterator instance (py_iter)
1425 * self._ptr is a borrowed reference to the
1426 * native bt_private_connection_private_message_iterator
1429 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1434 status = bt_py3_exc_to_self_message_iterator_status();
1435 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
1437 * Looks like there wasn't any exception from the Python
1438 * side, but we're still in an error state here.
1440 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
1444 * Clear any exception: we're returning a bad status anyway. If
1445 * this call originated from Python, then the user gets an
1446 * appropriate creation error.
1451 Py_XDECREF(py_comp_cls);
1452 Py_XDECREF(py_iter_cls);
1453 Py_XDECREF(py_iter_ptr);
1454 Py_XDECREF(py_component_port_output_ptr);
1455 Py_XDECREF(py_init_method_result);
1456 Py_XDECREF(py_iter);
1460 static bt_self_message_iterator_status
1461 bt_py3_component_class_source_message_iterator_init(
1462 bt_self_message_iterator *self_message_iterator,
1463 bt_self_component_source *self_component_source,
1464 bt_self_component_port_output *self_component_port_output)
1466 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
1467 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1470 static bt_self_message_iterator_status
1471 bt_py3_component_class_filter_message_iterator_init(
1472 bt_self_message_iterator *self_message_iterator,
1473 bt_self_component_filter *self_component_filter,
1474 bt_self_component_port_output *self_component_port_output)
1476 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
1477 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1481 bt_py3_component_class_message_iterator_finalize(
1482 bt_self_message_iterator *message_iterator)
1484 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1485 PyObject *py_method_result = NULL;
1487 BT_ASSERT(py_message_iter);
1489 /* Call user's _finalize() method */
1490 py_method_result = PyObject_CallMethod(py_message_iter,
1493 if (PyErr_Occurred()) {
1494 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
1498 * Ignore any exception raised by the _finalize() method because
1499 * it won't change anything at this point: the component is
1500 * being destroyed anyway.
1503 Py_XDECREF(py_method_result);
1504 Py_DECREF(py_message_iter);
1507 /* Valid for both sources and filters. */
1509 static bt_self_message_iterator_status
1510 bt_py3_component_class_message_iterator_next(
1511 bt_self_message_iterator *message_iterator,
1512 bt_message_array_const msgs, uint64_t capacity,
1515 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
1516 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1517 PyObject *py_method_result = NULL;
1519 BT_ASSERT(py_message_iter);
1520 py_method_result = PyObject_CallMethod(py_message_iter,
1521 "_next_from_native", NULL);
1522 if (!py_method_result) {
1523 status = bt_py3_exc_to_self_message_iterator_status();
1524 BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK);
1529 * The returned object, on success, is an integer object
1530 * (PyLong) containing the address of a native message
1531 * object (which is now ours).
1533 msgs[0] = PyLong_AsVoidPtr(py_method_result);
1536 /* Clear potential overflow error; should never happen */
1537 BT_ASSERT(!PyErr_Occurred());
1541 Py_XDECREF(py_method_result);
1545 static bt_self_component_status
1546 bt_py3_component_class_sink_consume(bt_self_component_sink *self_component_sink)
1548 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1549 PyObject *py_comp = bt_self_component_get_data(self_component);
1550 PyObject *py_method_result = NULL;
1551 bt_self_component_status status;
1554 py_method_result = PyObject_CallMethod(py_comp,
1557 status = bt_py3_exc_to_self_component_status();
1558 if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) {
1559 /* Pretty sure this should never happen, but just in case */
1560 BT_LOGE("User's _consume() method failed without raising an exception: "
1561 "status=%d", status);
1562 status = BT_SELF_COMPONENT_STATUS_ERROR;
1565 Py_XDECREF(py_method_result);
1570 int bt_py3_component_class_set_help_and_desc(
1571 bt_component_class *component_class,
1572 const char *description, const char *help)
1577 ret = bt_component_class_set_description(component_class, description);
1579 BT_LOGE("Cannot set component class's description: "
1580 "comp-cls-addr=%p", component_class);
1586 ret = bt_component_class_set_help(component_class, help);
1588 BT_LOGE("Cannot set component class's help text: "
1589 "comp-cls-addr=%p", component_class);
1601 bt_component_class_source *bt_py3_component_class_source_create(
1602 PyObject *py_cls, const char *name, const char *description,
1605 bt_component_class_source *component_class_source;
1606 bt_component_class *component_class;
1611 component_class_source = bt_component_class_source_create(name,
1612 bt_py3_component_class_message_iterator_next);
1613 if (!component_class_source) {
1614 BT_LOGE_STR("Cannot create source component class.");
1618 component_class = bt_component_class_source_as_component_class(component_class_source);
1620 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1624 ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init);
1625 BT_ASSERT(ret == 0);
1626 ret = bt_component_class_source_set_finalize_method (component_class_source, bt_py3_component_class_source_finalize);
1627 BT_ASSERT(ret == 0);
1628 ret = bt_component_class_source_set_accept_output_port_connection_method(component_class_source,
1629 bt_py3_component_class_source_accept_output_port_connection);
1630 BT_ASSERT(ret == 0);
1631 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1632 bt_py3_component_class_source_output_port_connected);
1633 BT_ASSERT(ret == 0);
1634 ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query);
1635 BT_ASSERT(ret == 0);
1636 ret = bt_component_class_source_set_message_iterator_init_method(
1637 component_class_source, bt_py3_component_class_source_message_iterator_init);
1638 BT_ASSERT(ret == 0);
1639 ret = bt_component_class_source_set_message_iterator_finalize_method(
1640 component_class_source, bt_py3_component_class_message_iterator_finalize);
1641 BT_ASSERT(ret == 0);
1643 register_cc_ptr_to_py_cls(component_class, py_cls);
1646 return component_class_source;
1650 bt_component_class_filter *bt_py3_component_class_filter_create(
1651 PyObject *py_cls, const char *name, const char *description,
1654 bt_component_class *component_class;
1655 bt_component_class_filter *component_class_filter;
1660 component_class_filter = bt_component_class_filter_create(name,
1661 bt_py3_component_class_message_iterator_next);
1662 if (!component_class_filter) {
1663 BT_LOGE_STR("Cannot create filter component class.");
1667 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1669 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1673 ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init);
1674 BT_ASSERT(ret == 0);
1675 ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize);
1676 BT_ASSERT(ret == 0);
1677 ret = bt_component_class_filter_set_accept_input_port_connection_method(component_class_filter,
1678 bt_py3_component_class_filter_accept_input_port_connection);
1679 BT_ASSERT(ret == 0);
1680 ret = bt_component_class_filter_set_accept_output_port_connection_method(component_class_filter,
1681 bt_py3_component_class_filter_accept_output_port_connection);
1682 BT_ASSERT(ret == 0);
1683 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1684 bt_py3_component_class_filter_input_port_connected);
1685 BT_ASSERT(ret == 0);
1686 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1687 bt_py3_component_class_filter_output_port_connected);
1688 BT_ASSERT(ret == 0);
1689 ret = bt_component_class_filter_set_query_method(component_class_filter, bt_py3_component_class_filter_query);
1690 BT_ASSERT(ret == 0);
1691 ret = bt_component_class_filter_set_message_iterator_init_method(
1692 component_class_filter, bt_py3_component_class_filter_message_iterator_init);
1693 BT_ASSERT(ret == 0);
1694 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1695 component_class_filter, bt_py3_component_class_message_iterator_finalize);
1696 BT_ASSERT(ret == 0);
1698 register_cc_ptr_to_py_cls(component_class, py_cls);
1701 return component_class_filter;
1705 bt_component_class_sink *bt_py3_component_class_sink_create(
1706 PyObject *py_cls, const char *name, const char *description,
1709 bt_component_class_sink *component_class_sink;
1710 bt_component_class *component_class;
1715 component_class_sink = bt_component_class_sink_create(name, bt_py3_component_class_sink_consume);
1717 if (!component_class_sink) {
1718 BT_LOGE_STR("Cannot create sink component class.");
1722 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1724 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1728 ret = bt_component_class_sink_set_init_method(component_class_sink, bt_py3_component_class_sink_init);
1729 BT_ASSERT(ret == 0);
1730 ret = bt_component_class_sink_set_finalize_method(component_class_sink, bt_py3_component_class_sink_finalize);
1731 BT_ASSERT(ret == 0);
1732 ret = bt_component_class_sink_set_accept_input_port_connection_method(component_class_sink,
1733 bt_py3_component_class_sink_accept_input_port_connection);
1734 BT_ASSERT(ret == 0);
1735 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1736 bt_py3_component_class_sink_input_port_connected);
1737 BT_ASSERT(ret == 0);
1738 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1739 bt_py3_component_class_sink_graph_is_configured);
1740 BT_ASSERT(ret == 0);
1741 ret = bt_component_class_sink_set_query_method(component_class_sink, bt_py3_component_class_sink_query);
1742 BT_ASSERT(ret == 0);
1744 register_cc_ptr_to_py_cls(component_class, py_cls);
1747 return component_class_sink;
1751 struct bt_component_class_source *bt_py3_component_class_source_create(
1752 PyObject *py_cls, const char *name, const char *description,
1754 struct bt_component_class_filter *bt_py3_component_class_filter_create(
1755 PyObject *py_cls, const char *name, const char *description,
1757 struct bt_component_class_sink *bt_py3_component_class_sink_create(
1758 PyObject *py_cls, const char *name, const char *description,
1760 void bt_py3_cc_init_from_bt2(void);
1761 void bt_py3_cc_exit_handler(void);