bt2: make bt2 add graph listener wrappers append error causes
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 23 Jul 2019 04:32:05 +0000 (00:32 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 24 Jul 2019 04:18:54 +0000 (00:18 -0400)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1745
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/bindings/python/bt2/bt2/graph.py
src/bindings/python/bt2/bt2/native_bt_graph.i
src/bindings/python/bt2/bt2/utils.py

index 1f280ba119ff4de691398980b9c4cd6e54f9eb2c..870ddb381956406c6e79b878c8baa8db178f2e22 100644 (file)
@@ -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):
index 13d635e9aad9a7efa809439e719f18fb6b0775c8..ee32e17fc62fb02c0da6b7c2894c20e831f4caa0 100644 (file)
@@ -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;
        }
 
index 2da3c31902ebc17d5d9b5adf1316a2dab52f1626..39b5f809dbd608e981885065a7a13c56c06fe0e5 100644 (file)
@@ -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)
 
This page took 0.027912 seconds and 4 git commands to generate.