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_memory_error = NULL;
98 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
99 static PyObject *py_mod_bt2_exc_stop_type = NULL;
100 static PyObject *py_mod_bt2_exc_invalid_object_type = NULL;
101 static PyObject *py_mod_bt2_exc_invalid_params_type = NULL;
104 void bt_bt2_cc_init_from_bt2(void)
107 * This is called once the bt2 package is loaded.
109 * Those modules and functions are needed while the package is
110 * used. Loading them here is safe because we know the bt2
111 * package is imported, and we know that the user cannot use the
112 * code here without importing bt2 first.
114 py_mod_bt2 = PyImport_ImportModule("bt2");
115 BT_ASSERT(py_mod_bt2);
116 py_mod_bt2_exc_error_type =
117 PyObject_GetAttrString(py_mod_bt2, "_Error");
118 BT_ASSERT(py_mod_bt2_exc_error_type);
119 py_mod_bt2_exc_memory_error =
120 PyObject_GetAttrString(py_mod_bt2, "_MemoryError");
121 BT_ASSERT(py_mod_bt2_exc_memory_error);
122 py_mod_bt2_exc_try_again_type =
123 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
124 BT_ASSERT(py_mod_bt2_exc_try_again_type);
125 py_mod_bt2_exc_stop_type =
126 PyObject_GetAttrString(py_mod_bt2, "Stop");
127 BT_ASSERT(py_mod_bt2_exc_stop_type);
128 py_mod_bt2_exc_invalid_object_type =
129 PyObject_GetAttrString(py_mod_bt2, "InvalidObject");
130 BT_ASSERT(py_mod_bt2_exc_invalid_object_type);
131 py_mod_bt2_exc_invalid_params_type =
132 PyObject_GetAttrString(py_mod_bt2, "InvalidParams");
133 BT_ASSERT(py_mod_bt2_exc_invalid_params_type);
137 void bt_bt2_cc_exit_handler(void)
140 * This is an exit handler (set by the bt2 package).
142 * We only give back the references that we took in
143 * bt_bt2_cc_init_from_bt2() here. The global variables continue
144 * to exist for the code of this file, but they are now borrowed
145 * references. If this code is executed, it means that somehow
146 * the modules are still loaded, so it should be safe to use
147 * them even without a strong reference.
149 * We cannot do this in the library's destructor because it
150 * gets executed once Python is already finalized.
152 Py_XDECREF(py_mod_bt2);
153 Py_XDECREF(py_mod_bt2_exc_error_type);
154 Py_XDECREF(py_mod_bt2_exc_try_again_type);
155 Py_XDECREF(py_mod_bt2_exc_stop_type);
156 Py_XDECREF(py_mod_bt2_exc_invalid_object_type);
157 Py_XDECREF(py_mod_bt2_exc_invalid_params_type);
161 /* Library destructor */
163 __attribute__((destructor))
165 void native_comp_class_dtor(void) {
166 /* Destroy component class association hash table */
167 if (bt_cc_ptr_to_py_cls) {
168 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
169 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
174 void restore_current_thread_error_and_append_exception_chain_recursive(
175 PyObject *py_exc_value,
176 bt_self_component_class *self_component_class,
177 bt_self_component *self_component,
178 bt_self_message_iterator *self_message_iterator,
179 const char *module_name)
181 PyObject *py_exc_cause_value;
182 PyObject *py_exc_type = NULL;
183 PyObject *py_exc_tb = NULL;
184 GString *gstr = NULL;
186 /* If this exception has a cause, handle that one first. */
187 py_exc_cause_value = PyException_GetCause(py_exc_value);
188 if (py_exc_cause_value) {
189 restore_current_thread_error_and_append_exception_chain_recursive(
190 py_exc_cause_value, self_component_class,
191 self_component, self_message_iterator, module_name);
195 * If the raised exception is a bt2._Error, restore the wrapped error.
197 if (PyErr_GivenExceptionMatches(py_exc_value, py_mod_bt2_exc_error_type)) {
198 PyObject *py_error_swig_ptr;
199 const bt_error *error;
203 * We never raise a bt2._Error with a cause: it should be the
206 BT_ASSERT(!py_exc_cause_value);
209 * We steal the error object from the exception, to move
210 * it back as the current thread's error.
212 py_error_swig_ptr = PyObject_GetAttrString(py_exc_value, "_ptr");
213 BT_ASSERT(py_error_swig_ptr);
215 ret = PyObject_SetAttrString(py_exc_value, "_ptr", Py_None);
218 ret = SWIG_ConvertPtr(py_error_swig_ptr, (void **) &error,
219 SWIGTYPE_p_bt_error, 0);
222 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(error);
224 Py_DECREF(py_error_swig_ptr);
227 py_exc_type = PyObject_Type(py_exc_value);
228 py_exc_tb = PyException_GetTraceback(py_exc_value);
230 gstr = bt_py_common_format_exception(py_exc_type, py_exc_value,
231 py_exc_tb, BT_LOG_OUTPUT_LEVEL, false);
233 /* bt_py_common_format_exception has already warned. */
237 if (self_component_class) {
238 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
239 self_component_class, "%s", gstr->str);
240 } else if (self_component) {
241 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
242 self_component, "%s", gstr->str);
243 } else if (self_message_iterator) {
244 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
245 self_message_iterator, "%s", gstr->str);
247 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
248 module_name, "%s", gstr->str);
253 g_string_free(gstr, TRUE);
256 Py_XDECREF(py_exc_cause_value);
257 Py_XDECREF(py_exc_type);
258 Py_XDECREF(py_exc_tb);
262 * If you have the following code:
266 * something_that_raises_bt2_error()
267 * except bt2._Error as e1:
268 * raise ValueError from e1
269 * except ValueError as e2:
270 * raise TypeError from e2
272 * We will have the following exception chain:
274 * TypeError -> ValueError -> bt2._Error
276 * Where the TypeError is the current exception (obtained from PyErr_Fetch).
278 * The bt2._Error contains a `struct bt_error *` that used to be the current
279 * thread's error, at the moment the exception was raised.
281 * This function gets to the bt2._Error and restores the wrapped
282 * `struct bt_error *` as the current thread's error.
284 * Then, for each exception in the chain, starting with the oldest one, it adds
285 * an error cause to the current thread's error.
288 void restore_bt_error_and_append_current_exception_chain(
289 bt_self_component_class *self_component_class,
290 bt_self_component *self_component,
291 bt_self_message_iterator *self_message_iterator,
292 const char *module_name)
294 BT_ASSERT(PyErr_Occurred());
296 /* Used to access and restore the current exception. */
297 PyObject *py_exc_type;
298 PyObject *py_exc_value;
301 /* Fetch and normalize the Python exception. */
302 PyErr_Fetch(&py_exc_type, &py_exc_value, &py_exc_tb);
303 PyErr_NormalizeException(&py_exc_type, &py_exc_value, &py_exc_tb);
304 BT_ASSERT(py_exc_type);
305 BT_ASSERT(py_exc_value);
306 BT_ASSERT(py_exc_tb);
309 * Set the exception's traceback so it's possible to get it using
310 * PyException_GetTraceback in
311 * restore_current_thread_error_and_append_exception_chain_recursive.
313 PyException_SetTraceback(py_exc_value, py_exc_tb);
315 restore_current_thread_error_and_append_exception_chain_recursive(py_exc_value,
316 self_component_class, self_component, self_message_iterator,
319 PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb);
323 void log_exception_and_maybe_append_error(int log_level,
325 bt_self_component_class *self_component_class,
326 bt_self_component *self_component,
327 bt_self_message_iterator *self_message_iterator,
328 const char *module_name)
332 BT_ASSERT(PyErr_Occurred());
333 gstr = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL);
335 /* bt_py_common_format_current_exception() logs errors */
339 BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str);
342 restore_bt_error_and_append_current_exception_chain(
343 self_component_class, self_component,
344 self_message_iterator, module_name);
350 g_string_free(gstr, TRUE);
355 void loge_exception(const char *module_name)
357 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL,
362 void loge_exception_message_iterator(
363 bt_self_message_iterator *self_message_iterator)
365 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL,
366 self_message_iterator, NULL);
370 void logw_exception(void)
372 log_exception_and_maybe_append_error(BT_LOG_WARNING, false, NULL, NULL,
377 int py_exc_to_status(bt_self_component_class *self_component_class,
378 bt_self_component *self_component,
379 bt_self_message_iterator *self_message_iterator,
380 const char *module_name)
382 int status = __BT_FUNC_STATUS_OK;
383 PyObject *exc = PyErr_Occurred();
389 if (PyErr_GivenExceptionMatches(exc,
390 py_mod_bt2_exc_try_again_type)) {
391 status = __BT_FUNC_STATUS_AGAIN;
392 } else if (PyErr_GivenExceptionMatches(exc,
393 py_mod_bt2_exc_stop_type)) {
394 status = __BT_FUNC_STATUS_END;
395 } else if (PyErr_GivenExceptionMatches(exc,
396 py_mod_bt2_exc_invalid_object_type)) {
397 status = __BT_FUNC_STATUS_INVALID_OBJECT;
398 } else if (PyErr_GivenExceptionMatches(exc,
399 py_mod_bt2_exc_invalid_params_type)) {
400 status = __BT_FUNC_STATUS_INVALID_PARAMS;
402 /* Unknown exception: convert to general error */
403 log_exception_and_maybe_append_error(BT_LOG_WARNING, true,
404 self_component_class, self_component,
405 self_message_iterator, module_name);
407 if (PyErr_GivenExceptionMatches(exc,
408 py_mod_bt2_exc_memory_error)) {
409 status = __BT_FUNC_STATUS_MEMORY_ERROR;
411 status = __BT_FUNC_STATUS_ERROR;
421 int py_exc_to_status_component_class(bt_self_component_class *self_component_class)
423 return py_exc_to_status(self_component_class, NULL, NULL, NULL);
427 int py_exc_to_status_component(bt_self_component *self_component)
429 return py_exc_to_status(NULL, self_component, NULL, NULL);
433 int py_exc_to_status_message_iterator(
434 bt_self_message_iterator *self_message_iterator)
436 return py_exc_to_status(NULL, NULL, self_message_iterator, NULL);
439 /* Component class proxy methods (delegate to the attached Python object) */
442 bt_component_class_init_method_status component_class_init(
443 bt_self_component *self_component,
444 void *self_component_v,
445 swig_type_info *self_comp_cls_type_swig_type,
446 const bt_value *params,
447 void *init_method_data)
449 const bt_component *component = bt_self_component_as_component(self_component);
450 const bt_component_class *component_class = bt_component_borrow_class_const(component);
451 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
452 PyObject *py_cls = NULL;
453 PyObject *py_comp = NULL;
454 PyObject *py_params_ptr = NULL;
455 PyObject *py_comp_ptr = NULL;
457 (void) init_method_data;
459 BT_ASSERT(self_component);
460 BT_ASSERT(self_component_v);
461 BT_ASSERT(self_comp_cls_type_swig_type);
464 * Get the user-defined Python class which created this
465 * component's class in the first place (borrowed
468 py_cls = lookup_cc_ptr_to_py_cls(component_class);
470 BT_LOGE("Cannot find Python class associated to native component class: "
471 "comp-cls-addr=%p", component_class);
475 /* Parameters pointer -> SWIG pointer Python object */
476 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
477 SWIGTYPE_p_bt_value, 0);
478 if (!py_params_ptr) {
479 BT_LOGE_STR("Failed to create a SWIG pointer object.");
483 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
484 self_comp_cls_type_swig_type, 0);
486 BT_LOGE_STR("Failed to create a SWIG pointer object.");
491 * Do the equivalent of this:
493 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
495 * _UserComponentType._bt_init_from_native() calls the Python
496 * component object's __init__() function.
498 py_comp = PyObject_CallMethod(py_cls,
499 "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
501 BT_LOGW("Failed to call Python class's _bt_init_from_native() method: "
502 "py-cls-addr=%p", py_cls);
503 status = py_exc_to_status_component(self_component);
508 * Our user Python component object is now fully created and
509 * initialized by the user. Since we just created it, this
510 * native component is its only (persistent) owner.
512 bt_self_component_set_data(self_component, py_comp);
517 status = __BT_FUNC_STATUS_ERROR;
520 * Clear any exception: we're returning a bad status anyway. If
521 * this call originated from Python (creation from a plugin's
522 * component class, for example), then the user gets an
523 * appropriate creation error.
529 Py_XDECREF(py_params_ptr);
530 Py_XDECREF(py_comp_ptr);
535 * Method of bt_component_class_source to initialize a bt_self_component_source
540 bt_component_class_init_method_status component_class_source_init(
541 bt_self_component_source *self_component_source,
542 const bt_value *params, void *init_method_data)
544 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
545 return component_class_init(
547 self_component_source,
548 SWIGTYPE_p_bt_self_component_source,
549 params, init_method_data);
553 bt_component_class_init_method_status component_class_filter_init(
554 bt_self_component_filter *self_component_filter,
555 const bt_value *params, void *init_method_data)
557 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
558 return component_class_init(
560 self_component_filter,
561 SWIGTYPE_p_bt_self_component_filter,
562 params, init_method_data);
566 bt_component_class_init_method_status component_class_sink_init(
567 bt_self_component_sink *self_component_sink,
568 const bt_value *params, void *init_method_data)
570 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
571 return component_class_init(
574 SWIGTYPE_p_bt_self_component_sink,
575 params, init_method_data);
579 void component_class_finalize(bt_self_component *self_component)
581 PyObject *py_comp = bt_self_component_get_data(self_component);
584 /* Call user's _finalize() method */
585 PyObject *py_method_result = PyObject_CallMethod(py_comp,
588 if (PyErr_Occurred()) {
589 BT_LOGW("User component's _finalize() method raised an exception: ignoring:");
594 * Ignore any exception raised by the _finalize() method because
595 * it won't change anything at this point: the component is
596 * being destroyed anyway.
599 Py_XDECREF(py_method_result);
604 void component_class_source_finalize(bt_self_component_source *self_component_source)
606 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
607 component_class_finalize(self_component);
611 void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
613 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
614 component_class_finalize(self_component);
618 void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
620 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
621 component_class_finalize(self_component);
625 bt_bool component_class_can_seek_beginning(
626 bt_self_message_iterator *self_message_iterator)
629 PyObject *py_result = NULL;
630 bt_bool can_seek_beginning = false;
632 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
635 py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
637 BT_ASSERT(!py_result || PyBool_Check(py_result));
640 can_seek_beginning = PyObject_IsTrue(py_result);
643 * Once can_seek_beginning can report errors, convert the
644 * exception to a status. For now, log and return false;
646 loge_exception_message_iterator(self_message_iterator);
650 Py_XDECREF(py_result);
652 return can_seek_beginning;
656 bt_component_class_message_iterator_seek_beginning_method_status
657 component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
661 bt_component_class_message_iterator_seek_beginning_method_status status;
663 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
665 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
667 BT_ASSERT(!py_result || py_result == Py_None);
668 status = py_exc_to_status_message_iterator(self_message_iterator);
669 Py_XDECREF(py_result);
674 bt_component_class_port_connected_method_status component_class_port_connected(
675 bt_self_component *self_component,
676 void *self_component_port,
677 swig_type_info *self_component_port_swig_type,
678 bt_port_type self_component_port_type,
679 const void *other_port,
680 swig_type_info *other_port_swig_type)
682 bt_component_class_port_connected_method_status status;
683 PyObject *py_comp = NULL;
684 PyObject *py_self_port_ptr = NULL;
685 PyObject *py_other_port_ptr = NULL;
686 PyObject *py_method_result = NULL;
688 py_comp = bt_self_component_get_data(self_component);
690 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
691 self_component_port_swig_type, 0);
692 if (!py_self_port_ptr) {
693 BT_LOGF_STR("Failed to create a SWIG pointer object.");
694 status = __BT_FUNC_STATUS_MEMORY_ERROR;
698 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
699 other_port_swig_type, 0);
700 if (!py_other_port_ptr) {
701 BT_LOGF_STR("Failed to create a SWIG pointer object.");
702 status = __BT_FUNC_STATUS_MEMORY_ERROR;
705 py_method_result = PyObject_CallMethod(py_comp,
706 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
707 self_component_port_type, py_other_port_ptr);
708 BT_ASSERT(!py_method_result || py_method_result == Py_None);
709 status = py_exc_to_status_component(self_component);
712 Py_XDECREF(py_self_port_ptr);
713 Py_XDECREF(py_other_port_ptr);
714 Py_XDECREF(py_method_result);
719 bt_component_class_port_connected_method_status
720 component_class_source_output_port_connected(
721 bt_self_component_source *self_component_source,
722 bt_self_component_port_output *self_component_port_output,
723 const bt_port_input *other_port_input)
725 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
727 return component_class_port_connected(
729 self_component_port_output,
730 SWIGTYPE_p_bt_self_component_port_output,
733 SWIGTYPE_p_bt_port_input);
737 bt_component_class_port_connected_method_status
738 component_class_filter_input_port_connected(
739 bt_self_component_filter *self_component_filter,
740 bt_self_component_port_input *self_component_port_input,
741 const bt_port_output *other_port_output)
743 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
745 return component_class_port_connected(
747 self_component_port_input,
748 SWIGTYPE_p_bt_self_component_port_input,
751 SWIGTYPE_p_bt_port_output);
755 bt_component_class_port_connected_method_status
756 component_class_filter_output_port_connected(
757 bt_self_component_filter *self_component_filter,
758 bt_self_component_port_output *self_component_port_output,
759 const bt_port_input *other_port_input)
761 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
763 return component_class_port_connected(
765 self_component_port_output,
766 SWIGTYPE_p_bt_self_component_port_output,
769 SWIGTYPE_p_bt_port_input);
773 bt_component_class_port_connected_method_status
774 component_class_sink_input_port_connected(
775 bt_self_component_sink *self_component_sink,
776 bt_self_component_port_input *self_component_port_input,
777 const bt_port_output *other_port_output)
779 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
781 return component_class_port_connected(
783 self_component_port_input,
784 SWIGTYPE_p_bt_self_component_port_input,
787 SWIGTYPE_p_bt_port_output);
791 bt_component_class_sink_graph_is_configured_method_status
792 component_class_sink_graph_is_configured(
793 bt_self_component_sink *self_component_sink)
795 PyObject *py_comp = NULL;
796 PyObject *py_method_result = NULL;
797 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
798 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
800 py_comp = bt_self_component_get_data(self_component);
801 py_method_result = PyObject_CallMethod(py_comp,
802 "_bt_graph_is_configured_from_native", NULL);
803 BT_ASSERT(!py_method_result || py_method_result == Py_None);
804 status = py_exc_to_status_component(self_component);
805 Py_XDECREF(py_method_result);
810 bt_component_class_query_method_status component_class_query(
811 const bt_component_class *component_class,
812 bt_self_component_class *self_component_class,
813 const bt_query_executor *query_executor,
814 const char *object, const bt_value *params,
815 bt_logging_level log_level,
816 const bt_value **result)
818 PyObject *py_cls = NULL;
819 PyObject *py_params_ptr = NULL;
820 PyObject *py_query_exec_ptr = NULL;
821 PyObject *py_query_func = NULL;
822 PyObject *py_object = NULL;
823 PyObject *py_results_addr = NULL;
824 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
826 py_cls = lookup_cc_ptr_to_py_cls(component_class);
828 BT_LOGE("Cannot find Python class associated to native component class: "
829 "comp-cls-addr=%p", component_class);
833 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
834 SWIGTYPE_p_bt_value, 0);
835 if (!py_params_ptr) {
836 BT_LOGE_STR("Failed to create a SWIG pointer object.");
840 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
841 SWIGTYPE_p_bt_query_executor, 0);
842 if (!py_query_exec_ptr) {
843 BT_LOGE_STR("Failed to create a SWIG pointer object.");
847 py_object = SWIG_FromCharPtr(object);
849 BT_LOGE_STR("Failed to create a Python string.");
853 py_results_addr = PyObject_CallMethod(py_cls,
854 "_bt_query_from_native", "(OOOi)", py_query_exec_ptr,
855 py_object, py_params_ptr, (int) log_level);
856 if (!py_results_addr) {
857 BT_LOGW("Failed to call Python class's _bt_query_from_native() method: "
858 "py-cls-addr=%p", py_cls);
859 status = py_exc_to_status_component_class(self_component_class);
864 * The returned object, on success, is an integer object
865 * (PyLong) containing the address of a BT value object (new
868 *result = PyLong_AsVoidPtr(py_results_addr);
869 BT_ASSERT(!PyErr_Occurred());
875 status = __BT_FUNC_STATUS_ERROR;
878 Py_XDECREF(py_params_ptr);
879 Py_XDECREF(py_query_exec_ptr);
880 Py_XDECREF(py_query_func);
881 Py_XDECREF(py_object);
882 Py_XDECREF(py_results_addr);
887 bt_component_class_query_method_status component_class_source_query(
888 bt_self_component_class_source *self_component_class_source,
889 const bt_query_executor *query_executor,
890 const char *object, const bt_value *params,
891 bt_logging_level log_level,
892 const bt_value **result)
894 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
895 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
896 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
898 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
902 bt_component_class_query_method_status component_class_filter_query(
903 bt_self_component_class_filter *self_component_class_filter,
904 const bt_query_executor *query_executor,
905 const char *object, const bt_value *params,
906 bt_logging_level log_level,
907 const bt_value **result)
909 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
910 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
911 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
913 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
917 bt_component_class_query_method_status component_class_sink_query(
918 bt_self_component_class_sink *self_component_class_sink,
919 const bt_query_executor *query_executor,
920 const char *object, const bt_value *params,
921 bt_logging_level log_level,
922 const bt_value **result)
924 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
925 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
926 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
928 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
932 bt_component_class_message_iterator_init_method_status
933 component_class_message_iterator_init(
934 bt_self_message_iterator *self_message_iterator,
935 bt_self_component *self_component,
936 bt_self_component_port_output *self_component_port_output)
938 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
939 PyObject *py_comp_cls = NULL;
940 PyObject *py_iter_cls = NULL;
941 PyObject *py_iter_ptr = NULL;
942 PyObject *py_component_port_output_ptr = NULL;
943 PyObject *py_init_method_result = NULL;
944 PyObject *py_iter = NULL;
947 py_comp = bt_self_component_get_data(self_component);
949 /* Find user's Python message iterator class */
950 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
952 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
956 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
958 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
962 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
963 SWIGTYPE_p_bt_self_message_iterator, 0);
965 const char *err = "Failed to create a SWIG pointer object.";
968 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
969 self_message_iterator, err);
974 * Create object with borrowed native message iterator
977 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
979 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
980 "(OO)", py_iter_cls, py_iter_ptr);
982 BT_LOGE("Failed to call Python class's __new__() method: "
983 "py-cls-addr=%p", py_iter_cls);
990 * py_iter.__init__(self_output_port)
992 * through the _init_for_native helper static method.
994 * At this point, py_iter._ptr is set, so this initialization
995 * function has access to self._component (which gives it the
996 * user Python component object from which the iterator was
999 py_component_port_output_ptr = SWIG_NewPointerObj(
1000 SWIG_as_voidptr(self_component_port_output),
1001 SWIGTYPE_p_bt_self_component_port_output, 0);
1002 if (!py_component_port_output_ptr) {
1003 const char *err = "Failed to create a SWIG pointer object.";
1006 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1007 self_message_iterator, err);
1011 py_init_method_result = PyObject_CallMethod(py_iter,
1012 "_bt_init_from_native", "O", py_component_port_output_ptr);
1013 if (!py_init_method_result) {
1014 BT_LOGE_STR("User's __init__() method failed:");
1019 * Since the Python code can never instantiate a user-defined
1020 * message iterator class, the native message iterator
1021 * object does NOT belong to a user Python message iterator
1022 * object (borrowed reference). However this Python object is
1023 * owned by this native message iterator object.
1025 * In the Python world, the lifetime of the native message
1026 * iterator is managed by a _GenericMessageIterator
1029 * _GenericMessageIterator instance:
1030 * owns a native bt_message_iterator object (iter)
1031 * owns a _UserMessageIterator instance (py_iter)
1032 * self._ptr is a borrowed reference to the
1033 * native bt_private_connection_private_message_iterator
1036 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1041 /* Handling of errors that cause a Python exception to be set. */
1042 status = py_exc_to_status_message_iterator(self_message_iterator);
1043 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
1047 /* Handling of errors that don't cause a Python exception to be set. */
1048 status = __BT_FUNC_STATUS_ERROR;
1051 BT_ASSERT(!PyErr_Occurred());
1053 Py_XDECREF(py_comp_cls);
1054 Py_XDECREF(py_iter_cls);
1055 Py_XDECREF(py_iter_ptr);
1056 Py_XDECREF(py_component_port_output_ptr);
1057 Py_XDECREF(py_init_method_result);
1058 Py_XDECREF(py_iter);
1063 bt_component_class_message_iterator_init_method_status
1064 component_class_source_message_iterator_init(
1065 bt_self_message_iterator *self_message_iterator,
1066 bt_self_component_source *self_component_source,
1067 bt_self_component_port_output *self_component_port_output)
1069 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
1071 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1075 bt_component_class_message_iterator_init_method_status
1076 component_class_filter_message_iterator_init(
1077 bt_self_message_iterator *self_message_iterator,
1078 bt_self_component_filter *self_component_filter,
1079 bt_self_component_port_output *self_component_port_output)
1081 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
1083 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1087 void component_class_message_iterator_finalize(
1088 bt_self_message_iterator *message_iterator)
1090 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1091 PyObject *py_method_result = NULL;
1093 BT_ASSERT(py_message_iter);
1095 /* Call user's _finalize() method */
1096 py_method_result = PyObject_CallMethod(py_message_iter,
1099 if (PyErr_Occurred()) {
1100 BT_LOGW("User's _finalize() method raised an exception: ignoring:");
1105 * Ignore any exception raised by the _finalize() method because
1106 * it won't change anything at this point: the component is
1107 * being destroyed anyway.
1110 Py_XDECREF(py_method_result);
1111 Py_DECREF(py_message_iter);
1114 /* Valid for both sources and filters. */
1117 bt_component_class_message_iterator_next_method_status
1118 component_class_message_iterator_next(
1119 bt_self_message_iterator *message_iterator,
1120 bt_message_array_const msgs, uint64_t capacity,
1123 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
1124 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1125 PyObject *py_method_result = NULL;
1127 BT_ASSERT(py_message_iter);
1128 py_method_result = PyObject_CallMethod(py_message_iter,
1129 "_bt_next_from_native", NULL);
1130 if (!py_method_result) {
1131 status = py_exc_to_status_message_iterator(message_iterator);
1132 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
1137 * The returned object, on success, is an integer object
1138 * (PyLong) containing the address of a native message
1139 * object (which is now ours).
1141 msgs[0] = PyLong_AsVoidPtr(py_method_result);
1144 /* Clear potential overflow error; should never happen */
1145 BT_ASSERT(!PyErr_Occurred());
1149 Py_XDECREF(py_method_result);
1154 bt_component_class_sink_consume_method_status
1155 component_class_sink_consume(bt_self_component_sink *self_component_sink)
1157 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1158 PyObject *py_comp = bt_self_component_get_data(self_component);
1159 PyObject *py_method_result = NULL;
1160 bt_component_class_sink_consume_method_status status;
1163 py_method_result = PyObject_CallMethod(py_comp,
1165 status = py_exc_to_status_component(self_component);
1166 if (!py_method_result && status == __BT_FUNC_STATUS_OK) {
1167 /* Pretty sure this should never happen, but just in case */
1168 BT_LOGE("User's _consume() method failed without raising an exception: "
1169 "status=%d", status);
1170 status = __BT_FUNC_STATUS_ERROR;
1173 Py_XDECREF(py_method_result);
1178 int component_class_set_help_and_desc(
1179 bt_component_class *component_class,
1180 const char *description, const char *help)
1185 ret = bt_component_class_set_description(component_class, description);
1187 BT_LOGE("Cannot set component class's description: "
1188 "comp-cls-addr=%p", component_class);
1194 ret = bt_component_class_set_help(component_class, help);
1196 BT_LOGE("Cannot set component class's help text: "
1197 "comp-cls-addr=%p", component_class);
1209 bt_component_class_source *bt_bt2_component_class_source_create(
1210 PyObject *py_cls, const char *name, const char *description,
1213 bt_component_class_source *component_class_source;
1214 bt_component_class *component_class;
1218 component_class_source = bt_component_class_source_create(name,
1219 component_class_message_iterator_next);
1220 if (!component_class_source) {
1221 BT_LOGE_STR("Cannot create source component class.");
1225 component_class = bt_component_class_source_as_component_class(component_class_source);
1227 if (component_class_set_help_and_desc(component_class, description, help)) {
1231 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
1232 BT_ASSERT(ret == 0);
1233 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1234 BT_ASSERT(ret == 0);
1235 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
1236 component_class_can_seek_beginning);
1237 BT_ASSERT(ret == 0);
1238 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
1239 component_class_seek_beginning);
1240 BT_ASSERT(ret == 0);
1241 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1242 component_class_source_output_port_connected);
1243 BT_ASSERT(ret == 0);
1244 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1245 BT_ASSERT(ret == 0);
1246 ret = bt_component_class_source_set_message_iterator_init_method(
1247 component_class_source, component_class_source_message_iterator_init);
1248 BT_ASSERT(ret == 0);
1249 ret = bt_component_class_source_set_message_iterator_finalize_method(
1250 component_class_source, component_class_message_iterator_finalize);
1251 BT_ASSERT(ret == 0);
1252 register_cc_ptr_to_py_cls(component_class, py_cls);
1255 return component_class_source;
1259 bt_component_class_filter *bt_bt2_component_class_filter_create(
1260 PyObject *py_cls, const char *name, const char *description,
1263 bt_component_class *component_class;
1264 bt_component_class_filter *component_class_filter;
1268 component_class_filter = bt_component_class_filter_create(name,
1269 component_class_message_iterator_next);
1270 if (!component_class_filter) {
1271 BT_LOGE_STR("Cannot create filter component class.");
1275 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1277 if (component_class_set_help_and_desc(component_class, description, help)) {
1281 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
1282 BT_ASSERT(ret == 0);
1283 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1284 BT_ASSERT(ret == 0);
1285 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
1286 component_class_can_seek_beginning);
1287 BT_ASSERT(ret == 0);
1288 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
1289 component_class_seek_beginning);
1290 BT_ASSERT(ret == 0);
1291 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1292 component_class_filter_input_port_connected);
1293 BT_ASSERT(ret == 0);
1294 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1295 component_class_filter_output_port_connected);
1296 BT_ASSERT(ret == 0);
1297 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1298 BT_ASSERT(ret == 0);
1299 ret = bt_component_class_filter_set_message_iterator_init_method(
1300 component_class_filter, component_class_filter_message_iterator_init);
1301 BT_ASSERT(ret == 0);
1302 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1303 component_class_filter, component_class_message_iterator_finalize);
1304 BT_ASSERT(ret == 0);
1305 register_cc_ptr_to_py_cls(component_class, py_cls);
1308 return component_class_filter;
1312 bt_component_class_sink *bt_bt2_component_class_sink_create(
1313 PyObject *py_cls, const char *name, const char *description,
1316 bt_component_class_sink *component_class_sink;
1317 bt_component_class *component_class;
1321 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1323 if (!component_class_sink) {
1324 BT_LOGE_STR("Cannot create sink component class.");
1328 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1330 if (component_class_set_help_and_desc(component_class, description, help)) {
1334 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
1335 BT_ASSERT(ret == 0);
1336 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1337 BT_ASSERT(ret == 0);
1338 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1339 component_class_sink_input_port_connected);
1340 BT_ASSERT(ret == 0);
1341 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1342 component_class_sink_graph_is_configured);
1343 BT_ASSERT(ret == 0);
1344 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1345 BT_ASSERT(ret == 0);
1346 register_cc_ptr_to_py_cls(component_class, py_cls);
1349 return component_class_sink;
1353 struct bt_component_class_source *bt_bt2_component_class_source_create(
1354 PyObject *py_cls, const char *name, const char *description,
1356 struct bt_component_class_filter *bt_bt2_component_class_filter_create(
1357 PyObject *py_cls, const char *name, const char *description,
1359 struct bt_component_class_sink *bt_bt2_component_class_sink_create(
1360 PyObject *py_cls, const char *name, const char *description,
1362 void bt_bt2_cc_init_from_bt2(void);
1363 void bt_bt2_cc_exit_handler(void);