Fix: bt2: autodisc: remove thread error while inserting status in map
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_autodisc.i.h
index aac92e129031fe77f2e26a01b583b5010e27506e..d6a4d24bbe1ae0d3f089ee6639ac8ca081111d43 100644 (file)
@@ -1,25 +1,7 @@
 /*
- * The MIT License (MIT)
+ * SPDX-License-Identifier: MIT
  *
  * Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
  */
 
 #include <autodisc/autodisc.h>
@@ -47,6 +29,7 @@
  * This function can also return None, if it failed to allocate memory
  * for the return value and status code.
  */
+static
 bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs,
                const bt_plugin_set *plugin_set)
 {
@@ -60,6 +43,7 @@ bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs,
        bt_value *components_list = NULL;
        bt_value *component_info = NULL;
        bt_value_map_insert_entry_status insert_entry_status;
+       const bt_error *error = NULL;
 
        BT_ASSERT(bt_value_get_type(inputs) == BT_VALUE_TYPE_ARRAY);
        for (i = 0; i < bt_value_array_get_length(inputs); i++) {
@@ -69,9 +53,10 @@ bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs,
 
        result = bt_value_map_create();
        if (!result) {
-               static const char * const err = "Failed to create a map value.";
-               BT_LOGE_STR(err);
-               BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, err);
+#define BT_FMT "Failed to create a map value."
+               BT_LOGE_STR(BT_FMT);
+               BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, BT_FMT);
+#undef BT_FMT
                PyErr_NoMemory();
                goto end;
        }
@@ -194,11 +179,23 @@ error:
 
 end:
        if (result) {
+               /*
+                * If an error happened, we must clear the error temporarily
+                * while we insert the status in the map.
+                */
+               error = bt_current_thread_take_error();
                insert_entry_status = bt_value_map_insert_signed_integer_entry(result, "status", status);
-               if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
+               if (insert_entry_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
+                       if (error) {
+                               bt_current_thread_move_error(error);
+                               error = NULL;
+                       }
+               } else {
                        BT_VALUE_PUT_REF_AND_RESET(result);
                        PyErr_NoMemory();
                }
+
+
        }
 
        auto_source_discovery_fini(&auto_disc);
@@ -206,5 +203,9 @@ end:
        bt_value_put_ref(components_list);
        bt_value_put_ref(component_info);
 
+       if (error) {
+               bt_error_release(error);
+       }
+
        return result;
 }
This page took 0.024801 seconds and 4 git commands to generate.