bt2: normalize the code to use some commonly used patterns
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i.h
index 479f9abf737595e2c738e0e43ac70d57ca08aca0..23e7d06a0dbcc5f1c831fa7b1bff588f490c2ef7 100644 (file)
@@ -23,6 +23,7 @@
  */
 
 #include "logging/comp-logging.h"
+#include "compat/glib.h"
 
 /*
  * This hash table associates a BT component class object address to a
 
 static GHashTable *bt_cc_ptr_to_py_cls;
 
+static
+void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class *comp_cls)
+{
+       gboolean existed;
+
+       if (!bt_cc_ptr_to_py_cls) {
+               return;
+       }
+
+       existed = g_hash_table_remove(bt_cc_ptr_to_py_cls, comp_cls);
+       BT_ASSERT(existed);
+}
+
 static
 void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
                PyObject *py_cls)
@@ -85,19 +99,22 @@ void native_comp_class_dtor(void) {
        if (bt_cc_ptr_to_py_cls) {
                BT_LOGD_STR("Destroying native component class to Python component class hash table.");
                g_hash_table_destroy(bt_cc_ptr_to_py_cls);
+               bt_cc_ptr_to_py_cls = NULL;
        }
 }
 
 static inline
-int py_exc_to_status(bt_self_component_class *self_component_class,
+int py_exc_to_status_clear(
+               bt_self_component_class *self_component_class,
                bt_self_component *self_component,
                bt_self_message_iterator *self_message_iterator,
                const char *module_name, int active_log_level)
 {
-       int status = __BT_FUNC_STATUS_OK;
+       int status;
        PyObject *exc = PyErr_Occurred();
 
        if (!exc) {
+               status = __BT_FUNC_STATUS_OK;
                goto end;
        }
 
@@ -129,7 +146,7 @@ int py_exc_to_status(bt_self_component_class *self_component_class,
                }
 
                BT_ASSERT(active_log_level != -1);
-               log_exception_and_maybe_append_error(BT_LOG_WARNING,
+               log_exception_and_maybe_append_cause(BT_LOG_WARNING,
                        active_log_level, true,
                        self_component_class, self_component,
                        self_message_iterator, module_name);
@@ -148,31 +165,37 @@ end:
 }
 
 static
-int py_exc_to_status_component_class(
+int py_exc_to_status_component_class_clear(
                bt_self_component_class *self_component_class,
                int active_log_level)
 {
-       return py_exc_to_status(self_component_class, NULL, NULL, NULL,
+       return py_exc_to_status_clear(self_component_class, NULL, NULL, NULL,
                active_log_level);
 }
 
 static
-int py_exc_to_status_component(bt_self_component *self_component)
+int py_exc_to_status_component_clear(bt_self_component *self_component)
 {
-       return py_exc_to_status(NULL, self_component, NULL, NULL, -1);
+       return py_exc_to_status_clear(NULL, self_component, NULL, NULL, -1);
 }
 
 static
-int py_exc_to_status_message_iterator(
+int py_exc_to_status_message_iterator_clear(
                bt_self_message_iterator *self_message_iterator)
 {
-       return py_exc_to_status(NULL, NULL, self_message_iterator, NULL, -1);
+       return py_exc_to_status_clear(NULL, NULL, self_message_iterator, NULL, -1);
+}
+
+static
+bool bt_bt2_is_python_component_class(const bt_component_class *comp_cls)
+{
+       return bt_g_hash_table_contains(bt_cc_ptr_to_py_cls, comp_cls);
 }
 
 /* Component class proxy methods (delegate to the attached Python object) */
 
 static
