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(int log_level)
180 BT_ASSERT(PyErr_Occurred() != NULL);
181 gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL);
183 /* bt_py_common_format_exception() logs errors */
187 BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str);
191 g_string_free(gstr, TRUE);
196 void loge_exception(void)
198 log_exception(BT_LOG_ERROR);
202 void logw_exception(void)
204 log_exception(BT_LOG_WARN);
208 int py_exc_to_status(void)
210 int status = __BT_FUNC_STATUS_OK;
211 PyObject *exc = PyErr_Occurred();
217 if (PyErr_GivenExceptionMatches(exc,
218 py_mod_bt2_exc_try_again_type)) {
219 status = __BT_FUNC_STATUS_AGAIN;
220 } else if (PyErr_GivenExceptionMatches(exc,
221 py_mod_bt2_exc_stop_type)) {
222 status = __BT_FUNC_STATUS_END;
223 } else if (PyErr_GivenExceptionMatches(exc,
224 py_mod_bt2_exc_invalid_object_type)) {
225 status = __BT_FUNC_STATUS_INVALID_OBJECT;
226 } else if (PyErr_GivenExceptionMatches(exc,
227 py_mod_bt2_exc_invalid_params_type)) {
228 status = __BT_FUNC_STATUS_INVALID_PARAMS;
229 } else if (PyErr_GivenExceptionMatches(exc,
230 py_mod_bt2_exc_unsupported_type)) {
231 status = __BT_FUNC_STATUS_UNSUPPORTED;
233 /* Unknown exception: convert to general error */
235 status = __BT_FUNC_STATUS_ERROR;
243 /* Component class proxy methods (delegate to the attached Python object) */
246 bt_component_class_init_method_status component_class_init(
247 bt_self_component *self_component,
248 void *self_component_v,
249 swig_type_info *self_comp_cls_type_swig_type,
250 const bt_value *params,
251 void *init_method_data)
253 const bt_component *component = bt_self_component_as_component(self_component);
254 const bt_component_class *component_class = bt_component_borrow_class_const(component);
255 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
256 PyObject *py_cls = NULL;
257 PyObject *py_comp = NULL;
258 PyObject *py_params_ptr = NULL;
259 PyObject *py_comp_ptr = NULL;
261 (void) init_method_data;
263 BT_ASSERT(self_component);
264 BT_ASSERT(self_component_v);
265 BT_ASSERT(self_comp_cls_type_swig_type);
268 * Get the user-defined Python class which created this
269 * component's class in the first place (borrowed
272 py_cls = lookup_cc_ptr_to_py_cls(component_class);
274 BT_LOGE("Cannot find Python class associated to native component class: "
275 "comp-cls-addr=%p", component_class);
279 /* Parameters pointer -> SWIG pointer Python object */
280 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
281 SWIGTYPE_p_bt_value, 0);
282 if (!py_params_ptr) {
283 BT_LOGE_STR("Failed to create a SWIG pointer object.");
287 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
288 self_comp_cls_type_swig_type, 0);
290 BT_LOGE_STR("Failed to create a SWIG pointer object.");
295 * Do the equivalent of this:
297 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
299 * _UserComponentType._bt_init_from_native() calls the Python
300 * component object's __init__() function.
302 py_comp = PyObject_CallMethod(py_cls,
303 "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
305 BT_LOGW("Failed to call Python class's _bt_init_from_native() method: "
306 "py-cls-addr=%p", py_cls);
312 * Our user Python component object is now fully created and
313 * initialized by the user. Since we just created it, this
314 * native component is its only (persistent) owner.
316 bt_self_component_set_data(self_component, py_comp);
321 status = __BT_FUNC_STATUS_ERROR;
324 * Clear any exception: we're returning a bad status anyway. If
325 * this call originated from Python (creation from a plugin's
326 * component class, for example), then the user gets an
327 * appropriate creation error.
333 Py_XDECREF(py_params_ptr);
334 Py_XDECREF(py_comp_ptr);
339 * Method of bt_component_class_source to initialize a bt_self_component_source
344 bt_component_class_init_method_status component_class_source_init(
345 bt_self_component_source *self_component_source,
346 const bt_value *params, void *init_method_data)
348 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
349 return component_class_init(
351 self_component_source,
352 SWIGTYPE_p_bt_self_component_source,
353 params, init_method_data);
357 bt_component_class_init_method_status component_class_filter_init(
358 bt_self_component_filter *self_component_filter,
359 const bt_value *params, void *init_method_data)
361 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
362 return component_class_init(
364 self_component_filter,
365 SWIGTYPE_p_bt_self_component_filter,
366 params, init_method_data);
370 bt_component_class_init_method_status component_class_sink_init(
371 bt_self_component_sink *self_component_sink,
372 const bt_value *params, void *init_method_data)
374 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
375 return component_class_init(
378 SWIGTYPE_p_bt_self_component_sink,
379 params, init_method_data);
383 void component_class_finalize(bt_self_component *self_component)
385 PyObject *py_comp = bt_self_component_get_data(self_component);
388 /* Call user's _finalize() method */
389 PyObject *py_method_result = PyObject_CallMethod(py_comp,
392 if (PyErr_Occurred()) {
393 BT_LOGW("User component's _finalize() method raised an exception: ignoring:");
398 * Ignore any exception raised by the _finalize() method because
399 * it won't change anything at this point: the component is
400 * being destroyed anyway.
403 Py_XDECREF(py_method_result);
408 void component_class_source_finalize(bt_self_component_source *self_component_source)
410 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
411 component_class_finalize(self_component);
415 void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
417 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
418 component_class_finalize(self_component);
422 void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
424 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
425 component_class_finalize(self_component);
429 bt_bool component_class_can_seek_beginning(
430 bt_self_message_iterator *self_message_iterator)
433 PyObject *py_result = NULL;
434 bt_bool can_seek_beginning = false;
436 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
439 py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
441 BT_ASSERT(!py_result || PyBool_Check(py_result));
444 can_seek_beginning = PyObject_IsTrue(py_result);
447 * Once can_seek_beginning can report errors, convert the
448 * exception to a status. For now, log and return false;
454 Py_XDECREF(py_result);
456 return can_seek_beginning;
460 bt_component_class_message_iterator_seek_beginning_method_status
461 component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
465 bt_component_class_message_iterator_seek_beginning_method_status status;
467 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
469 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
471 BT_ASSERT(!py_result || py_result == Py_None);
472 status = py_exc_to_status();
473 Py_XDECREF(py_result);
478 bt_component_class_port_connected_method_status component_class_port_connected(
479 bt_self_component *self_component,
480 void *self_component_port,
481 swig_type_info *self_component_port_swig_type,
482 bt_port_type self_component_port_type,
483 const void *other_port,
484 swig_type_info *other_port_swig_type)
486 bt_component_class_port_connected_method_status status;
487 PyObject *py_comp = NULL;
488 PyObject *py_self_port_ptr = NULL;
489 PyObject *py_other_port_ptr = NULL;
490 PyObject *py_method_result = NULL;
492 py_comp = bt_self_component_get_data(self_component);
494 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
495 self_component_port_swig_type, 0);
496 if (!py_self_port_ptr) {
497 BT_LOGF_STR("Failed to create a SWIG pointer object.");
498 status = __BT_FUNC_STATUS_MEMORY_ERROR;
502 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
503 other_port_swig_type, 0);
504 if (!py_other_port_ptr) {
505 BT_LOGF_STR("Failed to create a SWIG pointer object.");
506 status = __BT_FUNC_STATUS_MEMORY_ERROR;
509 py_method_result = PyObject_CallMethod(py_comp,
510 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
511 self_component_port_type, py_other_port_ptr);
512 BT_ASSERT(!py_method_result || py_method_result == Py_None);
513 status = py_exc_to_status();
516 Py_XDECREF(py_self_port_ptr);
517 Py_XDECREF(py_other_port_ptr);
518 Py_XDECREF(py_method_result);
523 bt_component_class_port_connected_method_status
524 component_class_source_output_port_connected(
525 bt_self_component_source *self_component_source,
526 bt_self_component_port_output *self_component_port_output,
527 const bt_port_input *other_port_input)
529 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
531 return component_class_port_connected(
533 self_component_port_output,
534 SWIGTYPE_p_bt_self_component_port_output,
537 SWIGTYPE_p_bt_port_input);
541 bt_component_class_port_connected_method_status
542 component_class_filter_input_port_connected(
543 bt_self_component_filter *self_component_filter,
544 bt_self_component_port_input *self_component_port_input,
545 const bt_port_output *other_port_output)
547 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
549 return component_class_port_connected(
551 self_component_port_input,
552 SWIGTYPE_p_bt_self_component_port_input,
555 SWIGTYPE_p_bt_port_output);
559 bt_component_class_port_connected_method_status
560 component_class_filter_output_port_connected(
561 bt_self_component_filter *self_component_filter,
562 bt_self_component_port_output *self_component_port_output,
563 const bt_port_input *other_port_input)
565 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
567 return component_class_port_connected(
569 self_component_port_output,
570 SWIGTYPE_p_bt_self_component_port_output,
573 SWIGTYPE_p_bt_port_input);
577 bt_component_class_port_connected_method_status
578 component_class_sink_input_port_connected(
579 bt_self_component_sink *self_component_sink,
580 bt_self_component_port_input *self_component_port_input,
581 const bt_port_output *other_port_output)
583 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
585 return component_class_port_connected(
587 self_component_port_input,
588 SWIGTYPE_p_bt_self_component_port_input,
591 SWIGTYPE_p_bt_port_output);
595 bt_component_class_sink_graph_is_configured_method_status
596 component_class_sink_graph_is_configured(
597 bt_self_component_sink *self_component_sink)
599 PyObject *py_comp = NULL;
600 PyObject *py_method_result = NULL;
601 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
602 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
604 py_comp = bt_self_component_get_data(self_component);
605 py_method_result = PyObject_CallMethod(py_comp,
606 "_bt_graph_is_configured_from_native", NULL);
607 BT_ASSERT(!py_method_result || py_method_result == Py_None);
608 status = py_exc_to_status();
609 Py_XDECREF(py_method_result);
614 bt_component_class_query_method_status component_class_query(
615 const bt_component_class *component_class,
616 const bt_query_executor *query_executor,
617 const char *object, const bt_value *params,
618 bt_logging_level log_level,
619 const bt_value **result)
621 PyObject *py_cls = NULL;
622 PyObject *py_params_ptr = NULL;
623 PyObject *py_query_exec_ptr = NULL;
624 PyObject *py_query_func = NULL;
625 PyObject *py_object = NULL;
626 PyObject *py_results_addr = NULL;
627 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
629 py_cls = lookup_cc_ptr_to_py_cls(component_class);
631 BT_LOGE("Cannot find Python class associated to native component class: "
632 "comp-cls-addr=%p", component_class);
636 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
637 SWIGTYPE_p_bt_value, 0);
638 if (!py_params_ptr) {
639 BT_LOGE_STR("Failed to create a SWIG pointer object.");
643 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
644 SWIGTYPE_p_bt_query_executor, 0);
645 if (!py_query_exec_ptr) {
646 BT_LOGE_STR("Failed to create a SWIG pointer object.");
650 py_object = SWIG_FromCharPtr(object);
652 BT_LOGE_STR("Failed to create a Python string.");
656 py_results_addr = PyObject_CallMethod(py_cls,
657 "_bt_query_from_native", "(OOOi)", py_query_exec_ptr,
658 py_object, py_params_ptr, (int) log_level);
659 if (!py_results_addr) {
660 BT_LOGW("Failed to call Python class's _bt_query_from_native() method: "
661 "py-cls-addr=%p", py_cls);
662 status = py_exc_to_status();
667 * The returned object, on success, is an integer object
668 * (PyLong) containing the address of a BT value object (new
671 *result = PyLong_AsVoidPtr(py_results_addr);
672 BT_ASSERT(!PyErr_Occurred());
678 status = __BT_FUNC_STATUS_ERROR;
681 Py_XDECREF(py_params_ptr);
682 Py_XDECREF(py_query_exec_ptr);
683 Py_XDECREF(py_query_func);
684 Py_XDECREF(py_object);
685 Py_XDECREF(py_results_addr);
690 bt_component_class_query_method_status component_class_source_query(
691 bt_self_component_class_source *self_component_class_source,
692 const bt_query_executor *query_executor,
693 const char *object, const bt_value *params,
694 bt_logging_level log_level,
695 const bt_value **result)
697 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
698 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
700 return component_class_query(component_class, query_executor, object, params, log_level, result);
704 bt_component_class_query_method_status component_class_filter_query(
705 bt_self_component_class_filter *self_component_class_filter,
706 const bt_query_executor *query_executor,
707 const char *object, const bt_value *params,
708 bt_logging_level log_level,
709 const bt_value **result)
711 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
712 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
714 return component_class_query(component_class, query_executor, object, params, log_level, result);
718 bt_component_class_query_method_status component_class_sink_query(
719 bt_self_component_class_sink *self_component_class_sink,
720 const bt_query_executor *query_executor,
721 const char *object, const bt_value *params,
722 bt_logging_level log_level,
723 const bt_value **result)
725 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
726 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
728 return component_class_query(component_class, query_executor, object, params, log_level, result);
732 bt_component_class_message_iterator_init_method_status
733 component_class_message_iterator_init(
734 bt_self_message_iterator *self_message_iterator,
735 bt_self_component *self_component,
736 bt_self_component_port_output *self_component_port_output)
738 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
739 PyObject *py_comp_cls = NULL;
740 PyObject *py_iter_cls = NULL;
741 PyObject *py_iter_ptr = NULL;
742 PyObject *py_component_port_output_ptr = NULL;
743 PyObject *py_init_method_result = NULL;
744 PyObject *py_iter = NULL;
747 py_comp = bt_self_component_get_data(self_component);
749 /* Find user's Python message iterator class */
750 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
752 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
756 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
758 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
762 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
763 SWIGTYPE_p_bt_self_message_iterator, 0);
765 BT_LOGE_STR("Failed to create a SWIG pointer object.");
770 * Create object with borrowed native message iterator
773 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
775 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
776 "(OO)", py_iter_cls, py_iter_ptr);
778 BT_LOGE("Failed to call Python class's __new__() method: "
779 "py-cls-addr=%p", py_iter_cls);
787 * py_iter.__init__(self_output_port)
789 * through the _init_for_native helper static method.
791 * At this point, py_iter._ptr is set, so this initialization
792 * function has access to self._component (which gives it the
793 * user Python component object from which the iterator was
796 py_component_port_output_ptr = SWIG_NewPointerObj(
797 SWIG_as_voidptr(self_component_port_output),
798 SWIGTYPE_p_bt_self_component_port_output, 0);
799 if (!py_component_port_output_ptr) {
800 BT_LOGE_STR("Failed to create a SWIG pointer object.");
804 py_init_method_result = PyObject_CallMethod(py_iter,
805 "_bt_init_from_native", "O", py_component_port_output_ptr);
806 if (!py_init_method_result) {
807 BT_LOGW_STR("User's __init__() method failed:");
813 * Since the Python code can never instantiate a user-defined
814 * message iterator class, the native message iterator
815 * object does NOT belong to a user Python message iterator
816 * object (borrowed reference). However this Python object is
817 * owned by this native message iterator object.
819 * In the Python world, the lifetime of the native message
820 * iterator is managed by a _GenericMessageIterator
823 * _GenericMessageIterator instance:
824 * owns a native bt_message_iterator object (iter)
825 * owns a _UserMessageIterator instance (py_iter)
826 * self._ptr is a borrowed reference to the
827 * native bt_private_connection_private_message_iterator
830 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
835 status = py_exc_to_status();
836 if (status == __BT_FUNC_STATUS_OK) {
838 * Looks like there wasn't any exception from the Python
839 * side, but we're still in an error state here.
841 status = __BT_FUNC_STATUS_ERROR;
845 * Clear any exception: we're returning a bad status anyway. If
846 * this call originated from Python, then the user gets an
847 * appropriate creation error.
852 Py_XDECREF(py_comp_cls);
853 Py_XDECREF(py_iter_cls);
854 Py_XDECREF(py_iter_ptr);
855 Py_XDECREF(py_component_port_output_ptr);
856 Py_XDECREF(py_init_method_result);
862 bt_component_class_message_iterator_init_method_status
863 component_class_source_message_iterator_init(
864 bt_self_message_iterator *self_message_iterator,
865 bt_self_component_source *self_component_source,
866 bt_self_component_port_output *self_component_port_output)
868 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
870 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
874 bt_component_class_message_iterator_init_method_status
875 component_class_filter_message_iterator_init(
876 bt_self_message_iterator *self_message_iterator,
877 bt_self_component_filter *self_component_filter,
878 bt_self_component_port_output *self_component_port_output)
880 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
882 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
886 void component_class_message_iterator_finalize(
887 bt_self_message_iterator *message_iterator)
889 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
890 PyObject *py_method_result = NULL;
892 BT_ASSERT(py_message_iter);
894 /* Call user's _finalize() method */
895 py_method_result = PyObject_CallMethod(py_message_iter,
898 if (PyErr_Occurred()) {
899 BT_LOGW("User's _finalize() method raised an exception: ignoring:");
904 * Ignore any exception raised by the _finalize() method because
905 * it won't change anything at this point: the component is
906 * being destroyed anyway.
909 Py_XDECREF(py_method_result);
910 Py_DECREF(py_message_iter);
913 /* Valid for both sources and filters. */
916 bt_component_class_message_iterator_next_method_status
917 component_class_message_iterator_next(
918 bt_self_message_iterator *message_iterator,
919 bt_message_array_const msgs, uint64_t capacity,
922 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
923 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
924 PyObject *py_method_result = NULL;
926 BT_ASSERT(py_message_iter);
927 py_method_result = PyObject_CallMethod(py_message_iter,
928 "_bt_next_from_native", NULL);
929 if (!py_method_result) {
930 status = py_exc_to_status();
931 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
936 * The returned object, on success, is an integer object
937 * (PyLong) containing the address of a native message
938 * object (which is now ours).
940 msgs[0] = PyLong_AsVoidPtr(py_method_result);
943 /* Clear potential overflow error; should never happen */
944 BT_ASSERT(!PyErr_Occurred());
948 Py_XDECREF(py_method_result);
953 bt_component_class_sink_consume_method_status
954 component_class_sink_consume(bt_self_component_sink *self_component_sink)
956 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
957 PyObject *py_comp = bt_self_component_get_data(self_component);
958 PyObject *py_method_result = NULL;
959 bt_component_class_sink_consume_method_status status;
962 py_method_result = PyObject_CallMethod(py_comp,
964 status = py_exc_to_status();
965 if (!py_method_result && status == __BT_FUNC_STATUS_OK) {
966 /* Pretty sure this should never happen, but just in case */
967 BT_LOGE("User's _consume() method failed without raising an exception: "
968 "status=%d", status);
969 status = __BT_FUNC_STATUS_ERROR;
972 Py_XDECREF(py_method_result);
977 int component_class_set_help_and_desc(
978 bt_component_class *component_class,
979 const char *description, const char *help)
984 ret = bt_component_class_set_description(component_class, description);
986 BT_LOGE("Cannot set component class's description: "
987 "comp-cls-addr=%p", component_class);
993 ret = bt_component_class_set_help(component_class, help);
995 BT_LOGE("Cannot set component class's help text: "
996 "comp-cls-addr=%p", component_class);
1008 bt_component_class_source *bt_bt2_component_class_source_create(
1009 PyObject *py_cls, const char *name, const char *description,
1012 bt_component_class_source *component_class_source;
1013 bt_component_class *component_class;
1017 component_class_source = bt_component_class_source_create(name,
1018 component_class_message_iterator_next);
1019 if (!component_class_source) {
1020 BT_LOGE_STR("Cannot create source component class.");
1024 component_class = bt_component_class_source_as_component_class(component_class_source);
1026 if (component_class_set_help_and_desc(component_class, description, help)) {
1030 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
1031 BT_ASSERT(ret == 0);
1032 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1033 BT_ASSERT(ret == 0);
1034 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
1035 component_class_can_seek_beginning);
1036 BT_ASSERT(ret == 0);
1037 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
1038 component_class_seek_beginning);
1039 BT_ASSERT(ret == 0);
1040 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1041 component_class_source_output_port_connected);
1042 BT_ASSERT(ret == 0);
1043 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1044 BT_ASSERT(ret == 0);
1045 ret = bt_component_class_source_set_message_iterator_init_method(
1046 component_class_source, component_class_source_message_iterator_init);
1047 BT_ASSERT(ret == 0);
1048 ret = bt_component_class_source_set_message_iterator_finalize_method(
1049 component_class_source, component_class_message_iterator_finalize);
1050 BT_ASSERT(ret == 0);
1051 register_cc_ptr_to_py_cls(component_class, py_cls);
1054 return component_class_source;
1058 bt_component_class_filter *bt_bt2_component_class_filter_create(
1059 PyObject *py_cls, const char *name, const char *description,
1062 bt_component_class *component_class;
1063 bt_component_class_filter *component_class_filter;
1067 component_class_filter = bt_component_class_filter_create(name,
1068 component_class_message_iterator_next);
1069 if (!component_class_filter) {
1070 BT_LOGE_STR("Cannot create filter component class.");
1074 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1076 if (component_class_set_help_and_desc(component_class, description, help)) {
1080 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
1081 BT_ASSERT(ret == 0);
1082 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1083 BT_ASSERT(ret == 0);
1084 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
1085 component_class_can_seek_beginning);
1086 BT_ASSERT(ret == 0);
1087 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
1088 component_class_seek_beginning);
1089 BT_ASSERT(ret == 0);
1090 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1091 component_class_filter_input_port_connected);
1092 BT_ASSERT(ret == 0);
1093 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1094 component_class_filter_output_port_connected);
1095 BT_ASSERT(ret == 0);
1096 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1097 BT_ASSERT(ret == 0);
1098 ret = bt_component_class_filter_set_message_iterator_init_method(
1099 component_class_filter, component_class_filter_message_iterator_init);
1100 BT_ASSERT(ret == 0);
1101 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1102 component_class_filter, component_class_message_iterator_finalize);
1103 BT_ASSERT(ret == 0);
1104 register_cc_ptr_to_py_cls(component_class, py_cls);
1107 return component_class_filter;
1111 bt_component_class_sink *bt_bt2_component_class_sink_create(
1112 PyObject *py_cls, const char *name, const char *description,
1115 bt_component_class_sink *component_class_sink;
1116 bt_component_class *component_class;
1120 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1122 if (!component_class_sink) {
1123 BT_LOGE_STR("Cannot create sink component class.");
1127 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1129 if (component_class_set_help_and_desc(component_class, description, help)) {
1133 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
1134 BT_ASSERT(ret == 0);
1135 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1136 BT_ASSERT(ret == 0);
1137 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1138 component_class_sink_input_port_connected);
1139 BT_ASSERT(ret == 0);
1140 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1141 component_class_sink_graph_is_configured);
1142 BT_ASSERT(ret == 0);
1143 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1144 BT_ASSERT(ret == 0);
1145 register_cc_ptr_to_py_cls(component_class, py_cls);
1148 return component_class_sink;
1152 struct bt_component_class_source *bt_bt2_component_class_source_create(
1153 PyObject *py_cls, const char *name, const char *description,
1155 struct bt_component_class_filter *bt_bt2_component_class_filter_create(
1156 PyObject *py_cls, const char *name, const char *description,
1158 struct bt_component_class_sink *bt_bt2_component_class_sink_create(
1159 PyObject *py_cls, const char *name, const char *description,
1161 void bt_bt2_cc_init_from_bt2(void);
1162 void bt_bt2_cc_exit_handler(void);