from bt2 import native_bt, object, utils
import bt2.notification_iterator
import collections.abc
+import bt2.values
import sys
import bt2
def help(self):
return native_bt.component_class_get_help(self._ptr)
+ def query_info(self, action, params=None):
+ return _query_info(self._ptr, action, params)
+
def __call__(self, params=None, name=None):
params = bt2.create_value(params)
comp_ptr = native_bt.component_create_with_init_method_data(self._ptr,
return '\n'.join(trimmed)
+def _query_info(comp_cls_ptr, action, params):
+ utils._check_str(action)
+
+ if params is None:
+ params_ptr = native_bt.value_null
+ else:
+ params = bt2.create_value(params)
+ params_ptr = params._ptr
+
+ results_ptr = native_bt.component_class_query_info(comp_cls_ptr, action,
+ params_ptr)
+
+ if results_ptr is None:
+ raise bt2.Error('cannot query info with action "{}"'.format(action))
+
+ if results_ptr == native_bt.value_null:
+ return
+
+ return bt2.values._create_from_ptr(results_ptr)
+
+
# Metaclass for component classes defined by Python code.
#
# The Python user can create a standard Python class which inherits one
def addr(cls):
return int(cls._cc_ptr)
+ def query_info(cls, action, params=None):
+ return _query_info(cls._cc_ptr, action, params)
+
+ def _query_info_from_bt(cls, action, params):
+ # this can raise, in which case the native call to
+ # bt_component_class_query_info() returns NULL
+ results = cls._query_info(action, params)
+ results = bt2.create_value(results)
+
+ if results is None:
+ results_addr = int(native_bt.value_null)
+ else:
+ # steal the underlying native value object for the caller
+ results_addr = int(results._ptr)
+ results._ptr = None
+
+ return results_addr
+
+ @staticmethod
+ def _query_info(action, params):
+ # BT catches this and returns NULL to the user
+ raise NotImplementedError
+
def __del__(cls):
if hasattr(cls, '_cc_ptr'):
native_bt.put(cls._cc_ptr)
struct bt_component_class *component_class);
const char *bt_component_class_get_help(
struct bt_component_class *component_class);
+struct bt_value *bt_component_class_query_info(
+ struct bt_component_class *component_class,
+ const char *action, struct bt_value *params);
enum bt_component_class_type bt_component_class_get_type(
struct bt_component_class *component_class);
}
}
+static struct bt_value *bt_py3_cc_query_info(
+ struct bt_component_class *component_class,
+ const char *action, struct bt_value *params)
+{
+ PyObject *py_cls = NULL;
+ PyObject *py_params = NULL;
+ PyObject *py_query_info_func = NULL;
+ PyObject *py_action = NULL;
+ PyObject *py_results_addr = NULL;
+ struct bt_value *results = NULL;
+
+ py_cls = lookup_cc_ptr_to_py_cls(component_class);
+ if (!py_cls) {
+ goto error;
+ }
+
+ py_params = bt_py3_bt_value_from_ptr(params);
+ if (!py_params) {
+ goto error;
+ }
+
+ py_action = SWIG_FromCharPtr(action);
+ if (!py_action) {
+ goto error;
+ }
+
+ py_results_addr = PyObject_CallMethod(py_cls,
+ "_query_info_from_bt", "(OO)", py_action, py_params);
+ if (!py_results_addr) {
+ goto error;
+ }
+
+ /*
+ * The returned object, on success, is an integer object
+ * (PyLong) containing the address of a BT value object
+ * (which is now ours).
+ */
+ results = (struct bt_value *) PyLong_AsUnsignedLongLong(
+ py_results_addr);
+
+ /* Clear potential overflow error; should never happen */
+ if (PyErr_Occurred()) {
+ results = NULL;
+ goto error;
+ }
+
+ if (!results) {
+ goto error;
+ }
+
+ goto end;
+
+error:
+ BT_PUT(results);
+ PyErr_Clear();
+
+end:
+ Py_XDECREF(py_params);
+ Py_XDECREF(py_query_info_func);
+ Py_XDECREF(py_action);
+ Py_XDECREF(py_results_addr);
+ return results;
+}
+
static enum bt_notification_iterator_status bt_py3_exc_to_notif_iter_status(void)
{
enum bt_notification_iterator_status status =
/*
* The returned object, on success, is an integer object
* (PyLong) containing the address of a BT notification
- * object (which is not ours).
+ * object (which is now ours).
*/
notif = (struct bt_notification *) PyLong_AsUnsignedLongLong(
py_get_method_result);
/* Clear potential overflow error; should never happen */
if (PyErr_Occurred()) {
+ notif = NULL;
goto error;
}
goto end;
}
+ ret = bt_component_class_set_query_info_method(cc,
+ bt_py3_cc_query_info);
+ if (ret) {
+ goto end;
+ }
+
end:
return ret;
}