lib: make can_seek_beginning and can_seek_ns_from_origin methods return a status
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i.h
index 479f9abf737595e2c738e0e43ac70d57ca08aca0..58c6c7740f8fd72ce476c7dda96896f894c93e4d 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,6 +99,7 @@ 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;
        }
 }
 
@@ -169,6 +184,12 @@ int py_exc_to_status_message_iterator(
        return py_exc_to_status(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
@@ -189,8 +210,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,13 +247,21 @@ 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: "
@@ -270,6 +297,152 @@ end:
        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 =
+               __BT_FUNC_STATUS_OK;
+
+       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;
+       }
+
+       /*
+        * 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.
+        */
+       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(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;
+               }
+       }
+
+       goto end;
+
+error:
+       PyErr_Clear();
+       status = __BT_FUNC_STATUS_ERROR;
+
+end:
+       Py_XDECREF(py_params_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.
@@ -365,13 +538,13 @@ 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);
 
@@ -380,19 +553,16 @@ bt_bool component_class_can_seek_beginning(
        BT_ASSERT(!py_result || PyBool_Check(py_result));
 
        if (py_result) {
-               can_seek_beginning = PyObject_IsTrue(py_result);
+               *can_seek = PyObject_IsTrue(py_result);
+               status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
        } 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 = py_exc_to_status_message_iterator(self_message_iterator);
+               BT_ASSERT(status != __BT_FUNC_STATUS_OK);
        }
 
        Py_XDECREF(py_result);
 
-       return can_seek_beginning;
+       return status;
 }
 
 static
@@ -558,7 +728,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 +744,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 +784,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,
                        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 +836,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 +844,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 +859,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,7 +874,7 @@ 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
@@ -1012,6 +1203,8 @@ bt_component_class_source *bt_bt2_component_class_source_create(
        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_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_init_method(
                component_class_source, component_class_source_message_iterator_init);
        BT_ASSERT(ret == 0);
@@ -1065,6 +1258,8 @@ 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_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_init_method(
                component_class_filter, component_class_filter_message_iterator_init);
        BT_ASSERT(ret == 0);
@@ -1112,6 +1307,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.028676 seconds and 4 git commands to generate.