bt2: make bt_bt2_trace_{,class}_add_destruction_listener return a status
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 23 Jul 2019 04:11:44 +0000 (00:11 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 24 Jul 2019 04:18:54 +0000 (00:18 -0400)
If the calls to bt_trace_class_add_destruction_listener and
bt_trace_add_destruction_listener in the Python bindings fail (due to an
hypothetical memory error that can't happen today), abort is called.

Strangely, back on the Python side, there is a "listener_id is None"
check that is a bit useless, since we would have aborted had there been
a failure.

Regularize the situation by making the
bt_bt2_trace_class_add_destruction_listener and
bt_bt2_trace_add_destruction_listener wrappers return a status, like any
other API function.  We can then use utils._handle_func_status like
everywhere else.

Change-Id: I17901a0278f1615b45e6adc3b7223f1b3b3ff35e
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1744
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/bindings/python/bt2/bt2/native_bt_trace.i
src/bindings/python/bt2/bt2/native_bt_trace_class.i
src/bindings/python/bt2/bt2/trace.py
src/bindings/python/bt2/bt2/trace_class.py

index 3448f646492cec8ef54060119e8a5e4cb7c1ef7d..1ecc47204a483a9c8cf304232146563df2dc077c 100644 (file)
@@ -50,24 +50,22 @@ trace_destroyed_listener(const bt_trace *trace, void *py_callable)
        Py_XDECREF(py_res);
 }
 
-uint64_t bt_bt2_trace_add_destruction_listener(bt_trace *trace, PyObject *py_callable)
+int bt_bt2_trace_add_destruction_listener(bt_trace *trace,
+               PyObject *py_callable, uint64_t *id)
 {
-       uint64_t id = UINT64_C(-1);
        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) {
-               BT_LOGF_STR("Failed to add trace destruction listener.");
-               abort();
+               trace, trace_destroyed_listener, py_callable, id);
+       if (status == __BT_FUNC_STATUS_OK) {
+               Py_INCREF(py_callable);
        }
 
-       Py_INCREF(py_callable);
-       return id;
+       return status;
 }
 %}
 
-uint64_t bt_bt2_trace_add_destruction_listener(bt_trace *trace,
-       PyObject *py_callable);
+int bt_bt2_trace_add_destruction_listener(bt_trace *trace,
+               PyObject *py_callable, uint64_t *id);
index fd48deebdb32b38562a2d247d1514c604db1e6c1..60c3e7d82ce085d350ed1e4e455ef0db825cb731 100644 (file)
@@ -51,25 +51,24 @@ trace_class_destroyed_listener(const bt_trace_class *trace_class, void *py_calla
        Py_XDECREF(py_res);
 }
 
-uint64_t bt_bt2_trace_class_add_destruction_listener(bt_trace_class *trace_class,
-       PyObject *py_callable)
+int bt_bt2_trace_class_add_destruction_listener(
+               bt_trace_class *trace_class, PyObject *py_callable,
+               uint64_t *id)
 {
-       uint64_t id = UINT64_C(-1);
        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) {
-               BT_LOGF_STR("Failed to add trace class destruction listener.");
-               abort();
+               trace_class, trace_class_destroyed_listener, py_callable, id);
+       if (status == __BT_FUNC_STATUS_OK) {
+               Py_INCREF(py_callable);
        }
 
-       Py_INCREF(py_callable);
-       return id;
+       return status;
 }
 %}
 
-uint64_t bt_bt2_trace_class_add_destruction_listener(bt_trace_class *trace_class,
-       PyObject *py_callable);
+int bt_bt2_trace_class_add_destruction_listener(
+               bt_trace_class *trace_class, PyObject *py_callable,
+               uint64_t *id);
index 5ddcbad29719123404c7ffc8d09c3ce878c77b54..86bd90d05ae1d58844e9654a2f6009fe3eb0d3b1 100644 (file)
@@ -185,8 +185,9 @@ class _Trace(object._SharedObject, collections.abc.Mapping):
             _trace_destruction_listener_from_native, listener
         )
 
-        listener_id = fn(self._ptr, listener_from_native)
-        if listener_id is None:
-            utils._raise_bt2_error('cannot add destruction listener to trace object')
+        status, listener_id = fn(self._ptr, listener_from_native)
+        utils._handle_func_status(
+            status, 'cannot add destruction listener to trace object'
+        )
 
         return bt2._ListenerHandle(listener_id, self)
index 7e7c179770508058846e8b9295e6bff49c839520..8032ed117b88532f3728c252aef55384804de33a 100644 (file)
@@ -333,10 +333,9 @@ class _TraceClass(object._SharedObject, collections.abc.Mapping):
             _trace_class_destruction_listener_from_native, listener
         )
 
-        listener_id = fn(self._ptr, listener_from_native)
-        if listener_id is None:
-            utils._raise_bt2_error(
-                'cannot add destruction listener to trace class object'
-            )
+        status, listener_id = fn(self._ptr, listener_from_native)
+        utils._handle_func_status(
+            status, 'cannot add destruction listener to trace class object'
+        )
 
         return bt2._ListenerHandle(listener_id, self)
This page took 0.0264 seconds and 4 git commands to generate.