From 981f33ddf743a4f38b959afb7afe66083ce0ed76 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 18 Oct 2019 23:57:30 -0400 Subject: [PATCH] bt2: make log functions clear error indicator In all call sites of logw_exception and loge_exception_append_cause, we clear the error indicator afterwards (call PyErr_Clear). Make these functions clear the error indicator themselves instead, and rename them accordingly. This could help avoid forgetting to clear the indicator in the future. Change-Id: I22855c1bb4af0e5b85bafc3f1009589ee08c0564 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/2225 Reviewed-by: Francis Deslauriers --- .../bt2/bt2/native_bt_component_class.i.h | 19 ++++++++----------- .../python/bt2/bt2/native_bt_graph.i.h | 6 ++---- .../bt2/bt2/native_bt_log_and_append_error.h | 6 ++++-- .../python/bt2/bt2/native_bt_trace.i.h | 3 +-- .../python/bt2/bt2/native_bt_trace_class.i.h | 3 +-- 5 files changed, 16 insertions(+), 21 deletions(-) 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 index 23e7d06a..a80d059d 100644 --- a/src/bindings/python/bt2/bt2/native_bt_component_class.i.h +++ b/src/bindings/python/bt2/bt2/native_bt_component_class.i.h @@ -505,16 +505,15 @@ void component_class_finalize(bt_self_component *self_component) bt_logging_level log_level = get_self_component_log_level( self_component); - BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, - "User component's _user_finalize() method raised an exception: ignoring:"); - logw_exception(log_level); - /* * Ignore any exception raised by the _user_finalize() method * because it won't change anything at this point: the component * is being destroyed anyway. */ - PyErr_Clear(); + 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; } @@ -1181,17 +1180,15 @@ void component_class_message_iterator_finalize( bt_logging_level log_level = get_self_component_log_level( self_comp); - BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp, - "User's _user_finalize() method raised an exception: ignoring:"); - logw_exception(get_self_message_iterator_log_level( - message_iterator)); - /* * Ignore any exception raised by the _user_finalize() method * because it won't change anything at this point: the component * is being destroyed anyway. */ - PyErr_Clear(); + 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); diff --git a/src/bindings/python/bt2/bt2/native_bt_graph.i.h b/src/bindings/python/bt2/bt2/native_bt_graph.i.h index 9725b957..e310e53b 100644 --- a/src/bindings/python/bt2/bt2/native_bt_graph.i.h +++ b/src/bindings/python/bt2/bt2/native_bt_graph.i.h @@ -60,10 +60,9 @@ static bt_graph_listener_func_status port_added_listener( py_res = PyObject_CallFunction(py_callable, "(OiOi)", py_component_ptr, component_class_type, py_port_ptr, port_type); if (!py_res) { - loge_exception_append_cause( + loge_exception_append_cause_clear( "Graph's port added listener (Python)", BT_LOG_OUTPUT_LEVEL); - PyErr_Clear(); status = __BT_FUNC_STATUS_ERROR; goto end; } @@ -305,10 +304,9 @@ bt_graph_listener_func_status ports_connected_listener( py_downstream_component_ptr, downstream_component_class_type, py_downstream_port_ptr); if (!py_res) { - loge_exception_append_cause( + loge_exception_append_cause_clear( "Graph's port connected listener (Python)", BT_LOG_OUTPUT_LEVEL); - PyErr_Clear(); status = __BT_FUNC_STATUS_ERROR; goto end; } diff --git a/src/bindings/python/bt2/bt2/native_bt_log_and_append_error.h b/src/bindings/python/bt2/bt2/native_bt_log_and_append_error.h index 11b6e414..77b4c77a 100644 --- a/src/bindings/python/bt2/bt2/native_bt_log_and_append_error.h +++ b/src/bindings/python/bt2/bt2/native_bt_log_and_append_error.h @@ -228,15 +228,17 @@ bt_logging_level get_self_message_iterator_log_level( } static inline -void loge_exception_append_cause(const char *module_name, int active_log_level) +void loge_exception_append_cause_clear(const char *module_name, int active_log_level) { log_exception_and_maybe_append_cause(BT_LOG_ERROR, active_log_level, true, NULL, NULL, NULL, module_name); + PyErr_Clear(); } static inline -void logw_exception(int active_log_level) +void logw_exception_clear(int active_log_level) { log_exception_and_maybe_append_cause(BT_LOG_WARNING, active_log_level, false, NULL, NULL, NULL, NULL); + PyErr_Clear(); } diff --git a/src/bindings/python/bt2/bt2/native_bt_trace.i.h b/src/bindings/python/bt2/bt2/native_bt_trace.i.h index c525fa11..e8cd75b7 100644 --- a/src/bindings/python/bt2/bt2/native_bt_trace.i.h +++ b/src/bindings/python/bt2/bt2/native_bt_trace.i.h @@ -37,8 +37,7 @@ trace_destroyed_listener(const bt_trace *trace, void *py_callable) py_res = PyObject_CallFunction(py_callable, "(O)", py_trace_ptr); if (!py_res) { - logw_exception(BT_LOG_OUTPUT_LEVEL); - PyErr_Clear(); + logw_exception_clear(BT_LOG_OUTPUT_LEVEL); goto end; } 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 index 329fda65..1e1eb3c1 100644 --- a/src/bindings/python/bt2/bt2/native_bt_trace_class.i.h +++ b/src/bindings/python/bt2/bt2/native_bt_trace_class.i.h @@ -37,8 +37,7 @@ trace_class_destroyed_listener(const bt_trace_class *trace_class, void *py_calla py_res = PyObject_CallFunction(py_callable, "(O)", py_trace_class_ptr); if (!py_res) { - logw_exception(BT_LOG_OUTPUT_LEVEL); - PyErr_Clear(); + logw_exception_clear(BT_LOG_OUTPUT_LEVEL); goto end; } -- 2.34.1