-bt_component_class_init_method_status component_class_init(
+bt_component_class_initialize_method_status component_class_init(
                bt_self_component *self_component,
                void *self_component_v,
                swig_type_info *self_comp_cls_type_swig_type,
@@ -181,7 +204,7 @@ bt_component_class_init_method_status component_class_init(
 {
        const bt_component *component = bt_self_component_as_component(self_component);
        const bt_component_class *component_class = bt_component_borrow_class_const(component);
-       bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
+       bt_component_class_initialize_method_status status;
        PyObject *py_cls = NULL;
        PyObject *py_comp = NULL;
        PyObject *py_params_ptr = NULL;
@@ -189,8 +212,6 @@ bt_component_class_init_method_status component_class_init(
        bt_logging_level log_level = get_self_component_log_level(
                self_component);
 
-       (void) init_method_data;
-
        BT_ASSERT(self_component);
        BT_ASSERT(self_component_v);
        BT_ASSERT(self_comp_cls_type_swig_type);
@@ -228,18 +249,26 @@ bt_component_class_init_method_status component_class_init(
        /*
         * Do the equivalent of this:
         *
-        *     py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
+        *     py_comp = py_cls._bt_init_from_native(py_comp_ptr,
+        *         py_params_ptr, init_method_data ? init_method_data : Py_None)
         *
         * _UserComponentType._bt_init_from_native() calls the Python
         * component object's __init__() function.
+        *
+        * We don't take any reference on `init_method_data` which, if
+        * not `NULL`, is assumed to be a `PyObject *`: the user's
+        * __init__() function will eventually take a reference if
+        * needed. If `init_method_data` is `NULL`, then we pass
+        * `Py_None` as the initialization's Python object.
         */
        py_comp = PyObject_CallMethod(py_cls,
-               "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
+               "_bt_init_from_native", "(OOO)", py_comp_ptr, py_params_ptr,
+               init_method_data ? init_method_data : Py_None);
        if (!py_comp) {
                BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component,
                        "Failed to call Python class's _bt_init_from_native() method: "
                        "py-cls-addr=%p", py_cls);
-               status = py_exc_to_status_component(self_component);
+               status = py_exc_to_status_component_clear(self_component);
                goto end;
        }
 
@@ -250,34 +279,180 @@ bt_component_class_init_method_status component_class_init(
         */
        bt_self_component_set_data(self_component, py_comp);
        py_comp = NULL;
+
+       status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
+
        goto end;
 
 error:
-       status = __BT_FUNC_STATUS_ERROR;
+       /* This error path is for non-Python errors only. */
+       status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
+
+end:
+       BT_ASSERT(!PyErr_Occurred());
+       Py_XDECREF(py_comp);
+       Py_XDECREF(py_params_ptr);
+       Py_XDECREF(py_comp_ptr);
+       return status;
+}
+
+static
+bt_component_class_get_supported_mip_versions_method_status
+component_class_get_supported_mip_versions(
+               const bt_component_class *component_class,
+               bt_self_component_class *self_component_class,
+               const bt_value *params, void *init_method_data,
+               bt_logging_level log_level,
+               bt_integer_range_set_unsigned *supported_versions)
+{
+       uint64_t i;
+       PyObject *py_cls = NULL;
+       PyObject *py_params_ptr = NULL;
+       PyObject *py_range_set_addr = NULL;
+       bt_integer_range_set_unsigned *ret_range_set = NULL;
+       bt_component_class_get_supported_mip_versions_method_status status;
+
+       py_cls = lookup_cc_ptr_to_py_cls(component_class);
+       if (!py_cls) {
+               BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
+                       "Cannot find Python class associated to native component class: "
+                       "comp-cls-addr=%p", component_class);
+               goto error;
+       }
+
+       py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
+               SWIGTYPE_p_bt_value, 0);
+       if (!py_params_ptr) {
+               BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
+                       "Failed to create a SWIG pointer object.");
+               goto error;
+       }
 
        /*
-        * Clear any exception: we're returning a bad status anyway. If
-        * this call originated from Python (creation from a plugin's
-        * component class, for example), then the user gets an
-        * appropriate creation error.
+        * We don't take any reference on `init_method_data` which, if
+        * not `NULL`, is assumed to be a `PyObject *`: the user's
+        * _user_get_supported_mip_versions() function will eventually
+        * take a reference if needed. If `init_method_data` is `NULL`,
+        * then we pass `Py_None` as the initialization's Python object.
         */
-       PyErr_Clear();
+       py_range_set_addr = PyObject_CallMethod(py_cls,
+               "_bt_get_supported_mip_versions_from_native", "(OOi)",
+               py_params_ptr, init_method_data ? init_method_data : Py_None,
+               (int) log_level);
+       if (!py_range_set_addr) {
+               BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
+                       "Failed to call Python class's _bt_get_supported_mip_versions_from_native() method: "
+                       "py-cls-addr=%p", py_cls);
+               status = py_exc_to_status_component_class_clear(self_component_class,
+                       log_level);
+               goto end;
+       }
+
+       /*
+        * The returned object, on success, is an integer object
+        * (PyLong) containing the address of a BT unsigned integer
+        * range set object (new reference).
+        */
+       ret_range_set = PyLong_AsVoidPtr(py_range_set_addr);
+       BT_ASSERT(!PyErr_Occurred());
+       BT_ASSERT(ret_range_set);
+
+       /* Copy returned ranges to input range set */
+       for (i = 0; i < bt_integer_range_set_get_range_count(
+                       bt_integer_range_set_unsigned_as_range_set_const(ret_range_set));
+                       i++) {
+               const bt_integer_range_unsigned *range =
+                       bt_integer_range_set_unsigned_borrow_range_by_index_const(
+                               ret_range_set, i);
+               bt_integer_range_set_add_range_status add_range_status;
+
+               add_range_status = bt_integer_range_set_unsigned_add_range(
+                       supported_versions,
+                       bt_integer_range_unsigned_get_lower(range),
+                       bt_integer_range_unsigned_get_upper(range));
+               if (add_range_status) {
+                       BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
+                               "Failed to add range to supported MIP versions range set.");
+                       goto error;
+               }
+       }
+
+       status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK;
+
+       goto end;
+
+error:
+       /* This error path is for non-Python errors only. */
+       status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR;
 
 end:
-       Py_XDECREF(py_comp);
+       BT_ASSERT(!PyErr_Occurred());
        Py_XDECREF(py_params_ptr);
-       Py_XDECREF(py_comp_ptr);
+       Py_XDECREF(py_range_set_addr);
+       bt_integer_range_set_unsigned_put_ref(ret_range_set);
        return status;
 }
 
