}
}
-
-// TODO: maybe we can wrap code in the Python methods (e.g. _query_from_native)
-// in a try catch and print the error there instead, it would be simpler.
static
void bt2_py_loge_exception(void)
{
- PyObject *type = NULL;
- PyObject *value = NULL;
- PyObject *traceback = NULL;
- PyObject *traceback_module = NULL;
- PyObject *format_exception_func = NULL;
- PyObject *exc_str_list = NULL;
- GString *msg_buf = NULL;
- Py_ssize_t i;
+ GString *gstr;
BT_ASSERT(PyErr_Occurred() != NULL);
-
- PyErr_Fetch(&type, &value, &traceback);
-
- BT_ASSERT(type != NULL);
-
- /*
- * traceback can be NULL, when we fail to call a Python function from the
- * native code (there is no Python stack at that point). E.g.:
- *
- * TypeError: _query_from_native() takes 5 positional arguments but 8 were given
- */
-
-
- /* Make sure `value` is what we expected - an instance of `type`. */
- PyErr_NormalizeException(&type, &value, &traceback);
-
- traceback_module = PyImport_ImportModule("traceback");
- if (!traceback_module) {
- BT_LOGE_STR("Failed to log Python exception (could not import traceback module).");
+ gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL);
+ if (!gstr) {
+ /* bt_py_common_format_exception() logs errors */
goto end;
}
- format_exception_func = PyObject_GetAttrString(traceback_module,
- traceback ? "format_exception" : "format_exception_only");
- if (!format_exception_func) {
- BT_LOGE_STR("Failed to log Python exception (could not find format_exception).");
- goto end;
- }
-
- if (!PyCallable_Check(format_exception_func)) {
- BT_LOGE_STR("Failed to log Python exception (format_exception is not callable).");
- goto end;
- }
-
- exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, type, value, traceback, NULL);
- if (!exc_str_list) {
- PyErr_Print();
- BT_LOGE_STR("Failed to log Python exception (call to format_exception failed).");
- goto end;
- }
-
- msg_buf = g_string_new(NULL);
-
- for (i = 0; i < PyList_Size(exc_str_list); i++) {
- PyObject *exc_str = PyList_GetItem(exc_str_list, i);
- const char *str = PyUnicode_AsUTF8(exc_str);
- if (!str) {
- BT_LOGE_STR("Failed to log Python exception (failed to convert exception to string).");
- goto end;
- }
-
- g_string_append(msg_buf, str);
- }
-
- BT_LOGE_STR(msg_buf->str);
+ BT_LOGE_STR(gstr->str);
end:
- if (msg_buf) {
- g_string_free(msg_buf, TRUE);
+ if (gstr) {
+ g_string_free(gstr, TRUE);
}
- Py_XDECREF(exc_str_list);
- Py_XDECREF(format_exception_func);
- Py_XDECREF(traceback_module);
-
- /* PyErr_Restore takes our references. */
- PyErr_Restore(type, value, traceback);
}
static
bt_py3_component_class_finalize(self_component);
}
+static
+bt_bool bt_py3_component_class_can_seek_beginning(
+ bt_self_message_iterator *self_message_iterator)
+{
+ PyObject *py_iter;
+ PyObject *py_result = NULL;
+ bt_bool can_seek_beginning = false;
+
+ py_iter = bt_self_message_iterator_get_data(self_message_iterator);
+ BT_ASSERT(py_iter);
+
+ py_result = PyObject_GetAttrString(py_iter, "_can_seek_beginning_from_native");
+
+ BT_ASSERT(!py_result || PyBool_Check(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;
+ */
+ bt2_py_loge_exception();
+ PyErr_Clear();
+ }
+
+ Py_XDECREF(py_result);
+
+ return can_seek_beginning;
+}
+
+static
+bt_self_message_iterator_status bt_py3_component_class_seek_beginning(
+ bt_self_message_iterator *self_message_iterator)
+{
+ PyObject *py_iter;
+ PyObject *py_result;
+ bt_self_message_iterator_status status;
+
+ py_iter = bt_self_message_iterator_get_data(self_message_iterator);
+ BT_ASSERT(py_iter);
+
+ py_result = PyObject_CallMethod(py_iter, "_seek_beginning_from_native", NULL);
+
+ BT_ASSERT(!py_result || py_result == Py_None);
+ status = bt_py3_exc_to_self_message_iterator_status();
+
+ Py_XDECREF(py_result);
+
+ return status;
+}
+
static
bt_self_component_status bt_py3_component_class_port_connected(
bt_self_component *self_component,
ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init);
BT_ASSERT(ret == 0);
- ret = bt_component_class_source_set_finalize_method (component_class_source, bt_py3_component_class_source_finalize);
+ ret = bt_component_class_source_set_finalize_method(component_class_source, bt_py3_component_class_source_finalize);
+ BT_ASSERT(ret == 0);
+ ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
+ bt_py3_component_class_can_seek_beginning);
+ BT_ASSERT(ret == 0);
+ ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
+ bt_py3_component_class_seek_beginning);
BT_ASSERT(ret == 0);
ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
bt_py3_component_class_source_output_port_connected);
BT_ASSERT(ret == 0);
ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize);
BT_ASSERT(ret == 0);
+ ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
+ bt_py3_component_class_can_seek_beginning);
+ BT_ASSERT(ret == 0);
+ ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
+ bt_py3_component_class_seek_beginning);
+ BT_ASSERT(ret == 0);
ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
bt_py3_component_class_filter_input_port_connected);
BT_ASSERT(ret == 0);