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 // TODO: maybe we can wrap code in the Python methods (e.g. _query_from_native)
172 // in a try catch and print the error there instead, it would be simpler.
174 void bt2_py_loge_exception(void)
176 PyObject *type = NULL;
177 PyObject *value = NULL;
178 PyObject *traceback = NULL;
179 PyObject *traceback_module = NULL;
180 PyObject *format_exception_func = NULL;
181 PyObject *exc_str_list = NULL;
182 GString *msg_buf = NULL;
185 BT_ASSERT(PyErr_Occurred() != NULL);
187 PyErr_Fetch(&type, &value, &traceback);
189 BT_ASSERT(type != NULL);
192 * traceback can be NULL, when we fail to call a Python function from the
193 * native code (there is no Python stack at that point). E.g.:
195 * TypeError: _query_from_native() takes 5 positional arguments but 8 were given
199 /* Make sure `value` is what we expected - an instance of `type`. */
200 PyErr_NormalizeException(&type, &value, &traceback);
202 traceback_module = PyImport_ImportModule("traceback");
203 if (!traceback_module) {
204 BT_LOGE_STR("Failed to log Python exception (could not import traceback module).");
208 format_exception_func = PyObject_GetAttrString(traceback_module,
209 traceback ? "format_exception" : "format_exception_only");
210 if (!format_exception_func) {
211 BT_LOGE_STR("Failed to log Python exception (could not find format_exception).");
215 if (!PyCallable_Check(format_exception_func)) {
216 BT_LOGE_STR("Failed to log Python exception (format_exception is not callable).");
220 exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, type, value, traceback, NULL);
223 BT_LOGE_STR("Failed to log Python exception (call to format_exception failed).");
227 msg_buf = g_string_new(NULL);
229 for (i = 0; i < PyList_Size(exc_str_list); i++) {
230 PyObject *exc_str = PyList_GetItem(exc_str_list, i);
231 const char *str = PyUnicode_AsUTF8(exc_str);
233 BT_LOGE_STR("Failed to log Python exception (failed to convert exception to string).");
237 g_string_append(msg_buf, str);
240 BT_LOGE_STR(msg_buf->str);
244 g_string_free(msg_buf, TRUE);
246 Py_XDECREF(exc_str_list);
247 Py_XDECREF(format_exception_func);
248 Py_XDECREF(traceback_module);
250 /* PyErr_Restore takes our references. */
251 PyErr_Restore(type, value, traceback);
255 bt_self_component_status bt_py3_exc_to_self_component_status(void)
257 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
258 PyObject *exc = PyErr_Occurred();
264 if (PyErr_GivenExceptionMatches(exc,
265 py_mod_bt2_exc_try_again_type)) {
266 status = BT_SELF_COMPONENT_STATUS_AGAIN;
267 } else if (PyErr_GivenExceptionMatches(exc,
268 py_mod_bt2_exc_stop_type)) {
269 status = BT_SELF_COMPONENT_STATUS_END;
271 bt2_py_loge_exception();
272 status = BT_SELF_COMPONENT_STATUS_ERROR;
280 /* Component class proxy methods (delegate to the attached Python object) */
283 bt_self_message_iterator_status bt_py3_exc_to_self_message_iterator_status(void)
285 enum bt_self_message_iterator_status status =
286 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
287 PyObject *exc = PyErr_Occurred();
293 if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) {
294 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
295 } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) {
296 status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN;
298 bt2_py_loge_exception();
299 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
308 enum bt_query_status bt_py3_exc_to_query_status(void)
310 enum bt_query_status status = BT_QUERY_STATUS_OK;
311 PyObject *exc = PyErr_Occurred();
317 if (PyErr_GivenExceptionMatches(exc,
318 py_mod_bt2_exc_invalid_query_object_type)) {
319 status = BT_QUERY_STATUS_INVALID_OBJECT;
320 } else if (PyErr_GivenExceptionMatches(exc,
321 py_mod_bt2_exc_invalid_query_params_type)) {
322 status = BT_QUERY_STATUS_INVALID_PARAMS;
323 } else if (PyErr_GivenExceptionMatches(exc,
324 py_mod_bt2_exc_try_again_type)) {
325 status = BT_QUERY_STATUS_AGAIN;
327 bt2_py_loge_exception();
328 status = BT_QUERY_STATUS_ERROR;
337 bt_self_component_status bt_py3_component_class_init(
338 bt_self_component *self_component,
339 void *self_component_v,
340 swig_type_info *self_comp_cls_type_swig_type,
341 const bt_value *params,
342 void *init_method_data)
344 const bt_component *component = bt_self_component_as_component(self_component);
345 const bt_component_class *component_class = bt_component_borrow_class_const(component);
346 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
347 PyObject *py_cls = NULL;
348 PyObject *py_comp = NULL;
349 PyObject *py_params_ptr = NULL;
350 PyObject *py_comp_ptr = NULL;
352 (void) init_method_data;
354 BT_ASSERT(self_component);
355 BT_ASSERT(self_component_v);
356 BT_ASSERT(self_comp_cls_type_swig_type);
359 * Get the user-defined Python class which created this
360 * component's class in the first place (borrowed
363 py_cls = lookup_cc_ptr_to_py_cls(component_class);
365 BT_LOGE("Cannot find Python class associated to native component class: "
366 "comp-cls-addr=%p", component_class);
370 /* Parameters pointer -> SWIG pointer Python object */
371 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
372 SWIGTYPE_p_bt_value, 0);
373 if (!py_params_ptr) {
374 BT_LOGE_STR("Failed to create a SWIG pointer object.");
378 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
379 self_comp_cls_type_swig_type, 0);
381 BT_LOGE_STR("Failed to create a SWIG pointer object.");
386 * Do the equivalent of this:
388 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
390 * _UserComponentType._init_from_native() calls the Python
391 * component object's __init__() function.
393 py_comp = PyObject_CallMethod(py_cls,
394 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
396 bt2_py_loge_exception();
397 BT_LOGE("Failed to call Python class's _init_from_native() method: "
398 "py-cls-addr=%p", py_cls);
404 * Our user Python component object is now fully created and
405 * initialized by the user. Since we just created it, this
406 * native component is its only (persistent) owner.
408 bt_self_component_set_data(self_component, py_comp);
413 status = BT_SELF_COMPONENT_STATUS_ERROR;
416 * Clear any exception: we're returning a bad status anyway. If
417 * this call originated from Python (creation from a plugin's
418 * component class, for example), then the user gets an
419 * appropriate creation error.
425 Py_XDECREF(py_params_ptr);
426 Py_XDECREF(py_comp_ptr);
431 * Method of bt_component_class_source to initialize a bt_self_component_source
436 bt_self_component_status bt_py3_component_class_source_init(
437 bt_self_component_source *self_component_source,
438 const bt_value *params, void *init_method_data)
440 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
441 return bt_py3_component_class_init(
443 self_component_source,
444 SWIGTYPE_p_bt_self_component_source,
445 params, init_method_data);
449 bt_self_component_status bt_py3_component_class_filter_init(
450 bt_self_component_filter *self_component_filter,
451 const bt_value *params, void *init_method_data)
453 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
454 return bt_py3_component_class_init(
456 self_component_filter,
457 SWIGTYPE_p_bt_self_component_filter,
458 params, init_method_data);
462 bt_self_component_status bt_py3_component_class_sink_init(
463 bt_self_component_sink *self_component_sink,
464 const bt_value *params, void *init_method_data)
466 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
467 return bt_py3_component_class_init(
470 SWIGTYPE_p_bt_self_component_sink,
471 params, init_method_data);
475 void bt_py3_component_class_finalize(bt_self_component *self_component)
477 PyObject *py_comp = bt_self_component_get_data(self_component);
480 /* Call user's _finalize() method */
481 PyObject *py_method_result = PyObject_CallMethod(py_comp,
484 if (PyErr_Occurred()) {
485 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
489 * Ignore any exception raised by the _finalize() method because
490 * it won't change anything at this point: the component is
491 * being destroyed anyway.
494 Py_XDECREF(py_method_result);
499 void bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source)
501 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
502 bt_py3_component_class_finalize(self_component);
506 void bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter)
508 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
509 bt_py3_component_class_finalize(self_component);
513 void bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink)
515 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
516 bt_py3_component_class_finalize(self_component);
520 bt_self_component_status bt_py3_component_class_port_connected(
521 bt_self_component *self_component,
522 void *self_component_port,
523 swig_type_info *self_component_port_swig_type,
524 bt_port_type self_component_port_type,
525 const void *other_port,
526 swig_type_info *other_port_swig_type)
528 bt_self_component_status status;
529 PyObject *py_comp = NULL;
530 PyObject *py_self_port_ptr = NULL;
531 PyObject *py_other_port_ptr = NULL;
532 PyObject *py_method_result = NULL;
534 py_comp = bt_self_component_get_data(self_component);
537 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
538 self_component_port_swig_type, 0);
539 if (!py_self_port_ptr) {
540 BT_LOGF_STR("Failed to create a SWIG pointer object.");
541 status = BT_SELF_COMPONENT_STATUS_NOMEM;
545 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
546 other_port_swig_type, 0);
547 if (!py_other_port_ptr) {
548 BT_LOGF_STR("Failed to create a SWIG pointer object.");
549 status = BT_SELF_COMPONENT_STATUS_NOMEM;
552 py_method_result = PyObject_CallMethod(py_comp,
553 "_port_connected_from_native", "(OiO)", py_self_port_ptr,
554 self_component_port_type, py_other_port_ptr);
556 BT_ASSERT(!py_method_result || py_method_result == Py_None);
558 status = bt_py3_exc_to_self_component_status();
561 Py_XDECREF(py_self_port_ptr);
562 Py_XDECREF(py_other_port_ptr);
563 Py_XDECREF(py_method_result);
569 bt_self_component_status bt_py3_component_class_source_output_port_connected(
570 bt_self_component_source *self_component_source,
571 bt_self_component_port_output *self_component_port_output,
572 const bt_port_input *other_port_input)
574 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
576 return bt_py3_component_class_port_connected(
578 self_component_port_output,
579 SWIGTYPE_p_bt_self_component_port_output,
582 SWIGTYPE_p_bt_port_input);
586 bt_self_component_status bt_py3_component_class_filter_input_port_connected(
587 bt_self_component_filter *self_component_filter,
588 bt_self_component_port_input *self_component_port_input,
589 const bt_port_output *other_port_output)
591 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
593 return bt_py3_component_class_port_connected(
595 self_component_port_input,
596 SWIGTYPE_p_bt_self_component_port_input,
599 SWIGTYPE_p_bt_port_output);
603 bt_self_component_status bt_py3_component_class_filter_output_port_connected(
604 bt_self_component_filter *self_component_filter,
605 bt_self_component_port_output *self_component_port_output,
606 const bt_port_input *other_port_input)
608 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
610 return bt_py3_component_class_port_connected(
612 self_component_port_output,
613 SWIGTYPE_p_bt_self_component_port_output,
616 SWIGTYPE_p_bt_port_input);
620 bt_self_component_status bt_py3_component_class_sink_input_port_connected(
621 bt_self_component_sink *self_component_sink,
622 bt_self_component_port_input *self_component_port_input,
623 const bt_port_output *other_port_output)
625 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
627 return bt_py3_component_class_port_connected(
629 self_component_port_input,
630 SWIGTYPE_p_bt_self_component_port_input,
633 SWIGTYPE_p_bt_port_output);
637 bt_self_component_status bt_py3_component_class_sink_graph_is_configured(
638 bt_self_component_sink *self_component_sink)
640 PyObject *py_comp = NULL;
641 PyObject *py_method_result = NULL;
642 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
643 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
645 py_comp = bt_self_component_get_data(self_component);
646 py_method_result = PyObject_CallMethod(py_comp,
647 "_graph_is_configured_from_native", NULL);
649 BT_ASSERT(!py_method_result || py_method_result == Py_None);
651 status = bt_py3_exc_to_self_component_status();
653 Py_XDECREF(py_method_result);
659 bt_query_status bt_py3_component_class_query(
660 const bt_component_class *component_class,
661 const bt_query_executor *query_executor,
662 const char *object, const bt_value *params,
663 bt_logging_level log_level,
664 const bt_value **result)
666 PyObject *py_cls = NULL;
667 PyObject *py_params_ptr = NULL;
668 PyObject *py_query_exec_ptr = NULL;
669 PyObject *py_query_func = NULL;
670 PyObject *py_object = NULL;
671 PyObject *py_results_addr = NULL;
672 bt_query_status status = BT_QUERY_STATUS_OK;
674 py_cls = lookup_cc_ptr_to_py_cls(component_class);
676 BT_LOGE("Cannot find Python class associated to native component class: "
677 "comp-cls-addr=%p", component_class);
681 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
682 SWIGTYPE_p_bt_value, 0);
683 if (!py_params_ptr) {
684 BT_LOGE_STR("Failed to create a SWIG pointer object.");
688 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
689 SWIGTYPE_p_bt_query_executor, 0);
690 if (!py_query_exec_ptr) {
691 BT_LOGE_STR("Failed to create a SWIG pointer object.");
695 py_object = SWIG_FromCharPtr(object);
697 BT_LOGE_STR("Failed to create a Python string.");
701 py_results_addr = PyObject_CallMethod(py_cls,
702 "_query_from_native", "(OOOi)", py_query_exec_ptr,
703 py_object, py_params_ptr, (int) log_level);
705 if (!py_results_addr) {
706 BT_LOGE("Failed to call Python class's _query_from_native() method: "
707 "py-cls-addr=%p", py_cls);
708 status = bt_py3_exc_to_query_status();
713 * The returned object, on success, is an integer object
714 * (PyLong) containing the address of a BT value object (new
717 *result = PyLong_AsVoidPtr(py_results_addr);
718 BT_ASSERT(!PyErr_Occurred());
724 status = BT_QUERY_STATUS_ERROR;
727 Py_XDECREF(py_params_ptr);
728 Py_XDECREF(py_query_exec_ptr);
729 Py_XDECREF(py_query_func);
730 Py_XDECREF(py_object);
731 Py_XDECREF(py_results_addr);
736 bt_query_status bt_py3_component_class_source_query(
737 bt_self_component_class_source *self_component_class_source,
738 const bt_query_executor *query_executor,
739 const char *object, const bt_value *params,
740 bt_logging_level log_level,
741 const bt_value **result)
743 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
744 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
745 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
749 bt_query_status bt_py3_component_class_filter_query(
750 bt_self_component_class_filter *self_component_class_filter,
751 const bt_query_executor *query_executor,
752 const char *object, const bt_value *params,
753 bt_logging_level log_level,
754 const bt_value **result)
756 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
757 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
758 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
762 bt_query_status bt_py3_component_class_sink_query(
763 bt_self_component_class_sink *self_component_class_sink,
764 const bt_query_executor *query_executor,
765 const char *object, const bt_value *params,
766 bt_logging_level log_level,
767 const bt_value **result)
769 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
770 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
771 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
775 bt_self_message_iterator_status bt_py3_component_class_message_iterator_init(
776 bt_self_message_iterator *self_message_iterator,
777 bt_self_component *self_component,
778 bt_self_component_port_output *self_component_port_output)
780 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
781 PyObject *py_comp_cls = NULL;
782 PyObject *py_iter_cls = NULL;
783 PyObject *py_iter_ptr = NULL;
784 PyObject *py_component_port_output_ptr = NULL;
785 PyObject *py_init_method_result = NULL;
786 PyObject *py_iter = NULL;
789 py_comp = bt_self_component_get_data(self_component);
791 /* Find user's Python message iterator class */
792 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
794 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
798 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
800 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
804 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
805 SWIGTYPE_p_bt_self_message_iterator, 0);
807 BT_LOGE_STR("Failed to create a SWIG pointer object.");
812 * Create object with borrowed native message iterator
815 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
817 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
818 "(OO)", py_iter_cls, py_iter_ptr);
820 BT_LOGE("Failed to call Python class's __new__() method: "
821 "py-cls-addr=%p", py_iter_cls);
822 bt2_py_loge_exception();
829 * py_iter.__init__(self_output_port)
831 * through the _init_for_native helper static method.
833 * At this point, py_iter._ptr is set, so this initialization
834 * function has access to self._component (which gives it the
835 * user Python component object from which the iterator was
838 py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output),
839 SWIGTYPE_p_bt_self_component_port_output, 0);
840 if (!py_component_port_output_ptr) {
841 BT_LOGE_STR("Failed to create a SWIG pointer object.");
845 py_init_method_result = PyObject_CallMethod(py_iter, "_init_from_native", "O", py_component_port_output_ptr);
846 if (!py_init_method_result) {
847 BT_LOGE_STR("User's __init__() method failed.");
848 bt2_py_loge_exception();
853 * Since the Python code can never instantiate a user-defined
854 * message iterator class, the native message iterator
855 * object does NOT belong to a user Python message iterator
856 * object (borrowed reference). However this Python object is
857 * owned by this native message iterator object.
859 * In the Python world, the lifetime of the native message
860 * iterator is managed by a _GenericMessageIterator
863 * _GenericMessageIterator instance:
864 * owns a native bt_message_iterator object (iter)
865 * owns a _UserMessageIterator instance (py_iter)
866 * self._ptr is a borrowed reference to the
867 * native bt_private_connection_private_message_iterator
870 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
875 status = bt_py3_exc_to_self_message_iterator_status();
876 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
878 * Looks like there wasn't any exception from the Python
879 * side, but we're still in an error state here.
881 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
885 * Clear any exception: we're returning a bad status anyway. If
886 * this call originated from Python, then the user gets an
887 * appropriate creation error.
892 Py_XDECREF(py_comp_cls);
893 Py_XDECREF(py_iter_cls);
894 Py_XDECREF(py_iter_ptr);
895 Py_XDECREF(py_component_port_output_ptr);
896 Py_XDECREF(py_init_method_result);
902 bt_self_message_iterator_status bt_py3_component_class_source_message_iterator_init(
903 bt_self_message_iterator *self_message_iterator,
904 bt_self_component_source *self_component_source,
905 bt_self_component_port_output *self_component_port_output)
907 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
908 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
912 bt_self_message_iterator_status bt_py3_component_class_filter_message_iterator_init(
913 bt_self_message_iterator *self_message_iterator,
914 bt_self_component_filter *self_component_filter,
915 bt_self_component_port_output *self_component_port_output)
917 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
918 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
922 void bt_py3_component_class_message_iterator_finalize(
923 bt_self_message_iterator *message_iterator)
925 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
926 PyObject *py_method_result = NULL;
928 BT_ASSERT(py_message_iter);
930 /* Call user's _finalize() method */
931 py_method_result = PyObject_CallMethod(py_message_iter,
934 if (PyErr_Occurred()) {
935 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
939 * Ignore any exception raised by the _finalize() method because
940 * it won't change anything at this point: the component is
941 * being destroyed anyway.
944 Py_XDECREF(py_method_result);
945 Py_DECREF(py_message_iter);
948 /* Valid for both sources and filters. */
951 bt_self_message_iterator_status bt_py3_component_class_message_iterator_next(
952 bt_self_message_iterator *message_iterator,
953 bt_message_array_const msgs, uint64_t capacity,
956 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
957 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
958 PyObject *py_method_result = NULL;
960 BT_ASSERT(py_message_iter);
961 py_method_result = PyObject_CallMethod(py_message_iter,
962 "_next_from_native", NULL);
963 if (!py_method_result) {
964 status = bt_py3_exc_to_self_message_iterator_status();
965 BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK);
970 * The returned object, on success, is an integer object
971 * (PyLong) containing the address of a native message
972 * object (which is now ours).
974 msgs[0] = PyLong_AsVoidPtr(py_method_result);
977 /* Clear potential overflow error; should never happen */
978 BT_ASSERT(!PyErr_Occurred());
982 Py_XDECREF(py_method_result);
987 bt_self_component_status bt_py3_component_class_sink_consume(
988 bt_self_component_sink *self_component_sink)
990 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
991 PyObject *py_comp = bt_self_component_get_data(self_component);
992 PyObject *py_method_result = NULL;
993 bt_self_component_status status;
996 py_method_result = PyObject_CallMethod(py_comp,
999 status = bt_py3_exc_to_self_component_status();
1000 if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) {
1001 /* Pretty sure this should never happen, but just in case */
1002 BT_LOGE("User's _consume() method failed without raising an exception: "
1003 "status=%d", status);
1004 status = BT_SELF_COMPONENT_STATUS_ERROR;
1007 Py_XDECREF(py_method_result);
1012 int bt_py3_component_class_set_help_and_desc(
1013 bt_component_class *component_class,
1014 const char *description, const char *help)
1019 ret = bt_component_class_set_description(component_class, description);
1021 BT_LOGE("Cannot set component class's description: "
1022 "comp-cls-addr=%p", component_class);
1028 ret = bt_component_class_set_help(component_class, help);
1030 BT_LOGE("Cannot set component class's help text: "
1031 "comp-cls-addr=%p", component_class);
1043 bt_component_class_source *bt_py3_component_class_source_create(
1044 PyObject *py_cls, const char *name, const char *description,
1047 bt_component_class_source *component_class_source;
1048 bt_component_class *component_class;
1053 component_class_source = bt_component_class_source_create(name,
1054 bt_py3_component_class_message_iterator_next);
1055 if (!component_class_source) {
1056 BT_LOGE_STR("Cannot create source component class.");
1060 component_class = bt_component_class_source_as_component_class(component_class_source);
1062 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1066 ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init);
1067 BT_ASSERT(ret == 0);
1068 ret = bt_component_class_source_set_finalize_method (component_class_source, bt_py3_component_class_source_finalize);
1069 BT_ASSERT(ret == 0);
1070 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1071 bt_py3_component_class_source_output_port_connected);
1072 BT_ASSERT(ret == 0);
1073 ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query);
1074 BT_ASSERT(ret == 0);
1075 ret = bt_component_class_source_set_message_iterator_init_method(
1076 component_class_source, bt_py3_component_class_source_message_iterator_init);
1077 BT_ASSERT(ret == 0);
1078 ret = bt_component_class_source_set_message_iterator_finalize_method(
1079 component_class_source, bt_py3_component_class_message_iterator_finalize);
1080 BT_ASSERT(ret == 0);
1082 register_cc_ptr_to_py_cls(component_class, py_cls);
1085 return component_class_source;
1089 bt_component_class_filter *bt_py3_component_class_filter_create(
1090 PyObject *py_cls, const char *name, const char *description,
1093 bt_component_class *component_class;
1094 bt_component_class_filter *component_class_filter;
1099 component_class_filter = bt_component_class_filter_create(name,
1100 bt_py3_component_class_message_iterator_next);
1101 if (!component_class_filter) {
1102 BT_LOGE_STR("Cannot create filter component class.");
1106 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1108 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1112 ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init);
1113 BT_ASSERT(ret == 0);
1114 ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize);
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);