+static
+bt_component_class_get_supported_mip_versions_method_status
+component_class_source_get_supported_mip_versions(
+               bt_self_component_class_source *self_component_class_source,
+               const bt_value *params, void *init_method_data,
+               bt_logging_level log_level,
+               bt_integer_range_set_unsigned *supported_versions)
+{
+       const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
+       const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
+       bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
+
+       return component_class_get_supported_mip_versions(
+               component_class, self_component_class,
+               params, init_method_data, log_level, supported_versions);
+}
+
+static
+bt_component_class_get_supported_mip_versions_method_status
+component_class_filter_get_supported_mip_versions(
+               bt_self_component_class_filter *self_component_class_filter,
+               const bt_value *params, void *init_method_data,
+               bt_logging_level log_level,
+               bt_integer_range_set_unsigned *supported_versions)
+{
+       const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
+       const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
+       bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
+
+       return component_class_get_supported_mip_versions(
+               component_class, self_component_class,
+               params, init_method_data, log_level, supported_versions);
+}
+
+static
+bt_component_class_get_supported_mip_versions_method_status
+component_class_sink_get_supported_mip_versions(
+               bt_self_component_class_sink *self_component_class_sink,
+               const bt_value *params, void *init_method_data,
+               bt_logging_level log_level,
+               bt_integer_range_set_unsigned *supported_versions)
+{
+       const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
+       const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
+       bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
+
+       return component_class_get_supported_mip_versions(
+               component_class, self_component_class,
+               params, init_method_data, log_level, supported_versions);
+}
+
 /*
  * Method of bt_component_class_source to initialize a bt_self_component_source
  * of that class.
  */
 
 static
