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 %include <babeltrace2/graph/component-class.h>
26 %include <babeltrace2/graph/component-class-const.h>
27 %include <babeltrace2/graph/component-class-source-const.h>
28 %include <babeltrace2/graph/component-class-source.h>
29 %include <babeltrace2/graph/component-class-filter-const.h>
30 %include <babeltrace2/graph/component-class-filter.h>
31 %include <babeltrace2/graph/component-class-sink-const.h>
32 %include <babeltrace2/graph/component-class-sink.h>
33 %include <babeltrace2/graph/self-component-class-source.h>
34 %include <babeltrace2/graph/self-component-class-filter.h>
35 %include <babeltrace2/graph/self-component-class-sink.h>
39 * This hash table associates a BT component class object address to a
40 * user-defined Python class (PyObject *). The keys and values are NOT
41 * owned by this hash table. The Python class objects are owned by the
42 * Python module, which should not be unloaded until it is not possible
43 * to create a user Python component anyway.
45 * This hash table is written to when a user-defined Python component
46 * class is created by one of the bt_bt2_component_class_*_create()
49 * This function is read from when a user calls bt_component_create()
50 * with a component class pointer created by one of the functions above.
51 * In this case, the original Python class needs to be found to
52 * instantiate it and associate the created Python component object with
53 * a BT component object instance.
56 static GHashTable *bt_cc_ptr_to_py_cls;
59 void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
62 if (!bt_cc_ptr_to_py_cls) {
64 * Lazy-initializing this GHashTable because GLib
65 * might not be initialized yet and it needs to be
66 * before we call g_hash_table_new()
68 BT_LOGD_STR("Creating native component class to Python component class hash table.");
69 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
70 BT_ASSERT(bt_cc_ptr_to_py_cls);
73 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
78 PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
80 if (!bt_cc_ptr_to_py_cls) {
81 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
82 "comp-cls-addr=%p", bt_cc);
86 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
87 (gconstpointer) bt_cc);
92 * Useful Python objects.
95 static PyObject *py_mod_bt2 = NULL;
96 static PyObject *py_mod_bt2_exc_error_type = NULL;
97 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
98 static PyObject *py_mod_bt2_exc_stop_type = NULL;
99 static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
100 static PyObject *py_mod_bt2_exc_invalid_object_type = NULL;
101 static PyObject *py_mod_bt2_exc_invalid_params_type = NULL;
102 static PyObject *py_mod_bt2_exc_unsupported_type = NULL;
105 void bt_bt2_cc_init_from_bt2(void)
108 * This is called once the bt2 package is loaded.
110 * Those modules and functions are needed while the package is
111 * used. Loading them here is safe because we know the bt2
112 * package is imported, and we know that the user cannot use the
113 * code here without importing bt2 first.
115 py_mod_bt2 = PyImport_ImportModule("bt2");
116 BT_ASSERT(py_mod_bt2);
117 py_mod_bt2_exc_error_type =
118 PyObject_GetAttrString(py_mod_bt2, "Error");
119 BT_ASSERT(py_mod_bt2_exc_error_type);
120 py_mod_bt2_exc_try_again_type =
121 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
122 BT_ASSERT(py_mod_bt2_exc_try_again_type);
123 py_mod_bt2_exc_stop_type =
124 PyObject_GetAttrString(py_mod_bt2, "Stop");
125 BT_ASSERT(py_mod_bt2_exc_stop_type);
126 py_mod_bt2_exc_invalid_object_type =
127 PyObject_GetAttrString(py_mod_bt2, "InvalidObject");
128 BT_ASSERT(py_mod_bt2_exc_invalid_object_type);
129 py_mod_bt2_exc_invalid_params_type =
130 PyObject_GetAttrString(py_mod_bt2, "InvalidParams");
131 BT_ASSERT(py_mod_bt2_exc_invalid_params_type);
132 py_mod_bt2_exc_unsupported_type =
133 PyObject_GetAttrString(py_mod_bt2, "Unsupported");
134 BT_ASSERT(py_mod_bt2_exc_unsupported_type);
138 void bt_bt2_cc_exit_handler(void)
141 * This is an exit handler (set by the bt2 package).
143 * We only give back the references that we took in
144 * bt_bt2_cc_init_from_bt2() here. The global variables continue
145 * to exist for the code of this file, but they are now borrowed
146 * references. If this code is executed, it means that somehow
147 * the modules are still loaded, so it should be safe to use
148 * them even without a strong reference.
150 * We cannot do this in the library's destructor because it
151 * gets executed once Python is already finalized.
153 Py_XDECREF(py_mod_bt2);
154 Py_XDECREF(py_mod_bt2_exc_error_type);
155 Py_XDECREF(py_mod_bt2_exc_try_again_type);
156 Py_XDECREF(py_mod_bt2_exc_stop_type);
157 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
158 Py_XDECREF(py_mod_bt2_exc_invalid_object_type);
159 Py_XDECREF(py_mod_bt2_exc_invalid_params_type);
163 /* Library destructor */
165 __attribute__((destructor))
167 void native_comp_class_dtor(void) {
168 /* Destroy component class association hash table */
169 if (bt_cc_ptr_to_py_cls) {
170 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
171 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
176 void log_exception_and_maybe_append_error(int log_level,
178 bt_self_component_class *self_component_class,
179 bt_self_component *self_component,
180 bt_self_message_iterator *self_message_iterator)
184 BT_ASSERT(PyErr_Occurred());
185 gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL);
187 /* bt_py_common_format_exception() logs errors */
191 BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str);
194 if (self_component_class) {
195 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
196 self_component_class, "%s", gstr->str);
197 } else if (self_component) {
198 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
199 self_component, "%s", gstr->str);
200 } else if (self_message_iterator) {
201 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
202 self_message_iterator, "%s", gstr->str);
204 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
205 "Python", "%s", gstr->str);
211 g_string_free(gstr, TRUE);
216 void loge_exception(void)
218 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL, NULL);
222 void loge_exception_message_iterator(
223 bt_self_message_iterator *self_message_iterator)
225 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL, self_message_iterator);
229 void logw_exception(void)
231 log_exception_and_maybe_append_error(BT_LOG_WARNING, false, NULL, NULL, NULL);
235 int py_exc_to_status(bt_self_component_class *self_component_class,
236 bt_self_component *self_component,
237 bt_self_message_iterator *self_message_iterator)
239 int status = __BT_FUNC_STATUS_OK;
240 PyObject *exc = PyErr_Occurred();
246 if (PyErr_GivenExceptionMatches(exc,
247 py_mod_bt2_exc_try_again_type)) {
248 status = __BT_FUNC_STATUS_AGAIN;
249 } else if (PyErr_GivenExceptionMatches(exc,
250 py_mod_bt2_exc_stop_type)) {
251 status = __BT_FUNC_STATUS_END;
252 } else if (PyErr_GivenExceptionMatches(exc,
253 py_mod_bt2_exc_invalid_object_type)) {
254 status = __BT_FUNC_STATUS_INVALID_OBJECT;
255 } else if (PyErr_GivenExceptionMatches(exc,
256 py_mod_bt2_exc_invalid_params_type)) {
257 status = __BT_FUNC_STATUS_INVALID_PARAMS;
258 } else if (PyErr_GivenExceptionMatches(exc,
259 py_mod_bt2_exc_unsupported_type)) {
260 status = __BT_FUNC_STATUS_UNSUPPORTED;
262 /* Unknown exception: convert to general error */
263 log_exception_and_maybe_append_error(BT_LOG_WARNING, true,
264 self_component_class, self_component,
265 self_message_iterator);
266 status = __BT_FUNC_STATUS_ERROR;
275 int py_exc_to_status_component_class(bt_self_component_class *self_component_class)
277 return py_exc_to_status(self_component_class, NULL, NULL);
281 int py_exc_to_status_component(bt_self_component *self_component)
283 return py_exc_to_status(NULL, self_component, NULL);
287 int py_exc_to_status_message_iterator(
288 bt_self_message_iterator *self_message_iterator)
290 return py_exc_to_status(NULL, NULL, self_message_iterator);
293 /* Component class proxy methods (delegate to the attached Python object) */
296 bt_component_class_init_method_status component_class_init(
297 bt_self_component *self_component,
298 void *self_component_v,
299 swig_type_info *self_comp_cls_type_swig_type,
300 const bt_value *params,
301 void *init_method_data)
303 const bt_component *component = bt_self_component_as_component(self_component);
304 const bt_component_class *component_class = bt_component_borrow_class_const(component);
305 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
306 PyObject *py_cls = NULL;
307 PyObject *py_comp = NULL;
308 PyObject *py_params_ptr = NULL;
309 PyObject *py_comp_ptr = NULL;
311 (void) init_method_data;
313 BT_ASSERT(self_component);
314 BT_ASSERT(self_component_v);
315 BT_ASSERT(self_comp_cls_type_swig_type);
318 * Get the user-defined Python class which created this
319 * component's class in the first place (borrowed
322 py_cls = lookup_cc_ptr_to_py_cls(component_class);
324 BT_LOGE("Cannot find Python class associated to native component class: "
325 "comp-cls-addr=%p", component_class);
329 /* Parameters pointer -> SWIG pointer Python object */
330 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
331 SWIGTYPE_p_bt_value, 0);
332 if (!py_params_ptr) {
333 BT_LOGE_STR("Failed to create a SWIG pointer object.");
337 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
338 self_comp_cls_type_swig_type, 0);
340 BT_LOGE_STR("Failed to create a SWIG pointer object.");
345 * Do the equivalent of this:
347 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
349 * _UserComponentType._bt_init_from_native() calls the Python
350 * component object's __init__() function.
352 py_comp = PyObject_CallMethod(py_cls,
353 "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
355 BT_LOGW("Failed to call Python class's _bt_init_from_native() method: "
356 "py-cls-addr=%p", py_cls);
357 status = py_exc_to_status_component(self_component);
362 * Our user Python component object is now fully created and
363 * initialized by the user. Since we just created it, this
364 * native component is its only (persistent) owner.
366 bt_self_component_set_data(self_component, py_comp);
371 status = __BT_FUNC_STATUS_ERROR;
374 * Clear any exception: we're returning a bad status anyway. If
375 * this call originated from Python (creation from a plugin's
376 * component class, for example), then the user gets an
377 * appropriate creation error.
383 Py_XDECREF(py_params_ptr);
384 Py_XDECREF(py_comp_ptr);
389 * Method of bt_component_class_source to initialize a bt_self_component_source
394 bt_component_class_init_method_status component_class_source_init(
395 bt_self_component_source *self_component_source,
396 const bt_value *params, void *init_method_data)
398 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
399 return component_class_init(
401 self_component_source,
402 SWIGTYPE_p_bt_self_component_source,
403 params, init_method_data);
407 bt_component_class_init_method_status component_class_filter_init(
408 bt_self_component_filter *self_component_filter,
409 const bt_value *params, void *init_method_data)
411 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
412 return component_class_init(
414 self_component_filter,
415 SWIGTYPE_p_bt_self_component_filter,
416 params, init_method_data);
420 bt_component_class_init_method_status component_class_sink_init(
421 bt_self_component_sink *self_component_sink,
422 const bt_value *params, void *init_method_data)
424 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
425 return component_class_init(
428 SWIGTYPE_p_bt_self_component_sink,
429 params, init_method_data);
433 void component_class_finalize(bt_self_component *self_component)
435 PyObject *py_comp = bt_self_component_get_data(self_component);
438 /* Call user's _finalize() method */
439 PyObject *py_method_result = PyObject_CallMethod(py_comp,
442 if (PyErr_Occurred()) {
443 BT_LOGW("User component's _finalize() method raised an exception: ignoring:");
448 * Ignore any exception raised by the _finalize() method because
449 * it won't change anything at this point: the component is
450 * being destroyed anyway.
453 Py_XDECREF(py_method_result);
458 void component_class_source_finalize(bt_self_component_source *self_component_source)
460 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
461 component_class_finalize(self_component);
465 void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
467 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
468 component_class_finalize(self_component);
472 void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
474 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
475 component_class_finalize(self_component);
479 bt_bool component_class_can_seek_beginning(
480 bt_self_message_iterator *self_message_iterator)
483 PyObject *py_result = NULL;
484 bt_bool can_seek_beginning = false;
486 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
489 py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
491 BT_ASSERT(!py_result || PyBool_Check(py_result));
494 can_seek_beginning = PyObject_IsTrue(py_result);
497 * Once can_seek_beginning can report errors, convert the
498 * exception to a status. For now, log and return false;
500 loge_exception_message_iterator(self_message_iterator);
504 Py_XDECREF(py_result);
506 return can_seek_beginning;
510 bt_component_class_message_iterator_seek_beginning_method_status
511 component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
515 bt_component_class_message_iterator_seek_beginning_method_status status;
517 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
519 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
521 BT_ASSERT(!py_result || py_result == Py_None);
522 status = py_exc_to_status_message_iterator(self_message_iterator);
523 Py_XDECREF(py_result);
528 bt_component_class_port_connected_method_status component_class_port_connected(
529 bt_self_component *self_component,
530 void *self_component_port,
531 swig_type_info *self_component_port_swig_type,
532 bt_port_type self_component_port_type,
533 const void *other_port,
534 swig_type_info *other_port_swig_type)
536 bt_component_class_port_connected_method_status status;
537 PyObject *py_comp = NULL;
538 PyObject *py_self_port_ptr = NULL;
539 PyObject *py_other_port_ptr = NULL;
540 PyObject *py_method_result = NULL;
542 py_comp = bt_self_component_get_data(self_component);
544 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
545 self_component_port_swig_type, 0);
546 if (!py_self_port_ptr) {
547 BT_LOGF_STR("Failed to create a SWIG pointer object.");
548 status = __BT_FUNC_STATUS_MEMORY_ERROR;
552 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
553 other_port_swig_type, 0);
554 if (!py_other_port_ptr) {
555 BT_LOGF_STR("Failed to create a SWIG pointer object.");
556 status = __BT_FUNC_STATUS_MEMORY_ERROR;
559 py_method_result = PyObject_CallMethod(py_comp,
560 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
561 self_component_port_type, py_other_port_ptr);
562 BT_ASSERT(!py_method_result || py_method_result == Py_None);
563 status = py_exc_to_status_component(self_component);
566 Py_XDECREF(py_self_port_ptr);
567 Py_XDECREF(py_other_port_ptr);
568 Py_XDECREF(py_method_result);
573 bt_component_class_port_connected_method_status
574 component_class_source_output_port_connected(
575 bt_self_component_source *self_component_source,
576 bt_self_component_port_output *self_component_port_output,
577 const bt_port_input *other_port_input)
579 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
581 return component_class_port_connected(
583 self_component_port_output,
584 SWIGTYPE_p_bt_self_component_port_output,
587 SWIGTYPE_p_bt_port_input);
591 bt_component_class_port_connected_method_status
592 component_class_filter_input_port_connected(
593 bt_self_component_filter *self_component_filter,
594 bt_self_component_port_input *self_component_port_input,
595 const bt_port_output *other_port_output)
597 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
599 return component_class_port_connected(
601 self_component_port_input,
602 SWIGTYPE_p_bt_self_component_port_input,
605 SWIGTYPE_p_bt_port_output);
609 bt_component_class_port_connected_method_status
610 component_class_filter_output_port_connected(
611 bt_self_component_filter *self_component_filter,
612 bt_self_component_port_output *self_component_port_output,
613 const bt_port_input *other_port_input)
615 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
617 return component_class_port_connected(
619 self_component_port_output,
620 SWIGTYPE_p_bt_self_component_port_output,
623 SWIGTYPE_p_bt_port_input);
627 bt_component_class_port_connected_method_status
628 component_class_sink_input_port_connected(
629 bt_self_component_sink *self_component_sink,
630 bt_self_component_port_input *self_component_port_input,
631 const bt_port_output *other_port_output)
633 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
635 return component_class_port_connected(
637 self_component_port_input,
638 SWIGTYPE_p_bt_self_component_port_input,
641 SWIGTYPE_p_bt_port_output);
645 bt_component_class_sink_graph_is_configured_method_status
646 component_class_sink_graph_is_configured(
647 bt_self_component_sink *self_component_sink)
649 PyObject *py_comp = NULL;
650 PyObject *py_method_result = NULL;
651 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
652 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
654 py_comp = bt_self_component_get_data(self_component);
655 py_method_result = PyObject_CallMethod(py_comp,
656 "_bt_graph_is_configured_from_native", NULL);
657 BT_ASSERT(!py_method_result || py_method_result == Py_None);
658 status = py_exc_to_status_component(self_component);
659 Py_XDECREF(py_method_result);
664 bt_component_class_query_method_status component_class_query(
665 const bt_component_class *component_class,
666 bt_self_component_class *self_component_class,
667 const bt_query_executor *query_executor,
668 const char *object, const bt_value *params,
669 bt_logging_level log_level,
670 const bt_value **result)
672 PyObject *py_cls = NULL;
673 PyObject *py_params_ptr = NULL;
674 PyObject *py_query_exec_ptr = NULL;
675 PyObject *py_query_func = NULL;
676 PyObject *py_object = NULL;
677 PyObject *py_results_addr = NULL;
678 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
680 py_cls = lookup_cc_ptr_to_py_cls(component_class);
682 BT_LOGE("Cannot find Python class associated to native component class: "
683 "comp-cls-addr=%p", component_class);
687 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
688 SWIGTYPE_p_bt_value, 0);
689 if (!py_params_ptr) {
690 BT_LOGE_STR("Failed to create a SWIG pointer object.");
694 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
695 SWIGTYPE_p_bt_query_executor, 0);
696 if (!py_query_exec_ptr) {
697 BT_LOGE_STR("Failed to create a SWIG pointer object.");
701 py_object = SWIG_FromCharPtr(object);
703 BT_LOGE_STR("Failed to create a Python string.");
707 py_results_addr = PyObject_CallMethod(py_cls,
708 "_bt_query_from_native", "(OOOi)", py_query_exec_ptr,
709 py_object, py_params_ptr, (int) log_level);
710 if (!py_results_addr) {
711 BT_LOGW("Failed to call Python class's _bt_query_from_native() method: "
712 "py-cls-addr=%p", py_cls);
713 status = py_exc_to_status_component_class(self_component_class);
718 * The returned object, on success, is an integer object
719 * (PyLong) containing the address of a BT value object (new
722 *result = PyLong_AsVoidPtr(py_results_addr);
723 BT_ASSERT(!PyErr_Occurred());
729 status = __BT_FUNC_STATUS_ERROR;
732 Py_XDECREF(py_params_ptr);
733 Py_XDECREF(py_query_exec_ptr);
734 Py_XDECREF(py_query_func);
735 Py_XDECREF(py_object);
736 Py_XDECREF(py_results_addr);
741 bt_component_class_query_method_status component_class_source_query(
742 bt_self_component_class_source *self_component_class_source,
743 const bt_query_executor *query_executor,
744 const char *object, const bt_value *params,
745 bt_logging_level log_level,
746 const bt_value **result)
748 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
749 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
750 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
752 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
756 bt_component_class_query_method_status component_class_filter_query(
757 bt_self_component_class_filter *self_component_class_filter,
758 const bt_query_executor *query_executor,
759 const char *object, const bt_value *params,
760 bt_logging_level log_level,
761 const bt_value **result)
763 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
764 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
765 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
767 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
771 bt_component_class_query_method_status component_class_sink_query(
772 bt_self_component_class_sink *self_component_class_sink,
773 const bt_query_executor *query_executor,
774 const char *object, const bt_value *params,
775 bt_logging_level log_level,
776 const bt_value **result)
778 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
779 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
780 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
782 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
786 bt_component_class_message_iterator_init_method_status
787 component_class_message_iterator_init(
788 bt_self_message_iterator *self_message_iterator,
789 bt_self_component *self_component,
790 bt_self_component_port_output *self_component_port_output)
792 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
793 PyObject *py_comp_cls = NULL;
794 PyObject *py_iter_cls = NULL;
795 PyObject *py_iter_ptr = NULL;
796 PyObject *py_component_port_output_ptr = NULL;
797 PyObject *py_init_method_result = NULL;
798 PyObject *py_iter = NULL;
801 py_comp = bt_self_component_get_data(self_component);
803 /* Find user's Python message iterator class */
804 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
806 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
810 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
812 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
816 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
817 SWIGTYPE_p_bt_self_message_iterator, 0);
819 const char *err = "Failed to create a SWIG pointer object.";
822 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
823 self_message_iterator, err);
828 * Create object with borrowed native message iterator
831 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
833 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
834 "(OO)", py_iter_cls, py_iter_ptr);
836 BT_LOGE("Failed to call Python class's __new__() method: "
837 "py-cls-addr=%p", py_iter_cls);
844 * py_iter.__init__(self_output_port)
846 * through the _init_for_native helper static method.
848 * At this point, py_iter._ptr is set, so this initialization
849 * function has access to self._component (which gives it the
850 * user Python component object from which the iterator was
853 py_component_port_output_ptr = SWIG_NewPointerObj(
854 SWIG_as_voidptr(self_component_port_output),
855 SWIGTYPE_p_bt_self_component_port_output, 0);
856 if (!py_component_port_output_ptr) {
857 const char *err = "Failed to create a SWIG pointer object.";
860 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
861 self_message_iterator, err);
865 py_init_method_result = PyObject_CallMethod(py_iter,
866 "_bt_init_from_native", "O", py_component_port_output_ptr);
867 if (!py_init_method_result) {
868 BT_LOGE_STR("User's __init__() method failed:");
873 * Since the Python code can never instantiate a user-defined
874 * message iterator class, the native message iterator
875 * object does NOT belong to a user Python message iterator
876 * object (borrowed reference). However this Python object is
877 * owned by this native message iterator object.
879 * In the Python world, the lifetime of the native message
880 * iterator is managed by a _GenericMessageIterator
883 * _GenericMessageIterator instance:
884 * owns a native bt_message_iterator object (iter)
885 * owns a _UserMessageIterator instance (py_iter)
886 * self._ptr is a borrowed reference to the
887 * native bt_private_connection_private_message_iterator
890 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
895 /* Handling of errors that cause a Python exception to be set. */
896 status = py_exc_to_status_message_iterator(self_message_iterator);
897 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
901 /* Handling of errors that don't cause a Python exception to be set. */
902 status = __BT_FUNC_STATUS_ERROR;
905 BT_ASSERT(!PyErr_Occurred());
907 Py_XDECREF(py_comp_cls);
908 Py_XDECREF(py_iter_cls);
909 Py_XDECREF(py_iter_ptr);
910 Py_XDECREF(py_component_port_output_ptr);
911 Py_XDECREF(py_init_method_result);
917 bt_component_class_message_iterator_init_method_status
918 component_class_source_message_iterator_init(
919 bt_self_message_iterator *self_message_iterator,
920 bt_self_component_source *self_component_source,
921 bt_self_component_port_output *self_component_port_output)
923 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
925 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
929 bt_component_class_message_iterator_init_method_status
930 component_class_filter_message_iterator_init(
931 bt_self_message_iterator *self_message_iterator,
932 bt_self_component_filter *self_component_filter,
933 bt_self_component_port_output *self_component_port_output)
935 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
937 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
941 void component_class_message_iterator_finalize(
942 bt_self_message_iterator *message_iterator)
944 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
945 PyObject *py_method_result = NULL;
947 BT_ASSERT(py_message_iter);
949 /* Call user's _finalize() method */
950 py_method_result = PyObject_CallMethod(py_message_iter,
953 if (PyErr_Occurred()) {
954 BT_LOGW("User's _finalize() method raised an exception: ignoring:");
959 * Ignore any exception raised by the _finalize() method because
960 * it won't change anything at this point: the component is
961 * being destroyed anyway.
964 Py_XDECREF(py_method_result);
965 Py_DECREF(py_message_iter);
968 /* Valid for both sources and filters. */
971 bt_component_class_message_iterator_next_method_status
972 component_class_message_iterator_next(
973 bt_self_message_iterator *message_iterator,
974 bt_message_array_const msgs, uint64_t capacity,
977 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
978 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
979 PyObject *py_method_result = NULL;
981 BT_ASSERT(py_message_iter);
982 py_method_result = PyObject_CallMethod(py_message_iter,
983 "_bt_next_from_native", NULL);
984 if (!py_method_result) {
985 status = py_exc_to_status_message_iterator(message_iterator);
986 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
991 * The returned object, on success, is an integer object
992 * (PyLong) containing the address of a native message
993 * object (which is now ours).
995 msgs[0] = PyLong_AsVoidPtr(py_method_result);
998 /* Clear potential overflow error; should never happen */
999 BT_ASSERT(!PyErr_Occurred());
1003 Py_XDECREF(py_method_result);
1008 bt_component_class_sink_consume_method_status
1009 component_class_sink_consume(bt_self_component_sink *self_component_sink)
1011 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1012 PyObject *py_comp = bt_self_component_get_data(self_component);
1013 PyObject *py_method_result = NULL;
1014 bt_component_class_sink_consume_method_status status;
1017 py_method_result = PyObject_CallMethod(py_comp,
1019 status = py_exc_to_status_component(self_component);
1020 if (!py_method_result && status == __BT_FUNC_STATUS_OK) {
1021 /* Pretty sure this should never happen, but just in case */
1022 BT_LOGE("User's _consume() method failed without raising an exception: "
1023 "status=%d", status);
1024 status = __BT_FUNC_STATUS_ERROR;
1027 Py_XDECREF(py_method_result);
1032 int component_class_set_help_and_desc(
1033 bt_component_class *component_class,
1034 const char *description, const char *help)
1039 ret = bt_component_class_set_description(component_class, description);
1041 BT_LOGE("Cannot set component class's description: "
1042 "comp-cls-addr=%p", component_class);
1048 ret = bt_component_class_set_help(component_class, help);
1050 BT_LOGE("Cannot set component class's help text: "
1051 "comp-cls-addr=%p", component_class);
1063 bt_component_class_source *bt_bt2_component_class_source_create(
1064 PyObject *py_cls, const char *name, const char *description,
1067 bt_component_class_source *component_class_source;
1068 bt_component_class *component_class;
1072 component_class_source = bt_component_class_source_create(name,
1073 component_class_message_iterator_next);
1074 if (!component_class_source) {
1075 BT_LOGE_STR("Cannot create source component class.");
1079 component_class = bt_component_class_source_as_component_class(component_class_source);
1081 if (component_class_set_help_and_desc(component_class, description, help)) {
1085 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
1086 BT_ASSERT(ret == 0);
1087 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1088 BT_ASSERT(ret == 0);
1089 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
1090 component_class_can_seek_beginning);
1091 BT_ASSERT(ret == 0);
1092 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
1093 component_class_seek_beginning);
1094 BT_ASSERT(ret == 0);
1095 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1096 component_class_source_output_port_connected);
1097 BT_ASSERT(ret == 0);
1098 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1099 BT_ASSERT(ret == 0);
1100 ret = bt_component_class_source_set_message_iterator_init_method(
1101 component_class_source, component_class_source_message_iterator_init);
1102 BT_ASSERT(ret == 0);
1103 ret = bt_component_class_source_set_message_iterator_finalize_method(
1104 component_class_source, component_class_message_iterator_finalize);
1105 BT_ASSERT(ret == 0);
1106 register_cc_ptr_to_py_cls(component_class, py_cls);
1109 return component_class_source;
1113 bt_component_class_filter *bt_bt2_component_class_filter_create(
1114 PyObject *py_cls, const char *name, const char *description,
1117 bt_component_class *component_class;
1118 bt_component_class_filter *component_class_filter;
1122 component_class_filter = bt_component_class_filter_create(name,
1123 component_class_message_iterator_next);
1124 if (!component_class_filter) {
1125 BT_LOGE_STR("Cannot create filter component class.");
1129 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1131 if (component_class_set_help_and_desc(component_class, description, help)) {
1135 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
1136 BT_ASSERT(ret == 0);
1137 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1138 BT_ASSERT(ret == 0);
1139 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
1140 component_class_can_seek_beginning);
1141 BT_ASSERT(ret == 0);
1142 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
1143 component_class_seek_beginning);
1144 BT_ASSERT(ret == 0);
1145 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1146 component_class_filter_input_port_connected);
1147 BT_ASSERT(ret == 0);
1148 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1149 component_class_filter_output_port_connected);
1150 BT_ASSERT(ret == 0);
1151 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1152 BT_ASSERT(ret == 0);
1153 ret = bt_component_class_filter_set_message_iterator_init_method(
1154 component_class_filter, component_class_filter_message_iterator_init);
1155 BT_ASSERT(ret == 0);
1156 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1157 component_class_filter, component_class_message_iterator_finalize);
1158 BT_ASSERT(ret == 0);
1159 register_cc_ptr_to_py_cls(component_class, py_cls);
1162 return component_class_filter;
1166 bt_component_class_sink *bt_bt2_component_class_sink_create(
1167 PyObject *py_cls, const char *name, const char *description,
1170 bt_component_class_sink *component_class_sink;
1171 bt_component_class *component_class;
1175 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1177 if (!component_class_sink) {
1178 BT_LOGE_STR("Cannot create sink component class.");
1182 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1184 if (component_class_set_help_and_desc(component_class, description, help)) {
1188 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
1189 BT_ASSERT(ret == 0);
1190 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1191 BT_ASSERT(ret == 0);
1192 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1193 component_class_sink_input_port_connected);
1194 BT_ASSERT(ret == 0);
1195 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1196 component_class_sink_graph_is_configured);
1197 BT_ASSERT(ret == 0);
1198 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1199 BT_ASSERT(ret == 0);
1200 register_cc_ptr_to_py_cls(component_class, py_cls);
1203 return component_class_sink;
1207 struct bt_component_class_source *bt_bt2_component_class_source_create(
1208 PyObject *py_cls, const char *name, const char *description,
1210 struct bt_component_class_filter *bt_bt2_component_class_filter_create(
1211 PyObject *py_cls, const char *name, const char *description,
1213 struct bt_component_class_sink *bt_bt2_component_class_sink_create(
1214 PyObject *py_cls, const char *name, const char *description,
1216 void bt_bt2_cc_init_from_bt2(void);
1217 void bt_bt2_cc_exit_handler(void);