From ee2cad25d5c65cd781169c897a53310cee9948fe Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 23 Jul 2019 00:11:44 -0400 Subject: [PATCH] bt2: make bt_bt2_trace_{,class}_add_destruction_listener return a status 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 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1744 Tested-by: jenkins Reviewed-by: Philippe Proulx --- src/bindings/python/bt2/bt2/native_bt_trace.i | 18 +++++++--------- .../python/bt2/bt2/native_bt_trace_class.i | 21 +++++++++---------- src/bindings/python/bt2/bt2/trace.py | 7 ++++--- src/bindings/python/bt2/bt2/trace_class.py | 9 ++++---- 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/bindings/python/bt2/bt2/native_bt_trace.i b/src/bindings/python/bt2/bt2/native_bt_trace.i index 3448f646..1ecc4720 100644 --- a/src/bindings/python/bt2/bt2/native_bt_trace.i +++ b/src/bindings/python/bt2/bt2/native_bt_trace.i @@ -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); 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 fd48deeb..60c3e7d8 100644 --- a/src/bindings/python/bt2/bt2/native_bt_trace_class.i +++ b/src/bindings/python/bt2/bt2/native_bt_trace_class.i @@ -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); diff --git a/src/bindings/python/bt2/bt2/trace.py b/src/bindings/python/bt2/bt2/trace.py index 5ddcbad2..86bd90d0 100644 --- a/src/bindings/python/bt2/bt2/trace.py +++ b/src/bindings/python/bt2/bt2/trace.py @@ -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) diff --git a/src/bindings/python/bt2/bt2/trace_class.py b/src/bindings/python/bt2/bt2/trace_class.py index 7e7c1797..8032ed11 100644 --- a/src/bindings/python/bt2/bt2/trace_class.py +++ b/src/bindings/python/bt2/bt2/trace_class.py @@ -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) -- 2.34.1