-bt_component_class_init_method_status component_class_source_init(
+bt_component_class_initialize_method_status component_class_source_init(
                bt_self_component_source *self_component_source,
+               bt_self_component_source_configuration *config,
                const bt_value *params, void *init_method_data)
 {
        bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
@@ -289,8 +464,9 @@ bt_component_class_init_method_status component_class_source_init(
 }
 
 static
-bt_component_class_init_method_status component_class_filter_init(
+bt_component_class_initialize_method_status component_class_filter_init(
                bt_self_component_filter *self_component_filter,
+               bt_self_component_filter_configuration *config,
                const bt_value *params, void *init_method_data)
 {
        bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
@@ -302,8 +478,9 @@ bt_component_class_init_method_status component_class_filter_init(
 }
 
 static
-bt_component_class_init_method_status component_class_sink_init(
+bt_component_class_initialize_method_status component_class_sink_init(
                bt_self_component_sink *self_component_sink,
+               bt_self_component_sink_configuration *config,
                const bt_value *params, void *init_method_data)
 {
        bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
@@ -318,27 +495,32 @@ static
 void component_class_finalize(bt_self_component *self_component)
 {
        PyObject *py_comp = bt_self_component_get_data(self_component);
+       PyObject *py_method_result;
+
        BT_ASSERT(py_comp);
 
        /* Call user's _user_finalize() method */
-       PyObject *py_method_result = PyObject_CallMethod(py_comp,
-               "_user_finalize", NULL);
-
-       if (PyErr_Occurred()) {
+       py_method_result = PyObject_CallMethod(py_comp, "_user_finalize", NULL);
+       if (!py_method_result) {
                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();
+               goto end;
        }
 
-       /*
-        * 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_ASSERT(py_method_result == Py_None);
+
+end:
        Py_XDECREF(py_method_result);
        Py_DECREF(py_comp);
 }
@@ -365,34 +547,33 @@ void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
 }
 
 static
-bt_bool component_class_can_seek_beginning(
-               bt_self_message_iterator *self_message_iterator)
+bt_component_class_message_iterator_can_seek_beginning_method_status
+component_class_can_seek_beginning(
+               bt_self_message_iterator *self_message_iterator, bt_bool *can_seek)
 {
        PyObject *py_iter;
        PyObject *py_result = NULL;
-       bt_bool can_seek_beginning = false;
-
+       bt_component_class_message_iterator_can_seek_beginning_method_status status;
        py_iter = bt_self_message_iterator_get_data(self_message_iterator);
+
        BT_ASSERT(py_iter);
 
-       py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
+       py_result = PyObject_CallMethod(py_iter,
+               "_bt_can_seek_beginning_from_native", NULL);
+       if (!py_result) {
+               status = py_exc_to_status_message_iterator_clear(self_message_iterator);
+               goto end;
+       }
 
-       BT_ASSERT(!py_result || PyBool_Check(py_result));
+       BT_ASSERT(PyBool_Check(py_result));
+       *can_seek = PyObject_IsTrue(py_result);
 
-       if (py_result) {
-               can_seek_beginning = PyObject_IsTrue(py_result);
-       } else {
-               /*
-                * Once can_seek_beginning can report errors, convert the
-                * exception to a status.  For now, log and return false;
-                */
-               loge_exception_message_iterator(self_message_iterator);
-               PyErr_Clear();
-       }
+       status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
 
+end:
        Py_XDECREF(py_result);
 
-       return can_seek_beginning;
+       return status;
 }
 
 static
@@ -405,11 +586,84 @@ component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
 
        py_iter = bt_self_message_iterator_get_data(self_message_iterator);
        BT_ASSERT(py_iter);
-       py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
+
+       py_result = PyObject_CallMethod(py_iter,
+               "_bt_seek_beginning_from_native",
                NULL);
-       BT_ASSERT(!py_result || py_result == Py_None);
-       status = py_exc_to_status_message_iterator(self_message_iterator);
+       if (!py_result) {
+               status = py_exc_to_status_message_iterator_clear(self_message_iterator);
+               goto end;
+       }
+
+       BT_ASSERT(py_result == Py_None);
+
+       status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
+
+end:
+       Py_XDECREF(py_result);
+
+       return status;
+}
+
+static
+bt_component_class_message_iterator_can_seek_ns_from_origin_method_status
+component_class_can_seek_ns_from_origin(
+               bt_self_message_iterator *self_message_iterator,
+               int64_t ns_from_origin, bt_bool *can_seek)
+{
+       PyObject *py_iter;
+       PyObject *py_result = NULL;
+       bt_component_class_message_iterator_can_seek_ns_from_origin_method_status status;
+
+       py_iter = bt_self_message_iterator_get_data(self_message_iterator);
+       BT_ASSERT(py_iter);
+
+       py_result = PyObject_CallMethod(py_iter,
+               "_bt_can_seek_ns_from_origin_from_native", "L", ns_from_origin);
+       if (!py_result) {
+               status = py_exc_to_status_message_iterator_clear(self_message_iterator);
+               goto end;
+       }
+
+       BT_ASSERT(PyBool_Check(py_result));
+       *can_seek = PyObject_IsTrue(py_result);
+
+       status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
+
+end:
        Py_XDECREF(py_result);
+
+       return status;
+}
+
+static
+bt_component_class_message_iterator_seek_ns_from_origin_method_status
+component_class_seek_ns_from_origin(
+               bt_self_message_iterator *self_message_iterator,
+               int64_t ns_from_origin)
+{
+       PyObject *py_iter;
+       PyObject *py_result;
+       bt_component_class_message_iterator_seek_ns_from_origin_method_status status;
+
+       py_iter = bt_self_message_iterator_get_data(self_message_iterator);
+       BT_ASSERT(py_iter);
+
+       py_result = PyObject_CallMethod(py_iter,
+               "_bt_seek_ns_from_origin_from_native", "L", ns_from_origin);
+       if (!py_result) {
+               status = py_exc_to_status_message_iterator_clear(self_message_iterator);
+               goto end;
+       }
+
+
+       BT_ASSERT(py_result == Py_None);
+
+       status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
+
+end:
+       Py_XDECREF(py_result);
+
        return status;
 }
 
@@ -447,18 +701,26 @@ bt_component_class_port_connected_method_status component_class_port_connected(
                BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
                        "Failed to create a SWIG pointer object.");
                status = __BT_FUNC_STATUS_MEMORY_ERROR;
-               goto end;       }
+               goto end;
+       }
 
        py_method_result = PyObject_CallMethod(py_comp,
                "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
                self_component_port_type, py_other_port_ptr);
-       BT_ASSERT(!py_method_result || py_method_result == Py_None);
-       status = py_exc_to_status_component(self_component);
+       if (!py_method_result) {
+               status = py_exc_to_status_component_clear(self_component);
+               goto end;
+       }
+
+       BT_ASSERT(py_method_result == Py_None);
+
+       status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
 
 end:
        Py_XDECREF(py_self_port_ptr);
        Py_XDECREF(py_other_port_ptr);
        Py_XDECREF(py_method_result);
+
        return status;
 }
 
@@ -541,14 +803,23 @@ component_class_sink_graph_is_configured(
 {
        PyObject *py_comp = NULL;
        PyObject *py_method_result = NULL;
-       bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
+       bt_component_class_sink_graph_is_configured_method_status status;
        bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
 
        py_comp = bt_self_component_get_data(self_component);
+
        py_method_result = PyObject_CallMethod(py_comp,
                "_bt_graph_is_configured_from_native", NULL);
-       BT_ASSERT(!py_method_result || py_method_result == Py_None);
-       status = py_exc_to_status_component(self_component);
+       if (!py_method_result) {
+               status = py_exc_to_status_component_clear(self_component);
+               goto end;
+       }
+
+       BT_ASSERT(py_method_result == Py_None);
+
+       status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;;
+
+end:
        Py_XDECREF(py_method_result);
        return status;
 }
@@ -558,7 +829,7 @@ bt_component_class_query_method_status component_class_query(
                const bt_component_class *component_class,
                bt_self_component_class *self_component_class,
                bt_private_query_executor *priv_query_executor,
-               const char *object, const bt_value *params,
+               const char *object, const bt_value *params, void *method_data,
                const bt_value **result)
 {
        PyObject *py_cls = NULL;
@@ -574,6 +845,14 @@ bt_component_class_query_method_status component_class_query(
        bt_logging_level log_level =
                bt_query_executor_get_logging_level(query_exec);
 
+       /*
+        * If there's any `method_data`, assume this component class is
+        * getting queried from Python, so that `method_data` is a
+        * Python object to pass to the user's _user_query() method.
+        */
+       BT_ASSERT(!method_data ||
+                       bt_bt2_is_python_component_class(component_class));
+
        py_cls = lookup_cc_ptr_to_py_cls(component_class);
        if (!py_cls) {
                BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
@@ -606,15 +885,28 @@ bt_component_class_query_method_status component_class_query(
                goto error;
        }
 
+       /*
+        * We don't take any reference on `method_data` which, if not
+        * `NULL`, is assumed to be a `PyObject *`: the user's
+        * _user_query() function will eventually take a reference if
+        * needed. If `method_data` is `NULL`, then we pass `Py_None` as
+        * the initialization's Python object.
+        */
        py_results_addr = PyObject_CallMethod(py_cls,
-               "_bt_query_from_native", "(OOO)", py_priv_query_exec_ptr,
-               py_object, py_params_ptr);
+               "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr,
+               py_object, py_params_ptr,
+               method_data ? method_data : Py_None);
        if (!py_results_addr) {
-               BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
-                       "Failed to call Python class's _bt_query_from_native() method: "
-                       "py-cls-addr=%p", py_cls);
-               status = py_exc_to_status_component_class(self_component_class,
+               status = py_exc_to_status_component_class_clear(self_component_class,
                        log_level);
+               if (status < 0) {
+                       static const char *fmt =
+                               "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p";
+                       BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
+                               fmt, py_cls);
+                       BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
+                               self_component_class, fmt, py_cls);
+               }
                goto end;
        }
 
@@ -645,7 +937,7 @@ static
 bt_component_class_query_method_status component_class_source_query(
                bt_self_component_class_source *self_component_class_source,
                bt_private_query_executor *priv_query_executor,
-               const char *object, const bt_value *params,
+               const char *object, const bt_value *params, void *method_data,
                const bt_value **result)
 {
        const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
@@ -653,14 +945,14 @@ bt_component_class_query_method_status component_class_source_query(
        bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
 
        return component_class_query(component_class, self_component_class,
-               priv_query_executor, object, params, result);
+               priv_query_executor, object, params, method_data, result);
 }
 
 static
 bt_component_class_query_method_status component_class_filter_query(
                bt_self_component_class_filter *self_component_class_filter,
                bt_private_query_executor *priv_query_executor,
-               const char *object, const bt_value *params,
+               const char *object, const bt_value *params, void *method_data,
                const bt_value **result)
 {
        const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
@@ -668,14 +960,14 @@ bt_component_class_query_method_status component_class_filter_query(
        bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
 
        return component_class_query(component_class, self_component_class,
-               priv_query_executor, object, params, result);
+               priv_query_executor, object, params, method_data, result);
 }
 
 static
 bt_component_class_query_method_status component_class_sink_query(
                bt_self_component_class_sink *self_component_class_sink,
                bt_private_query_executor *priv_query_executor,
-               const char *object, const bt_value *params,
+               const char *object, const bt_value *params, void *method_data,
                const bt_value **result)
 {
        const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
@@ -683,20 +975,22 @@ bt_component_class_query_method_status component_class_sink_query(
        bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
 
        return component_class_query(component_class, self_component_class,
-               priv_query_executor, object, params, result);
+               priv_query_executor, object, params, method_data, result);
 }
 
 static
-bt_component_class_message_iterator_init_method_status
+bt_component_class_message_iterator_initialize_method_status
 component_class_message_iterator_init(
                bt_self_message_iterator *self_message_iterator,
+               bt_self_message_iterator_configuration *config,
                bt_self_component *self_component,
                bt_self_component_port_output *self_component_port_output)
 {
-       bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
+       bt_component_class_message_iterator_initialize_method_status status = __BT_FUNC_STATUS_OK;
        PyObject *py_comp_cls = NULL;
        PyObject *py_iter_cls = NULL;
        PyObject *py_iter_ptr = NULL;
+       PyObject *py_config_ptr = NULL;
        PyObject *py_component_port_output_ptr = NULL;
        PyObject *py_init_method_result = NULL;
        PyObject *py_iter = NULL;
@@ -751,15 +1045,27 @@ component_class_message_iterator_init(
        /*
         * Initialize object:
         *
-        *     py_iter.__init__(self_output_port)
+        *     py_iter.__init__(config, self_output_port)
         *
-        * through the _init_for_native helper static method.
+        * through the _init_from_native helper static method.
         *
         * At this point, py_iter._ptr is set, so this initialization
         * function has access to self._component (which gives it the
         * user Python component object from which the iterator was
         * created).
         */
+       py_config_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(config),
+               SWIGTYPE_p_bt_self_message_iterator_configuration, 0);
+       if (!py_config_ptr) {
+               const char *err = "Failed to create a SWIG pointer object";
+
+               BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
+                       "%s", err);
+               BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
+                       self_message_iterator, err);
+               goto error;
+       }
+
        py_component_port_output_ptr = SWIG_NewPointerObj(
                SWIG_as_voidptr(self_component_port_output),
                SWIGTYPE_p_bt_self_component_port_output, 0);
@@ -774,7 +1080,8 @@ component_class_message_iterator_init(
        }
 
        py_init_method_result = PyObject_CallMethod(py_iter,
-               "_bt_init_from_native", "O", py_component_port_output_ptr);
+               "_bt_init_from_native", "OO", py_config_ptr,
+               py_component_port_output_ptr);
        if (!py_init_method_result) {
                BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
                        "User's __init__() method failed:");
@@ -805,8 +1112,7 @@ component_class_message_iterator_init(
 
 python_error:
        /* Handling of errors that cause a Python exception to be set. */
-       status = py_exc_to_status_message_iterator(self_message_iterator);
-       BT_ASSERT(status != __BT_FUNC_STATUS_OK);
+       status = py_exc_to_status_message_iterator_clear(self_message_iterator);
        goto end;
 
 error:
@@ -826,27 +1132,33 @@ end:
 }
 
 static
-bt_component_class_message_iterator_init_method_status
+bt_component_class_message_iterator_initialize_method_status
 component_class_source_message_iterator_init(
                bt_self_message_iterator *self_message_iterator,
+               bt_self_message_iterator_configuration *config,
                bt_self_component_source *self_component_source,
                bt_self_component_port_output *self_component_port_output)
 {
-       bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
+       bt_self_component *self_component =
+               bt_self_component_source_as_self_component(self_component_source);
 
-       return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
+       return component_class_message_iterator_init(self_message_iterator,
+               config, self_component, self_component_port_output);
 }
 
 static
-bt_component_class_message_iterator_init_method_status
+bt_component_class_message_iterator_initialize_method_status
 component_class_filter_message_iterator_init(
                bt_self_message_iterator *self_message_iterator,
+               bt_self_message_iterator_configuration *config,
                bt_self_component_filter *self_component_filter,
                bt_self_component_port_output *self_component_port_output)
 {
-       bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
+       bt_self_component *self_component =
+               bt_self_component_filter_as_self_component(self_component_filter);
 
-       return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
+       return component_class_message_iterator_init(self_message_iterator,
+               config, self_component, self_component_port_output);
 }
 
 static
@@ -862,8 +1174,7 @@ void component_class_message_iterator_finalize(
        /* Call user's _user_finalize() method */
        py_method_result = PyObject_CallMethod(py_message_iter,
                "_user_finalize", NULL);
-
-       if (PyErr_Occurred()) {
+       if (!py_method_result) {
                bt_self_component *self_comp =
                        bt_self_message_iterator_borrow_component(
                                message_iterator);
@@ -874,14 +1185,15 @@ void component_class_message_iterator_finalize(
                        "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();
        }
 
-       /*
-        * 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();
        Py_XDECREF(py_method_result);
        Py_DECREF(py_message_iter);
 }
@@ -895,16 +1207,16 @@ component_class_message_iterator_next(
                bt_message_array_const msgs, uint64_t capacity,
                uint64_t *count)
 {
-       bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
+       bt_component_class_message_iterator_next_method_status status;
        PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
        PyObject *py_method_result = NULL;
 
        BT_ASSERT(py_message_iter);
+
        py_method_result = PyObject_CallMethod(py_message_iter,
                "_bt_next_from_native", NULL);
        if (!py_method_result) {
-               status = py_exc_to_status_message_iterator(message_iterator);
-               BT_ASSERT(status != __BT_FUNC_STATUS_OK);
+               status = py_exc_to_status_message_iterator_clear(message_iterator);
                goto end;
        }
 
@@ -916,9 +1228,10 @@ component_class_message_iterator_next(
        msgs[0] = PyLong_AsVoidPtr(py_method_result);
        *count = 1;
 
-       /* Clear potential overflow error; should never happen */
+       /* Overflow errors should never happen. */
        BT_ASSERT(!PyErr_Occurred());
-       goto end;
+
+       status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
 
 end:
        Py_XDECREF(py_method_result);
@@ -935,10 +1248,17 @@ component_class_sink_consume(bt_self_component_sink *self_component_sink)
        bt_component_class_sink_consume_method_status status;
 
        BT_ASSERT(py_comp);
+
        py_method_result = PyObject_CallMethod(py_comp,
                "_user_consume", NULL);
-       status = py_exc_to_status_component(self_component);
-       BT_ASSERT(py_method_result || status != __BT_FUNC_STATUS_OK);
+       if (!py_method_result) {
+               status = py_exc_to_status_component_clear(self_component);
+               goto end;
+       }
+
+       status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
+
+end:
        Py_XDECREF(py_method_result);
        return status;
 }
@@ -997,22 +1317,24 @@ bt_component_class_source *bt_bt2_component_class_source_create(
                goto end;
        }
 
-       ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
+       ret = bt_component_class_source_set_initialize_method(component_class_source, component_class_source_init);
        BT_ASSERT(ret == 0);
        ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
        BT_ASSERT(ret == 0);
-       ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
-               component_class_can_seek_beginning);
-       BT_ASSERT(ret == 0);
-       ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
-               component_class_seek_beginning);
+       ret = bt_component_class_source_set_message_iterator_seek_beginning_methods(component_class_source,
+               component_class_seek_beginning, component_class_can_seek_beginning);
+       ret = bt_component_class_source_set_message_iterator_seek_ns_from_origin_methods(
+               component_class_source, component_class_seek_ns_from_origin,
+               component_class_can_seek_ns_from_origin);
        BT_ASSERT(ret == 0);
        ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
                component_class_source_output_port_connected);
        BT_ASSERT(ret == 0);
        ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
        BT_ASSERT(ret == 0);
