From 416379bc0706641778bba7efbebaa5c6027e81b8 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 23 Jul 2019 00:32:05 -0400 Subject: [PATCH] bt2: make bt2 add graph listener wrappers append error causes If bt_bt2_graph_add_port_added_listener or bt_bt2_graph_add_ports_connected_listener fail, it can be either because one of the calls to the Babeltrace API fails or a Python object creation fails. On the Python side, we raise a bt2.Error on failure. This seems fine, although in a subsequent patch, it will become a pre-condition for raising bt2.Error that we have an error set for the current thread. Add some calls to BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN in the errors paths of these wrappers that don't come from the library, i.e. on which there would not already be an error cause appended. On the Python side, use bt2.Error directly instead of utils._raise_bt2_error. Since these were the last call sites of utils._raise_bt2_error, remove it. Change-Id: I1556b626a941e9cc127fb036d6e3b9b6346fde88 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/1745 Reviewed-by: Philippe Proulx Tested-by: jenkins --- src/bindings/python/bt2/bt2/graph.py | 6 +- src/bindings/python/bt2/bt2/native_bt_graph.i | 56 +++++++++++++++++++ src/bindings/python/bt2/bt2/utils.py | 7 --- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/bindings/python/bt2/bt2/graph.py b/src/bindings/python/bt2/bt2/graph.py index 1f280ba1..870ddb38 100644 --- a/src/bindings/python/bt2/bt2/graph.py +++ b/src/bindings/python/bt2/bt2/graph.py @@ -147,7 +147,8 @@ class Graph(object._SharedObject): listener_ids = fn(self._ptr, listener_from_native) if listener_ids is None: - utils._raise_bt2_error('cannot add listener to graph object') + raise bt2.Error('cannot add listener to graph object') + return bt2._ListenerHandle(listener_ids, self) def add_ports_connected_listener(self, listener): @@ -161,7 +162,8 @@ class Graph(object._SharedObject): listener_ids = fn(self._ptr, listener_from_native) if listener_ids is None: - utils._raise_bt2_error('cannot add listener to graph object') + raise bt2.Error('cannot add listener to graph object') + return bt2._ListenerHandle(listener_ids, self) def run(self): diff --git a/src/bindings/python/bt2/bt2/native_bt_graph.i b/src/bindings/python/bt2/bt2/native_bt_graph.i index 13d635e9..ee32e17f 100644 --- a/src/bindings/python/bt2/bt2/native_bt_graph.i +++ b/src/bindings/python/bt2/bt2/native_bt_graph.i @@ -209,6 +209,8 @@ PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, PyObject *py_listener_id = NULL; int 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); @@ -219,6 +221,8 @@ PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, */ 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; } @@ -227,11 +231,17 @@ PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, graph, source_component_output_port_added_listener, graph_listener_removed, 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_FromLong(listener_id); if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); goto error; } @@ -243,11 +253,17 @@ PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, graph, filter_component_input_port_added_listener, graph_listener_removed, 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_FromLong(listener_id); if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); goto error; } @@ -259,11 +275,17 @@ PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, graph, filter_component_output_port_added_listener, graph_listener_removed, 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_FromLong(listener_id); if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); goto error; } @@ -275,11 +297,17 @@ PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, graph, sink_component_input_port_added_listener, graph_listener_removed, 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_FromLong(listener_id); if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); goto error; } @@ -448,6 +476,8 @@ PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph, PyObject *py_listener_id = NULL; int listener_id; bt_graph_add_listener_status status; + const char * const module_name = + "graph_add_ports_connected_listener() (Python)"; BT_ASSERT(graph); BT_ASSERT(py_callable); @@ -456,6 +486,8 @@ PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph, * 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; } @@ -464,11 +496,17 @@ PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph, graph, source_filter_component_ports_connected_listener, graph_listener_removed, py_callable, &listener_id); if (status != __BT_FUNC_STATUS_OK) { + /* + * bt_graph_add_source_filter_component_ports_connected_listener + * has already logged/appended an error cause. + */ goto error; } py_listener_id = PyLong_FromLong(listener_id); if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); goto error; } @@ -480,11 +518,17 @@ PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph, graph, source_sink_component_ports_connected_listener, graph_listener_removed, py_callable, &listener_id); if (status != __BT_FUNC_STATUS_OK) { + /* + * bt_graph_add_source_sink_component_ports_connected_listener + * has already logged/appended an error cause. + */ goto error; } py_listener_id = PyLong_FromLong(listener_id); if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); goto error; } @@ -496,11 +540,17 @@ PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph, graph, filter_filter_component_ports_connected_listener, graph_listener_removed, py_callable, &listener_id); if (status != __BT_FUNC_STATUS_OK) { + /* + * bt_graph_add_filter_filter_component_ports_connected_listener + * has already logged/appended an error cause. + */ goto error; } py_listener_id = PyLong_FromLong(listener_id); if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); goto error; } @@ -512,11 +562,17 @@ PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph, graph, filter_sink_component_ports_connected_listener, graph_listener_removed, py_callable, &listener_id); if (status != __BT_FUNC_STATUS_OK) { + /* + * bt_graph_add_filter_sink_component_ports_connected_listener + * has already logged/appended an error cause. + */ goto error; } py_listener_id = PyLong_FromLong(listener_id); if (!py_listener_id) { + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, + "Failed to allocate one PyLong."); goto error; } diff --git a/src/bindings/python/bt2/bt2/utils.py b/src/bindings/python/bt2/bt2/utils.py index 2da3c319..39b5f809 100644 --- a/src/bindings/python/bt2/bt2/utils.py +++ b/src/bindings/python/bt2/bt2/utils.py @@ -113,13 +113,6 @@ def _check_alignment(a): raise ValueError('{} is not a power of two'.format(a)) -def _raise_bt2_error(msg): - if msg is None: - raise bt2.Error - else: - raise bt2.Error(msg) - - def _check_log_level(log_level): _check_int(log_level) -- 2.34.1