*/
#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)
(gconstpointer) bt_cc);
}
-
-/*
- * Useful Python objects.
- */
-
-static PyObject *py_mod_bt2 = NULL;
-static PyObject *py_mod_bt2_exc_error_type = NULL;
-static PyObject *py_mod_bt2_exc_memory_error = NULL;
-static PyObject *py_mod_bt2_exc_try_again_type = NULL;
-static PyObject *py_mod_bt2_exc_stop_type = NULL;
-static PyObject *py_mod_bt2_exc_unknown_object_type = NULL;
-
-static
-void bt_bt2_cc_init_from_bt2(void)
-{
- /*
- * This is called once the bt2 package is loaded.
- *
- * Those modules and functions are needed while the package is
- * used. Loading them here is safe because we know the bt2
- * package is imported, and we know that the user cannot use the
- * code here without importing bt2 first.
- */
- py_mod_bt2 = PyImport_ImportModule("bt2");
- BT_ASSERT(py_mod_bt2);
- py_mod_bt2_exc_error_type =
- PyObject_GetAttrString(py_mod_bt2, "_Error");
- BT_ASSERT(py_mod_bt2_exc_error_type);
- py_mod_bt2_exc_memory_error =
- PyObject_GetAttrString(py_mod_bt2, "_MemoryError");
- BT_ASSERT(py_mod_bt2_exc_memory_error);
- py_mod_bt2_exc_try_again_type =
- PyObject_GetAttrString(py_mod_bt2, "TryAgain");
- BT_ASSERT(py_mod_bt2_exc_try_again_type);
- py_mod_bt2_exc_stop_type =
- PyObject_GetAttrString(py_mod_bt2, "Stop");
- BT_ASSERT(py_mod_bt2_exc_stop_type);
- py_mod_bt2_exc_unknown_object_type =
- PyObject_GetAttrString(py_mod_bt2, "UnknownObject");
- BT_ASSERT(py_mod_bt2_exc_unknown_object_type);
-}
-
-static
-void bt_bt2_cc_exit_handler(void)
-{
- /*
- * This is an exit handler (set by the bt2 package).
- *
- * We only give back the references that we took in
- * bt_bt2_cc_init_from_bt2() here. The global variables continue
- * to exist for the code of this file, but they are now borrowed
- * references. If this code is executed, it means that somehow
- * the modules are still loaded, so it should be safe to use
- * them even without a strong reference.
- *
- * We cannot do this in the library's destructor because it
- * gets executed once Python is already finalized.
- */
- Py_XDECREF(py_mod_bt2);
- Py_XDECREF(py_mod_bt2_exc_error_type);
- Py_XDECREF(py_mod_bt2_exc_try_again_type);
- Py_XDECREF(py_mod_bt2_exc_stop_type);
- Py_XDECREF(py_mod_bt2_exc_unknown_object_type);
-}
-
-
/* Library destructor */
__attribute__((destructor))
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
-void restore_current_thread_error_and_append_exception_chain_recursive(
- int active_log_level, PyObject *py_exc_value,
- bt_self_component_class *self_component_class,
- bt_self_component *self_component,
- bt_self_message_iterator *self_message_iterator,
- const char *module_name)
-{
- PyObject *py_exc_cause_value;
- PyObject *py_exc_type = NULL;
- PyObject *py_exc_tb = NULL;
- GString *gstr = NULL;
-
- /* If this exception has a cause, handle that one first. */
- py_exc_cause_value = PyException_GetCause(py_exc_value);
- if (py_exc_cause_value) {
- restore_current_thread_error_and_append_exception_chain_recursive(
- active_log_level, py_exc_cause_value,
- self_component_class, self_component,
- self_message_iterator, module_name);
- }
-
- /*
- * If the raised exception is a bt2._Error, restore the wrapped error.
- */
- if (PyErr_GivenExceptionMatches(py_exc_value, py_mod_bt2_exc_error_type)) {
- PyObject *py_error_swig_ptr;
- const bt_error *error;
- int ret;
-
- /*
- * We never raise a bt2._Error with a cause: it should be the
- * end of the chain.
- */
- BT_ASSERT(!py_exc_cause_value);
-
- /*
- * We steal the error object from the exception, to move
- * it back as the current thread's error.
- */
- py_error_swig_ptr = PyObject_GetAttrString(py_exc_value, "_ptr");
- BT_ASSERT(py_error_swig_ptr);
-
- ret = PyObject_SetAttrString(py_exc_value, "_ptr", Py_None);
- BT_ASSERT(ret == 0);
-
- ret = SWIG_ConvertPtr(py_error_swig_ptr, (void **) &error,
- SWIGTYPE_p_bt_error, 0);
- BT_ASSERT(ret == 0);
-
- BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(error);
-
- Py_DECREF(py_error_swig_ptr);
- }
-
- py_exc_type = PyObject_Type(py_exc_value);
- py_exc_tb = PyException_GetTraceback(py_exc_value);
-
- gstr = bt_py_common_format_exception(py_exc_type, py_exc_value,
- py_exc_tb, active_log_level, false);
- if (!gstr) {
- /* bt_py_common_format_exception has already warned. */
- goto end;
- }
-
- if (self_component_class) {
- BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
- self_component_class, "%s", gstr->str);
- } else if (self_component) {
- BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
- self_component, "%s", gstr->str);
- } else if (self_message_iterator) {
- BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
- self_message_iterator, "%s", gstr->str);
- } else {
- BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
- module_name, "%s", gstr->str);
- }
-
-end:
- if (gstr) {
- g_string_free(gstr, TRUE);
- }
-
- Py_XDECREF(py_exc_cause_value);
- Py_XDECREF(py_exc_type);
- Py_XDECREF(py_exc_tb);
-}
-
-/*
- * If you have the following code:
- *
- * try:
- * try:
- * something_that_raises_bt2_error()
- * except bt2._Error as e1:
- * raise ValueError from e1
- * except ValueError as e2:
- * raise TypeError from e2
- *
- * We will have the following exception chain:
- *
- * TypeError -> ValueError -> bt2._Error
- *
- * Where the TypeError is the current exception (obtained from PyErr_Fetch).
- *
- * The bt2._Error contains a `struct bt_error *` that used to be the current
- * thread's error, at the moment the exception was raised.
- *
- * This function gets to the bt2._Error and restores the wrapped
- * `struct bt_error *` as the current thread's error.
- *
- * Then, for each exception in the chain, starting with the oldest one, it adds
- * an error cause to the current thread's error.
- */
-static
-void restore_bt_error_and_append_current_exception_chain(
- int active_log_level,
- bt_self_component_class *self_component_class,
- bt_self_component *self_component,
- bt_self_message_iterator *self_message_iterator,
- const char *module_name)
-{
- BT_ASSERT(PyErr_Occurred());
-
- /* Used to access and restore the current exception. */
- PyObject *py_exc_type;
- PyObject *py_exc_value;
- PyObject *py_exc_tb;
-
- /* Fetch and normalize the Python exception. */
- PyErr_Fetch(&py_exc_type, &py_exc_value, &py_exc_tb);
- PyErr_NormalizeException(&py_exc_type, &py_exc_value, &py_exc_tb);
- BT_ASSERT(py_exc_type);
- BT_ASSERT(py_exc_value);
- BT_ASSERT(py_exc_tb);
-
- /*
- * Set the exception's traceback so it's possible to get it using
- * PyException_GetTraceback in
- * restore_current_thread_error_and_append_exception_chain_recursive.
- */
- PyException_SetTraceback(py_exc_value, py_exc_tb);
-
- restore_current_thread_error_and_append_exception_chain_recursive(
- active_log_level, py_exc_value, self_component_class,
- self_component, self_message_iterator, module_name);
-
- PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb);
-}
-
-static inline
-void log_exception_and_maybe_append_error(int func_log_level,
- int active_log_level, bool append_error,
- bt_self_component_class *self_component_class,
- bt_self_component *self_component,
- bt_self_message_iterator *self_message_iterator,
- const char *module_name)
-{
- GString *gstr;
-
- BT_ASSERT(PyErr_Occurred());
- gstr = bt_py_common_format_current_exception(active_log_level);
- if (!gstr) {
- /* bt_py_common_format_current_exception() logs errors */
- goto end;
- }
-
- BT_COMP_LOG_CUR_LVL(func_log_level, active_log_level, self_component,
- "%s", gstr->str);
-
- if (append_error) {
- restore_bt_error_and_append_current_exception_chain(
- active_log_level, self_component_class, self_component,
- self_message_iterator, module_name);
-
- }
-
-end:
- if (gstr) {
- g_string_free(gstr, TRUE);
- }
-}
-
-static
-bt_logging_level get_self_component_log_level(bt_self_component *self_comp)
-{
- return bt_component_get_logging_level(
- bt_self_component_as_component(self_comp));
-}
-
-static
-bt_logging_level get_self_message_iterator_log_level(
- bt_self_message_iterator *self_msg_iter)
-{
- bt_self_component *self_comp =
- bt_self_message_iterator_borrow_component(self_msg_iter);
-
- return get_self_component_log_level(self_comp);
-}
-
-static inline
-void loge_exception(const char *module_name, int active_log_level)
-{
- log_exception_and_maybe_append_error(BT_LOG_ERROR, active_log_level,
- true, NULL, NULL, NULL, module_name);
-}
-
-static
-void loge_exception_message_iterator(
- bt_self_message_iterator *self_message_iterator)
-{
- bt_logging_level log_level = get_self_message_iterator_log_level(
- self_message_iterator);
-
- log_exception_and_maybe_append_error(BT_LOG_ERROR, log_level,
- true, NULL, NULL, self_message_iterator, NULL);
-}
-
-static inline
-void logw_exception(int active_log_level)
-{
- log_exception_and_maybe_append_error(BT_LOG_WARNING, active_log_level,
- false, NULL, NULL, NULL, NULL);
-}
-
static inline
int py_exc_to_status(bt_self_component_class *self_component_class,
bt_self_component *self_component,
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
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);
/*
* 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: "
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.
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;
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,
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: "
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);
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);
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);
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_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);
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);
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: