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-const.h>
26 %include <babeltrace2/graph/component-class-source-const.h>
27 %include <babeltrace2/graph/component-class-source.h>
28 %include <babeltrace2/graph/component-class-filter-const.h>
29 %include <babeltrace2/graph/component-class-filter.h>
30 %include <babeltrace2/graph/component-class-sink-const.h>
31 %include <babeltrace2/graph/component-class-sink.h>
32 %include <babeltrace2/graph/self-component-class-source.h>
33 %include <babeltrace2/graph/self-component-class-filter.h>
34 %include <babeltrace2/graph/self-component-class-sink.h>
38 * This hash table associates a BT component class object address to a
39 * user-defined Python class (PyObject *). The keys and values are NOT
40 * owned by this hash table. The Python class objects are owned by the
41 * Python module, which should not be unloaded until it is not possible
42 * to create a user Python component anyway.
44 * This hash table is written to when a user-defined Python component
45 * class is created by one of the bt_py3_component_class_*_create()
48 * This function is read from when a user calls bt_component_create()
49 * with a component class pointer created by one of the functions above.
50 * In this case, the original Python class needs to be found to
51 * instantiate it and associate the created Python component object with
52 * a BT component object instance.
55 static GHashTable *bt_cc_ptr_to_py_cls;
58 void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
61 if (!bt_cc_ptr_to_py_cls) {
63 * Lazy-initializing this GHashTable because GLib
64 * might not be initialized yet and it needs to be
65 * before we call g_hash_table_new()
67 BT_LOGD_STR("Creating native component class to Python component class hash table.");
68 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
69 BT_ASSERT(bt_cc_ptr_to_py_cls);
72 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
77 PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
79 if (!bt_cc_ptr_to_py_cls) {
80 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
81 "comp-cls-addr=%p", bt_cc);
85 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
86 (gconstpointer) bt_cc);
91 * Useful Python objects.
94 static PyObject *py_mod_bt2 = NULL;
95 static PyObject *py_mod_bt2_exc_error_type = NULL;
96 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
97 static PyObject *py_mod_bt2_exc_stop_type = NULL;
98 static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
99 static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL;
100 static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL;
103 void bt_py3_cc_init_from_bt2(void)
106 * This is called once the bt2 package is loaded.
108 * Those modules and functions are needed while the package is
109 * used. Loading them here is safe because we know the bt2
110 * package is imported, and we know that the user cannot use the
111 * code here without importing bt2 first.
113 py_mod_bt2 = PyImport_ImportModule("bt2");
114 BT_ASSERT(py_mod_bt2);
115 py_mod_bt2_exc_error_type =
116 PyObject_GetAttrString(py_mod_bt2, "Error");
117 BT_ASSERT(py_mod_bt2_exc_error_type);
118 py_mod_bt2_exc_try_again_type =
119 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
120 BT_ASSERT(py_mod_bt2_exc_try_again_type);
121 py_mod_bt2_exc_stop_type =
122 PyObject_GetAttrString(py_mod_bt2, "Stop");
123 BT_ASSERT(py_mod_bt2_exc_stop_type);
124 py_mod_bt2_exc_invalid_query_object_type =
125 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject");
126 BT_ASSERT(py_mod_bt2_exc_invalid_query_object_type);
127 py_mod_bt2_exc_invalid_query_params_type =
128 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams");
129 BT_ASSERT(py_mod_bt2_exc_invalid_query_params_type);
133 void bt_py3_cc_exit_handler(void)
136 * This is an exit handler (set by the bt2 package).
138 * We only give back the references that we took in
139 * bt_py3_cc_init_from_bt2() here. The global variables continue
140 * to exist for the code of this file, but they are now borrowed
141 * references. If this code is executed, it means that somehow
142 * the modules are still loaded, so it should be safe to use
143 * them even without a strong reference.
145 * We cannot do this in the library's destructor because it
146 * gets executed once Python is already finalized.
148 Py_XDECREF(py_mod_bt2);
149 Py_XDECREF(py_mod_bt2_exc_error_type);
150 Py_XDECREF(py_mod_bt2_exc_try_again_type);
151 Py_XDECREF(py_mod_bt2_exc_stop_type);
152 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
153 Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type);
154 Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type);
158 /* Library destructor */
160 __attribute__((destructor))
162 void bt_py3_native_comp_class_dtor(void) {
163 /* Destroy component class association hash table */
164 if (bt_cc_ptr_to_py_cls) {
165 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
166 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
171 void bt2_py_loge_exception(void)
175 BT_ASSERT(PyErr_Occurred() != NULL);
176 gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL);
178 /* bt_py_common_format_exception() logs errors */
182 BT_LOGE_STR(gstr->str);
186 g_string_free(gstr, TRUE);
191 bt_self_component_status bt_py3_exc_to_self_component_status(void)
193 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
194 PyObject *exc = PyErr_Occurred();
200 if (PyErr_GivenExceptionMatches(exc,
201 py_mod_bt2_exc_try_again_type)) {
202 status = BT_SELF_COMPONENT_STATUS_AGAIN;
203 } else if (PyErr_GivenExceptionMatches(exc,
204 py_mod_bt2_exc_stop_type)) {
205 status = BT_SELF_COMPONENT_STATUS_END;
207 bt2_py_loge_exception();
208 status = BT_SELF_COMPONENT_STATUS_ERROR;
216 /* Component class proxy methods (delegate to the attached Python object) */
219 bt_self_message_iterator_status bt_py3_exc_to_self_message_iterator_status(void)
221 enum bt_self_message_iterator_status status =
222 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
223 PyObject *exc = PyErr_Occurred();
229 if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) {
230 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
231 } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) {
232 status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN;
234 bt2_py_loge_exception();
235 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
244 enum bt_query_status bt_py3_exc_to_query_status(void)
246 enum bt_query_status status = BT_QUERY_STATUS_OK;
247 PyObject *exc = PyErr_Occurred();
253 if (PyErr_GivenExceptionMatches(exc,
254 py_mod_bt2_exc_invalid_query_object_type)) {
255 status = BT_QUERY_STATUS_INVALID_OBJECT;
256 } else if (PyErr_GivenExceptionMatches(exc,
257 py_mod_bt2_exc_invalid_query_params_type)) {
258 status = BT_QUERY_STATUS_INVALID_PARAMS;
259 } else if (PyErr_GivenExceptionMatches(exc,
260 py_mod_bt2_exc_try_again_type)) {
261 status = BT_QUERY_STATUS_AGAIN;
263 bt2_py_loge_exception();
264 status = BT_QUERY_STATUS_ERROR;
273 bt_self_component_status bt_py3_component_class_init(
274 bt_self_component *self_component,
275 void *self_component_v,
276 swig_type_info *self_comp_cls_type_swig_type,
277 const bt_value *params,
278 void *init_method_data)
280 const bt_component *component = bt_self_component_as_component(self_component);
281 const bt_component_class *component_class = bt_component_borrow_class_const(component);
282 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
283 PyObject *py_cls = NULL;
284 PyObject *py_comp = NULL;
285 PyObject *py_params_ptr = NULL;
286 PyObject *py_comp_ptr = NULL;
288 (void) init_method_data;
290 BT_ASSERT(self_component);
291 BT_ASSERT(self_component_v);
292 BT_ASSERT(self_comp_cls_type_swig_type);
295 * Get the user-defined Python class which created this
296 * component's class in the first place (borrowed
299 py_cls = lookup_cc_ptr_to_py_cls(component_class);
301 BT_LOGE("Cannot find Python class associated to native component class: "
302 "comp-cls-addr=%p", component_class);
306 /* Parameters pointer -> SWIG pointer Python object */
307 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
308 SWIGTYPE_p_bt_value, 0);
309 if (!py_params_ptr) {
310 BT_LOGE_STR("Failed to create a SWIG pointer object.");
314 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
315 self_comp_cls_type_swig_type, 0);
317 BT_LOGE_STR("Failed to create a SWIG pointer object.");
322 * Do the equivalent of this:
324 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
326 * _UserComponentType._init_from_native() calls the Python
327 * component object's __init__() function.
329 py_comp = PyObject_CallMethod(py_cls,
330 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
332 bt2_py_loge_exception();
333 BT_LOGE("Failed to call Python class's _init_from_native() method: "
334 "py-cls-addr=%p", py_cls);
340 * Our user Python component object is now fully created and
341 * initialized by the user. Since we just created it, this
342 * native component is its only (persistent) owner.
344 bt_self_component_set_data(self_component, py_comp);
349 status = BT_SELF_COMPONENT_STATUS_ERROR;
352 * Clear any exception: we're returning a bad status anyway. If
353 * this call originated from Python (creation from a plugin's
354 * component class, for example), then the user gets an
355 * appropriate creation error.
361 Py_XDECREF(py_params_ptr);
362 Py_XDECREF(py_comp_ptr);
367 * Method of bt_component_class_source to initialize a bt_self_component_source
372 bt_self_component_status bt_py3_component_class_source_init(
373 bt_self_component_source *self_component_source,
374 const bt_value *params, void *init_method_data)
376 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
377 return bt_py3_component_class_init(
379 self_component_source,
380 SWIGTYPE_p_bt_self_component_source,
381 params, init_method_data);
385 bt_self_component_status bt_py3_component_class_filter_init(
386 bt_self_component_filter *self_component_filter,
387 const bt_value *params, void *init_method_data)
389 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
390 return bt_py3_component_class_init(
392 self_component_filter,
393 SWIGTYPE_p_bt_self_component_filter,
394 params, init_method_data);
398 bt_self_component_status bt_py3_component_class_sink_init(
399 bt_self_component_sink *self_component_sink,
400 const bt_value *params, void *init_method_data)
402 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
403 return bt_py3_component_class_init(
406 SWIGTYPE_p_bt_self_component_sink,
407 params, init_method_data);
411 void bt_py3_component_class_finalize(bt_self_component *self_component)
413 PyObject *py_comp = bt_self_component_get_data(self_component);
416 /* Call user's _finalize() method */
417 PyObject *py_method_result = PyObject_CallMethod(py_comp,
420 if (PyErr_Occurred()) {
421 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
425 * Ignore any exception raised by the _finalize() method because
426 * it won't change anything at this point: the component is
427 * being destroyed anyway.
430 Py_XDECREF(py_method_result);
435 void bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source)
437 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
438 bt_py3_component_class_finalize(self_component);
442 void bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter)
444 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
445 bt_py3_component_class_finalize(self_component);
449 void bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink)
451 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
452 bt_py3_component_class_finalize(self_component);
456 bt_bool bt_py3_component_class_can_seek_beginning(
457 bt_self_message_iterator *self_message_iterator)
460 PyObject *py_result = NULL;
461 bt_bool can_seek_beginning = false;
463 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
466 py_result = PyObject_GetAttrString(py_iter, "_can_seek_beginning_from_native");
468 BT_ASSERT(!py_result || PyBool_Check(py_result));
471 can_seek_beginning = PyObject_IsTrue(py_result);
474 * Once can_seek_beginning can report errors, convert the
475 * exception to a status. For now, log and return false;
477 bt2_py_loge_exception();
481 Py_XDECREF(py_result);
483 return can_seek_beginning;
487 bt_self_message_iterator_status bt_py3_component_class_seek_beginning(
488 bt_self_message_iterator *self_message_iterator)
492 bt_self_message_iterator_status status;
494 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
497 py_result = PyObject_CallMethod(py_iter, "_seek_beginning_from_native", NULL);
499 BT_ASSERT(!py_result || py_result == Py_None);
500 status = bt_py3_exc_to_self_message_iterator_status();
502 Py_XDECREF(py_result);
508 bt_self_component_status bt_py3_component_class_port_connected(
509 bt_self_component *self_component,
510 void *self_component_port,
511 swig_type_info *self_component_port_swig_type,
512 bt_port_type self_component_port_type,
513 const void *other_port,
514 swig_type_info *other_port_swig_type)
516 bt_self_component_status status;
517 PyObject *py_comp = NULL;
518 PyObject *py_self_port_ptr = NULL;
519 PyObject *py_other_port_ptr = NULL;
520 PyObject *py_method_result = NULL;
522 py_comp = bt_self_component_get_data(self_component);
525 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
526 self_component_port_swig_type, 0);
527 if (!py_self_port_ptr) {
528 BT_LOGF_STR("Failed to create a SWIG pointer object.");
529 status = BT_SELF_COMPONENT_STATUS_NOMEM;
533 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
534 other_port_swig_type, 0);
535 if (!py_other_port_ptr) {
536 BT_LOGF_STR("Failed to create a SWIG pointer object.");
537 status = BT_SELF_COMPONENT_STATUS_NOMEM;
540 py_method_result = PyObject_CallMethod(py_comp,
541 "_port_connected_from_native", "(OiO)", py_self_port_ptr,
542 self_component_port_type, py_other_port_ptr);
544 BT_ASSERT(!py_method_result || py_method_result == Py_None);
546 status = bt_py3_exc_to_self_component_status();
549 Py_XDECREF(py_self_port_ptr);
550 Py_XDECREF(py_other_port_ptr);
551 Py_XDECREF(py_method_result);
557 bt_self_component_status bt_py3_component_class_source_output_port_connected(
558 bt_self_component_source *self_component_source,
559 bt_self_component_port_output *self_component_port_output,
560 const bt_port_input *other_port_input)
562 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
564 return bt_py3_component_class_port_connected(
566 self_component_port_output,
567 SWIGTYPE_p_bt_self_component_port_output,
570 SWIGTYPE_p_bt_port_input);
574 bt_self_component_status bt_py3_component_class_filter_input_port_connected(
575 bt_self_component_filter *self_component_filter,
576 bt_self_component_port_input *self_component_port_input,
577 const bt_port_output *other_port_output)
579 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
581 return bt_py3_component_class_port_connected(
583 self_component_port_input,
584 SWIGTYPE_p_bt_self_component_port_input,
587 SWIGTYPE_p_bt_port_output);
591 bt_self_component_status bt_py3_component_class_filter_output_port_connected(
592 bt_self_component_filter *self_component_filter,
593 bt_self_component_port_output *self_component_port_output,
594 const bt_port_input *other_port_input)
596 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
598 return bt_py3_component_class_port_connected(
600 self_component_port_output,
601 SWIGTYPE_p_bt_self_component_port_output,
604 SWIGTYPE_p_bt_port_input);
608 bt_self_component_status bt_py3_component_class_sink_input_port_connected(
609 bt_self_component_sink *self_component_sink,
610 bt_self_component_port_input *self_component_port_input,
611 const bt_port_output *other_port_output)
613 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
615 return bt_py3_component_class_port_connected(
617 self_component_port_input,
618 SWIGTYPE_p_bt_self_component_port_input,
621 SWIGTYPE_p_bt_port_output);
625 bt_self_component_status bt_py3_component_class_sink_graph_is_configured(
626 bt_self_component_sink *self_component_sink)
628 PyObject *py_comp = NULL;
629 PyObject *py_method_result = NULL;
630 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
631 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
633 py_comp = bt_self_component_get_data(self_component);
634 py_method_result = PyObject_CallMethod(py_comp,
635 "_graph_is_configured_from_native", NULL);
637 BT_ASSERT(!py_method_result || py_method_result == Py_None);
639 status = bt_py3_exc_to_self_component_status();
641 Py_XDECREF(py_method_result);
647 bt_query_status bt_py3_component_class_query(
648 const bt_component_class *component_class,
649 const bt_query_executor *query_executor,
650 const char *object, const bt_value *params,
651 bt_logging_level log_level,
652 const bt_value **result)
654 PyObject *py_cls = NULL;
655 PyObject *py_params_ptr = NULL;
656 PyObject *py_query_exec_ptr = NULL;
657 PyObject *py_query_func = NULL;
658 PyObject *py_object = NULL;
659 PyObject *py_results_addr = NULL;
660 bt_query_status status = BT_QUERY_STATUS_OK;
662 py_cls = lookup_cc_ptr_to_py_cls(component_class);
664 BT_LOGE("Cannot find Python class associated to native component class: "
665 "comp-cls-addr=%p", component_class);
669 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
670 SWIGTYPE_p_bt_value, 0);
671 if (!py_params_ptr) {
672 BT_LOGE_STR("Failed to create a SWIG pointer object.");
676 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
677 SWIGTYPE_p_bt_query_executor, 0);
678 if (!py_query_exec_ptr) {
679 BT_LOGE_STR("Failed to create a SWIG pointer object.");
683 py_object = SWIG_FromCharPtr(object);
685 BT_LOGE_STR("Failed to create a Python string.");
689 py_results_addr = PyObject_CallMethod(py_cls,
690 "_query_from_native", "(OOOi)", py_query_exec_ptr,
691 py_object, py_params_ptr, (int) log_level);
693 if (!py_results_addr) {
694 BT_LOGE("Failed to call Python class's _query_from_native() method: "
695 "py-cls-addr=%p", py_cls);
696 status = bt_py3_exc_to_query_status();
701 * The returned object, on success, is an integer object
702 * (PyLong) containing the address of a BT value object (new
705 *result = PyLong_AsVoidPtr(py_results_addr);
706 BT_ASSERT(!PyErr_Occurred());
712 status = BT_QUERY_STATUS_ERROR;
715 Py_XDECREF(py_params_ptr);
716 Py_XDECREF(py_query_exec_ptr);
717 Py_XDECREF(py_query_func);
718 Py_XDECREF(py_object);
719 Py_XDECREF(py_results_addr);
724 bt_query_status bt_py3_component_class_source_query(
725 bt_self_component_class_source *self_component_class_source,
726 const bt_query_executor *query_executor,
727 const char *object, const bt_value *params,
728 bt_logging_level log_level,
729 const bt_value **result)
731 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
732 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
733 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
737 bt_query_status bt_py3_component_class_filter_query(
738 bt_self_component_class_filter *self_component_class_filter,
739 const bt_query_executor *query_executor,
740 const char *object, const bt_value *params,
741 bt_logging_level log_level,
742 const bt_value **result)
744 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
745 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
746 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
750 bt_query_status bt_py3_component_class_sink_query(
751 bt_self_component_class_sink *self_component_class_sink,
752 const bt_query_executor *query_executor,
753 const char *object, const bt_value *params,
754 bt_logging_level log_level,
755 const bt_value **result)
757 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
758 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
759 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
763 bt_self_message_iterator_status bt_py3_component_class_message_iterator_init(
764 bt_self_message_iterator *self_message_iterator,
765 bt_self_component *self_component,
766 bt_self_component_port_output *self_component_port_output)
768 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
769 PyObject *py_comp_cls = NULL;
770 PyObject *py_iter_cls = NULL;
771 PyObject *py_iter_ptr = NULL;
772 PyObject *py_component_port_output_ptr = NULL;
773 PyObject *py_init_method_result = NULL;
774 PyObject *py_iter = NULL;
777 py_comp = bt_self_component_get_data(self_component);
779 /* Find user's Python message iterator class */
780 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
782 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
786 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
788 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
792 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
793 SWIGTYPE_p_bt_self_message_iterator, 0);
795 BT_LOGE_STR("Failed to create a SWIG pointer object.");
800 * Create object with borrowed native message iterator
803 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
805 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
806 "(OO)", py_iter_cls, py_iter_ptr);
808 BT_LOGE("Failed to call Python class's __new__() method: "
809 "py-cls-addr=%p", py_iter_cls);
810 bt2_py_loge_exception();
817 * py_iter.__init__(self_output_port)
819 * through the _init_for_native helper static method.
821 * At this point, py_iter._ptr is set, so this initialization
822 * function has access to self._component (which gives it the
823 * user Python component object from which the iterator was
826 py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output),
827 SWIGTYPE_p_bt_self_component_port_output, 0);
828 if (!py_component_port_output_ptr) {
829 BT_LOGE_STR("Failed to create a SWIG pointer object.");
833 py_init_method_result = PyObject_CallMethod(py_iter, "_init_from_native", "O", py_component_port_output_ptr);
834 if (!py_init_method_result) {
835 BT_LOGE_STR("User's __init__() method failed.");
836 bt2_py_loge_exception();
841 * Since the Python code can never instantiate a user-defined
842 * message iterator class, the native message iterator
843 * object does NOT belong to a user Python message iterator
844 * object (borrowed reference). However this Python object is
845 * owned by this native message iterator object.
847 * In the Python world, the lifetime of the native message
848 * iterator is managed by a _GenericMessageIterator
851 * _GenericMessageIterator instance:
852 * owns a native bt_message_iterator object (iter)
853 * owns a _UserMessageIterator instance (py_iter)
854 * self._ptr is a borrowed reference to the
855 * native bt_private_connection_private_message_iterator
858 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
863 status = bt_py3_exc_to_self_message_iterator_status();
864 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
866 * Looks like there wasn't any exception from the Python
867 * side, but we're still in an error state here.
869 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
873 * Clear any exception: we're returning a bad status anyway. If
874 * this call originated from Python, then the user gets an
875 * appropriate creation error.
880 Py_XDECREF(py_comp_cls);
881 Py_XDECREF(py_iter_cls);
882 Py_XDECREF(py_iter_ptr);
883 Py_XDECREF(py_component_port_output_ptr);
884 Py_XDECREF(py_init_method_result);
890 bt_self_message_iterator_status bt_py3_component_class_source_message_iterator_init(
891 bt_self_message_iterator *self_message_iterator,
892 bt_self_component_source *self_component_source,
893 bt_self_component_port_output *self_component_port_output)
895 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
896 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
900 bt_self_message_iterator_status bt_py3_component_class_filter_message_iterator_init(
901 bt_self_message_iterator *self_message_iterator,
902 bt_self_component_filter *self_component_filter,
903 bt_self_component_port_output *self_component_port_output)
905 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
906 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
910 void bt_py3_component_class_message_iterator_finalize(
911 bt_self_message_iterator *message_iterator)
913 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
914 PyObject *py_method_result = NULL;
916 BT_ASSERT(py_message_iter);
918 /* Call user's _finalize() method */
919 py_method_result = PyObject_CallMethod(py_message_iter,
922 if (PyErr_Occurred()) {
923 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
927 * Ignore any exception raised by the _finalize() method because
928 * it won't change anything at this point: the component is
929 * being destroyed anyway.
932 Py_XDECREF(py_method_result);
933 Py_DECREF(py_message_iter);
936 /* Valid for both sources and filters. */
939 bt_self_message_iterator_status bt_py3_component_class_message_iterator_next(
940 bt_self_message_iterator *message_iterator,
941 bt_message_array_const msgs, uint64_t capacity,
944 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
945 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
946 PyObject *py_method_result = NULL;
948 BT_ASSERT(py_message_iter);
949 py_method_result = PyObject_CallMethod(py_message_iter,
950 "_next_from_native", NULL);
951 if (!py_method_result) {
952 status = bt_py3_exc_to_self_message_iterator_status();
953 BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK);
958 * The returned object, on success, is an integer object
959 * (PyLong) containing the address of a native message
960 * object (which is now ours).
962 msgs[0] = PyLong_AsVoidPtr(py_method_result);
965 /* Clear potential overflow error; should never happen */
966 BT_ASSERT(!PyErr_Occurred());
970 Py_XDECREF(py_method_result);
975 bt_self_component_status bt_py3_component_class_sink_consume(
976 bt_self_component_sink *self_component_sink)
978 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
979 PyObject *py_comp = bt_self_component_get_data(self_component);
980 PyObject *py_method_result = NULL;
981 bt_self_component_status status;
984 py_method_result = PyObject_CallMethod(py_comp,
987 status = bt_py3_exc_to_self_component_status();
988 if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) {
989 /* Pretty sure this should never happen, but just in case */
990 BT_LOGE("User's _consume() method failed without raising an exception: "
991 "status=%d", status);
992 status = BT_SELF_COMPONENT_STATUS_ERROR;
995 Py_XDECREF(py_method_result);
1000 int bt_py3_component_class_set_help_and_desc(
1001 bt_component_class *component_class,
1002 const char *description, const char *help)
1007 ret = bt_component_class_set_description(component_class, description);
1009 BT_LOGE("Cannot set component class's description: "
1010 "comp-cls-addr=%p", component_class);
1016 ret = bt_component_class_set_help(component_class, help);
1018 BT_LOGE("Cannot set component class's help text: "
1019 "comp-cls-addr=%p", component_class);
1031 bt_component_class_source *bt_py3_component_class_source_create(
1032 PyObject *py_cls, const char *name, const char *description,
1035 bt_component_class_source *component_class_source;
1036 bt_component_class *component_class;
1041 component_class_source = bt_component_class_source_create(name,
1042 bt_py3_component_class_message_iterator_next);
1043 if (!component_class_source) {
1044 BT_LOGE_STR("Cannot create source component class.");
1048 component_class = bt_component_class_source_as_component_class(component_class_source);
1050 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1054 ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init);
1055 BT_ASSERT(ret == 0);
1056 ret = bt_component_class_source_set_finalize_method(component_class_source, bt_py3_component_class_source_finalize);
1057 BT_ASSERT(ret == 0);
1058 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
1059 bt_py3_component_class_can_seek_beginning);
1060 BT_ASSERT(ret == 0);
1061 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
1062 bt_py3_component_class_seek_beginning);
1063 BT_ASSERT(ret == 0);
1064 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1065 bt_py3_component_class_source_output_port_connected);
1066 BT_ASSERT(ret == 0);
1067 ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query);
1068 BT_ASSERT(ret == 0);
1069 ret = bt_component_class_source_set_message_iterator_init_method(
1070 component_class_source, bt_py3_component_class_source_message_iterator_init);
1071 BT_ASSERT(ret == 0);
1072 ret = bt_component_class_source_set_message_iterator_finalize_method(
1073 component_class_source, bt_py3_component_class_message_iterator_finalize);
1074 BT_ASSERT(ret == 0);
1076 register_cc_ptr_to_py_cls(component_class, py_cls);
1079 return component_class_source;
1083 bt_component_class_filter *bt_py3_component_class_filter_create(
1084 PyObject *py_cls, const char *name, const char *description,
1087 bt_component_class *component_class;
1088 bt_component_class_filter *component_class_filter;
1093 component_class_filter = bt_component_class_filter_create(name,
1094 bt_py3_component_class_message_iterator_next);
1095 if (!component_class_filter) {
1096 BT_LOGE_STR("Cannot create filter component class.");
1100 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1102 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1106 ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init);
1107 BT_ASSERT(ret == 0);
1108 ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize);
1109 BT_ASSERT(ret == 0);
1110 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
1111 bt_py3_component_class_can_seek_beginning);
1112 BT_ASSERT(ret == 0);
1113 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
1114 bt_py3_component_class_seek_beginning);
1115 BT_ASSERT(ret == 0);
1116 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1117 bt_py3_component_class_filter_input_port_connected);
1118 BT_ASSERT(ret == 0);
1119 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1120 bt_py3_component_class_filter_output_port_connected);
1121 BT_ASSERT(ret == 0);
1122 ret = bt_component_class_filter_set_query_method(component_class_filter, bt_py3_component_class_filter_query);
1123 BT_ASSERT(ret == 0);
1124 ret = bt_component_class_filter_set_message_iterator_init_method(
1125 component_class_filter, bt_py3_component_class_filter_message_iterator_init);
1126 BT_ASSERT(ret == 0);
1127 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1128 component_class_filter, bt_py3_component_class_message_iterator_finalize);
1129 BT_ASSERT(ret == 0);
1131 register_cc_ptr_to_py_cls(component_class, py_cls);
1134 return component_class_filter;
1138 bt_component_class_sink *bt_py3_component_class_sink_create(
1139 PyObject *py_cls, const char *name, const char *description,
1142 bt_component_class_sink *component_class_sink;
1143 bt_component_class *component_class;
1148 component_class_sink = bt_component_class_sink_create(name, bt_py3_component_class_sink_consume);
1150 if (!component_class_sink) {
1151 BT_LOGE_STR("Cannot create sink component class.");
1155 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1157 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1161 ret = bt_component_class_sink_set_init_method(component_class_sink, bt_py3_component_class_sink_init);
1162 BT_ASSERT(ret == 0);
1163 ret = bt_component_class_sink_set_finalize_method(component_class_sink, bt_py3_component_class_sink_finalize);
1164 BT_ASSERT(ret == 0);
1165 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1166 bt_py3_component_class_sink_input_port_connected);
1167 BT_ASSERT(ret == 0);
1168 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1169 bt_py3_component_class_sink_graph_is_configured);
1170 BT_ASSERT(ret == 0);
1171 ret = bt_component_class_sink_set_query_method(component_class_sink, bt_py3_component_class_sink_query);
1172 BT_ASSERT(ret == 0);
1174 register_cc_ptr_to_py_cls(component_class, py_cls);
1177 return component_class_sink;
1181 struct bt_component_class_source *bt_py3_component_class_source_create(
1182 PyObject *py_cls, const char *name, const char *description,
1184 struct bt_component_class_filter *bt_py3_component_class_filter_create(
1185 PyObject *py_cls, const char *name, const char *description,
1187 struct bt_component_class_sink *bt_py3_component_class_sink_create(
1188 PyObject *py_cls, const char *name, const char *description,
1190 void bt_py3_cc_init_from_bt2(void);
1191 void bt_py3_cc_exit_handler(void);