From df4b0828816de5632b09a0c3946b0cad00af811e Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 31 Oct 2024 23:25:29 -0400 Subject: [PATCH] bt2: rename .i.h -> .i.hpp Change-Id: If6968e678727c79d11835473be81321367362f13 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/13434 Reviewed-by: Philippe Proulx --- src/bindings/python/bt2/Makefile.am | 22 +- .../python/bt2/bt2/native_bt_autodisc.i | 2 +- .../python/bt2/bt2/native_bt_autodisc.i.h | 216 --- .../python/bt2/bt2/native_bt_autodisc.i.hpp | 207 +++ .../bt2/bt2/native_bt_component_class.i | 2 +- .../bt2/bt2/native_bt_component_class.i.h | 1544 ----------------- .../bt2/bt2/native_bt_component_class.i.hpp | 1427 +++++++++++++++ src/bindings/python/bt2/bt2/native_bt_error.i | 2 +- .../python/bt2/bt2/native_bt_error.i.h | 48 - .../python/bt2/bt2/native_bt_error.i.hpp | 45 + src/bindings/python/bt2/bt2/native_bt_graph.i | 2 +- .../python/bt2/bt2/native_bt_graph.i.h | 264 --- .../python/bt2/bt2/native_bt_graph.i.hpp | 233 +++ .../bt2/bt2/native_bt_message_iterator.i | 2 +- .../bt2/bt2/native_bt_message_iterator.i.h | 125 -- .../bt2/bt2/native_bt_message_iterator.i.hpp | 114 ++ src/bindings/python/bt2/bt2/native_bt_mip.i | 2 +- src/bindings/python/bt2/bt2/native_bt_mip.i.h | 21 - .../python/bt2/bt2/native_bt_mip.i.hpp | 19 + .../python/bt2/bt2/native_bt_plugin.i | 2 +- .../python/bt2/bt2/native_bt_plugin.i.h | 102 -- .../python/bt2/bt2/native_bt_plugin.i.hpp | 94 + .../python/bt2/bt2/native_bt_query_exec.i | 2 +- .../python/bt2/bt2/native_bt_query_exec.i.h | 19 - .../python/bt2/bt2/native_bt_query_exec.i.hpp | 18 + src/bindings/python/bt2/bt2/native_bt_trace.i | 2 +- .../python/bt2/bt2/native_bt_trace.i.h | 54 - .../python/bt2/bt2/native_bt_trace.i.hpp | 49 + .../python/bt2/bt2/native_bt_trace_class.i | 2 +- .../python/bt2/bt2/native_bt_trace_class.i.h | 56 - .../bt2/bt2/native_bt_trace_class.i.hpp | 52 + src/bindings/python/bt2/bt2/native_bt_value.i | 2 +- .../python/bt2/bt2/native_bt_value.i.h | 53 - .../python/bt2/bt2/native_bt_value.i.hpp | 53 + 34 files changed, 2333 insertions(+), 2524 deletions(-) delete mode 100644 src/bindings/python/bt2/bt2/native_bt_autodisc.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_autodisc.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_component_class.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_component_class.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_error.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_error.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_graph.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_graph.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_message_iterator.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_message_iterator.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_mip.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_mip.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_plugin.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_plugin.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_query_exec.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_query_exec.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_trace.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_trace.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_trace_class.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_trace_class.i.hpp delete mode 100644 src/bindings/python/bt2/bt2/native_bt_value.i.h create mode 100644 src/bindings/python/bt2/bt2/native_bt_value.i.hpp diff --git a/src/bindings/python/bt2/Makefile.am b/src/bindings/python/bt2/Makefile.am index 4819d421..e1a43da1 100644 --- a/src/bindings/python/bt2/Makefile.am +++ b/src/bindings/python/bt2/Makefile.am @@ -12,46 +12,46 @@ INSTALLED_FILES=$(builddir)/installed_files.txt SWIG_INTERFACE_FILES = \ bt2/native_bt.i \ bt2/native_bt_autodisc.i \ - bt2/native_bt_autodisc.i.h \ + bt2/native_bt_autodisc.i.hpp \ bt2/native_bt_bt2_objects.h \ bt2/native_bt_clock_class.i \ bt2/native_bt_clock_snapshot.i \ bt2/native_bt_component.i \ bt2/native_bt_component_class.i \ - bt2/native_bt_component_class.i.h \ + bt2/native_bt_component_class.i.hpp \ bt2/native_bt_connection.i \ bt2/native_bt_error.i \ - bt2/native_bt_error.i.h \ + bt2/native_bt_error.i.hpp \ bt2/native_bt_event.i \ bt2/native_bt_event_class.i \ bt2/native_bt_field.i \ bt2/native_bt_field_class.i \ bt2/native_bt_field_path.i \ bt2/native_bt_graph.i \ - bt2/native_bt_graph.i.h \ + bt2/native_bt_graph.i.hpp \ bt2/native_bt_integer_range_set.i \ bt2/native_bt_interrupter.i \ bt2/native_bt_log_and_append_error.h \ bt2/native_bt_logging.i \ bt2/native_bt_message.i \ bt2/native_bt_message_iterator.i \ - bt2/native_bt_message_iterator.i.h \ + bt2/native_bt_message_iterator.i.hpp \ bt2/native_bt_mip.i \ - bt2/native_bt_mip.i.h \ + bt2/native_bt_mip.i.hpp \ bt2/native_bt_packet.i \ bt2/native_bt_plugin.i \ - bt2/native_bt_plugin.i.h \ + bt2/native_bt_plugin.i.hpp \ bt2/native_bt_port.i \ bt2/native_bt_query_exec.i \ - bt2/native_bt_query_exec.i.h \ + bt2/native_bt_query_exec.i.hpp \ bt2/native_bt_stream.i \ bt2/native_bt_stream_class.i \ bt2/native_bt_trace.i \ - bt2/native_bt_trace.i.h \ + bt2/native_bt_trace.i.hpp \ bt2/native_bt_trace_class.i \ - bt2/native_bt_trace_class.i.h \ + bt2/native_bt_trace_class.i.hpp \ bt2/native_bt_value.i \ - bt2/native_bt_value.i.h \ + bt2/native_bt_value.i.hpp \ bt2/native_bt_version.i # Non-generated files built into the native library. diff --git a/src/bindings/python/bt2/bt2/native_bt_autodisc.i b/src/bindings/python/bt2/bt2/native_bt_autodisc.i index 18fcfcb6..e8d71d2f 100644 --- a/src/bindings/python/bt2/bt2/native_bt_autodisc.i +++ b/src/bindings/python/bt2/bt2/native_bt_autodisc.i @@ -8,5 +8,5 @@ bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs, const bt_plugin_set *plugin_set); %{ -#include "native_bt_autodisc.i.h" +#include "native_bt_autodisc.i.hpp" %} diff --git a/src/bindings/python/bt2/bt2/native_bt_autodisc.i.h b/src/bindings/python/bt2/bt2/native_bt_autodisc.i.h deleted file mode 100644 index 5b856b69..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_autodisc.i.h +++ /dev/null @@ -1,216 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2016 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_AUTODISC_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_AUTODISC_I_H - -#include -#include - -/* - * Interface between the Python bindings and the auto source discovery system: - * input strings go in, specs for components to instantiate go out. - * - * `inputs` must be an array of strings, the list of inputs in which to look - * for traces. `plugin_set` is the set of plugins to query. - * - * Returns a map with the following entries: - * - * - status: signed integer, return status of this function - * - results: array, each element is an array describing one auto - * source discovery result (see `struct - * auto_source_discovery_result` for more details about these): - * - * - 0: plugin name, string - * - 1: class name, string - * - 2: inputs, array of strings - * - 3: original input indices, array of unsigned integers - * - * This function can also return None, if it failed to allocate memory - * for the return value and status code. - */ -static -bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs, - const bt_plugin_set *plugin_set) -{ - uint64_t i; - int status = 0; - static const char * const module_name = "Automatic source discovery"; - const bt_plugin **plugins = NULL; - const uint64_t plugin_count = bt_plugin_set_get_plugin_count(plugin_set); - struct auto_source_discovery auto_disc = { 0 }; - bt_value *result = NULL; - bt_value *components_list = NULL; - bt_value *component_info = NULL; - bt_value_map_insert_entry_status insert_entry_status; - const bt_error *error = NULL; - - BT_ASSERT(bt_value_get_type(inputs) == BT_VALUE_TYPE_ARRAY); - for (i = 0; i < bt_value_array_get_length(inputs); i++) { - const bt_value *elem = bt_value_array_borrow_element_by_index_const(inputs, i); - BT_ASSERT(bt_value_get_type(elem) == BT_VALUE_TYPE_STRING); - } - - result = bt_value_map_create(); - if (!result) { -#define BT_FMT "Failed to create a map value." - BT_LOGE_STR(BT_FMT); - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, BT_FMT); -#undef BT_FMT - PyErr_NoMemory(); - goto end; - } - - status = auto_source_discovery_init(&auto_disc); - if (status != 0) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to initialize auto source discovery structure."); - goto error; - } - - /* Create array of bt_plugin pointers from plugin set. */ - plugins = g_new(const bt_plugin *, plugin_count); - if (!plugins) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to allocate plugins array."); - status = __BT_FUNC_STATUS_MEMORY_ERROR; - goto error; - } - - for (i = 0; i < plugin_count; i++) { - plugins[i] = bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); - } - - status = auto_discover_source_components( - inputs, - plugins, - plugin_count, - NULL, - (bt_logging_level) bt_python_bindings_bt2_log_level, - &auto_disc, - NULL); - if (status != 0) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to auto discover sources."); - goto error; - } - - components_list = bt_value_array_create(); - if (!components_list) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to allocate one array value."); - status = __BT_FUNC_STATUS_MEMORY_ERROR; - goto error; - } - - insert_entry_status = bt_value_map_insert_entry(result, "results", components_list); - if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to insert a map entry."); - status = (int) insert_entry_status; - goto error; - } - - for (i = 0; i < auto_disc.results->len; i++) { - const auto autodisc_result = static_cast( - g_ptr_array_index(auto_disc.results, i)); - bt_value_array_append_element_status append_element_status; - - component_info = bt_value_array_create(); - if (!component_info) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to allocate one array value."); - status = __BT_FUNC_STATUS_MEMORY_ERROR; - goto error; - } - - append_element_status = bt_value_array_append_string_element( - component_info, autodisc_result->plugin_name); - if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to append one array element."); - status = (int) append_element_status; - goto error; - } - - append_element_status = bt_value_array_append_string_element( - component_info, autodisc_result->source_cc_name); - if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to append one array element."); - status = (int) append_element_status; - goto error; - } - - append_element_status = bt_value_array_append_element( - component_info, autodisc_result->inputs); - if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to append one array element."); - status = (int) append_element_status; - goto error; - } - - append_element_status = bt_value_array_append_element( - component_info, autodisc_result->original_input_indices); - if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to append one array element."); - status = (int) append_element_status; - goto error; - } - - append_element_status = bt_value_array_append_element(components_list, - component_info); - if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to append one array element."); - status = (int) append_element_status; - goto error; - } - - BT_VALUE_PUT_REF_AND_RESET(component_info); - } - - status = 0; - goto end; -error: - BT_ASSERT(status != 0); - -end: - if (result) { - /* - * If an error happened, we must clear the error temporarily - * while we insert the status in the map. - */ - error = bt_current_thread_take_error(); - insert_entry_status = bt_value_map_insert_signed_integer_entry(result, "status", status); - if (insert_entry_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { - if (error) { - bt_current_thread_move_error(error); - error = NULL; - } - } else { - BT_VALUE_PUT_REF_AND_RESET(result); - PyErr_NoMemory(); - } - - - } - - auto_source_discovery_fini(&auto_disc); - g_free(plugins); - bt_value_put_ref(components_list); - bt_value_put_ref(component_info); - - if (error) { - bt_error_release(error); - } - - return result; -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_AUTODISC_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_autodisc.i.hpp b/src/bindings/python/bt2/bt2/native_bt_autodisc.i.hpp new file mode 100644 index 00000000..adcd067c --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_autodisc.i.hpp @@ -0,0 +1,207 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2016 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_AUTODISC_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_AUTODISC_I_HPP + +#include +#include + +/* + * Interface between the Python bindings and the auto source discovery system: + * input strings go in, specs for components to instantiate go out. + * + * `inputs` must be an array of strings, the list of inputs in which to look + * for traces. `plugin_set` is the set of plugins to query. + * + * Returns a map with the following entries: + * + * - status: signed integer, return status of this function + * - results: array, each element is an array describing one auto + * source discovery result (see `struct + * auto_source_discovery_result` for more details about these): + * + * - 0: plugin name, string + * - 1: class name, string + * - 2: inputs, array of strings + * - 3: original input indices, array of unsigned integers + * + * This function can also return None, if it failed to allocate memory + * for the return value and status code. + */ +static bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs, + const bt_plugin_set *plugin_set) +{ + uint64_t i; + int status = 0; + static const char * const module_name = "Automatic source discovery"; + const bt_plugin **plugins = NULL; + const uint64_t plugin_count = bt_plugin_set_get_plugin_count(plugin_set); + struct auto_source_discovery auto_disc = {0}; + bt_value *result = NULL; + bt_value *components_list = NULL; + bt_value *component_info = NULL; + bt_value_map_insert_entry_status insert_entry_status; + const bt_error *error = NULL; + + BT_ASSERT(bt_value_get_type(inputs) == BT_VALUE_TYPE_ARRAY); + for (i = 0; i < bt_value_array_get_length(inputs); i++) { + const bt_value *elem = bt_value_array_borrow_element_by_index_const(inputs, i); + BT_ASSERT(bt_value_get_type(elem) == BT_VALUE_TYPE_STRING); + } + + result = bt_value_map_create(); + if (!result) { +#define BT_FMT "Failed to create a map value." + BT_LOGE_STR(BT_FMT); + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, BT_FMT); +#undef BT_FMT + PyErr_NoMemory(); + goto end; + } + + status = auto_source_discovery_init(&auto_disc); + if (status != 0) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( + module_name, "Failed to initialize auto source discovery structure."); + goto error; + } + + /* Create array of bt_plugin pointers from plugin set. */ + plugins = g_new(const bt_plugin *, plugin_count); + if (!plugins) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate plugins array."); + status = __BT_FUNC_STATUS_MEMORY_ERROR; + goto error; + } + + for (i = 0; i < plugin_count; i++) { + plugins[i] = bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); + } + + status = auto_discover_source_components(inputs, plugins, plugin_count, NULL, + (bt_logging_level) bt_python_bindings_bt2_log_level, + &auto_disc, NULL); + if (status != 0) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to auto discover sources."); + goto error; + } + + components_list = bt_value_array_create(); + if (!components_list) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one array value."); + status = __BT_FUNC_STATUS_MEMORY_ERROR; + goto error; + } + + insert_entry_status = bt_value_map_insert_entry(result, "results", components_list); + if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to insert a map entry."); + status = (int) insert_entry_status; + goto error; + } + + for (i = 0; i < auto_disc.results->len; i++) { + const auto autodisc_result = + static_cast(g_ptr_array_index(auto_disc.results, i)); + bt_value_array_append_element_status append_element_status; + + component_info = bt_value_array_create(); + if (!component_info) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( + module_name, "Failed to allocate one array value."); + status = __BT_FUNC_STATUS_MEMORY_ERROR; + goto error; + } + + append_element_status = + bt_value_array_append_string_element(component_info, autodisc_result->plugin_name); + if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( + module_name, "Failed to append one array element."); + status = (int) append_element_status; + goto error; + } + + append_element_status = + bt_value_array_append_string_element(component_info, autodisc_result->source_cc_name); + if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( + module_name, "Failed to append one array element."); + status = (int) append_element_status; + goto error; + } + + append_element_status = + bt_value_array_append_element(component_info, autodisc_result->inputs); + if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( + module_name, "Failed to append one array element."); + status = (int) append_element_status; + goto error; + } + + append_element_status = + bt_value_array_append_element(component_info, autodisc_result->original_input_indices); + if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( + module_name, "Failed to append one array element."); + status = (int) append_element_status; + goto error; + } + + append_element_status = bt_value_array_append_element(components_list, component_info); + if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( + module_name, "Failed to append one array element."); + status = (int) append_element_status; + goto error; + } + + BT_VALUE_PUT_REF_AND_RESET(component_info); + } + + status = 0; + goto end; +error: + BT_ASSERT(status != 0); + +end: + if (result) { + /* + * If an error happened, we must clear the error temporarily + * while we insert the status in the map. + */ + error = bt_current_thread_take_error(); + insert_entry_status = bt_value_map_insert_signed_integer_entry(result, "status", status); + if (insert_entry_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { + if (error) { + bt_current_thread_move_error(error); + error = NULL; + } + } else { + BT_VALUE_PUT_REF_AND_RESET(result); + PyErr_NoMemory(); + } + } + + auto_source_discovery_fini(&auto_disc); + g_free(plugins); + bt_value_put_ref(components_list); + bt_value_put_ref(component_info); + + if (error) { + bt_error_release(error); + } + + return result; +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_AUTODISC_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_component_class.i b/src/bindings/python/bt2/bt2/native_bt_component_class.i index 790b5558..1750a1ab 100644 --- a/src/bindings/python/bt2/bt2/native_bt_component_class.i +++ b/src/bindings/python/bt2/bt2/native_bt_component_class.i @@ -9,7 +9,7 @@ %include %{ -#include "native_bt_component_class.i.h" +#include "native_bt_component_class.i.hpp" %} struct bt_component_class_source *bt_bt2_component_class_source_create( diff --git a/src/bindings/python/bt2/bt2/native_bt_component_class.i.h b/src/bindings/python/bt2/bt2/native_bt_component_class.i.h deleted file mode 100644 index 7c93580f..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_component_class.i.h +++ /dev/null @@ -1,1544 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2017 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_COMPONENT_CLASS_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_COMPONENT_CLASS_I_H - -#include "logging/comp-logging.h" -#include "compat/glib.h" - -/* - * This hash table associates a BT component class object address to a - * user-defined Python class (PyObject *). The keys and values are NOT - * owned by this hash table. The Python class objects are owned by the - * Python module, which should not be unloaded until it is not possible - * to create a user Python component anyway. - * - * This hash table is written to when a user-defined Python component - * class is created by one of the bt_bt2_component_class_*_create() - * functions. - * - * This function is read from when a user calls bt_component_create() - * with a component class pointer created by one of the functions above. - * In this case, the original Python class needs to be found to - * instantiate it and associate the created Python component object with - * a BT component object instance. - */ - -#define BT_FMT_SWIG_ALLOC_FAILED "Failed to create a SWIG pointer object." - -static GHashTable *bt_cc_ptr_to_py_cls; - -static -void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class *comp_cls) -{ - gboolean existed; - - if (!bt_cc_ptr_to_py_cls) { - return; - } - - existed = g_hash_table_remove(bt_cc_ptr_to_py_cls, comp_cls); - BT_ASSERT(existed); -} - -static -void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, - PyObject *py_cls) -{ - if (!bt_cc_ptr_to_py_cls) { - /* - * Lazy-initializing this GHashTable because GLib - * might not be initialized yet and it needs to be - * before we call g_hash_table_new() - */ - BT_LOGD_STR("Creating native component class to Python component class hash table."); - bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal); - BT_ASSERT(bt_cc_ptr_to_py_cls); - } - - g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc, - (gpointer) py_cls); -} - -static -PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc) -{ - if (!bt_cc_ptr_to_py_cls) { - BT_LOGW("Cannot look up Python component class because hash table is NULL: " - "comp-cls-addr=%p", bt_cc); - return NULL; - } - - return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls, - (gconstpointer) bt_cc); -} - -/* Library destructor */ - -__attribute__((destructor)) -static -void native_comp_class_dtor(void) { - /* Destroy component class association hash table */ - if (bt_cc_ptr_to_py_cls) { - BT_LOGD_STR("Destroying native component class to Python component class hash table."); - g_hash_table_destroy(bt_cc_ptr_to_py_cls); - bt_cc_ptr_to_py_cls = NULL; - } -} - -static inline -int py_exc_to_status_clear( - bt_self_component_class *self_component_class, - bt_self_component *self_component, - bt_self_message_iterator *self_message_iterator, - const char *module_name, int active_log_level) -{ - int status; - PyObject *exc = PyErr_Occurred(); - - if (!exc) { - status = __BT_FUNC_STATUS_OK; - goto end; - } - - if (PyErr_GivenExceptionMatches(exc, - py_mod_bt2_exc_try_again_type)) { - status = __BT_FUNC_STATUS_AGAIN; - } else if (PyErr_GivenExceptionMatches(exc, - py_mod_bt2_exc_stop_type)) { - status = __BT_FUNC_STATUS_END; - } else if (PyErr_GivenExceptionMatches(exc, - py_mod_bt2_exc_unknown_object_type)) { - status = __BT_FUNC_STATUS_UNKNOWN_OBJECT; - } else { - /* - * Unknown exception: convert to general error. - * - * Because we only want to fetch the log level when - * we actually get an exception, and not systematically - * when we call py_exc_to_status() (as py_exc_to_status() - * can return `__BT_FUNC_STATUS_OK`), we get it here - * depending on the actor's type. - */ - if (self_component) { - active_log_level = get_self_component_log_level( - self_component); - } else if (self_message_iterator) { - active_log_level = get_self_message_iterator_log_level( - self_message_iterator); - } - - BT_ASSERT(active_log_level != -1); - log_exception_and_maybe_append_cause(BT_LOG_WARNING, - active_log_level, true, - self_component_class, self_component, - self_message_iterator, module_name); - - if (PyErr_GivenExceptionMatches(exc, - py_mod_bt2_exc_memory_error)) { - status = __BT_FUNC_STATUS_MEMORY_ERROR; - } else { - status = __BT_FUNC_STATUS_ERROR; - } - } - -end: - PyErr_Clear(); - return status; -} - -static -int py_exc_to_status_component_class_clear( - bt_self_component_class *self_component_class, - int active_log_level) -{ - return py_exc_to_status_clear(self_component_class, NULL, NULL, NULL, - active_log_level); -} - -static -int py_exc_to_status_component_clear(bt_self_component *self_component) -{ - return py_exc_to_status_clear(NULL, self_component, NULL, NULL, -1); -} - -static -int py_exc_to_status_message_iterator_clear( - bt_self_message_iterator *self_message_iterator) -{ - return py_exc_to_status_clear(NULL, NULL, self_message_iterator, NULL, -1); -} - -static -bool bt_bt2_is_python_component_class(const bt_component_class *comp_cls) -{ - return bt_g_hash_table_contains(bt_cc_ptr_to_py_cls, comp_cls); -} - -/* Component class proxy methods (delegate to the attached Python object) */ - -static -bt_component_class_initialize_method_status component_class_init( - bt_self_component *self_component, - void *self_component_v, - swig_type_info *self_comp_cls_type_swig_type, - const bt_value *params, - void *init_method_data) -{ - const bt_component *component = bt_self_component_as_component(self_component); - const bt_component_class *component_class = bt_component_borrow_class_const(component); - bt_component_class_initialize_method_status status; - PyObject *py_cls = NULL; - PyObject *py_comp = NULL; - PyObject *py_params_ptr = NULL; - PyObject *py_comp_ptr = NULL; - bt_logging_level log_level = get_self_component_log_level( - self_component); - - BT_ASSERT(self_component); - BT_ASSERT(self_component_v); - BT_ASSERT(self_comp_cls_type_swig_type); - - /* - * Get the user-defined Python class which created this - * component's class in the first place (borrowed - * reference). - */ - py_cls = lookup_cc_ptr_to_py_cls(component_class); - if (!py_cls) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "Cannot find Python class associated to native component class: " - "comp-cls-addr=%p", component_class); - goto error; - } - - /* Parameters pointer -> SWIG pointer Python object */ - py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), - SWIGTYPE_p_bt_value, 0); - if (!py_params_ptr) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - BT_FMT_SWIG_ALLOC_FAILED); - goto error; - } - - py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), - self_comp_cls_type_swig_type, 0); - if (!py_comp_ptr) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - BT_FMT_SWIG_ALLOC_FAILED); - goto error; - } - - /* - * Do the equivalent of this: - * - * py_comp = py_cls._bt_init_from_native(py_comp_ptr, - * py_params_ptr, init_method_data ? init_method_data : Py_None) - * - * _UserComponentType._bt_init_from_native() calls the Python - * component object's __init__() function. - * - * We don't take any reference on `init_method_data` which, if - * not `NULL`, is assumed to be a `PyObject *`: the user's - * __init__() function will eventually take a reference if - * needed. If `init_method_data` is `NULL`, then we pass - * `Py_None` as the initialization's Python object. - */ - py_comp = PyObject_CallMethod(py_cls, - "_bt_init_from_native", "(OOO)", py_comp_ptr, py_params_ptr, - init_method_data ? init_method_data : Py_None); - if (!py_comp) { - BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, - "Failed to call Python class's _bt_init_from_native() method: " - "py-cls-addr=%p", py_cls); - status = static_cast( - py_exc_to_status_component_clear(self_component)); - goto end; - } - - /* - * Our user Python component object is now fully created and - * initialized by the user. Since we just created it, this - * native component is its only (persistent) owner. - */ - bt_self_component_set_data(self_component, py_comp); - py_comp = NULL; - - status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; - - goto end; - -error: - /* This error path is for non-Python errors only. */ - status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; - -end: - BT_ASSERT(!PyErr_Occurred()); - Py_XDECREF(py_comp); - Py_XDECREF(py_params_ptr); - Py_XDECREF(py_comp_ptr); - return status; -} - -static -bt_component_class_get_supported_mip_versions_method_status -component_class_get_supported_mip_versions( - const bt_component_class *component_class, - bt_self_component_class *self_component_class, - const bt_value *params, void *init_method_data, - bt_logging_level log_level, - bt_integer_range_set_unsigned *supported_versions) -{ - uint64_t i; - PyObject *py_cls = NULL; - PyObject *py_params_ptr = NULL; - PyObject *py_range_set_addr = NULL; - bt_integer_range_set_unsigned *ret_range_set = NULL; - bt_component_class_get_supported_mip_versions_method_status status; - - py_cls = lookup_cc_ptr_to_py_cls(component_class); - if (!py_cls) { - BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, - (enum bt_log_level) log_level, BT_LOG_TAG, - "Cannot find Python class associated to native component class: " - "comp-cls-addr=%p", component_class); - goto error; - } - - py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), - SWIGTYPE_p_bt_value, 0); - if (!py_params_ptr) { - BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, - (enum bt_log_level) log_level, BT_LOG_TAG, - BT_FMT_SWIG_ALLOC_FAILED); - goto error; - } - - /* - * We don't take any reference on `init_method_data` which, if - * not `NULL`, is assumed to be a `PyObject *`: the user's - * _user_get_supported_mip_versions() function will eventually - * take a reference if needed. If `init_method_data` is `NULL`, - * then we pass `Py_None` as the initialization's Python object. - */ - py_range_set_addr = PyObject_CallMethod(py_cls, - "_bt_get_supported_mip_versions_from_native", "(OOi)", - py_params_ptr, init_method_data ? init_method_data : Py_None, - (int) log_level); - if (!py_range_set_addr) { - BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_WARNING, - (enum bt_log_level) log_level, BT_LOG_TAG, - "Failed to call Python class's _bt_get_supported_mip_versions_from_native() method: " - "py-cls-addr=%p", py_cls); - status = static_cast( - py_exc_to_status_component_class_clear(self_component_class, log_level)); - goto end; - } - - /* - * The returned object, on success, is an integer object - * (PyLong) containing the address of a BT unsigned integer - * range set object (new reference). - */ - ret_range_set = static_cast( - PyLong_AsVoidPtr(py_range_set_addr)); - BT_ASSERT(!PyErr_Occurred()); - BT_ASSERT(ret_range_set); - - /* Copy returned ranges to input range set */ - for (i = 0; i < bt_integer_range_set_get_range_count( - bt_integer_range_set_unsigned_as_range_set_const(ret_range_set)); - i++) { - const bt_integer_range_unsigned *range = - bt_integer_range_set_unsigned_borrow_range_by_index_const( - ret_range_set, i); - bt_integer_range_set_add_range_status add_range_status; - - add_range_status = bt_integer_range_set_unsigned_add_range( - supported_versions, - bt_integer_range_unsigned_get_lower(range), - bt_integer_range_unsigned_get_upper(range)); - if (add_range_status) { - BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, - (enum bt_log_level) log_level, BT_LOG_TAG, - "Failed to add range to supported MIP versions range set."); - goto error; - } - } - - status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK; - - goto end; - -error: - /* This error path is for non-Python errors only. */ - status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR; - -end: - BT_ASSERT(!PyErr_Occurred()); - Py_XDECREF(py_params_ptr); - Py_XDECREF(py_range_set_addr); - bt_integer_range_set_unsigned_put_ref(ret_range_set); - return status; -} - -static -bt_component_class_get_supported_mip_versions_method_status -component_class_source_get_supported_mip_versions( - bt_self_component_class_source *self_component_class_source, - const bt_value *params, void *init_method_data, - bt_logging_level log_level, - bt_integer_range_set_unsigned *supported_versions) -{ - const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); - const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); - bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source); - - return component_class_get_supported_mip_versions( - component_class, self_component_class, - params, init_method_data, log_level, supported_versions); -} - -static -bt_component_class_get_supported_mip_versions_method_status -component_class_filter_get_supported_mip_versions( - bt_self_component_class_filter *self_component_class_filter, - const bt_value *params, void *init_method_data, - bt_logging_level log_level, - bt_integer_range_set_unsigned *supported_versions) -{ - const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); - const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); - bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter); - - return component_class_get_supported_mip_versions( - component_class, self_component_class, - params, init_method_data, log_level, supported_versions); -} - -static -bt_component_class_get_supported_mip_versions_method_status -component_class_sink_get_supported_mip_versions( - bt_self_component_class_sink *self_component_class_sink, - const bt_value *params, void *init_method_data, - bt_logging_level log_level, - bt_integer_range_set_unsigned *supported_versions) -{ - const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); - const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); - bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink); - - return component_class_get_supported_mip_versions( - component_class, self_component_class, - params, init_method_data, log_level, supported_versions); -} - -/* - * Method of bt_component_class_source to initialize a bt_self_component_source - * of that class. - */ - -static -bt_component_class_initialize_method_status component_class_source_init( - bt_self_component_source *self_component_source, - bt_self_component_source_configuration *config, - const bt_value *params, void *init_method_data) -{ - bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); - return component_class_init( - self_component, - self_component_source, - SWIGTYPE_p_bt_self_component_source, - params, init_method_data); -} - -static -bt_component_class_initialize_method_status component_class_filter_init( - bt_self_component_filter *self_component_filter, - bt_self_component_filter_configuration *config, - const bt_value *params, void *init_method_data) -{ - bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); - return component_class_init( - self_component, - self_component_filter, - SWIGTYPE_p_bt_self_component_filter, - params, init_method_data); -} - -static -bt_component_class_initialize_method_status component_class_sink_init( - bt_self_component_sink *self_component_sink, - bt_self_component_sink_configuration *config, - const bt_value *params, void *init_method_data) -{ - bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); - return component_class_init( - self_component, - self_component_sink, - SWIGTYPE_p_bt_self_component_sink, - params, init_method_data); -} - -static -void component_class_finalize(bt_self_component *self_component) -{ - const auto py_comp = static_cast( - bt_self_component_get_data(self_component)); - PyObject *py_method_result; - - BT_ASSERT(py_comp); - - /* Call user's _user_finalize() method */ - py_method_result = PyObject_CallMethod(py_comp, "_user_finalize", NULL); - if (!py_method_result) { - bt_logging_level log_level = get_self_component_log_level( - self_component); - - /* - * Ignore any exception raised by the _user_finalize() method - * because it won't change anything at this point: the component - * is being destroyed anyway. - */ - BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, - "User component's _user_finalize() method raised an exception: ignoring:"); - logw_exception_clear(log_level); - - goto end; - } - - BT_ASSERT(py_method_result == Py_None); - -end: - Py_XDECREF(py_method_result); - Py_DECREF(py_comp); -} - -/* Decref the Python object in the user data associated to `port`. */ - -static -void delete_port_user_data(bt_self_component_port *port) -{ - Py_DECREF(bt_self_component_port_get_data(port)); -} - -static -void delete_port_input_user_data(bt_self_component_port_input *port_input) -{ - bt_self_component_port *port = - bt_self_component_port_input_as_self_component_port(port_input); - - delete_port_user_data(port); -} - -static -void delete_port_output_user_data(bt_self_component_port_output *port_output) -{ - bt_self_component_port *port = - bt_self_component_port_output_as_self_component_port(port_output); - - delete_port_user_data(port); -} - -static -void component_class_source_finalize(bt_self_component_source *self_component_source) -{ - uint64_t i; - bt_self_component *self_component; - const bt_component_source *component_source; - - self_component = bt_self_component_source_as_self_component( - self_component_source); - component_source = bt_self_component_source_as_component_source( - self_component_source); - - component_class_finalize(self_component); - - /* - * Free the user data Python object attached to the port. The - * corresponding incref was done by the `void *` typemap in - * native_bt_port.i. - */ - for (i = 0; i < bt_component_source_get_output_port_count(component_source); i++) { - bt_self_component_port_output *port_output; - - port_output = bt_self_component_source_borrow_output_port_by_index( - self_component_source, i); - - delete_port_output_user_data(port_output); - } -} - -static -void component_class_filter_finalize(bt_self_component_filter *self_component_filter) -{ - uint64_t i; - bt_self_component *self_component; - const bt_component_filter *component_filter; - - self_component = bt_self_component_filter_as_self_component( - self_component_filter); - component_filter = bt_self_component_filter_as_component_filter( - self_component_filter); - - component_class_finalize(self_component); - - /* - * Free the user data Python object attached to the port. The - * corresponding incref was done by the `void *` typemap in - * native_bt_port.i. - */ - for (i = 0; i < bt_component_filter_get_input_port_count(component_filter); i++) { - bt_self_component_port_input *port_input; - - port_input = bt_self_component_filter_borrow_input_port_by_index( - self_component_filter, i); - - delete_port_input_user_data(port_input); - } - - for (i = 0; i < bt_component_filter_get_output_port_count(component_filter); i++) { - bt_self_component_port_output *port_output; - - port_output = bt_self_component_filter_borrow_output_port_by_index( - self_component_filter, i); - - delete_port_output_user_data(port_output); - } -} - -static -void component_class_sink_finalize(bt_self_component_sink *self_component_sink) -{ - uint64_t i; - bt_self_component *self_component; - const bt_component_sink *component_sink; - - self_component = bt_self_component_sink_as_self_component( - self_component_sink); - component_sink = bt_self_component_sink_as_component_sink( - self_component_sink); - - component_class_finalize(self_component); - - /* - * Free the user data Python object attached to the port. The - * corresponding incref was done by the `void *` typemap in - * native_bt_port.i. - */ - for (i = 0; i < bt_component_sink_get_input_port_count(component_sink); i++) { - bt_self_component_port_input *port_input; - - port_input = bt_self_component_sink_borrow_input_port_by_index( - self_component_sink, i); - - delete_port_input_user_data(port_input); - } -} - -static -bt_message_iterator_class_can_seek_beginning_method_status -component_class_can_seek_beginning( - bt_self_message_iterator *self_message_iterator, bt_bool *can_seek) -{ - PyObject *py_result = NULL; - bt_message_iterator_class_can_seek_beginning_method_status status; - const auto py_iter = static_cast( - bt_self_message_iterator_get_data(self_message_iterator)); - - BT_ASSERT(py_iter); - - py_result = PyObject_CallMethod(py_iter, - "_bt_can_seek_beginning_from_native", NULL); - if (!py_result) { - status = static_cast( - py_exc_to_status_message_iterator_clear(self_message_iterator)); - goto end; - } - - BT_ASSERT(PyBool_Check(py_result)); - *can_seek = PyObject_IsTrue(py_result); - - status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; - -end: - Py_XDECREF(py_result); - - return status; -} - -static -bt_message_iterator_class_seek_beginning_method_status -component_class_seek_beginning(bt_self_message_iterator *self_message_iterator) -{ - PyObject *py_result; - bt_message_iterator_class_seek_beginning_method_status status; - const auto py_iter = static_cast( - bt_self_message_iterator_get_data(self_message_iterator)); - - BT_ASSERT(py_iter); - - py_result = PyObject_CallMethod(py_iter, - "_bt_seek_beginning_from_native", - NULL); - if (!py_result) { - status = static_cast( - py_exc_to_status_message_iterator_clear(self_message_iterator)); - goto end; - } - - BT_ASSERT(py_result == Py_None); - - status = BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK; - -end: - Py_XDECREF(py_result); - - return status; -} - -static -bt_message_iterator_class_can_seek_ns_from_origin_method_status -component_class_can_seek_ns_from_origin( - bt_self_message_iterator *self_message_iterator, - int64_t ns_from_origin, bt_bool *can_seek) -{ - PyObject *py_result = NULL; - bt_message_iterator_class_can_seek_ns_from_origin_method_status status; - const auto py_iter = static_cast( - bt_self_message_iterator_get_data(self_message_iterator)); - - BT_ASSERT(py_iter); - - py_result = PyObject_CallMethod(py_iter, - "_bt_can_seek_ns_from_origin_from_native", "L", ns_from_origin); - if (!py_result) { - status = static_cast( - py_exc_to_status_message_iterator_clear(self_message_iterator)); - goto end; - } - - BT_ASSERT(PyBool_Check(py_result)); - *can_seek = PyObject_IsTrue(py_result); - - status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; - -end: - Py_XDECREF(py_result); - - return status; -} - -static -bt_message_iterator_class_seek_ns_from_origin_method_status -component_class_seek_ns_from_origin( - bt_self_message_iterator *self_message_iterator, - int64_t ns_from_origin) -{ - PyObject *py_result; - bt_message_iterator_class_seek_ns_from_origin_method_status status; - const auto py_iter = static_cast( - bt_self_message_iterator_get_data(self_message_iterator)); - - BT_ASSERT(py_iter); - - py_result = PyObject_CallMethod(py_iter, - "_bt_seek_ns_from_origin_from_native", "L", ns_from_origin); - if (!py_result) { - status = static_cast( - py_exc_to_status_message_iterator_clear(self_message_iterator)); - goto end; - } - - - BT_ASSERT(py_result == Py_None); - - status = BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; - -end: - Py_XDECREF(py_result); - - return status; -} - -static -bt_component_class_port_connected_method_status component_class_port_connected( - bt_self_component *self_component, - void *self_component_port, - swig_type_info *self_component_port_swig_type, - bt_port_type self_component_port_type, - const void *other_port, - swig_type_info *other_port_swig_type) -{ - bt_component_class_port_connected_method_status status; - PyObject *py_self_port_ptr = NULL; - PyObject *py_other_port_ptr = NULL; - PyObject *py_method_result = NULL; - bt_logging_level log_level = get_self_component_log_level( - self_component); - const auto py_comp = static_cast( - bt_self_component_get_data(self_component)); - - BT_ASSERT(py_comp); - py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), - self_component_port_swig_type, 0); - if (!py_self_port_ptr) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - BT_FMT_SWIG_ALLOC_FAILED); - status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; - goto end; - } - - py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), - other_port_swig_type, 0); - if (!py_other_port_ptr) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - BT_FMT_SWIG_ALLOC_FAILED); - status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; - goto end; - } - - py_method_result = PyObject_CallMethod(py_comp, - "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr, - self_component_port_type, py_other_port_ptr); - if (!py_method_result) { - status = static_cast( - py_exc_to_status_component_clear(self_component)); - goto end; - } - - BT_ASSERT(py_method_result == Py_None); - - status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; - -end: - Py_XDECREF(py_self_port_ptr); - Py_XDECREF(py_other_port_ptr); - Py_XDECREF(py_method_result); - - return status; -} - -static -bt_component_class_port_connected_method_status -component_class_source_output_port_connected( - bt_self_component_source *self_component_source, - bt_self_component_port_output *self_component_port_output, - const bt_port_input *other_port_input) -{ - bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); - - return component_class_port_connected( - self_component, - self_component_port_output, - SWIGTYPE_p_bt_self_component_port_output, - BT_PORT_TYPE_OUTPUT, - other_port_input, - SWIGTYPE_p_bt_port_input); -} - -static -bt_component_class_port_connected_method_status -component_class_filter_input_port_connected( - bt_self_component_filter *self_component_filter, - bt_self_component_port_input *self_component_port_input, - const bt_port_output *other_port_output) -{ - bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); - - return component_class_port_connected( - self_component, - self_component_port_input, - SWIGTYPE_p_bt_self_component_port_input, - BT_PORT_TYPE_INPUT, - other_port_output, - SWIGTYPE_p_bt_port_output); -} - -static -bt_component_class_port_connected_method_status -component_class_filter_output_port_connected( - bt_self_component_filter *self_component_filter, - bt_self_component_port_output *self_component_port_output, - const bt_port_input *other_port_input) -{ - bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); - - return component_class_port_connected( - self_component, - self_component_port_output, - SWIGTYPE_p_bt_self_component_port_output, - BT_PORT_TYPE_OUTPUT, - other_port_input, - SWIGTYPE_p_bt_port_input); -} - -static -bt_component_class_port_connected_method_status -component_class_sink_input_port_connected( - bt_self_component_sink *self_component_sink, - bt_self_component_port_input *self_component_port_input, - const bt_port_output *other_port_output) -{ - bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); - - return component_class_port_connected( - self_component, - self_component_port_input, - SWIGTYPE_p_bt_self_component_port_input, - BT_PORT_TYPE_INPUT, - other_port_output, - SWIGTYPE_p_bt_port_output); -} - -static -bt_component_class_sink_graph_is_configured_method_status -component_class_sink_graph_is_configured( - bt_self_component_sink *self_component_sink) -{ - PyObject *py_method_result = NULL; - bt_component_class_sink_graph_is_configured_method_status status; - bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); - const auto py_comp = static_cast( - bt_self_component_get_data(self_component)); - - py_method_result = PyObject_CallMethod(py_comp, - "_bt_graph_is_configured_from_native", NULL); - if (!py_method_result) { - status = static_cast( - py_exc_to_status_component_clear(self_component)); - goto end; - } - - BT_ASSERT(py_method_result == Py_None); - - status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;; - -end: - Py_XDECREF(py_method_result); - return status; -} - -static -bt_component_class_query_method_status component_class_query( - const bt_component_class *component_class, - bt_self_component_class *self_component_class, - bt_private_query_executor *priv_query_executor, - const char *object, const bt_value *params, void *method_data, - const bt_value **result) -{ - PyObject *py_cls = NULL; - PyObject *py_params_ptr = NULL; - PyObject *py_priv_query_exec_ptr = NULL; - PyObject *py_query_func = NULL; - PyObject *py_object = NULL; - PyObject *py_results_addr = NULL; - bt_component_class_query_method_status status = - BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; - const bt_query_executor *query_exec = - bt_private_query_executor_as_query_executor_const( - priv_query_executor); - bt_logging_level log_level = - bt_query_executor_get_logging_level(query_exec); - - /* - * If there's any `method_data`, assume this component class is - * getting queried from Python, so that `method_data` is a - * Python object to pass to the user's _user_query() method. - */ - BT_ASSERT(!method_data || - bt_bt2_is_python_component_class(component_class)); - - py_cls = lookup_cc_ptr_to_py_cls(component_class); - if (!py_cls) { - BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, - (enum bt_log_level) log_level, BT_LOG_TAG, - "Cannot find Python class associated to native component class: " - "comp-cls-addr=%p", component_class); - goto error; - } - - py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), - SWIGTYPE_p_bt_value, 0); - if (!py_params_ptr) { - BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, - (enum bt_log_level) log_level, BT_LOG_TAG, - BT_FMT_SWIG_ALLOC_FAILED); - goto error; - } - - py_priv_query_exec_ptr = SWIG_NewPointerObj( - SWIG_as_voidptr(priv_query_executor), - SWIGTYPE_p_bt_private_query_executor, 0); - if (!py_priv_query_exec_ptr) { - BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, - (enum bt_log_level) log_level, BT_LOG_TAG, - BT_FMT_SWIG_ALLOC_FAILED); - goto error; - } - - py_object = SWIG_FromCharPtr(object); - if (!py_object) { - BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, - (enum bt_log_level) log_level, BT_LOG_TAG, - "Failed to create a Python string."); - goto error; - } - - /* - * We don't take any reference on `method_data` which, if not - * `NULL`, is assumed to be a `PyObject *`: the user's - * _user_query() function will eventually take a reference if - * needed. If `method_data` is `NULL`, then we pass `Py_None` as - * the initialization's Python object. - */ - py_results_addr = PyObject_CallMethod(py_cls, - "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr, - py_object, py_params_ptr, - method_data ? method_data : Py_None); - if (!py_results_addr) { - status = static_cast( - py_exc_to_status_component_class_clear(self_component_class, log_level)); - if (status < 0) { -#define BT_FMT "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p" - BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_WARNING, - (enum bt_log_level) log_level, - BT_LOG_TAG, BT_FMT, py_cls); - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( - self_component_class, BT_FMT, py_cls); -#undef BT_FMT - } - goto end; - } - - /* - * The returned object, on success, is an integer object - * (PyLong) containing the address of a BT value object (new - * reference). - */ - *result = static_cast(PyLong_AsVoidPtr(py_results_addr)); - BT_ASSERT(!PyErr_Occurred()); - BT_ASSERT(*result); - goto end; - -error: - PyErr_Clear(); - status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; - -end: - Py_XDECREF(py_params_ptr); - Py_XDECREF(py_priv_query_exec_ptr); - Py_XDECREF(py_query_func); - Py_XDECREF(py_object); - Py_XDECREF(py_results_addr); - return status; -} - -static -bt_component_class_query_method_status component_class_source_query( - bt_self_component_class_source *self_component_class_source, - bt_private_query_executor *priv_query_executor, - const char *object, const bt_value *params, void *method_data, - const bt_value **result) -{ - const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); - const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); - bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source); - - return component_class_query(component_class, self_component_class, - priv_query_executor, object, params, method_data, result); -} - -static -bt_component_class_query_method_status component_class_filter_query( - bt_self_component_class_filter *self_component_class_filter, - bt_private_query_executor *priv_query_executor, - const char *object, const bt_value *params, void *method_data, - const bt_value **result) -{ - const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); - const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); - bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter); - - return component_class_query(component_class, self_component_class, - priv_query_executor, object, params, method_data, result); -} - -static -bt_component_class_query_method_status component_class_sink_query( - bt_self_component_class_sink *self_component_class_sink, - bt_private_query_executor *priv_query_executor, - const char *object, const bt_value *params, void *method_data, - const bt_value **result) -{ - const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); - const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); - bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink); - - return component_class_query(component_class, self_component_class, - priv_query_executor, object, params, method_data, result); -} - -static -bt_message_iterator_class_initialize_method_status -component_class_message_iterator_init( - bt_self_message_iterator *self_message_iterator, - bt_self_message_iterator_configuration *config, - bt_self_component_port_output *self_component_port_output) -{ - bt_message_iterator_class_initialize_method_status status = - BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; - PyObject *py_comp_cls = NULL; - PyObject *py_iter_cls = NULL; - PyObject *py_iter_ptr = NULL; - PyObject *py_config_ptr = NULL; - PyObject *py_component_port_output_ptr = NULL; - PyObject *py_init_method_result = NULL; - PyObject *py_iter = NULL; - bt_self_component *self_component = - bt_self_message_iterator_borrow_component( - self_message_iterator); - bt_logging_level log_level = get_self_component_log_level( - self_component); - const auto py_comp = static_cast( - bt_self_component_get_data(self_component)); - - /* Find user's Python message iterator class */ - py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); - if (!py_comp_cls) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "Cannot get Python object's `__class__` attribute."); - goto python_error; - } - - py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); - if (!py_iter_cls) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "Cannot get Python class's `_iter_cls` attribute."); - goto python_error; - } - - py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), - SWIGTYPE_p_bt_self_message_iterator, 0); - if (!py_iter_ptr) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - BT_FMT_SWIG_ALLOC_FAILED); - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( - self_message_iterator, BT_FMT_SWIG_ALLOC_FAILED); - goto error; - } - - /* - * Create object with borrowed native message iterator - * reference: - * - * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) - */ - py_iter = PyObject_CallMethod(py_iter_cls, "__new__", - "(OO)", py_iter_cls, py_iter_ptr); - if (!py_iter) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "Failed to call Python class's __new__() method: " - "py-cls-addr=%p", py_iter_cls); - goto python_error; - } - - /* - * Initialize object: - * - * py_iter.__init__(config, self_output_port) - * - * through the _init_from_native helper static method. - * - * At this point, py_iter._ptr is set, so this initialization - * function has access to self._component (which gives it the - * user Python component object from which the iterator was - * created). - */ - py_config_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(config), - SWIGTYPE_p_bt_self_message_iterator_configuration, 0); - if (!py_config_ptr) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - BT_FMT_SWIG_ALLOC_FAILED); - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( - self_message_iterator, BT_FMT_SWIG_ALLOC_FAILED); - goto error; - } - - py_component_port_output_ptr = SWIG_NewPointerObj( - SWIG_as_voidptr(self_component_port_output), - SWIGTYPE_p_bt_self_component_port_output, 0); - if (!py_component_port_output_ptr) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "%s", BT_FMT_SWIG_ALLOC_FAILED); - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( - self_message_iterator, BT_FMT_SWIG_ALLOC_FAILED); - goto error; - } - - py_init_method_result = PyObject_CallMethod(py_iter, - "_bt_init_from_native", "OO", py_config_ptr, - py_component_port_output_ptr); - if (!py_init_method_result) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "User's __init__() method failed:"); - goto python_error; - } - - /* - * Since the Python code can never instantiate a user-defined - * message iterator class, the native message iterator - * object does NOT belong to a user Python message iterator - * object (borrowed reference). However this Python object is - * owned by this native message iterator object. - * - * In the Python world, the lifetime of the native message - * iterator is managed by a _GenericMessageIterator - * instance: - * - * _GenericMessageIterator instance: - * owns a native bt_message_iterator object (iter) - * owns a _UserMessageIterator instance (py_iter) - * self._ptr is a borrowed reference to the - * native bt_private_connection_private_message_iterator - * object (iter) - */ - bt_self_message_iterator_set_data(self_message_iterator, py_iter); - py_iter = NULL; - goto end; - -python_error: - /* Handling of errors that cause a Python exception to be set. */ - status = static_cast( - py_exc_to_status_message_iterator_clear(self_message_iterator)); - goto end; - -error: - /* Handling of errors that don't cause a Python exception to be set. */ - status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; - -end: - BT_ASSERT(!PyErr_Occurred()); - - Py_XDECREF(py_comp_cls); - Py_XDECREF(py_iter_cls); - Py_XDECREF(py_iter_ptr); - Py_XDECREF(py_config_ptr); - Py_XDECREF(py_component_port_output_ptr); - Py_XDECREF(py_init_method_result); - Py_XDECREF(py_iter); - return status; -} - -static -void component_class_message_iterator_finalize( - bt_self_message_iterator *message_iterator) -{ - const auto py_message_iter = static_cast( - bt_self_message_iterator_get_data(message_iterator)); - PyObject *py_method_result = NULL; - - BT_ASSERT(py_message_iter); - - /* Call user's _user_finalize() method */ - py_method_result = PyObject_CallMethod(py_message_iter, - "_user_finalize", NULL); - if (!py_method_result) { - bt_self_component *self_comp = - bt_self_message_iterator_borrow_component( - message_iterator); - bt_logging_level log_level = get_self_component_log_level( - self_comp); - - /* - * Ignore any exception raised by the _user_finalize() method - * because it won't change anything at this point: the component - * is being destroyed anyway. - */ - BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp, - "User's _user_finalize() method raised an exception: ignoring:"); - logw_exception_clear(get_self_message_iterator_log_level( - message_iterator)); - } - - Py_XDECREF(py_method_result); - Py_DECREF(py_message_iter); -} - -/* Valid for both sources and filters. */ - -static -bt_message_iterator_class_next_method_status -component_class_message_iterator_next( - bt_self_message_iterator *message_iterator, - bt_message_array_const msgs, uint64_t capacity, - uint64_t *count) -{ - bt_message_iterator_class_next_method_status status; - const auto py_message_iter = static_cast( - bt_self_message_iterator_get_data(message_iterator)); - PyObject *py_method_result = NULL; - - BT_ASSERT_DBG(py_message_iter); - py_method_result = PyObject_CallMethod(py_message_iter, - "_bt_next_from_native", NULL); - if (!py_method_result) { - status = static_cast( - py_exc_to_status_message_iterator_clear(message_iterator)); - goto end; - } - - /* - * The returned object, on success, is an integer object - * (PyLong) containing the address of a native message - * object (which is now ours). - */ - msgs[0] = static_cast(PyLong_AsVoidPtr(py_method_result)); - *count = 1; - - /* Overflow errors should never happen. */ - BT_ASSERT_DBG(!PyErr_Occurred()); - - status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; - -end: - Py_XDECREF(py_method_result); - return status; -} - -static -bt_component_class_sink_consume_method_status -component_class_sink_consume(bt_self_component_sink *self_component_sink) -{ - bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); - const auto py_comp = static_cast(bt_self_component_get_data(self_component)); - PyObject *py_method_result = NULL; - bt_component_class_sink_consume_method_status status; - - BT_ASSERT_DBG(py_comp); - - py_method_result = PyObject_CallMethod(py_comp, - "_user_consume", NULL); - if (!py_method_result) { - status = static_cast( - py_exc_to_status_component_clear(self_component)); - goto end; - } - - status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; - -end: - Py_XDECREF(py_method_result); - return status; -} - -static -int component_class_set_help_and_desc( - bt_component_class *component_class, - const char *description, const char *help) -{ - int ret; - - if (description) { - ret = bt_component_class_set_description(component_class, description); - if (ret) { - BT_LOGE("Cannot set component class's description: " - "comp-cls-addr=%p", component_class); - goto end; - } - } - - if (help) { - ret = bt_component_class_set_help(component_class, help); - if (ret) { - BT_LOGE("Cannot set component class's help text: " - "comp-cls-addr=%p", component_class); - goto end; - } - } - - ret = 0; - -end: - return ret; -} - -static -bt_message_iterator_class *create_message_iterator_class(void) -{ - bt_message_iterator_class *message_iterator_class; - bt_message_iterator_class_set_method_status ret; - - message_iterator_class = bt_message_iterator_class_create( - component_class_message_iterator_next); - if (!message_iterator_class) { - BT_LOGE_STR("Cannot create message iterator class."); - goto end; - } - - ret = bt_message_iterator_class_set_seek_beginning_methods( - message_iterator_class, component_class_seek_beginning, - component_class_can_seek_beginning); - BT_ASSERT(ret == 0); - ret = bt_message_iterator_class_set_seek_ns_from_origin_methods( - message_iterator_class, component_class_seek_ns_from_origin, - component_class_can_seek_ns_from_origin); - BT_ASSERT(ret == 0); - ret = bt_message_iterator_class_set_initialize_method( - message_iterator_class, component_class_message_iterator_init); - BT_ASSERT(ret == 0); - ret = bt_message_iterator_class_set_finalize_method( - message_iterator_class, component_class_message_iterator_finalize); - BT_ASSERT(ret == 0); - -end: - return message_iterator_class; -} - -static -bt_component_class_source *bt_bt2_component_class_source_create( - PyObject *py_cls, const char *name, const char *description, - const char *help) -{ - bt_component_class_source *component_class_source = NULL; - bt_message_iterator_class *message_iterator_class; - bt_component_class *component_class; - int ret; - - BT_ASSERT(py_cls); - - message_iterator_class = create_message_iterator_class(); - if (!message_iterator_class) { - goto end; - } - - component_class_source = bt_component_class_source_create(name, - message_iterator_class); - if (!component_class_source) { - BT_LOGE_STR("Cannot create source component class."); - goto end; - } - - component_class = bt_component_class_source_as_component_class(component_class_source); - - if (component_class_set_help_and_desc(component_class, description, help)) { - goto end; - } - - ret = bt_component_class_source_set_initialize_method(component_class_source, component_class_source_init); - BT_ASSERT(ret == 0); - ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize); - BT_ASSERT(ret == 0); - ret = bt_component_class_source_set_output_port_connected_method(component_class_source, - component_class_source_output_port_connected); - BT_ASSERT(ret == 0); - ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query); - BT_ASSERT(ret == 0); - ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions); - BT_ASSERT(ret == 0); - register_cc_ptr_to_py_cls(component_class, py_cls); - -end: - bt_message_iterator_class_put_ref(message_iterator_class); - return component_class_source; -} - -static -bt_component_class_filter *bt_bt2_component_class_filter_create( - PyObject *py_cls, const char *name, const char *description, - const char *help) -{ - bt_component_class_filter *component_class_filter = NULL; - bt_message_iterator_class *message_iterator_class; - bt_component_class *component_class; - int ret; - - BT_ASSERT(py_cls); - - message_iterator_class = create_message_iterator_class(); - if (!message_iterator_class) { - goto end; - } - - component_class_filter = bt_component_class_filter_create(name, - message_iterator_class); - if (!component_class_filter) { - BT_LOGE_STR("Cannot create filter component class."); - goto end; - } - - component_class = bt_component_class_filter_as_component_class(component_class_filter); - - if (component_class_set_help_and_desc(component_class, description, help)) { - goto end; - } - - ret = bt_component_class_filter_set_initialize_method(component_class_filter, component_class_filter_init); - BT_ASSERT(ret == 0); - ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize); - BT_ASSERT(ret == 0); - ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, - component_class_filter_input_port_connected); - BT_ASSERT(ret == 0); - ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter, - component_class_filter_output_port_connected); - BT_ASSERT(ret == 0); - ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query); - BT_ASSERT(ret == 0); - ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions); - BT_ASSERT(ret == 0); - register_cc_ptr_to_py_cls(component_class, py_cls); - -end: - bt_message_iterator_class_put_ref(message_iterator_class); - return component_class_filter; -} - -static -bt_component_class_sink *bt_bt2_component_class_sink_create( - PyObject *py_cls, const char *name, const char *description, - const char *help) -{ - bt_component_class_sink *component_class_sink; - bt_component_class *component_class; - int ret; - - BT_ASSERT(py_cls); - component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume); - - if (!component_class_sink) { - BT_LOGE_STR("Cannot create sink component class."); - goto end; - } - - component_class = bt_component_class_sink_as_component_class(component_class_sink); - - if (component_class_set_help_and_desc(component_class, description, help)) { - goto end; - } - - ret = bt_component_class_sink_set_initialize_method(component_class_sink, component_class_sink_init); - BT_ASSERT(ret == 0); - ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize); - BT_ASSERT(ret == 0); - ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink, - component_class_sink_input_port_connected); - BT_ASSERT(ret == 0); - ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink, - component_class_sink_graph_is_configured); - BT_ASSERT(ret == 0); - ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query); - BT_ASSERT(ret == 0); - ret = bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink, component_class_sink_get_supported_mip_versions); - BT_ASSERT(ret == 0); - register_cc_ptr_to_py_cls(component_class, py_cls); - -end: - return component_class_sink; -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_COMPONENT_CLASS_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_component_class.i.hpp b/src/bindings/python/bt2/bt2/native_bt_component_class.i.hpp new file mode 100644 index 00000000..bfa388ca --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_component_class.i.hpp @@ -0,0 +1,1427 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2017 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_COMPONENT_CLASS_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_COMPONENT_CLASS_I_HPP + +#include "logging/comp-logging.h" + +#include "compat/glib.h" + +/* + * This hash table associates a BT component class object address to a + * user-defined Python class (PyObject *). The keys and values are NOT + * owned by this hash table. The Python class objects are owned by the + * Python module, which should not be unloaded until it is not possible + * to create a user Python component anyway. + * + * This hash table is written to when a user-defined Python component + * class is created by one of the bt_bt2_component_class_*_create() + * functions. + * + * This function is read from when a user calls bt_component_create() + * with a component class pointer created by one of the functions above. + * In this case, the original Python class needs to be found to + * instantiate it and associate the created Python component object with + * a BT component object instance. + */ + +#define BT_FMT_SWIG_ALLOC_FAILED "Failed to create a SWIG pointer object." + +static GHashTable *bt_cc_ptr_to_py_cls; + +static void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class *comp_cls) +{ + gboolean existed; + + if (!bt_cc_ptr_to_py_cls) { + return; + } + + existed = g_hash_table_remove(bt_cc_ptr_to_py_cls, comp_cls); + BT_ASSERT(existed); +} + +static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, PyObject *py_cls) +{ + if (!bt_cc_ptr_to_py_cls) { + /* + * Lazy-initializing this GHashTable because GLib + * might not be initialized yet and it needs to be + * before we call g_hash_table_new() + */ + BT_LOGD_STR("Creating native component class to Python component class hash table."); + bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal); + BT_ASSERT(bt_cc_ptr_to_py_cls); + } + + g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc, (gpointer) py_cls); +} + +static PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc) +{ + if (!bt_cc_ptr_to_py_cls) { + BT_LOGW("Cannot look up Python component class because hash table is NULL: " + "comp-cls-addr=%p", + bt_cc); + return NULL; + } + + return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls, (gconstpointer) bt_cc); +} + +/* Library destructor */ + +__attribute__((destructor)) static void native_comp_class_dtor(void) +{ + /* Destroy component class association hash table */ + if (bt_cc_ptr_to_py_cls) { + BT_LOGD_STR("Destroying native component class to Python component class hash table."); + g_hash_table_destroy(bt_cc_ptr_to_py_cls); + bt_cc_ptr_to_py_cls = NULL; + } +} + +static inline int py_exc_to_status_clear(bt_self_component_class *self_component_class, + bt_self_component *self_component, + bt_self_message_iterator *self_message_iterator, + const char *module_name, int active_log_level) +{ + int status; + PyObject *exc = PyErr_Occurred(); + + if (!exc) { + status = __BT_FUNC_STATUS_OK; + goto end; + } + + if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) { + status = __BT_FUNC_STATUS_AGAIN; + } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) { + status = __BT_FUNC_STATUS_END; + } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_unknown_object_type)) { + status = __BT_FUNC_STATUS_UNKNOWN_OBJECT; + } else { + /* + * Unknown exception: convert to general error. + * + * Because we only want to fetch the log level when + * we actually get an exception, and not systematically + * when we call py_exc_to_status() (as py_exc_to_status() + * can return `__BT_FUNC_STATUS_OK`), we get it here + * depending on the actor's type. + */ + if (self_component) { + active_log_level = get_self_component_log_level(self_component); + } else if (self_message_iterator) { + active_log_level = get_self_message_iterator_log_level(self_message_iterator); + } + + BT_ASSERT(active_log_level != -1); + log_exception_and_maybe_append_cause(BT_LOG_WARNING, active_log_level, true, + self_component_class, self_component, + self_message_iterator, module_name); + + if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_memory_error)) { + status = __BT_FUNC_STATUS_MEMORY_ERROR; + } else { + status = __BT_FUNC_STATUS_ERROR; + } + } + +end: + PyErr_Clear(); + return status; +} + +static int py_exc_to_status_component_class_clear(bt_self_component_class *self_component_class, + int active_log_level) +{ + return py_exc_to_status_clear(self_component_class, NULL, NULL, NULL, active_log_level); +} + +static int py_exc_to_status_component_clear(bt_self_component *self_component) +{ + return py_exc_to_status_clear(NULL, self_component, NULL, NULL, -1); +} + +static int py_exc_to_status_message_iterator_clear(bt_self_message_iterator *self_message_iterator) +{ + return py_exc_to_status_clear(NULL, NULL, self_message_iterator, NULL, -1); +} + +static bool bt_bt2_is_python_component_class(const bt_component_class *comp_cls) +{ + return bt_g_hash_table_contains(bt_cc_ptr_to_py_cls, comp_cls); +} + +/* Component class proxy methods (delegate to the attached Python object) */ + +static bt_component_class_initialize_method_status +component_class_init(bt_self_component *self_component, void *self_component_v, + swig_type_info *self_comp_cls_type_swig_type, const bt_value *params, + void *init_method_data) +{ + const bt_component *component = bt_self_component_as_component(self_component); + const bt_component_class *component_class = bt_component_borrow_class_const(component); + bt_component_class_initialize_method_status status; + PyObject *py_cls = NULL; + PyObject *py_comp = NULL; + PyObject *py_params_ptr = NULL; + PyObject *py_comp_ptr = NULL; + bt_logging_level log_level = get_self_component_log_level(self_component); + + BT_ASSERT(self_component); + BT_ASSERT(self_component_v); + BT_ASSERT(self_comp_cls_type_swig_type); + + /* + * Get the user-defined Python class which created this + * component's class in the first place (borrowed + * reference). + */ + py_cls = lookup_cc_ptr_to_py_cls(component_class); + if (!py_cls) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, + "Cannot find Python class associated to native component class: " + "comp-cls-addr=%p", + component_class); + goto error; + } + + /* Parameters pointer -> SWIG pointer Python object */ + py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), SWIGTYPE_p_bt_value, 0); + if (!py_params_ptr) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, BT_FMT_SWIG_ALLOC_FAILED); + goto error; + } + + py_comp_ptr = + SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), self_comp_cls_type_swig_type, 0); + if (!py_comp_ptr) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, BT_FMT_SWIG_ALLOC_FAILED); + goto error; + } + + /* + * Do the equivalent of this: + * + * py_comp = py_cls._bt_init_from_native(py_comp_ptr, + * py_params_ptr, init_method_data ? init_method_data : Py_None) + * + * _UserComponentType._bt_init_from_native() calls the Python + * component object's __init__() function. + * + * We don't take any reference on `init_method_data` which, if + * not `NULL`, is assumed to be a `PyObject *`: the user's + * __init__() function will eventually take a reference if + * needed. If `init_method_data` is `NULL`, then we pass + * `Py_None` as the initialization's Python object. + */ + py_comp = PyObject_CallMethod(py_cls, "_bt_init_from_native", "(OOO)", py_comp_ptr, + py_params_ptr, init_method_data ? init_method_data : Py_None); + if (!py_comp) { + BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, + "Failed to call Python class's _bt_init_from_native() method: " + "py-cls-addr=%p", + py_cls); + status = static_cast( + py_exc_to_status_component_clear(self_component)); + goto end; + } + + /* + * Our user Python component object is now fully created and + * initialized by the user. Since we just created it, this + * native component is its only (persistent) owner. + */ + bt_self_component_set_data(self_component, py_comp); + py_comp = NULL; + + status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; + + goto end; + +error: + /* This error path is for non-Python errors only. */ + status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; + +end: + BT_ASSERT(!PyErr_Occurred()); + Py_XDECREF(py_comp); + Py_XDECREF(py_params_ptr); + Py_XDECREF(py_comp_ptr); + return status; +} + +static bt_component_class_get_supported_mip_versions_method_status +component_class_get_supported_mip_versions(const bt_component_class *component_class, + bt_self_component_class *self_component_class, + const bt_value *params, void *init_method_data, + bt_logging_level log_level, + bt_integer_range_set_unsigned *supported_versions) +{ + uint64_t i; + PyObject *py_cls = NULL; + PyObject *py_params_ptr = NULL; + PyObject *py_range_set_addr = NULL; + bt_integer_range_set_unsigned *ret_range_set = NULL; + bt_component_class_get_supported_mip_versions_method_status status; + + py_cls = lookup_cc_ptr_to_py_cls(component_class); + if (!py_cls) { + BT_LOG_WRITE_PRINTF_CUR_LVL( + BT_LOG_ERROR, (enum bt_log_level) log_level, BT_LOG_TAG, + "Cannot find Python class associated to native component class: " + "comp-cls-addr=%p", + component_class); + goto error; + } + + py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), SWIGTYPE_p_bt_value, 0); + if (!py_params_ptr) { + BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, (enum bt_log_level) log_level, BT_LOG_TAG, + BT_FMT_SWIG_ALLOC_FAILED); + goto error; + } + + /* + * We don't take any reference on `init_method_data` which, if + * not `NULL`, is assumed to be a `PyObject *`: the user's + * _user_get_supported_mip_versions() function will eventually + * take a reference if needed. If `init_method_data` is `NULL`, + * then we pass `Py_None` as the initialization's Python object. + */ + py_range_set_addr = PyObject_CallMethod( + py_cls, "_bt_get_supported_mip_versions_from_native", "(OOi)", py_params_ptr, + init_method_data ? init_method_data : Py_None, (int) log_level); + if (!py_range_set_addr) { + BT_LOG_WRITE_PRINTF_CUR_LVL( + BT_LOG_WARNING, (enum bt_log_level) log_level, BT_LOG_TAG, + "Failed to call Python class's _bt_get_supported_mip_versions_from_native() method: " + "py-cls-addr=%p", + py_cls); + status = static_cast( + py_exc_to_status_component_class_clear(self_component_class, log_level)); + goto end; + } + + /* + * The returned object, on success, is an integer object + * (PyLong) containing the address of a BT unsigned integer + * range set object (new reference). + */ + ret_range_set = + static_cast(PyLong_AsVoidPtr(py_range_set_addr)); + BT_ASSERT(!PyErr_Occurred()); + BT_ASSERT(ret_range_set); + + /* Copy returned ranges to input range set */ + for (i = 0; i < bt_integer_range_set_get_range_count( + bt_integer_range_set_unsigned_as_range_set_const(ret_range_set)); + i++) { + const bt_integer_range_unsigned *range = + bt_integer_range_set_unsigned_borrow_range_by_index_const(ret_range_set, i); + bt_integer_range_set_add_range_status add_range_status; + + add_range_status = bt_integer_range_set_unsigned_add_range( + supported_versions, bt_integer_range_unsigned_get_lower(range), + bt_integer_range_unsigned_get_upper(range)); + if (add_range_status) { + BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, (enum bt_log_level) log_level, BT_LOG_TAG, + "Failed to add range to supported MIP versions range set."); + goto error; + } + } + + status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK; + + goto end; + +error: + /* This error path is for non-Python errors only. */ + status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR; + +end: + BT_ASSERT(!PyErr_Occurred()); + Py_XDECREF(py_params_ptr); + Py_XDECREF(py_range_set_addr); + bt_integer_range_set_unsigned_put_ref(ret_range_set); + return status; +} + +static bt_component_class_get_supported_mip_versions_method_status +component_class_source_get_supported_mip_versions( + bt_self_component_class_source *self_component_class_source, const bt_value *params, + void *init_method_data, bt_logging_level log_level, + bt_integer_range_set_unsigned *supported_versions) +{ + const bt_component_class_source *component_class_source = + bt_self_component_class_source_as_component_class_source(self_component_class_source); + const bt_component_class *component_class = + bt_component_class_source_as_component_class_const(component_class_source); + bt_self_component_class *self_component_class = + bt_self_component_class_source_as_self_component_class(self_component_class_source); + + return component_class_get_supported_mip_versions(component_class, self_component_class, params, + init_method_data, log_level, + supported_versions); +} + +static bt_component_class_get_supported_mip_versions_method_status +component_class_filter_get_supported_mip_versions( + bt_self_component_class_filter *self_component_class_filter, const bt_value *params, + void *init_method_data, bt_logging_level log_level, + bt_integer_range_set_unsigned *supported_versions) +{ + const bt_component_class_filter *component_class_filter = + bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); + const bt_component_class *component_class = + bt_component_class_filter_as_component_class_const(component_class_filter); + bt_self_component_class *self_component_class = + bt_self_component_class_filter_as_self_component_class(self_component_class_filter); + + return component_class_get_supported_mip_versions(component_class, self_component_class, params, + init_method_data, log_level, + supported_versions); +} + +static bt_component_class_get_supported_mip_versions_method_status +component_class_sink_get_supported_mip_versions( + bt_self_component_class_sink *self_component_class_sink, const bt_value *params, + void *init_method_data, bt_logging_level log_level, + bt_integer_range_set_unsigned *supported_versions) +{ + const bt_component_class_sink *component_class_sink = + bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); + const bt_component_class *component_class = + bt_component_class_sink_as_component_class_const(component_class_sink); + bt_self_component_class *self_component_class = + bt_self_component_class_sink_as_self_component_class(self_component_class_sink); + + return component_class_get_supported_mip_versions(component_class, self_component_class, params, + init_method_data, log_level, + supported_versions); +} + +/* + * Method of bt_component_class_source to initialize a bt_self_component_source + * of that class. + */ + +static bt_component_class_initialize_method_status +component_class_source_init(bt_self_component_source *self_component_source, + bt_self_component_source_configuration *config, const bt_value *params, + void *init_method_data) +{ + bt_self_component *self_component = + bt_self_component_source_as_self_component(self_component_source); + return component_class_init(self_component, self_component_source, + SWIGTYPE_p_bt_self_component_source, params, init_method_data); +} + +static bt_component_class_initialize_method_status +component_class_filter_init(bt_self_component_filter *self_component_filter, + bt_self_component_filter_configuration *config, const bt_value *params, + void *init_method_data) +{ + bt_self_component *self_component = + bt_self_component_filter_as_self_component(self_component_filter); + return component_class_init(self_component, self_component_filter, + SWIGTYPE_p_bt_self_component_filter, params, init_method_data); +} + +static bt_component_class_initialize_method_status +component_class_sink_init(bt_self_component_sink *self_component_sink, + bt_self_component_sink_configuration *config, const bt_value *params, + void *init_method_data) +{ + bt_self_component *self_component = + bt_self_component_sink_as_self_component(self_component_sink); + return component_class_init(self_component, self_component_sink, + SWIGTYPE_p_bt_self_component_sink, params, init_method_data); +} + +static void component_class_finalize(bt_self_component *self_component) +{ + const auto py_comp = static_cast(bt_self_component_get_data(self_component)); + PyObject *py_method_result; + + BT_ASSERT(py_comp); + + /* Call user's _user_finalize() method */ + py_method_result = PyObject_CallMethod(py_comp, "_user_finalize", NULL); + if (!py_method_result) { + bt_logging_level log_level = get_self_component_log_level(self_component); + + /* + * Ignore any exception raised by the _user_finalize() method + * because it won't change anything at this point: the component + * is being destroyed anyway. + */ + BT_COMP_LOG_CUR_LVL( + BT_LOG_WARNING, log_level, self_component, + "User component's _user_finalize() method raised an exception: ignoring:"); + logw_exception_clear(log_level); + + goto end; + } + + BT_ASSERT(py_method_result == Py_None); + +end: + Py_XDECREF(py_method_result); + Py_DECREF(py_comp); +} + +/* Decref the Python object in the user data associated to `port`. */ + +static void delete_port_user_data(bt_self_component_port *port) +{ + Py_DECREF(bt_self_component_port_get_data(port)); +} + +static void delete_port_input_user_data(bt_self_component_port_input *port_input) +{ + bt_self_component_port *port = bt_self_component_port_input_as_self_component_port(port_input); + + delete_port_user_data(port); +} + +static void delete_port_output_user_data(bt_self_component_port_output *port_output) +{ + bt_self_component_port *port = + bt_self_component_port_output_as_self_component_port(port_output); + + delete_port_user_data(port); +} + +static void component_class_source_finalize(bt_self_component_source *self_component_source) +{ + uint64_t i; + bt_self_component *self_component; + const bt_component_source *component_source; + + self_component = bt_self_component_source_as_self_component(self_component_source); + component_source = bt_self_component_source_as_component_source(self_component_source); + + component_class_finalize(self_component); + + /* + * Free the user data Python object attached to the port. The + * corresponding incref was done by the `void *` typemap in + * native_bt_port.i. + */ + for (i = 0; i < bt_component_source_get_output_port_count(component_source); i++) { + bt_self_component_port_output *port_output; + + port_output = + bt_self_component_source_borrow_output_port_by_index(self_component_source, i); + + delete_port_output_user_data(port_output); + } +} + +static void component_class_filter_finalize(bt_self_component_filter *self_component_filter) +{ + uint64_t i; + bt_self_component *self_component; + const bt_component_filter *component_filter; + + self_component = bt_self_component_filter_as_self_component(self_component_filter); + component_filter = bt_self_component_filter_as_component_filter(self_component_filter); + + component_class_finalize(self_component); + + /* + * Free the user data Python object attached to the port. The + * corresponding incref was done by the `void *` typemap in + * native_bt_port.i. + */ + for (i = 0; i < bt_component_filter_get_input_port_count(component_filter); i++) { + bt_self_component_port_input *port_input; + + port_input = bt_self_component_filter_borrow_input_port_by_index(self_component_filter, i); + + delete_port_input_user_data(port_input); + } + + for (i = 0; i < bt_component_filter_get_output_port_count(component_filter); i++) { + bt_self_component_port_output *port_output; + + port_output = + bt_self_component_filter_borrow_output_port_by_index(self_component_filter, i); + + delete_port_output_user_data(port_output); + } +} + +static void component_class_sink_finalize(bt_self_component_sink *self_component_sink) +{ + uint64_t i; + bt_self_component *self_component; + const bt_component_sink *component_sink; + + self_component = bt_self_component_sink_as_self_component(self_component_sink); + component_sink = bt_self_component_sink_as_component_sink(self_component_sink); + + component_class_finalize(self_component); + + /* + * Free the user data Python object attached to the port. The + * corresponding incref was done by the `void *` typemap in + * native_bt_port.i. + */ + for (i = 0; i < bt_component_sink_get_input_port_count(component_sink); i++) { + bt_self_component_port_input *port_input; + + port_input = bt_self_component_sink_borrow_input_port_by_index(self_component_sink, i); + + delete_port_input_user_data(port_input); + } +} + +static bt_message_iterator_class_can_seek_beginning_method_status +component_class_can_seek_beginning(bt_self_message_iterator *self_message_iterator, + bt_bool *can_seek) +{ + PyObject *py_result = NULL; + bt_message_iterator_class_can_seek_beginning_method_status status; + const auto py_iter = + static_cast(bt_self_message_iterator_get_data(self_message_iterator)); + + BT_ASSERT(py_iter); + + py_result = PyObject_CallMethod(py_iter, "_bt_can_seek_beginning_from_native", NULL); + if (!py_result) { + status = static_cast( + py_exc_to_status_message_iterator_clear(self_message_iterator)); + goto end; + } + + BT_ASSERT(PyBool_Check(py_result)); + *can_seek = PyObject_IsTrue(py_result); + + status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; + +end: + Py_XDECREF(py_result); + + return status; +} + +static bt_message_iterator_class_seek_beginning_method_status +component_class_seek_beginning(bt_self_message_iterator *self_message_iterator) +{ + PyObject *py_result; + bt_message_iterator_class_seek_beginning_method_status status; + const auto py_iter = + static_cast(bt_self_message_iterator_get_data(self_message_iterator)); + + BT_ASSERT(py_iter); + + py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native", NULL); + if (!py_result) { + status = static_cast( + py_exc_to_status_message_iterator_clear(self_message_iterator)); + goto end; + } + + BT_ASSERT(py_result == Py_None); + + status = BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK; + +end: + Py_XDECREF(py_result); + + return status; +} + +static bt_message_iterator_class_can_seek_ns_from_origin_method_status +component_class_can_seek_ns_from_origin(bt_self_message_iterator *self_message_iterator, + int64_t ns_from_origin, bt_bool *can_seek) +{ + PyObject *py_result = NULL; + bt_message_iterator_class_can_seek_ns_from_origin_method_status status; + const auto py_iter = + static_cast(bt_self_message_iterator_get_data(self_message_iterator)); + + BT_ASSERT(py_iter); + + py_result = PyObject_CallMethod(py_iter, "_bt_can_seek_ns_from_origin_from_native", "L", + ns_from_origin); + if (!py_result) { + status = static_cast( + py_exc_to_status_message_iterator_clear(self_message_iterator)); + goto end; + } + + BT_ASSERT(PyBool_Check(py_result)); + *can_seek = PyObject_IsTrue(py_result); + + status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; + +end: + Py_XDECREF(py_result); + + return status; +} + +static bt_message_iterator_class_seek_ns_from_origin_method_status +component_class_seek_ns_from_origin(bt_self_message_iterator *self_message_iterator, + int64_t ns_from_origin) +{ + PyObject *py_result; + bt_message_iterator_class_seek_ns_from_origin_method_status status; + const auto py_iter = + static_cast(bt_self_message_iterator_get_data(self_message_iterator)); + + BT_ASSERT(py_iter); + + py_result = + PyObject_CallMethod(py_iter, "_bt_seek_ns_from_origin_from_native", "L", ns_from_origin); + if (!py_result) { + status = static_cast( + py_exc_to_status_message_iterator_clear(self_message_iterator)); + goto end; + } + + BT_ASSERT(py_result == Py_None); + + status = BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; + +end: + Py_XDECREF(py_result); + + return status; +} + +static bt_component_class_port_connected_method_status +component_class_port_connected(bt_self_component *self_component, void *self_component_port, + swig_type_info *self_component_port_swig_type, + bt_port_type self_component_port_type, const void *other_port, + swig_type_info *other_port_swig_type) +{ + bt_component_class_port_connected_method_status status; + PyObject *py_self_port_ptr = NULL; + PyObject *py_other_port_ptr = NULL; + PyObject *py_method_result = NULL; + bt_logging_level log_level = get_self_component_log_level(self_component); + const auto py_comp = static_cast(bt_self_component_get_data(self_component)); + + BT_ASSERT(py_comp); + py_self_port_ptr = + SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), self_component_port_swig_type, 0); + if (!py_self_port_ptr) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, BT_FMT_SWIG_ALLOC_FAILED); + status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; + goto end; + } + + py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), other_port_swig_type, 0); + if (!py_other_port_ptr) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, BT_FMT_SWIG_ALLOC_FAILED); + status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; + goto end; + } + + py_method_result = + PyObject_CallMethod(py_comp, "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr, + self_component_port_type, py_other_port_ptr); + if (!py_method_result) { + status = static_cast( + py_exc_to_status_component_clear(self_component)); + goto end; + } + + BT_ASSERT(py_method_result == Py_None); + + status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; + +end: + Py_XDECREF(py_self_port_ptr); + Py_XDECREF(py_other_port_ptr); + Py_XDECREF(py_method_result); + + return status; +} + +static bt_component_class_port_connected_method_status component_class_source_output_port_connected( + bt_self_component_source *self_component_source, + bt_self_component_port_output *self_component_port_output, + const bt_port_input *other_port_input) +{ + bt_self_component *self_component = + bt_self_component_source_as_self_component(self_component_source); + + return component_class_port_connected( + self_component, self_component_port_output, SWIGTYPE_p_bt_self_component_port_output, + BT_PORT_TYPE_OUTPUT, other_port_input, SWIGTYPE_p_bt_port_input); +} + +static bt_component_class_port_connected_method_status +component_class_filter_input_port_connected(bt_self_component_filter *self_component_filter, + bt_self_component_port_input *self_component_port_input, + const bt_port_output *other_port_output) +{ + bt_self_component *self_component = + bt_self_component_filter_as_self_component(self_component_filter); + + return component_class_port_connected( + self_component, self_component_port_input, SWIGTYPE_p_bt_self_component_port_input, + BT_PORT_TYPE_INPUT, other_port_output, SWIGTYPE_p_bt_port_output); +} + +static bt_component_class_port_connected_method_status component_class_filter_output_port_connected( + bt_self_component_filter *self_component_filter, + bt_self_component_port_output *self_component_port_output, + const bt_port_input *other_port_input) +{ + bt_self_component *self_component = + bt_self_component_filter_as_self_component(self_component_filter); + + return component_class_port_connected( + self_component, self_component_port_output, SWIGTYPE_p_bt_self_component_port_output, + BT_PORT_TYPE_OUTPUT, other_port_input, SWIGTYPE_p_bt_port_input); +} + +static bt_component_class_port_connected_method_status +component_class_sink_input_port_connected(bt_self_component_sink *self_component_sink, + bt_self_component_port_input *self_component_port_input, + const bt_port_output *other_port_output) +{ + bt_self_component *self_component = + bt_self_component_sink_as_self_component(self_component_sink); + + return component_class_port_connected( + self_component, self_component_port_input, SWIGTYPE_p_bt_self_component_port_input, + BT_PORT_TYPE_INPUT, other_port_output, SWIGTYPE_p_bt_port_output); +} + +static bt_component_class_sink_graph_is_configured_method_status +component_class_sink_graph_is_configured(bt_self_component_sink *self_component_sink) +{ + PyObject *py_method_result = NULL; + bt_component_class_sink_graph_is_configured_method_status status; + bt_self_component *self_component = + bt_self_component_sink_as_self_component(self_component_sink); + const auto py_comp = static_cast(bt_self_component_get_data(self_component)); + + py_method_result = PyObject_CallMethod(py_comp, "_bt_graph_is_configured_from_native", NULL); + if (!py_method_result) { + status = static_cast( + py_exc_to_status_component_clear(self_component)); + goto end; + } + + BT_ASSERT(py_method_result == Py_None); + + status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; + ; + +end: + Py_XDECREF(py_method_result); + return status; +} + +static bt_component_class_query_method_status +component_class_query(const bt_component_class *component_class, + bt_self_component_class *self_component_class, + bt_private_query_executor *priv_query_executor, const char *object, + const bt_value *params, void *method_data, const bt_value **result) +{ + PyObject *py_cls = NULL; + PyObject *py_params_ptr = NULL; + PyObject *py_priv_query_exec_ptr = NULL; + PyObject *py_query_func = NULL; + PyObject *py_object = NULL; + PyObject *py_results_addr = NULL; + bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; + const bt_query_executor *query_exec = + bt_private_query_executor_as_query_executor_const(priv_query_executor); + bt_logging_level log_level = bt_query_executor_get_logging_level(query_exec); + + /* + * If there's any `method_data`, assume this component class is + * getting queried from Python, so that `method_data` is a + * Python object to pass to the user's _user_query() method. + */ + BT_ASSERT(!method_data || bt_bt2_is_python_component_class(component_class)); + + py_cls = lookup_cc_ptr_to_py_cls(component_class); + if (!py_cls) { + BT_LOG_WRITE_PRINTF_CUR_LVL( + BT_LOG_ERROR, (enum bt_log_level) log_level, BT_LOG_TAG, + "Cannot find Python class associated to native component class: " + "comp-cls-addr=%p", + component_class); + goto error; + } + + py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), SWIGTYPE_p_bt_value, 0); + if (!py_params_ptr) { + BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, (enum bt_log_level) log_level, BT_LOG_TAG, + BT_FMT_SWIG_ALLOC_FAILED); + goto error; + } + + py_priv_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(priv_query_executor), + SWIGTYPE_p_bt_private_query_executor, 0); + if (!py_priv_query_exec_ptr) { + BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, (enum bt_log_level) log_level, BT_LOG_TAG, + BT_FMT_SWIG_ALLOC_FAILED); + goto error; + } + + py_object = SWIG_FromCharPtr(object); + if (!py_object) { + BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_ERROR, (enum bt_log_level) log_level, BT_LOG_TAG, + "Failed to create a Python string."); + goto error; + } + + /* + * We don't take any reference on `method_data` which, if not + * `NULL`, is assumed to be a `PyObject *`: the user's + * _user_query() function will eventually take a reference if + * needed. If `method_data` is `NULL`, then we pass `Py_None` as + * the initialization's Python object. + */ + py_results_addr = + PyObject_CallMethod(py_cls, "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr, + py_object, py_params_ptr, method_data ? method_data : Py_None); + if (!py_results_addr) { + status = static_cast( + py_exc_to_status_component_class_clear(self_component_class, log_level)); + if (status < 0) { +#define BT_FMT "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p" + BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_WARNING, (enum bt_log_level) log_level, BT_LOG_TAG, + BT_FMT, py_cls); + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(self_component_class, BT_FMT, + py_cls); +#undef BT_FMT + } + goto end; + } + + /* + * The returned object, on success, is an integer object + * (PyLong) containing the address of a BT value object (new + * reference). + */ + *result = static_cast(PyLong_AsVoidPtr(py_results_addr)); + BT_ASSERT(!PyErr_Occurred()); + BT_ASSERT(*result); + goto end; + +error: + PyErr_Clear(); + status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; + +end: + Py_XDECREF(py_params_ptr); + Py_XDECREF(py_priv_query_exec_ptr); + Py_XDECREF(py_query_func); + Py_XDECREF(py_object); + Py_XDECREF(py_results_addr); + return status; +} + +static bt_component_class_query_method_status +component_class_source_query(bt_self_component_class_source *self_component_class_source, + bt_private_query_executor *priv_query_executor, const char *object, + const bt_value *params, void *method_data, const bt_value **result) +{ + const bt_component_class_source *component_class_source = + bt_self_component_class_source_as_component_class_source(self_component_class_source); + const bt_component_class *component_class = + bt_component_class_source_as_component_class_const(component_class_source); + bt_self_component_class *self_component_class = + bt_self_component_class_source_as_self_component_class(self_component_class_source); + + return component_class_query(component_class, self_component_class, priv_query_executor, object, + params, method_data, result); +} + +static bt_component_class_query_method_status +component_class_filter_query(bt_self_component_class_filter *self_component_class_filter, + bt_private_query_executor *priv_query_executor, const char *object, + const bt_value *params, void *method_data, const bt_value **result) +{ + const bt_component_class_filter *component_class_filter = + bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); + const bt_component_class *component_class = + bt_component_class_filter_as_component_class_const(component_class_filter); + bt_self_component_class *self_component_class = + bt_self_component_class_filter_as_self_component_class(self_component_class_filter); + + return component_class_query(component_class, self_component_class, priv_query_executor, object, + params, method_data, result); +} + +static bt_component_class_query_method_status +component_class_sink_query(bt_self_component_class_sink *self_component_class_sink, + bt_private_query_executor *priv_query_executor, const char *object, + const bt_value *params, void *method_data, const bt_value **result) +{ + const bt_component_class_sink *component_class_sink = + bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); + const bt_component_class *component_class = + bt_component_class_sink_as_component_class_const(component_class_sink); + bt_self_component_class *self_component_class = + bt_self_component_class_sink_as_self_component_class(self_component_class_sink); + + return component_class_query(component_class, self_component_class, priv_query_executor, object, + params, method_data, result); +} + +static bt_message_iterator_class_initialize_method_status +component_class_message_iterator_init(bt_self_message_iterator *self_message_iterator, + bt_self_message_iterator_configuration *config, + bt_self_component_port_output *self_component_port_output) +{ + bt_message_iterator_class_initialize_method_status status = + BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; + PyObject *py_comp_cls = NULL; + PyObject *py_iter_cls = NULL; + PyObject *py_iter_ptr = NULL; + PyObject *py_config_ptr = NULL; + PyObject *py_component_port_output_ptr = NULL; + PyObject *py_init_method_result = NULL; + PyObject *py_iter = NULL; + bt_self_component *self_component = + bt_self_message_iterator_borrow_component(self_message_iterator); + bt_logging_level log_level = get_self_component_log_level(self_component); + const auto py_comp = static_cast(bt_self_component_get_data(self_component)); + + /* Find user's Python message iterator class */ + py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); + if (!py_comp_cls) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, + "Cannot get Python object's `__class__` attribute."); + goto python_error; + } + + py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); + if (!py_iter_cls) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, + "Cannot get Python class's `_iter_cls` attribute."); + goto python_error; + } + + py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), + SWIGTYPE_p_bt_self_message_iterator, 0); + if (!py_iter_ptr) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, BT_FMT_SWIG_ALLOC_FAILED); + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(self_message_iterator, + BT_FMT_SWIG_ALLOC_FAILED); + goto error; + } + + /* + * Create object with borrowed native message iterator + * reference: + * + * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) + */ + py_iter = PyObject_CallMethod(py_iter_cls, "__new__", "(OO)", py_iter_cls, py_iter_ptr); + if (!py_iter) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, + "Failed to call Python class's __new__() method: " + "py-cls-addr=%p", + py_iter_cls); + goto python_error; + } + + /* + * Initialize object: + * + * py_iter.__init__(config, self_output_port) + * + * through the _init_from_native helper static method. + * + * At this point, py_iter._ptr is set, so this initialization + * function has access to self._component (which gives it the + * user Python component object from which the iterator was + * created). + */ + py_config_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(config), + SWIGTYPE_p_bt_self_message_iterator_configuration, 0); + if (!py_config_ptr) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, BT_FMT_SWIG_ALLOC_FAILED); + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(self_message_iterator, + BT_FMT_SWIG_ALLOC_FAILED); + goto error; + } + + py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output), + SWIGTYPE_p_bt_self_component_port_output, 0); + if (!py_component_port_output_ptr) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, "%s", + BT_FMT_SWIG_ALLOC_FAILED); + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(self_message_iterator, + BT_FMT_SWIG_ALLOC_FAILED); + goto error; + } + + py_init_method_result = PyObject_CallMethod(py_iter, "_bt_init_from_native", "OO", + py_config_ptr, py_component_port_output_ptr); + if (!py_init_method_result) { + BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, + "User's __init__() method failed:"); + goto python_error; + } + + /* + * Since the Python code can never instantiate a user-defined + * message iterator class, the native message iterator + * object does NOT belong to a user Python message iterator + * object (borrowed reference). However this Python object is + * owned by this native message iterator object. + * + * In the Python world, the lifetime of the native message + * iterator is managed by a _GenericMessageIterator + * instance: + * + * _GenericMessageIterator instance: + * owns a native bt_message_iterator object (iter) + * owns a _UserMessageIterator instance (py_iter) + * self._ptr is a borrowed reference to the + * native bt_private_connection_private_message_iterator + * object (iter) + */ + bt_self_message_iterator_set_data(self_message_iterator, py_iter); + py_iter = NULL; + goto end; + +python_error: + /* Handling of errors that cause a Python exception to be set. */ + status = static_cast( + py_exc_to_status_message_iterator_clear(self_message_iterator)); + goto end; + +error: + /* Handling of errors that don't cause a Python exception to be set. */ + status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; + +end: + BT_ASSERT(!PyErr_Occurred()); + + Py_XDECREF(py_comp_cls); + Py_XDECREF(py_iter_cls); + Py_XDECREF(py_iter_ptr); + Py_XDECREF(py_config_ptr); + Py_XDECREF(py_component_port_output_ptr); + Py_XDECREF(py_init_method_result); + Py_XDECREF(py_iter); + return status; +} + +static void component_class_message_iterator_finalize(bt_self_message_iterator *message_iterator) +{ + const auto py_message_iter = + static_cast(bt_self_message_iterator_get_data(message_iterator)); + PyObject *py_method_result = NULL; + + BT_ASSERT(py_message_iter); + + /* Call user's _user_finalize() method */ + py_method_result = PyObject_CallMethod(py_message_iter, "_user_finalize", NULL); + if (!py_method_result) { + bt_self_component *self_comp = bt_self_message_iterator_borrow_component(message_iterator); + bt_logging_level log_level = get_self_component_log_level(self_comp); + + /* + * Ignore any exception raised by the _user_finalize() method + * because it won't change anything at this point: the component + * is being destroyed anyway. + */ + BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp, + "User's _user_finalize() method raised an exception: ignoring:"); + logw_exception_clear(get_self_message_iterator_log_level(message_iterator)); + } + + Py_XDECREF(py_method_result); + Py_DECREF(py_message_iter); +} + +/* Valid for both sources and filters. */ + +static bt_message_iterator_class_next_method_status +component_class_message_iterator_next(bt_self_message_iterator *message_iterator, + bt_message_array_const msgs, uint64_t capacity, + uint64_t *count) +{ + bt_message_iterator_class_next_method_status status; + const auto py_message_iter = + static_cast(bt_self_message_iterator_get_data(message_iterator)); + PyObject *py_method_result = NULL; + + BT_ASSERT_DBG(py_message_iter); + py_method_result = PyObject_CallMethod(py_message_iter, "_bt_next_from_native", NULL); + if (!py_method_result) { + status = static_cast( + py_exc_to_status_message_iterator_clear(message_iterator)); + goto end; + } + + /* + * The returned object, on success, is an integer object + * (PyLong) containing the address of a native message + * object (which is now ours). + */ + msgs[0] = static_cast(PyLong_AsVoidPtr(py_method_result)); + *count = 1; + + /* Overflow errors should never happen. */ + BT_ASSERT_DBG(!PyErr_Occurred()); + + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; + +end: + Py_XDECREF(py_method_result); + return status; +} + +static bt_component_class_sink_consume_method_status +component_class_sink_consume(bt_self_component_sink *self_component_sink) +{ + bt_self_component *self_component = + bt_self_component_sink_as_self_component(self_component_sink); + const auto py_comp = static_cast(bt_self_component_get_data(self_component)); + PyObject *py_method_result = NULL; + bt_component_class_sink_consume_method_status status; + + BT_ASSERT_DBG(py_comp); + + py_method_result = PyObject_CallMethod(py_comp, "_user_consume", NULL); + if (!py_method_result) { + status = static_cast( + py_exc_to_status_component_clear(self_component)); + goto end; + } + + status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; + +end: + Py_XDECREF(py_method_result); + return status; +} + +static int component_class_set_help_and_desc(bt_component_class *component_class, + const char *description, const char *help) +{ + int ret; + + if (description) { + ret = bt_component_class_set_description(component_class, description); + if (ret) { + BT_LOGE("Cannot set component class's description: " + "comp-cls-addr=%p", + component_class); + goto end; + } + } + + if (help) { + ret = bt_component_class_set_help(component_class, help); + if (ret) { + BT_LOGE("Cannot set component class's help text: " + "comp-cls-addr=%p", + component_class); + goto end; + } + } + + ret = 0; + +end: + return ret; +} + +static bt_message_iterator_class *create_message_iterator_class(void) +{ + bt_message_iterator_class *message_iterator_class; + bt_message_iterator_class_set_method_status ret; + + message_iterator_class = + bt_message_iterator_class_create(component_class_message_iterator_next); + if (!message_iterator_class) { + BT_LOGE_STR("Cannot create message iterator class."); + goto end; + } + + ret = bt_message_iterator_class_set_seek_beginning_methods( + message_iterator_class, component_class_seek_beginning, component_class_can_seek_beginning); + BT_ASSERT(ret == 0); + ret = bt_message_iterator_class_set_seek_ns_from_origin_methods( + message_iterator_class, component_class_seek_ns_from_origin, + component_class_can_seek_ns_from_origin); + BT_ASSERT(ret == 0); + ret = bt_message_iterator_class_set_initialize_method(message_iterator_class, + component_class_message_iterator_init); + BT_ASSERT(ret == 0); + ret = bt_message_iterator_class_set_finalize_method(message_iterator_class, + component_class_message_iterator_finalize); + BT_ASSERT(ret == 0); + +end: + return message_iterator_class; +} + +static bt_component_class_source *bt_bt2_component_class_source_create(PyObject *py_cls, + const char *name, + const char *description, + const char *help) +{ + bt_component_class_source *component_class_source = NULL; + bt_message_iterator_class *message_iterator_class; + bt_component_class *component_class; + int ret; + + BT_ASSERT(py_cls); + + message_iterator_class = create_message_iterator_class(); + if (!message_iterator_class) { + goto end; + } + + component_class_source = bt_component_class_source_create(name, message_iterator_class); + if (!component_class_source) { + BT_LOGE_STR("Cannot create source component class."); + goto end; + } + + component_class = bt_component_class_source_as_component_class(component_class_source); + + if (component_class_set_help_and_desc(component_class, description, help)) { + goto end; + } + + ret = bt_component_class_source_set_initialize_method(component_class_source, + component_class_source_init); + BT_ASSERT(ret == 0); + ret = bt_component_class_source_set_finalize_method(component_class_source, + component_class_source_finalize); + BT_ASSERT(ret == 0); + ret = bt_component_class_source_set_output_port_connected_method( + component_class_source, component_class_source_output_port_connected); + BT_ASSERT(ret == 0); + ret = bt_component_class_source_set_query_method(component_class_source, + component_class_source_query); + BT_ASSERT(ret == 0); + ret = bt_component_class_source_set_get_supported_mip_versions_method( + component_class_source, component_class_source_get_supported_mip_versions); + BT_ASSERT(ret == 0); + register_cc_ptr_to_py_cls(component_class, py_cls); + +end: + bt_message_iterator_class_put_ref(message_iterator_class); + return component_class_source; +} + +static bt_component_class_filter *bt_bt2_component_class_filter_create(PyObject *py_cls, + const char *name, + const char *description, + const char *help) +{ + bt_component_class_filter *component_class_filter = NULL; + bt_message_iterator_class *message_iterator_class; + bt_component_class *component_class; + int ret; + + BT_ASSERT(py_cls); + + message_iterator_class = create_message_iterator_class(); + if (!message_iterator_class) { + goto end; + } + + component_class_filter = bt_component_class_filter_create(name, message_iterator_class); + if (!component_class_filter) { + BT_LOGE_STR("Cannot create filter component class."); + goto end; + } + + component_class = bt_component_class_filter_as_component_class(component_class_filter); + + if (component_class_set_help_and_desc(component_class, description, help)) { + goto end; + } + + ret = bt_component_class_filter_set_initialize_method(component_class_filter, + component_class_filter_init); + BT_ASSERT(ret == 0); + ret = bt_component_class_filter_set_finalize_method(component_class_filter, + component_class_filter_finalize); + BT_ASSERT(ret == 0); + ret = bt_component_class_filter_set_input_port_connected_method( + component_class_filter, component_class_filter_input_port_connected); + BT_ASSERT(ret == 0); + ret = bt_component_class_filter_set_output_port_connected_method( + component_class_filter, component_class_filter_output_port_connected); + BT_ASSERT(ret == 0); + ret = bt_component_class_filter_set_query_method(component_class_filter, + component_class_filter_query); + BT_ASSERT(ret == 0); + ret = bt_component_class_filter_set_get_supported_mip_versions_method( + component_class_filter, component_class_filter_get_supported_mip_versions); + BT_ASSERT(ret == 0); + register_cc_ptr_to_py_cls(component_class, py_cls); + +end: + bt_message_iterator_class_put_ref(message_iterator_class); + return component_class_filter; +} + +static bt_component_class_sink *bt_bt2_component_class_sink_create(PyObject *py_cls, + const char *name, + const char *description, + const char *help) +{ + bt_component_class_sink *component_class_sink; + bt_component_class *component_class; + int ret; + + BT_ASSERT(py_cls); + component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume); + + if (!component_class_sink) { + BT_LOGE_STR("Cannot create sink component class."); + goto end; + } + + component_class = bt_component_class_sink_as_component_class(component_class_sink); + + if (component_class_set_help_and_desc(component_class, description, help)) { + goto end; + } + + ret = bt_component_class_sink_set_initialize_method(component_class_sink, + component_class_sink_init); + BT_ASSERT(ret == 0); + ret = bt_component_class_sink_set_finalize_method(component_class_sink, + component_class_sink_finalize); + BT_ASSERT(ret == 0); + ret = bt_component_class_sink_set_input_port_connected_method( + component_class_sink, component_class_sink_input_port_connected); + BT_ASSERT(ret == 0); + ret = bt_component_class_sink_set_graph_is_configured_method( + component_class_sink, component_class_sink_graph_is_configured); + BT_ASSERT(ret == 0); + ret = + bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query); + BT_ASSERT(ret == 0); + ret = bt_component_class_sink_set_get_supported_mip_versions_method( + component_class_sink, component_class_sink_get_supported_mip_versions); + BT_ASSERT(ret == 0); + register_cc_ptr_to_py_cls(component_class, py_cls); + +end: + return component_class_sink; +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_COMPONENT_CLASS_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_error.i b/src/bindings/python/bt2/bt2/native_bt_error.i index 75089056..54571e74 100644 --- a/src/bindings/python/bt2/bt2/native_bt_error.i +++ b/src/bindings/python/bt2/bt2/native_bt_error.i @@ -11,7 +11,7 @@ %include %{ -#include "native_bt_error.i.h" +#include "native_bt_error.i.hpp" %} static diff --git a/src/bindings/python/bt2/bt2/native_bt_error.i.h b/src/bindings/python/bt2/bt2/native_bt_error.i.h deleted file mode 100644 index 5e542d17..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_error.i.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2019 Efficios, Inc. - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_ERROR_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_ERROR_I_H - -#include - -static -PyObject *bt_bt2_format_bt_error_cause(const bt_error_cause *error_cause) -{ - gchar *error_cause_str; - PyObject *py_error_cause_str = NULL; - - error_cause_str = format_bt_error_cause(error_cause, 80, - (bt_logging_level) bt_python_bindings_bt2_log_level, - BT_COMMON_COLOR_WHEN_NEVER); - BT_ASSERT(error_cause_str); - - py_error_cause_str = PyString_FromString(error_cause_str); - - g_free(error_cause_str); - - return py_error_cause_str; -} - -static -PyObject *bt_bt2_format_bt_error(const bt_error *error) -{ - gchar *error_str; - PyObject *py_error_str = NULL; - - error_str = format_bt_error(error, 80, - (bt_logging_level) bt_python_bindings_bt2_log_level, - BT_COMMON_COLOR_WHEN_NEVER); - BT_ASSERT(error_str); - - py_error_str = PyString_FromString(error_str); - - g_free(error_str); - - return py_error_str; -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_ERROR_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_error.i.hpp b/src/bindings/python/bt2/bt2/native_bt_error.i.hpp new file mode 100644 index 00000000..ba8cef09 --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_error.i.hpp @@ -0,0 +1,45 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2019 Efficios, Inc. + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_ERROR_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_ERROR_I_HPP + +#include + +static PyObject *bt_bt2_format_bt_error_cause(const bt_error_cause *error_cause) +{ + gchar *error_cause_str; + PyObject *py_error_cause_str = NULL; + + error_cause_str = + format_bt_error_cause(error_cause, 80, (bt_logging_level) bt_python_bindings_bt2_log_level, + BT_COMMON_COLOR_WHEN_NEVER); + BT_ASSERT(error_cause_str); + + py_error_cause_str = PyString_FromString(error_cause_str); + + g_free(error_cause_str); + + return py_error_cause_str; +} + +static PyObject *bt_bt2_format_bt_error(const bt_error *error) +{ + gchar *error_str; + PyObject *py_error_str = NULL; + + error_str = format_bt_error(error, 80, (bt_logging_level) bt_python_bindings_bt2_log_level, + BT_COMMON_COLOR_WHEN_NEVER); + BT_ASSERT(error_str); + + py_error_str = PyString_FromString(error_str); + + g_free(error_str); + + return py_error_str; +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_ERROR_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_graph.i b/src/bindings/python/bt2/bt2/native_bt_graph.i index d559b252..354ab9fe 100644 --- a/src/bindings/python/bt2/bt2/native_bt_graph.i +++ b/src/bindings/python/bt2/bt2/native_bt_graph.i @@ -88,7 +88,7 @@ /* Helper functions for Python */ %{ -#include "native_bt_graph.i.h" +#include "native_bt_graph.i.hpp" %} PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, diff --git a/src/bindings/python/bt2/bt2/native_bt_graph.i.h b/src/bindings/python/bt2/bt2/native_bt_graph.i.h deleted file mode 100644 index dd51df30..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_graph.i.h +++ /dev/null @@ -1,264 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2017 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_H - -static bt_graph_listener_func_status port_added_listener( - const void *component, - swig_type_info *component_swig_type, - bt_component_class_type component_class_type, - const void *port, - swig_type_info *port_swig_type, - bt_port_type port_type, - void *py_callable) -{ - PyObject *py_component_ptr = NULL; - PyObject *py_port_ptr = NULL; - PyObject *py_res = NULL; - bt_graph_listener_func_status status; - - py_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component), component_swig_type, 0); - if (!py_component_ptr) { - BT_LOGF_STR("Failed to create component SWIG pointer object."); - status = BT_GRAPH_LISTENER_FUNC_STATUS_MEMORY_ERROR; - goto end; - } - - py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), port_swig_type, 0); - if (!py_port_ptr) { - BT_LOGF_STR("Failed to create port SWIG pointer object."); - status = BT_GRAPH_LISTENER_FUNC_STATUS_MEMORY_ERROR; - goto end; - } - - py_res = PyObject_CallFunction(static_cast(py_callable), "(OiOi)", - py_component_ptr, component_class_type, py_port_ptr, port_type); - if (!py_res) { - loge_exception_append_cause_clear( - "Graph's port added listener (Python)", - BT_LOG_OUTPUT_LEVEL); - status = BT_GRAPH_LISTENER_FUNC_STATUS_ERROR; - goto end; - } - - BT_ASSERT(py_res == Py_None); - status = BT_GRAPH_LISTENER_FUNC_STATUS_OK; - -end: - Py_XDECREF(py_res); - Py_XDECREF(py_port_ptr); - Py_XDECREF(py_component_ptr); - return status; -} - -static -bt_graph_listener_func_status -source_component_output_port_added_listener(const bt_component_source *component_source, - const bt_port_output *port_output, void *py_callable) -{ - return port_added_listener( - component_source, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE, - port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); -} - -static -bt_graph_listener_func_status -filter_component_input_port_added_listener(const bt_component_filter *component_filter, - const bt_port_input *port_input, void *py_callable) -{ - return port_added_listener( - component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, - port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); -} - -static -bt_graph_listener_func_status -filter_component_output_port_added_listener(const bt_component_filter *component_filter, - const bt_port_output *port_output, void *py_callable) -{ - return port_added_listener( - component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, - port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); -} - -static -bt_graph_listener_func_status -sink_component_input_port_added_listener(const bt_component_sink *component_sink, - const bt_port_input *port_input, void *py_callable) -{ - return port_added_listener( - component_sink, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK, - port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); -} - -static -PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, - PyObject *py_callable) -{ - PyObject *py_listener_ids = NULL; - PyObject *py_listener_id = NULL; - bt_listener_id listener_id; - bt_graph_add_listener_status status; - const char * const module_name = - "graph_add_port_added_listener() (Python)"; - - BT_ASSERT(graph); - BT_ASSERT(py_callable); - - /* - * Behind the scene, we will be registering 4 different listeners and - * return all of their ids. - */ - py_listener_ids = PyTuple_New(4); - if (!py_listener_ids) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to allocate one PyTuple."); - goto error; - } - - /* source output port */ - status = bt_graph_add_source_component_output_port_added_listener( - graph, source_component_output_port_added_listener, - py_callable, &listener_id); - if (status != __BT_FUNC_STATUS_OK) { - /* - * bt_graph_add_source_component_output_port_added_listener has - * already logged/appended an error cause. - */ - goto error; - } - - py_listener_id = PyLong_FromUnsignedLongLong(listener_id); - if (!py_listener_id) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to allocate one PyLong."); - goto error; - } - - PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id); - py_listener_id = NULL; - - /* filter input port */ - status = bt_graph_add_filter_component_input_port_added_listener( - graph, filter_component_input_port_added_listener, - py_callable, &listener_id); - if (status != __BT_FUNC_STATUS_OK) { - /* - * bt_graph_add_filter_component_input_port_added_listener has - * already logged/appended an error cause. - */ - goto error; - } - - py_listener_id = PyLong_FromUnsignedLongLong(listener_id); - if (!py_listener_id) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to allocate one PyLong."); - goto error; - } - - PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id); - py_listener_id = NULL; - - /* filter output port */ - status = bt_graph_add_filter_component_output_port_added_listener( - graph, filter_component_output_port_added_listener, - py_callable, &listener_id); - if (status != __BT_FUNC_STATUS_OK) { - /* - * bt_graph_add_filter_component_output_port_added_listener has - * already logged/appended an error cause. - */ - goto error; - } - - py_listener_id = PyLong_FromUnsignedLongLong(listener_id); - if (!py_listener_id) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to allocate one PyLong."); - goto error; - } - - PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id); - py_listener_id = NULL; - - /* sink input port */ - status = bt_graph_add_sink_component_input_port_added_listener( - graph, sink_component_input_port_added_listener, - py_callable, &listener_id); - if (status != __BT_FUNC_STATUS_OK) { - /* - * bt_graph_add_sink_component_input_port_added_listener has - * already logged/appended an error cause. - */ - goto error; - } - - py_listener_id = PyLong_FromUnsignedLongLong(listener_id); - if (!py_listener_id) { - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, - "Failed to allocate one PyLong."); - goto error; - } - - PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id); - py_listener_id = NULL; - goto end; - -error: - Py_XDECREF(py_listener_ids); - py_listener_ids = Py_None; - Py_INCREF(py_listener_ids); - -end: - Py_XDECREF(py_listener_id); - return py_listener_ids; -} - -static -bt_graph_add_component_status -bt_bt2_graph_add_source_component( - bt_graph *graph, - const bt_component_class_source *component_class, - const char *name, const bt_value *params, - PyObject *obj, bt_logging_level log_level, - const bt_component_source **component) -{ - return bt_graph_add_source_component_with_initialize_method_data(graph, - component_class, name, params, obj == Py_None ? NULL : obj, - log_level, component); -} - -static -bt_graph_add_component_status -bt_bt2_graph_add_filter_component( - bt_graph *graph, - const bt_component_class_filter *component_class, - const char *name, const bt_value *params, - PyObject *obj, bt_logging_level log_level, - const bt_component_filter **component) -{ - return bt_graph_add_filter_component_with_initialize_method_data(graph, - component_class, name, params, obj == Py_None ? NULL : obj, - log_level, component); -} - -static -bt_graph_add_component_status -bt_bt2_graph_add_sink_component( - bt_graph *graph, - const bt_component_class_sink *component_class, - const char *name, const bt_value *params, - PyObject *obj, bt_logging_level log_level, - const bt_component_sink **component) -{ - return bt_graph_add_sink_component_with_initialize_method_data(graph, - component_class, name, params, obj == Py_None ? NULL : obj, - log_level, component); -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_graph.i.hpp b/src/bindings/python/bt2/bt2/native_bt_graph.i.hpp new file mode 100644 index 00000000..1cccaa9f --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_graph.i.hpp @@ -0,0 +1,233 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2017 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_HPP + +static bt_graph_listener_func_status +port_added_listener(const void *component, swig_type_info *component_swig_type, + bt_component_class_type component_class_type, const void *port, + swig_type_info *port_swig_type, bt_port_type port_type, void *py_callable) +{ + PyObject *py_component_ptr = NULL; + PyObject *py_port_ptr = NULL; + PyObject *py_res = NULL; + bt_graph_listener_func_status status; + + py_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component), component_swig_type, 0); + if (!py_component_ptr) { + BT_LOGF_STR("Failed to create component SWIG pointer object."); + status = BT_GRAPH_LISTENER_FUNC_STATUS_MEMORY_ERROR; + goto end; + } + + py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), port_swig_type, 0); + if (!py_port_ptr) { + BT_LOGF_STR("Failed to create port SWIG pointer object."); + status = BT_GRAPH_LISTENER_FUNC_STATUS_MEMORY_ERROR; + goto end; + } + + py_res = PyObject_CallFunction(static_cast(py_callable), "(OiOi)", py_component_ptr, + component_class_type, py_port_ptr, port_type); + if (!py_res) { + loge_exception_append_cause_clear("Graph's port added listener (Python)", + BT_LOG_OUTPUT_LEVEL); + status = BT_GRAPH_LISTENER_FUNC_STATUS_ERROR; + goto end; + } + + BT_ASSERT(py_res == Py_None); + status = BT_GRAPH_LISTENER_FUNC_STATUS_OK; + +end: + Py_XDECREF(py_res); + Py_XDECREF(py_port_ptr); + Py_XDECREF(py_component_ptr); + return status; +} + +static bt_graph_listener_func_status +source_component_output_port_added_listener(const bt_component_source *component_source, + const bt_port_output *port_output, void *py_callable) +{ + return port_added_listener(component_source, SWIGTYPE_p_bt_component_source, + BT_COMPONENT_CLASS_TYPE_SOURCE, port_output, + SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); +} + +static bt_graph_listener_func_status +filter_component_input_port_added_listener(const bt_component_filter *component_filter, + const bt_port_input *port_input, void *py_callable) +{ + return port_added_listener(component_filter, SWIGTYPE_p_bt_component_filter, + BT_COMPONENT_CLASS_TYPE_FILTER, port_input, SWIGTYPE_p_bt_port_input, + BT_PORT_TYPE_INPUT, py_callable); +} + +static bt_graph_listener_func_status +filter_component_output_port_added_listener(const bt_component_filter *component_filter, + const bt_port_output *port_output, void *py_callable) +{ + return port_added_listener(component_filter, SWIGTYPE_p_bt_component_filter, + BT_COMPONENT_CLASS_TYPE_FILTER, port_output, + SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); +} + +static bt_graph_listener_func_status +sink_component_input_port_added_listener(const bt_component_sink *component_sink, + const bt_port_input *port_input, void *py_callable) +{ + return port_added_listener(component_sink, SWIGTYPE_p_bt_component_sink, + BT_COMPONENT_CLASS_TYPE_SINK, port_input, SWIGTYPE_p_bt_port_input, + BT_PORT_TYPE_INPUT, py_callable); +} + +static PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, PyObject *py_callable) +{ + PyObject *py_listener_ids = NULL; + PyObject *py_listener_id = NULL; + bt_listener_id listener_id; + bt_graph_add_listener_status status; + const char * const module_name = "graph_add_port_added_listener() (Python)"; + + BT_ASSERT(graph); + BT_ASSERT(py_callable); + + /* + * Behind the scene, we will be registering 4 different listeners and + * return all of their ids. + */ + py_listener_ids = PyTuple_New(4); + if (!py_listener_ids) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyTuple."); + goto error; + } + + /* source output port */ + status = bt_graph_add_source_component_output_port_added_listener( + graph, source_component_output_port_added_listener, py_callable, &listener_id); + if (status != __BT_FUNC_STATUS_OK) { + /* + * bt_graph_add_source_component_output_port_added_listener has + * already logged/appended an error cause. + */ + goto error; + } + + py_listener_id = PyLong_FromUnsignedLongLong(listener_id); + if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); + goto error; + } + + PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id); + py_listener_id = NULL; + + /* filter input port */ + status = bt_graph_add_filter_component_input_port_added_listener( + graph, filter_component_input_port_added_listener, py_callable, &listener_id); + if (status != __BT_FUNC_STATUS_OK) { + /* + * bt_graph_add_filter_component_input_port_added_listener has + * already logged/appended an error cause. + */ + goto error; + } + + py_listener_id = PyLong_FromUnsignedLongLong(listener_id); + if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); + goto error; + } + + PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id); + py_listener_id = NULL; + + /* filter output port */ + status = bt_graph_add_filter_component_output_port_added_listener( + graph, filter_component_output_port_added_listener, py_callable, &listener_id); + if (status != __BT_FUNC_STATUS_OK) { + /* + * bt_graph_add_filter_component_output_port_added_listener has + * already logged/appended an error cause. + */ + goto error; + } + + py_listener_id = PyLong_FromUnsignedLongLong(listener_id); + if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); + goto error; + } + + PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id); + py_listener_id = NULL; + + /* sink input port */ + status = bt_graph_add_sink_component_input_port_added_listener( + graph, sink_component_input_port_added_listener, py_callable, &listener_id); + if (status != __BT_FUNC_STATUS_OK) { + /* + * bt_graph_add_sink_component_input_port_added_listener has + * already logged/appended an error cause. + */ + goto error; + } + + py_listener_id = PyLong_FromUnsignedLongLong(listener_id); + if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); + goto error; + } + + PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id); + py_listener_id = NULL; + goto end; + +error: + Py_XDECREF(py_listener_ids); + py_listener_ids = Py_None; + Py_INCREF(py_listener_ids); + +end: + Py_XDECREF(py_listener_id); + return py_listener_ids; +} + +static bt_graph_add_component_status +bt_bt2_graph_add_source_component(bt_graph *graph, const bt_component_class_source *component_class, + const char *name, const bt_value *params, PyObject *obj, + bt_logging_level log_level, const bt_component_source **component) +{ + return bt_graph_add_source_component_with_initialize_method_data( + graph, component_class, name, params, obj == Py_None ? NULL : obj, log_level, component); +} + +static bt_graph_add_component_status +bt_bt2_graph_add_filter_component(bt_graph *graph, const bt_component_class_filter *component_class, + const char *name, const bt_value *params, PyObject *obj, + bt_logging_level log_level, const bt_component_filter **component) +{ + return bt_graph_add_filter_component_with_initialize_method_data( + graph, component_class, name, params, obj == Py_None ? NULL : obj, log_level, component); +} + +static bt_graph_add_component_status +bt_bt2_graph_add_sink_component(bt_graph *graph, const bt_component_class_sink *component_class, + const char *name, const bt_value *params, PyObject *obj, + bt_logging_level log_level, const bt_component_sink **component) +{ + return bt_graph_add_sink_component_with_initialize_method_data( + graph, component_class, name, params, obj == Py_None ? NULL : obj, log_level, component); +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_message_iterator.i b/src/bindings/python/bt2/bt2/native_bt_message_iterator.i index d54b8e1d..a10fd902 100644 --- a/src/bindings/python/bt2/bt2/native_bt_message_iterator.i +++ b/src/bindings/python/bt2/bt2/native_bt_message_iterator.i @@ -30,7 +30,7 @@ /* Helper functions for Python */ %{ -#include "native_bt_message_iterator.i.h" +#include "native_bt_message_iterator.i.hpp" %} bt_message_iterator_create_from_message_iterator_status diff --git a/src/bindings/python/bt2/bt2/native_bt_message_iterator.i.h b/src/bindings/python/bt2/bt2/native_bt_message_iterator.i.h deleted file mode 100644 index 13b9a093..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_message_iterator.i.h +++ /dev/null @@ -1,125 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2017 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MESSAGE_ITERATOR_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MESSAGE_ITERATOR_I_H - -static -bt_message_iterator_create_from_message_iterator_status -bt_bt2_message_iterator_create_from_message_iterator( - bt_self_message_iterator *self_msg_iter, - bt_self_component_port_input *input_port, - bt_message_iterator **message_iterator) -{ - bt_message_iterator_create_from_message_iterator_status - status; - - status = bt_message_iterator_create_from_message_iterator( - self_msg_iter, input_port, message_iterator); - - if (status != BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) { - *message_iterator = NULL; - } - - return status; -} - -static -bt_message_iterator_create_from_sink_component_status -bt_bt2_message_iterator_create_from_sink_component( - bt_self_component_sink *self_comp, - bt_self_component_port_input *input_port, - bt_message_iterator **message_iterator) -{ - bt_message_iterator_create_from_sink_component_status - status; - - status = bt_message_iterator_create_from_sink_component( - self_comp, input_port, message_iterator); - - if (status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { - *message_iterator = NULL; - } - - return status; -} - -static PyObject *bt_bt2_get_user_component_from_user_msg_iter( - bt_self_message_iterator *self_message_iterator) -{ - bt_self_component *self_component = bt_self_message_iterator_borrow_component(self_message_iterator); - - BT_ASSERT_DBG(self_component); - - const auto py_comp = static_cast( - bt_self_component_get_data(self_component)); - - BT_ASSERT_DBG(py_comp); - - /* Return new reference */ - Py_INCREF(py_comp); - return py_comp; -} - -static inline -PyObject *create_pylist_from_messages(bt_message_array_const messages, - uint64_t message_count) -{ - uint64_t i; - PyObject *py_msg_list = PyList_New(message_count); - - BT_ASSERT(py_msg_list); - - for (i = 0; i < message_count; i++) { - PyList_SET_ITEM(py_msg_list, i, - SWIG_NewPointerObj(SWIG_as_voidptr(messages[i]), - SWIGTYPE_p_bt_message, 0)); - } - - return py_msg_list; -} - -static -PyObject *get_msg_range_common(bt_message_iterator_next_status status, - bt_message_array_const messages, uint64_t message_count) -{ - PyObject *py_status; - PyObject *py_return_tuple; - PyObject *py_msg_list; - - py_return_tuple = PyTuple_New(2); - BT_ASSERT(py_return_tuple); - - /* Set tuple[0], status. */ - py_status = SWIG_From_long_SS_long(status); - PyTuple_SET_ITEM(py_return_tuple, 0, py_status); - - /* Set tuple[1], message list on success, None otherwise. */ - if (status == __BT_FUNC_STATUS_OK) { - py_msg_list = create_pylist_from_messages(messages, message_count); - } else { - py_msg_list = Py_None; - Py_INCREF(py_msg_list); - } - - PyTuple_SET_ITEM(py_return_tuple, 1, py_msg_list); - - return py_return_tuple; -} - -static PyObject *bt_bt2_self_component_port_input_get_msg_range( - bt_message_iterator *iter) -{ - bt_message_array_const messages; - uint64_t message_count = 0; - bt_message_iterator_next_status status; - - status = bt_message_iterator_next(iter, - &messages, &message_count); - return get_msg_range_common(status, messages, message_count); -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MESSAGE_ITERATOR_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_message_iterator.i.hpp b/src/bindings/python/bt2/bt2/native_bt_message_iterator.i.hpp new file mode 100644 index 00000000..f44c1a3f --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_message_iterator.i.hpp @@ -0,0 +1,114 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2017 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MESSAGE_ITERATOR_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MESSAGE_ITERATOR_I_HPP + +static bt_message_iterator_create_from_message_iterator_status +bt_bt2_message_iterator_create_from_message_iterator(bt_self_message_iterator *self_msg_iter, + bt_self_component_port_input *input_port, + bt_message_iterator **message_iterator) +{ + bt_message_iterator_create_from_message_iterator_status status; + + status = bt_message_iterator_create_from_message_iterator(self_msg_iter, input_port, + message_iterator); + + if (status != BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) { + *message_iterator = NULL; + } + + return status; +} + +static bt_message_iterator_create_from_sink_component_status +bt_bt2_message_iterator_create_from_sink_component(bt_self_component_sink *self_comp, + bt_self_component_port_input *input_port, + bt_message_iterator **message_iterator) +{ + bt_message_iterator_create_from_sink_component_status status; + + status = + bt_message_iterator_create_from_sink_component(self_comp, input_port, message_iterator); + + if (status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { + *message_iterator = NULL; + } + + return status; +} + +static PyObject * +bt_bt2_get_user_component_from_user_msg_iter(bt_self_message_iterator *self_message_iterator) +{ + bt_self_component *self_component = + bt_self_message_iterator_borrow_component(self_message_iterator); + + BT_ASSERT_DBG(self_component); + + const auto py_comp = static_cast(bt_self_component_get_data(self_component)); + + BT_ASSERT_DBG(py_comp); + + /* Return new reference */ + Py_INCREF(py_comp); + return py_comp; +} + +static inline PyObject *create_pylist_from_messages(bt_message_array_const messages, + uint64_t message_count) +{ + uint64_t i; + PyObject *py_msg_list = PyList_New(message_count); + + BT_ASSERT(py_msg_list); + + for (i = 0; i < message_count; i++) { + PyList_SET_ITEM(py_msg_list, i, + SWIG_NewPointerObj(SWIG_as_voidptr(messages[i]), SWIGTYPE_p_bt_message, 0)); + } + + return py_msg_list; +} + +static PyObject *get_msg_range_common(bt_message_iterator_next_status status, + bt_message_array_const messages, uint64_t message_count) +{ + PyObject *py_status; + PyObject *py_return_tuple; + PyObject *py_msg_list; + + py_return_tuple = PyTuple_New(2); + BT_ASSERT(py_return_tuple); + + /* Set tuple[0], status. */ + py_status = SWIG_From_long_SS_long(status); + PyTuple_SET_ITEM(py_return_tuple, 0, py_status); + + /* Set tuple[1], message list on success, None otherwise. */ + if (status == __BT_FUNC_STATUS_OK) { + py_msg_list = create_pylist_from_messages(messages, message_count); + } else { + py_msg_list = Py_None; + Py_INCREF(py_msg_list); + } + + PyTuple_SET_ITEM(py_return_tuple, 1, py_msg_list); + + return py_return_tuple; +} + +static PyObject *bt_bt2_self_component_port_input_get_msg_range(bt_message_iterator *iter) +{ + bt_message_array_const messages; + uint64_t message_count = 0; + bt_message_iterator_next_status status; + + status = bt_message_iterator_next(iter, &messages, &message_count); + return get_msg_range_common(status, messages, message_count); +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MESSAGE_ITERATOR_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_mip.i b/src/bindings/python/bt2/bt2/native_bt_mip.i index b7d28850..1070b0db 100644 --- a/src/bindings/python/bt2/bt2/native_bt_mip.i +++ b/src/bindings/python/bt2/bt2/native_bt_mip.i @@ -8,7 +8,7 @@ %include %{ -#include "native_bt_mip.i.h" +#include "native_bt_mip.i.hpp" %} bt_component_descriptor_set_add_descriptor_status diff --git a/src/bindings/python/bt2/bt2/native_bt_mip.i.h b/src/bindings/python/bt2/bt2/native_bt_mip.i.h deleted file mode 100644 index 9e64084c..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_mip.i.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2017 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MIP_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MIP_I_H - -static -bt_component_descriptor_set_add_descriptor_status -bt_bt2_component_descriptor_set_add_descriptor_with_initialize_method_data( - bt_component_descriptor_set *comp_descr_set, - const bt_component_class *comp_cls, - const bt_value *params, PyObject *obj) -{ - return bt_component_descriptor_set_add_descriptor_with_initialize_method_data( - comp_descr_set, comp_cls, params, obj == Py_None ? NULL : obj); -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MIP_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_mip.i.hpp b/src/bindings/python/bt2/bt2/native_bt_mip.i.hpp new file mode 100644 index 00000000..d18fec13 --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_mip.i.hpp @@ -0,0 +1,19 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2017 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MIP_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MIP_I_HPP + +static bt_component_descriptor_set_add_descriptor_status +bt_bt2_component_descriptor_set_add_descriptor_with_initialize_method_data( + bt_component_descriptor_set *comp_descr_set, const bt_component_class *comp_cls, + const bt_value *params, PyObject *obj) +{ + return bt_component_descriptor_set_add_descriptor_with_initialize_method_data( + comp_descr_set, comp_cls, params, obj == Py_None ? NULL : obj); +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MIP_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_plugin.i b/src/bindings/python/bt2/bt2/native_bt_plugin.i index dc4f4480..5218b95a 100644 --- a/src/bindings/python/bt2/bt2/native_bt_plugin.i +++ b/src/bindings/python/bt2/bt2/native_bt_plugin.i @@ -51,7 +51,7 @@ /* Helpers */ %{ -#include "native_bt_plugin.i.h" +#include "native_bt_plugin.i.hpp" %} bt_property_availability bt_bt2_plugin_get_version( diff --git a/src/bindings/python/bt2/bt2/native_bt_plugin.i.h b/src/bindings/python/bt2/bt2/native_bt_plugin.i.h deleted file mode 100644 index 1edf0813..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_plugin.i.h +++ /dev/null @@ -1,102 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2017 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_PLUGIN_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_PLUGIN_I_H - -/* - * Those bt_bt2_*() functions below ensure that when the API function - * fails, the output parameter is set to `NULL`. This is necessary - * because the epilogue of the `something **OUT` typemap will use that - * value to make a Python object. We can't rely on the initial value of - * `*OUT`; it could point to unreadable memory. - */ - -static -bt_property_availability bt_bt2_plugin_get_version( - const bt_plugin *plugin, unsigned int *major, - unsigned int *minor, unsigned int *patch, const char **extra) -{ - bt_property_availability ret; - - ret = bt_plugin_get_version(plugin, major, minor, patch, extra); - - if (ret == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) { - *extra = NULL; - } - - return ret; -} - -static -bt_plugin_find_status bt_bt2_plugin_find(const char *plugin_name, - bt_bool find_in_std_env_var, bt_bool find_in_user_dir, - bt_bool find_in_sys_dir, bt_bool find_in_static, - bt_bool fail_on_load_error, const bt_plugin **plugin) -{ - bt_plugin_find_status status; - - status = bt_plugin_find(plugin_name, find_in_std_env_var, - find_in_user_dir, find_in_sys_dir, find_in_static, - fail_on_load_error, plugin); - if (status != __BT_FUNC_STATUS_OK) { - *plugin = NULL; - } - - return status; -} - -static -bt_plugin_find_all_status bt_bt2_plugin_find_all(bt_bool find_in_std_env_var, - bt_bool find_in_user_dir, bt_bool find_in_sys_dir, - bt_bool find_in_static, bt_bool fail_on_load_error, - const bt_plugin_set **plugin_set) -{ - bt_plugin_find_all_status status; - - status = bt_plugin_find_all(find_in_std_env_var, - find_in_user_dir, find_in_sys_dir, find_in_static, - fail_on_load_error, plugin_set); - if (status != __BT_FUNC_STATUS_OK) { - *plugin_set = NULL; - } - - return status; -} - -static -bt_plugin_find_all_from_file_status bt_bt2_plugin_find_all_from_file( - const char *path, bt_bool fail_on_load_error, - const bt_plugin_set **plugin_set) -{ - bt_plugin_find_all_from_file_status status; - - status = bt_plugin_find_all_from_file(path, fail_on_load_error, - plugin_set); - if (status != __BT_FUNC_STATUS_OK) { - *plugin_set = NULL; - } - - return status; -} - -static -bt_plugin_find_all_from_dir_status bt_bt2_plugin_find_all_from_dir( - const char *path, bt_bool recurse, bt_bool fail_on_load_error, - const bt_plugin_set **plugin_set) -{ - bt_plugin_find_all_from_dir_status status; - - status = bt_plugin_find_all_from_dir(path, recurse, fail_on_load_error, - plugin_set); - if (status != __BT_FUNC_STATUS_OK) { - *plugin_set = NULL; - } - - return status; -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_PLUGIN_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_plugin.i.hpp b/src/bindings/python/bt2/bt2/native_bt_plugin.i.hpp new file mode 100644 index 00000000..054c8de1 --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_plugin.i.hpp @@ -0,0 +1,94 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2017 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_PLUGIN_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_PLUGIN_I_HPP + +/* + * Those bt_bt2_*() functions below ensure that when the API function + * fails, the output parameter is set to `NULL`. This is necessary + * because the epilogue of the `something **OUT` typemap will use that + * value to make a Python object. We can't rely on the initial value of + * `*OUT`; it could point to unreadable memory. + */ + +static bt_property_availability bt_bt2_plugin_get_version(const bt_plugin *plugin, + unsigned int *major, unsigned int *minor, + unsigned int *patch, const char **extra) +{ + bt_property_availability ret; + + ret = bt_plugin_get_version(plugin, major, minor, patch, extra); + + if (ret == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) { + *extra = NULL; + } + + return ret; +} + +static bt_plugin_find_status bt_bt2_plugin_find(const char *plugin_name, + bt_bool find_in_std_env_var, + bt_bool find_in_user_dir, bt_bool find_in_sys_dir, + bt_bool find_in_static, bt_bool fail_on_load_error, + const bt_plugin **plugin) +{ + bt_plugin_find_status status; + + status = bt_plugin_find(plugin_name, find_in_std_env_var, find_in_user_dir, find_in_sys_dir, + find_in_static, fail_on_load_error, plugin); + if (status != __BT_FUNC_STATUS_OK) { + *plugin = NULL; + } + + return status; +} + +static bt_plugin_find_all_status +bt_bt2_plugin_find_all(bt_bool find_in_std_env_var, bt_bool find_in_user_dir, + bt_bool find_in_sys_dir, bt_bool find_in_static, bt_bool fail_on_load_error, + const bt_plugin_set **plugin_set) +{ + bt_plugin_find_all_status status; + + status = bt_plugin_find_all(find_in_std_env_var, find_in_user_dir, find_in_sys_dir, + find_in_static, fail_on_load_error, plugin_set); + if (status != __BT_FUNC_STATUS_OK) { + *plugin_set = NULL; + } + + return status; +} + +static bt_plugin_find_all_from_file_status +bt_bt2_plugin_find_all_from_file(const char *path, bt_bool fail_on_load_error, + const bt_plugin_set **plugin_set) +{ + bt_plugin_find_all_from_file_status status; + + status = bt_plugin_find_all_from_file(path, fail_on_load_error, plugin_set); + if (status != __BT_FUNC_STATUS_OK) { + *plugin_set = NULL; + } + + return status; +} + +static bt_plugin_find_all_from_dir_status +bt_bt2_plugin_find_all_from_dir(const char *path, bt_bool recurse, bt_bool fail_on_load_error, + const bt_plugin_set **plugin_set) +{ + bt_plugin_find_all_from_dir_status status; + + status = bt_plugin_find_all_from_dir(path, recurse, fail_on_load_error, plugin_set); + if (status != __BT_FUNC_STATUS_OK) { + *plugin_set = NULL; + } + + return status; +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_PLUGIN_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_query_exec.i b/src/bindings/python/bt2/bt2/native_bt_query_exec.i index 77838b1f..b13bc985 100644 --- a/src/bindings/python/bt2/bt2/native_bt_query_exec.i +++ b/src/bindings/python/bt2/bt2/native_bt_query_exec.i @@ -8,7 +8,7 @@ %include %{ -#include "native_bt_query_exec.i.h" +#include "native_bt_query_exec.i.hpp" %} bt_query_executor *bt_bt2_query_executor_create( diff --git a/src/bindings/python/bt2/bt2/native_bt_query_exec.i.h b/src/bindings/python/bt2/bt2/native_bt_query_exec.i.h deleted file mode 100644 index 9c26acff..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_query_exec.i.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2017 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_QUERY_EXEC_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_QUERY_EXEC_I_H - -static -bt_query_executor *bt_bt2_query_executor_create( - const bt_component_class *component_class, const char *object, - const bt_value *params, PyObject *py_obj) -{ - return bt_query_executor_create_with_method_data(component_class, - object, params, py_obj == Py_None ? NULL : py_obj); -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_QUERY_EXEC_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_query_exec.i.hpp b/src/bindings/python/bt2/bt2/native_bt_query_exec.i.hpp new file mode 100644 index 00000000..714cfef4 --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_query_exec.i.hpp @@ -0,0 +1,18 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2017 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_QUERY_EXEC_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_QUERY_EXEC_I_HPP + +static bt_query_executor *bt_bt2_query_executor_create(const bt_component_class *component_class, + const char *object, const bt_value *params, + PyObject *py_obj) +{ + return bt_query_executor_create_with_method_data(component_class, object, params, + py_obj == Py_None ? NULL : py_obj); +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_QUERY_EXEC_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_trace.i b/src/bindings/python/bt2/bt2/native_bt_trace.i index 335e09ea..dacc02ab 100644 --- a/src/bindings/python/bt2/bt2/native_bt_trace.i +++ b/src/bindings/python/bt2/bt2/native_bt_trace.i @@ -7,7 +7,7 @@ %include %{ -#include "native_bt_trace.i.h" +#include "native_bt_trace.i.hpp" %} int bt_bt2_trace_add_destruction_listener(bt_trace *trace, diff --git a/src/bindings/python/bt2/bt2/native_bt_trace.i.h b/src/bindings/python/bt2/bt2/native_bt_trace.i.h deleted file mode 100644 index f6dd64e0..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_trace.i.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2016 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_I_H - -static void -trace_destroyed_listener(const bt_trace *trace, void *py_callable) -{ - PyObject *py_trace_ptr = NULL; - PyObject *py_res = NULL; - - py_trace_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(trace), - SWIGTYPE_p_bt_trace, 0); - if (!py_trace_ptr) { - BT_LOGF_STR("Failed to create a SWIG pointer object."); - bt_common_abort(); - } - - py_res = PyObject_CallFunction(static_cast(py_callable), "(O)", - py_trace_ptr); - if (!py_res) { - logw_exception_clear(BT_LOG_OUTPUT_LEVEL); - goto end; - } - - BT_ASSERT(py_res == Py_None); - -end: - Py_DECREF(py_trace_ptr); - Py_XDECREF(py_res); -} - -static -int bt_bt2_trace_add_destruction_listener(bt_trace *trace, - PyObject *py_callable, bt_listener_id *id) -{ - bt_trace_add_listener_status status; - - BT_ASSERT(trace); - BT_ASSERT(py_callable); - status = bt_trace_add_destruction_listener( - trace, trace_destroyed_listener, py_callable, id); - if (status == __BT_FUNC_STATUS_OK) { - Py_INCREF(py_callable); - } - - return status; -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_trace.i.hpp b/src/bindings/python/bt2/bt2/native_bt_trace.i.hpp new file mode 100644 index 00000000..6405eb79 --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_trace.i.hpp @@ -0,0 +1,49 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2016 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_I_HPP + +static void trace_destroyed_listener(const bt_trace *trace, void *py_callable) +{ + PyObject *py_trace_ptr = NULL; + PyObject *py_res = NULL; + + py_trace_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(trace), SWIGTYPE_p_bt_trace, 0); + if (!py_trace_ptr) { + BT_LOGF_STR("Failed to create a SWIG pointer object."); + bt_common_abort(); + } + + py_res = PyObject_CallFunction(static_cast(py_callable), "(O)", py_trace_ptr); + if (!py_res) { + logw_exception_clear(BT_LOG_OUTPUT_LEVEL); + goto end; + } + + BT_ASSERT(py_res == Py_None); + +end: + Py_DECREF(py_trace_ptr); + Py_XDECREF(py_res); +} + +static int bt_bt2_trace_add_destruction_listener(bt_trace *trace, PyObject *py_callable, + bt_listener_id *id) +{ + bt_trace_add_listener_status status; + + BT_ASSERT(trace); + BT_ASSERT(py_callable); + status = bt_trace_add_destruction_listener(trace, trace_destroyed_listener, py_callable, id); + if (status == __BT_FUNC_STATUS_OK) { + Py_INCREF(py_callable); + } + + return status; +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_trace_class.i b/src/bindings/python/bt2/bt2/native_bt_trace_class.i index 668cc5a9..94a63fe4 100644 --- a/src/bindings/python/bt2/bt2/native_bt_trace_class.i +++ b/src/bindings/python/bt2/bt2/native_bt_trace_class.i @@ -8,7 +8,7 @@ /* Helper functions for Python */ %{ -#include "native_bt_trace_class.i.h" +#include "native_bt_trace_class.i.hpp" %} int bt_bt2_trace_class_add_destruction_listener( diff --git a/src/bindings/python/bt2/bt2/native_bt_trace_class.i.h b/src/bindings/python/bt2/bt2/native_bt_trace_class.i.h deleted file mode 100644 index 3f293086..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_trace_class.i.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2016 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_CLASS_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_CLASS_I_H - -static void -trace_class_destroyed_listener(const bt_trace_class *trace_class, void *py_callable) -{ - PyObject *py_trace_class_ptr = NULL; - PyObject *py_res = NULL; - - py_trace_class_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(trace_class), - SWIGTYPE_p_bt_trace_class, 0); - if (!py_trace_class_ptr) { - BT_LOGF_STR("Failed to create a SWIG pointer object."); - bt_common_abort(); - } - - py_res = PyObject_CallFunction(static_cast(py_callable), "(O)", - py_trace_class_ptr); - if (!py_res) { - logw_exception_clear(BT_LOG_OUTPUT_LEVEL); - goto end; - } - - BT_ASSERT(py_res == Py_None); - -end: - Py_DECREF(py_trace_class_ptr); - Py_XDECREF(py_res); -} - -static -int bt_bt2_trace_class_add_destruction_listener( - bt_trace_class *trace_class, PyObject *py_callable, - bt_listener_id *id) -{ - bt_trace_class_add_listener_status status; - - BT_ASSERT(trace_class); - BT_ASSERT(py_callable); - - status = bt_trace_class_add_destruction_listener( - trace_class, trace_class_destroyed_listener, py_callable, id); - if (status == __BT_FUNC_STATUS_OK) { - Py_INCREF(py_callable); - } - - return status; -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_CLASS_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_trace_class.i.hpp b/src/bindings/python/bt2/bt2/native_bt_trace_class.i.hpp new file mode 100644 index 00000000..d23dc013 --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_trace_class.i.hpp @@ -0,0 +1,52 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2016 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_CLASS_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_CLASS_I_HPP + +static void trace_class_destroyed_listener(const bt_trace_class *trace_class, void *py_callable) +{ + PyObject *py_trace_class_ptr = NULL; + PyObject *py_res = NULL; + + py_trace_class_ptr = + SWIG_NewPointerObj(SWIG_as_voidptr(trace_class), SWIGTYPE_p_bt_trace_class, 0); + if (!py_trace_class_ptr) { + BT_LOGF_STR("Failed to create a SWIG pointer object."); + bt_common_abort(); + } + + py_res = PyObject_CallFunction(static_cast(py_callable), "(O)", py_trace_class_ptr); + if (!py_res) { + logw_exception_clear(BT_LOG_OUTPUT_LEVEL); + goto end; + } + + BT_ASSERT(py_res == Py_None); + +end: + Py_DECREF(py_trace_class_ptr); + Py_XDECREF(py_res); +} + +static int bt_bt2_trace_class_add_destruction_listener(bt_trace_class *trace_class, + PyObject *py_callable, bt_listener_id *id) +{ + bt_trace_class_add_listener_status status; + + BT_ASSERT(trace_class); + BT_ASSERT(py_callable); + + status = bt_trace_class_add_destruction_listener(trace_class, trace_class_destroyed_listener, + py_callable, id); + if (status == __BT_FUNC_STATUS_OK) { + Py_INCREF(py_callable); + } + + return status; +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_TRACE_CLASS_I_HPP */ diff --git a/src/bindings/python/bt2/bt2/native_bt_value.i b/src/bindings/python/bt2/bt2/native_bt_value.i index 28bec6bb..06966a05 100644 --- a/src/bindings/python/bt2/bt2/native_bt_value.i +++ b/src/bindings/python/bt2/bt2/native_bt_value.i @@ -7,7 +7,7 @@ %include %{ -#include "native_bt_value.i.h" +#include "native_bt_value.i.hpp" %} struct bt_value *bt_value_map_get_keys(const struct bt_value *map_obj); diff --git a/src/bindings/python/bt2/bt2/native_bt_value.i.h b/src/bindings/python/bt2/bt2/native_bt_value.i.h deleted file mode 100644 index c391aa9c..00000000 --- a/src/bindings/python/bt2/bt2/native_bt_value.i.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (c) 2016 Philippe Proulx - */ - -#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_VALUE_I_H -#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_VALUE_I_H - -struct bt_value_map_get_keys_data { - struct bt_value *keys; -}; - -static bt_value_map_foreach_entry_const_func_status bt_value_map_get_keys_cb( - const char *key, const struct bt_value *object, void *data) -{ - const auto priv_data = static_cast(data); - bt_value_array_append_element_status status = - bt_value_array_append_string_element(priv_data->keys, key); - - BT_ASSERT(status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK || - status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR); - return static_cast(status); -} - -static struct bt_value *bt_value_map_get_keys(const struct bt_value *map_obj) -{ - bt_value_map_foreach_entry_const_status status; - struct bt_value_map_get_keys_data data; - - data.keys = bt_value_array_create(); - if (!data.keys) { - return NULL; - } - - status = bt_value_map_foreach_entry_const(map_obj, bt_value_map_get_keys_cb, - &data); - if (status != __BT_FUNC_STATUS_OK) { - goto error; - } - - goto end; - -error: - if (data.keys) { - BT_VALUE_PUT_REF_AND_RESET(data.keys); - } - -end: - return data.keys; -} - -#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_VALUE_I_H */ diff --git a/src/bindings/python/bt2/bt2/native_bt_value.i.hpp b/src/bindings/python/bt2/bt2/native_bt_value.i.hpp new file mode 100644 index 00000000..f2a5709b --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_value.i.hpp @@ -0,0 +1,53 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2016 Philippe Proulx + */ + +#ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_VALUE_I_HPP +#define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_VALUE_I_HPP + +struct bt_value_map_get_keys_data +{ + struct bt_value *keys; +}; + +static bt_value_map_foreach_entry_const_func_status +bt_value_map_get_keys_cb(const char *key, const struct bt_value *object, void *data) +{ + const auto priv_data = static_cast(data); + bt_value_array_append_element_status status = + bt_value_array_append_string_element(priv_data->keys, key); + + BT_ASSERT(status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK || + status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR); + return static_cast(status); +} + +static struct bt_value *bt_value_map_get_keys(const struct bt_value *map_obj) +{ + bt_value_map_foreach_entry_const_status status; + struct bt_value_map_get_keys_data data; + + data.keys = bt_value_array_create(); + if (!data.keys) { + return NULL; + } + + status = bt_value_map_foreach_entry_const(map_obj, bt_value_map_get_keys_cb, &data); + if (status != __BT_FUNC_STATUS_OK) { + goto error; + } + + goto end; + +error: + if (data.keys) { + BT_VALUE_PUT_REF_AND_RESET(data.keys); + } + +end: + return data.keys; +} + +#endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_VALUE_I_HPP */ -- 2.34.1