-       ret = bt_component_class_source_set_message_iterator_init_method(
+       ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions);
+       BT_ASSERT(ret == 0);
+       ret = bt_component_class_source_set_message_iterator_initialize_method(
                component_class_source, component_class_source_message_iterator_init);
        BT_ASSERT(ret == 0);
        ret = bt_component_class_source_set_message_iterator_finalize_method(
@@ -1047,16 +1369,16 @@ bt_component_class_filter *bt_bt2_component_class_filter_create(
                goto end;
        }
 
-       ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
+       ret = bt_component_class_filter_set_initialize_method(component_class_filter, component_class_filter_init);
        BT_ASSERT(ret == 0);
        ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
        BT_ASSERT(ret == 0);
-       ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
-               component_class_can_seek_beginning);
-       BT_ASSERT(ret == 0);
-       ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
-               component_class_seek_beginning);
+       ret = bt_component_class_filter_set_message_iterator_seek_beginning_methods(component_class_filter,
+               component_class_seek_beginning, component_class_can_seek_beginning);
        BT_ASSERT(ret == 0);
+       ret = bt_component_class_filter_set_message_iterator_seek_ns_from_origin_methods(
+               component_class_filter, component_class_seek_ns_from_origin,
+               component_class_can_seek_ns_from_origin);
        ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
                component_class_filter_input_port_connected);
        BT_ASSERT(ret == 0);
@@ -1065,7 +1387,9 @@ bt_component_class_filter *bt_bt2_component_class_filter_create(
        BT_ASSERT(ret == 0);
        ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
        BT_ASSERT(ret == 0);
-       ret = bt_component_class_filter_set_message_iterator_init_method(
+       ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions);
+       BT_ASSERT(ret == 0);
+       ret = bt_component_class_filter_set_message_iterator_initialize_method(
                component_class_filter, component_class_filter_message_iterator_init);
        BT_ASSERT(ret == 0);
        ret = bt_component_class_filter_set_message_iterator_finalize_method(
@@ -1100,7 +1424,7 @@ bt_component_class_sink *bt_bt2_component_class_sink_create(
                goto end;
        }
 
-       ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
+       ret = bt_component_class_sink_set_initialize_method(component_class_sink, component_class_sink_init);
        BT_ASSERT(ret == 0);
        ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
        BT_ASSERT(ret == 0);
@@ -1112,6 +1436,8 @@ bt_component_class_sink *bt_bt2_component_class_sink_create(
        BT_ASSERT(ret == 0);
        ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
        BT_ASSERT(ret == 0);
+       ret = bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink, component_class_sink_get_supported_mip_versions);
+       BT_ASSERT(ret == 0);
        register_cc_ptr_to_py_cls(component_class, py_cls);
 
 end:
This page took 0.034905 seconds and 4 git commands to generate.