2 * The MIT License (MIT)
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 %include <babeltrace2/graph/component-class-const.h>
26 %include <babeltrace2/graph/component-class-source-const.h>
27 %include <babeltrace2/graph/component-class-source.h>
28 %include <babeltrace2/graph/component-class-filter-const.h>
29 %include <babeltrace2/graph/component-class-filter.h>
30 %include <babeltrace2/graph/component-class-sink-const.h>
31 %include <babeltrace2/graph/component-class-sink.h>
32 %include <babeltrace2/graph/self-component-class-source.h>
33 %include <babeltrace2/graph/self-component-class-filter.h>
34 %include <babeltrace2/graph/self-component-class-sink.h>
38 * This hash table associates a BT component class object address to a
39 * user-defined Python class (PyObject *). The keys and values are NOT
40 * owned by this hash table. The Python class objects are owned by the
41 * Python module, which should not be unloaded until it is not possible
42 * to create a user Python component anyway.
44 * This hash table is written to when a user-defined Python component
45 * class is created by one of the bt_py3_component_class_*_create()
48 * This function is read from when a user calls bt_component_create()
49 * with a component class pointer created by one of the functions above.
50 * In this case, the original Python class needs to be found to
51 * instantiate it and associate the created Python component object with
52 * a BT component object instance.
55 static GHashTable *bt_cc_ptr_to_py_cls;
57 static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
60 if (!bt_cc_ptr_to_py_cls) {
62 * Lazy-initializing this GHashTable because GLib
63 * might not be initialized yet and it needs to be
64 * before we call g_hash_table_new()
66 BT_LOGD_STR("Creating native component class to Python component class hash table.");
67 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
68 BT_ASSERT(bt_cc_ptr_to_py_cls);
71 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
75 static PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
77 if (!bt_cc_ptr_to_py_cls) {
78 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
79 "comp-cls-addr=%p", bt_cc);
83 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
84 (gconstpointer) bt_cc);
89 * Useful Python objects.
92 static PyObject *py_mod_bt2 = NULL;
93 static PyObject *py_mod_bt2_exc_error_type = NULL;
94 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
95 static PyObject *py_mod_bt2_exc_stop_type = NULL;
96 static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
97 static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL;
98 static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL;
100 static void bt_py3_cc_init_from_bt2(void)
103 * This is called once the bt2 package is loaded.
105 * Those modules and functions are needed while the package is
106 * used. Loading them here is safe because we know the bt2
107 * package is imported, and we know that the user cannot use the
108 * code here without importing bt2 first.
110 py_mod_bt2 = PyImport_ImportModule("bt2");
111 BT_ASSERT(py_mod_bt2);
112 py_mod_bt2_exc_error_type =
113 PyObject_GetAttrString(py_mod_bt2, "Error");
114 BT_ASSERT(py_mod_bt2_exc_error_type);
115 py_mod_bt2_exc_try_again_type =
116 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
117 BT_ASSERT(py_mod_bt2_exc_try_again_type);
118 py_mod_bt2_exc_stop_type =
119 PyObject_GetAttrString(py_mod_bt2, "Stop");
120 BT_ASSERT(py_mod_bt2_exc_stop_type);
121 py_mod_bt2_exc_invalid_query_object_type =
122 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject");
123 BT_ASSERT(py_mod_bt2_exc_invalid_query_object_type);
124 py_mod_bt2_exc_invalid_query_params_type =
125 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams");
126 BT_ASSERT(py_mod_bt2_exc_invalid_query_params_type);
129 static void bt_py3_cc_exit_handler(void)
132 * This is an exit handler (set by the bt2 package).
134 * We only give back the references that we took in
135 * bt_py3_cc_init_from_bt2() here. The global variables continue
136 * to exist for the code of this file, but they are now borrowed
137 * references. If this code is executed, it means that somehow
138 * the modules are still loaded, so it should be safe to use
139 * them even without a strong reference.
141 * We cannot do this in the library's destructor because it
142 * gets executed once Python is already finalized.
144 Py_XDECREF(py_mod_bt2);
145 Py_XDECREF(py_mod_bt2_exc_error_type);
146 Py_XDECREF(py_mod_bt2_exc_try_again_type);
147 Py_XDECREF(py_mod_bt2_exc_stop_type);
148 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
149 Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type);
150 Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type);
154 /* Library destructor */
156 __attribute__((destructor))
157 static void bt_py3_native_comp_class_dtor(void) {
158 /* Destroy component class association hash table */
159 if (bt_cc_ptr_to_py_cls) {
160 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
161 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
166 // TODO: maybe we can wrap code in the Python methods (e.g. _query_from_native)
167 // in a try catch and print the error there instead, it would be simpler.
169 void bt2_py_loge_exception(void)
171 PyObject *type = NULL;
172 PyObject *value = NULL;
173 PyObject *traceback = NULL;
174 PyObject *traceback_module = NULL;
175 PyObject *format_exception_func = NULL;
176 PyObject *exc_str_list = NULL;
177 GString *msg_buf = NULL;
180 BT_ASSERT(PyErr_Occurred() != NULL);
182 PyErr_Fetch(&type, &value, &traceback);
184 BT_ASSERT(type != NULL);
187 * traceback can be NULL, when we fail to call a Python function from the
188 * native code (there is no Python stack at that point). E.g.:
190 * TypeError: _query_from_native() takes 5 positional arguments but 8 were given
194 /* Make sure `value` is what we expected - an instance of `type`. */
195 PyErr_NormalizeException(&type, &value, &traceback);
197 traceback_module = PyImport_ImportModule("traceback");
198 if (!traceback_module) {
199 BT_LOGE_STR("Failed to log Python exception (could not import traceback module).");
203 format_exception_func = PyObject_GetAttrString(traceback_module,
204 traceback ? "format_exception" : "format_exception_only");
205 if (!format_exception_func) {
206 BT_LOGE_STR("Failed to log Python exception (could not find format_exception).");
210 if (!PyCallable_Check(format_exception_func)) {
211 BT_LOGE_STR("Failed to log Python exception (format_exception is not callable).");
215 exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, type, value, traceback, NULL);
218 BT_LOGE_STR("Failed to log Python exception (call to format_exception failed).");
222 msg_buf = g_string_new(NULL);
224 for (i = 0; i < PyList_Size(exc_str_list); i++) {
225 PyObject *exc_str = PyList_GetItem(exc_str_list, i);
226 const char *str = PyUnicode_AsUTF8(exc_str);
228 BT_LOGE_STR("Failed to log Python exception (failed to convert exception to string).");
232 g_string_append(msg_buf, str);
235 BT_LOGE_STR(msg_buf->str);
239 g_string_free(msg_buf, TRUE);
241 Py_XDECREF(exc_str_list);
242 Py_XDECREF(format_exception_func);
243 Py_XDECREF(traceback_module);
245 /* PyErr_Restore takes our references. */
246 PyErr_Restore(type, value, traceback);
249 static bt_self_component_status bt_py3_exc_to_self_component_status(void)
251 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
252 PyObject *exc = PyErr_Occurred();
258 if (PyErr_GivenExceptionMatches(exc,
259 py_mod_bt2_exc_try_again_type)) {
260 status = BT_SELF_COMPONENT_STATUS_AGAIN;
261 } else if (PyErr_GivenExceptionMatches(exc,
262 py_mod_bt2_exc_stop_type)) {
263 status = BT_SELF_COMPONENT_STATUS_END;
265 bt2_py_loge_exception();
266 status = BT_SELF_COMPONENT_STATUS_ERROR;
274 /* Component class proxy methods (delegate to the attached Python object) */
276 static bt_self_message_iterator_status
277 bt_py3_exc_to_self_message_iterator_status(void)
279 enum bt_self_message_iterator_status status =
280 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
281 PyObject *exc = PyErr_Occurred();
287 if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) {
288 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
289 } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) {
290 status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN;
292 bt2_py_loge_exception();
293 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
301 static enum bt_query_status bt_py3_exc_to_query_status(void)
303 enum bt_query_status status = BT_QUERY_STATUS_OK;
304 PyObject *exc = PyErr_Occurred();
310 if (PyErr_GivenExceptionMatches(exc,
311 py_mod_bt2_exc_invalid_query_object_type)) {
312 status = BT_QUERY_STATUS_INVALID_OBJECT;
313 } else if (PyErr_GivenExceptionMatches(exc,
314 py_mod_bt2_exc_invalid_query_params_type)) {
315 status = BT_QUERY_STATUS_INVALID_PARAMS;
316 } else if (PyErr_GivenExceptionMatches(exc,
317 py_mod_bt2_exc_try_again_type)) {
318 status = BT_QUERY_STATUS_AGAIN;
320 bt2_py_loge_exception();
321 status = BT_QUERY_STATUS_ERROR;
329 static bt_self_component_status
330 bt_py3_component_class_init(
331 bt_self_component *self_component,
332 void *self_component_v,
333 swig_type_info *self_comp_cls_type_swig_type,
334 const bt_value *params,
335 void *init_method_data)
337 const bt_component *component = bt_self_component_as_component(self_component);
338 const bt_component_class *component_class = bt_component_borrow_class_const(component);
339 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
340 PyObject *py_cls = NULL;
341 PyObject *py_comp = NULL;
342 PyObject *py_params_ptr = NULL;
343 PyObject *py_comp_ptr = NULL;
345 (void) init_method_data;
347 BT_ASSERT(self_component);
348 BT_ASSERT(self_component_v);
349 BT_ASSERT(self_comp_cls_type_swig_type);
352 * Get the user-defined Python class which created this
353 * component's class in the first place (borrowed
356 py_cls = lookup_cc_ptr_to_py_cls(component_class);
358 BT_LOGE("Cannot find Python class associated to native component class: "
359 "comp-cls-addr=%p", component_class);
363 /* Parameters pointer -> SWIG pointer Python object */
364 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
365 SWIGTYPE_p_bt_value, 0);
366 if (!py_params_ptr) {
367 BT_LOGE_STR("Failed to create a SWIG pointer object.");
371 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
372 self_comp_cls_type_swig_type, 0);
374 BT_LOGE_STR("Failed to create a SWIG pointer object.");
379 * Do the equivalent of this:
381 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
383 * _UserComponentType._init_from_native() calls the Python
384 * component object's __init__() function.
386 py_comp = PyObject_CallMethod(py_cls,
387 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
389 bt2_py_loge_exception();
390 BT_LOGE("Failed to call Python class's _init_from_native() method: "
391 "py-cls-addr=%p", py_cls);
397 * Our user Python component object is now fully created and
398 * initialized by the user. Since we just created it, this
399 * native component is its only (persistent) owner.
401 bt_self_component_set_data(self_component, py_comp);
406 status = BT_SELF_COMPONENT_STATUS_ERROR;
409 * Clear any exception: we're returning a bad status anyway. If
410 * this call originated from Python (creation from a plugin's
411 * component class, for example), then the user gets an
412 * appropriate creation error.
418 Py_XDECREF(py_params_ptr);
419 Py_XDECREF(py_comp_ptr);
424 * Method of bt_component_class_source to initialize a bt_self_component_source
428 static bt_self_component_status
429 bt_py3_component_class_source_init(bt_self_component_source *self_component_source,
430 const bt_value *params, void *init_method_data)
432 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
433 return bt_py3_component_class_init(
435 self_component_source,
436 SWIGTYPE_p_bt_self_component_source,
437 params, init_method_data);
440 static bt_self_component_status
441 bt_py3_component_class_filter_init(bt_self_component_filter *self_component_filter,
442 const bt_value *params, void *init_method_data)
444 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
445 return bt_py3_component_class_init(
447 self_component_filter,
448 SWIGTYPE_p_bt_self_component_filter,
449 params, init_method_data);
452 static bt_self_component_status
453 bt_py3_component_class_sink_init(bt_self_component_sink *self_component_sink,
454 const bt_value *params, void *init_method_data)
456 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
457 return bt_py3_component_class_init(
460 SWIGTYPE_p_bt_self_component_sink,
461 params, init_method_data);
464 static void bt_py3_component_class_finalize(bt_self_component *self_component)
466 PyObject *py_comp = bt_self_component_get_data(self_component);
469 /* Call user's _finalize() method */
470 PyObject *py_method_result = PyObject_CallMethod(py_comp,
473 if (PyErr_Occurred()) {
474 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
478 * Ignore any exception raised by the _finalize() method because
479 * it won't change anything at this point: the component is
480 * being destroyed anyway.
483 Py_XDECREF(py_method_result);
488 bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source)
490 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
491 bt_py3_component_class_finalize(self_component);
495 bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter)
497 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
498 bt_py3_component_class_finalize(self_component);
502 bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink)
504 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
505 bt_py3_component_class_finalize(self_component);
508 static bt_self_component_status
509 bt_py3_component_class_port_connected(
510 bt_self_component *self_component,
511 void *self_component_port,
512 swig_type_info *self_component_port_swig_type,
513 bt_port_type self_component_port_type,
514 const void *other_port,
515 swig_type_info *other_port_swig_type)
517 bt_self_component_status status;
518 PyObject *py_comp = NULL;
519 PyObject *py_self_port_ptr = NULL;
520 PyObject *py_other_port_ptr = NULL;
521 PyObject *py_method_result = NULL;
523 py_comp = bt_self_component_get_data(self_component);
526 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
527 self_component_port_swig_type, 0);
528 if (!py_self_port_ptr) {
529 BT_LOGF_STR("Failed to create a SWIG pointer object.");
530 status = BT_SELF_COMPONENT_STATUS_NOMEM;
534 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
535 other_port_swig_type, 0);
536 if (!py_other_port_ptr) {
537 BT_LOGF_STR("Failed to create a SWIG pointer object.");
538 status = BT_SELF_COMPONENT_STATUS_NOMEM;
541 py_method_result = PyObject_CallMethod(py_comp,
542 "_port_connected_from_native", "(OiO)", py_self_port_ptr,
543 self_component_port_type, py_other_port_ptr);
545 BT_ASSERT(!py_method_result || py_method_result == Py_None);
547 status = bt_py3_exc_to_self_component_status();
550 Py_XDECREF(py_self_port_ptr);
551 Py_XDECREF(py_other_port_ptr);
552 Py_XDECREF(py_method_result);
557 static bt_self_component_status
558 bt_py3_component_class_source_output_port_connected(
559 bt_self_component_source *self_component_source,
560 bt_self_component_port_output *self_component_port_output,
561 const bt_port_input *other_port_input)
563 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
565 return bt_py3_component_class_port_connected(
567 self_component_port_output,
568 SWIGTYPE_p_bt_self_component_port_output,
571 SWIGTYPE_p_bt_port_input);
574 static bt_self_component_status
575 bt_py3_component_class_filter_input_port_connected(
576 bt_self_component_filter *self_component_filter,
577 bt_self_component_port_input *self_component_port_input,
578 const bt_port_output *other_port_output)
580 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
582 return bt_py3_component_class_port_connected(
584 self_component_port_input,
585 SWIGTYPE_p_bt_self_component_port_input,
588 SWIGTYPE_p_bt_port_output);
591 static bt_self_component_status
592 bt_py3_component_class_filter_output_port_connected(
593 bt_self_component_filter *self_component_filter,
594 bt_self_component_port_output *self_component_port_output,
595 const bt_port_input *other_port_input)
597 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
599 return bt_py3_component_class_port_connected(
601 self_component_port_output,
602 SWIGTYPE_p_bt_self_component_port_output,
605 SWIGTYPE_p_bt_port_input);
608 static bt_self_component_status
609 bt_py3_component_class_sink_input_port_connected(
610 bt_self_component_sink *self_component_sink,
611 bt_self_component_port_input *self_component_port_input,
612 const bt_port_output *other_port_output)
614 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
616 return bt_py3_component_class_port_connected(
618 self_component_port_input,
619 SWIGTYPE_p_bt_self_component_port_input,
622 SWIGTYPE_p_bt_port_output);
625 static bt_self_component_status
626 bt_py3_component_class_sink_graph_is_configured(bt_self_component_sink *self_component_sink)
628 PyObject *py_comp = NULL;
629 PyObject *py_method_result = NULL;
630 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
631 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
633 py_comp = bt_self_component_get_data(self_component);
634 py_method_result = PyObject_CallMethod(py_comp,
635 "_graph_is_configured_from_native", NULL);
637 BT_ASSERT(!py_method_result || py_method_result == Py_None);
639 status = bt_py3_exc_to_self_component_status();
641 Py_XDECREF(py_method_result);
646 static bt_query_status
647 bt_py3_component_class_query(
648 const bt_component_class *component_class,
649 const bt_query_executor *query_executor,
650 const char *object, const bt_value *params,
651 bt_logging_level log_level,
652 const bt_value **result)
654 PyObject *py_cls = NULL;
655 PyObject *py_params_ptr = NULL;
656 PyObject *py_query_exec_ptr = NULL;
657 PyObject *py_query_func = NULL;
658 PyObject *py_object = NULL;
659 PyObject *py_results_addr = NULL;
660 bt_query_status status = BT_QUERY_STATUS_OK;
662 py_cls = lookup_cc_ptr_to_py_cls(component_class);
664 BT_LOGE("Cannot find Python class associated to native component class: "
665 "comp-cls-addr=%p", component_class);
669 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
670 SWIGTYPE_p_bt_value, 0);
671 if (!py_params_ptr) {
672 BT_LOGE_STR("Failed to create a SWIG pointer object.");
676 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
677 SWIGTYPE_p_bt_query_executor, 0);
678 if (!py_query_exec_ptr) {
679 BT_LOGE_STR("Failed to create a SWIG pointer object.");
683 py_object = SWIG_FromCharPtr(object);
685 BT_LOGE_STR("Failed to create a Python string.");
689 py_results_addr = PyObject_CallMethod(py_cls,
690 "_query_from_native", "(OOOi)", py_query_exec_ptr,
691 py_object, py_params_ptr, (int) log_level);
693 if (!py_results_addr) {
694 BT_LOGE("Failed to call Python class's _query_from_native() method: "
695 "py-cls-addr=%p", py_cls);
696 status = bt_py3_exc_to_query_status();
701 * The returned object, on success, is an integer object
702 * (PyLong) containing the address of a BT value object (new
705 *result = PyLong_AsVoidPtr(py_results_addr);
706 BT_ASSERT(!PyErr_Occurred());
712 status = BT_QUERY_STATUS_ERROR;
715 Py_XDECREF(py_params_ptr);
716 Py_XDECREF(py_query_exec_ptr);
717 Py_XDECREF(py_query_func);
718 Py_XDECREF(py_object);
719 Py_XDECREF(py_results_addr);
723 static bt_query_status
724 bt_py3_component_class_source_query(
725 bt_self_component_class_source *self_component_class_source,
726 const bt_query_executor *query_executor,
727 const char *object, const bt_value *params,
728 bt_logging_level log_level,
729 const bt_value **result)
731 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
732 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
733 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
736 static bt_query_status
737 bt_py3_component_class_filter_query(
738 bt_self_component_class_filter *self_component_class_filter,
739 const bt_query_executor *query_executor,
740 const char *object, const bt_value *params,
741 bt_logging_level log_level,
742 const bt_value **result)
744 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
745 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
746 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
749 static bt_query_status
750 bt_py3_component_class_sink_query(
751 bt_self_component_class_sink *self_component_class_sink,
752 const bt_query_executor *query_executor,
753 const char *object, const bt_value *params,
754 bt_logging_level log_level,
755 const bt_value **result)
757 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
758 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
759 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
762 static bt_self_message_iterator_status
763 bt_py3_component_class_message_iterator_init(
764 bt_self_message_iterator *self_message_iterator,
765 bt_self_component *self_component,
766 bt_self_component_port_output *self_component_port_output)
768 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
769 PyObject *py_comp_cls = NULL;
770 PyObject *py_iter_cls = NULL;
771 PyObject *py_iter_ptr = NULL;
772 PyObject *py_component_port_output_ptr = NULL;
773 PyObject *py_init_method_result = NULL;
774 PyObject *py_iter = NULL;
777 py_comp = bt_self_component_get_data(self_component);
779 /* Find user's Python message iterator class */
780 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
782 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
786 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
788 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
792 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
793 SWIGTYPE_p_bt_self_message_iterator, 0);
795 BT_LOGE_STR("Failed to create a SWIG pointer object.");
800 * Create object with borrowed native message iterator
803 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
805 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
806 "(OO)", py_iter_cls, py_iter_ptr);
808 BT_LOGE("Failed to call Python class's __new__() method: "
809 "py-cls-addr=%p", py_iter_cls);
810 bt2_py_loge_exception();
817 * py_iter.__init__(self_output_port)
819 * through the _init_for_native helper static method.
821 * At this point, py_iter._ptr is set, so this initialization
822 * function has access to self._component (which gives it the
823 * user Python component object from which the iterator was
826 py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output),
827 SWIGTYPE_p_bt_self_component_port_output, 0);
828 if (!py_component_port_output_ptr) {
829 BT_LOGE_STR("Failed to create a SWIG pointer object.");
833 py_init_method_result = PyObject_CallMethod(py_iter, "_init_from_native", "O", py_component_port_output_ptr);
834 if (!py_init_method_result) {
835 BT_LOGE_STR("User's __init__() method failed.");
836 bt2_py_loge_exception();
841 * Since the Python code can never instantiate a user-defined
842 * message iterator class, the native message iterator
843 * object does NOT belong to a user Python message iterator
844 * object (borrowed reference). However this Python object is
845 * owned by this native message iterator object.
847 * In the Python world, the lifetime of the native message
848 * iterator is managed by a _GenericMessageIterator
851 * _GenericMessageIterator instance:
852 * owns a native bt_message_iterator object (iter)
853 * owns a _UserMessageIterator instance (py_iter)
854 * self._ptr is a borrowed reference to the
855 * native bt_private_connection_private_message_iterator
858 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
863 status = bt_py3_exc_to_self_message_iterator_status();
864 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
866 * Looks like there wasn't any exception from the Python
867 * side, but we're still in an error state here.
869 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
873 * Clear any exception: we're returning a bad status anyway. If
874 * this call originated from Python, then the user gets an
875 * appropriate creation error.
880 Py_XDECREF(py_comp_cls);
881 Py_XDECREF(py_iter_cls);
882 Py_XDECREF(py_iter_ptr);
883 Py_XDECREF(py_component_port_output_ptr);
884 Py_XDECREF(py_init_method_result);
889 static bt_self_message_iterator_status
890 bt_py3_component_class_source_message_iterator_init(
891 bt_self_message_iterator *self_message_iterator,
892 bt_self_component_source *self_component_source,
893 bt_self_component_port_output *self_component_port_output)
895 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
896 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
899 static bt_self_message_iterator_status
900 bt_py3_component_class_filter_message_iterator_init(
901 bt_self_message_iterator *self_message_iterator,
902 bt_self_component_filter *self_component_filter,
903 bt_self_component_port_output *self_component_port_output)
905 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
906 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
910 bt_py3_component_class_message_iterator_finalize(
911 bt_self_message_iterator *message_iterator)
913 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
914 PyObject *py_method_result = NULL;
916 BT_ASSERT(py_message_iter);
918 /* Call user's _finalize() method */
919 py_method_result = PyObject_CallMethod(py_message_iter,
922 if (PyErr_Occurred()) {
923 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
927 * Ignore any exception raised by the _finalize() method because
928 * it won't change anything at this point: the component is
929 * being destroyed anyway.
932 Py_XDECREF(py_method_result);
933 Py_DECREF(py_message_iter);
936 /* Valid for both sources and filters. */
938 static bt_self_message_iterator_status
939 bt_py3_component_class_message_iterator_next(
940 bt_self_message_iterator *message_iterator,
941 bt_message_array_const msgs, uint64_t capacity,
944 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
945 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
946 PyObject *py_method_result = NULL;
948 BT_ASSERT(py_message_iter);
949 py_method_result = PyObject_CallMethod(py_message_iter,
950 "_next_from_native", NULL);
951 if (!py_method_result) {
952 status = bt_py3_exc_to_self_message_iterator_status();
953 BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK);
958 * The returned object, on success, is an integer object
959 * (PyLong) containing the address of a native message
960 * object (which is now ours).
962 msgs[0] = PyLong_AsVoidPtr(py_method_result);
965 /* Clear potential overflow error; should never happen */
966 BT_ASSERT(!PyErr_Occurred());
970 Py_XDECREF(py_method_result);
974 static bt_self_component_status
975 bt_py3_component_class_sink_consume(bt_self_component_sink *self_component_sink)
977 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
978 PyObject *py_comp = bt_self_component_get_data(self_component);
979 PyObject *py_method_result = NULL;
980 bt_self_component_status status;
983 py_method_result = PyObject_CallMethod(py_comp,
986 status = bt_py3_exc_to_self_component_status();
987 if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) {
988 /* Pretty sure this should never happen, but just in case */
989 BT_LOGE("User's _consume() method failed without raising an exception: "
990 "status=%d", status);
991 status = BT_SELF_COMPONENT_STATUS_ERROR;
994 Py_XDECREF(py_method_result);
999 int bt_py3_component_class_set_help_and_desc(
1000 bt_component_class *component_class,
1001 const char *description, const char *help)
1006 ret = bt_component_class_set_description(component_class, description);
1008 BT_LOGE("Cannot set component class's description: "
1009 "comp-cls-addr=%p", component_class);
1015 ret = bt_component_class_set_help(component_class, help);
1017 BT_LOGE("Cannot set component class's help text: "
1018 "comp-cls-addr=%p", component_class);
1030 bt_component_class_source *bt_py3_component_class_source_create(
1031 PyObject *py_cls, const char *name, const char *description,
1034 bt_component_class_source *component_class_source;
1035 bt_component_class *component_class;
1040 component_class_source = bt_component_class_source_create(name,
1041 bt_py3_component_class_message_iterator_next);
1042 if (!component_class_source) {
1043 BT_LOGE_STR("Cannot create source component class.");
1047 component_class = bt_component_class_source_as_component_class(component_class_source);
1049 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1053 ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init);
1054 BT_ASSERT(ret == 0);
1055 ret = bt_component_class_source_set_finalize_method (component_class_source, bt_py3_component_class_source_finalize);
1056 BT_ASSERT(ret == 0);
1057 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1058 bt_py3_component_class_source_output_port_connected);
1059 BT_ASSERT(ret == 0);
1060 ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query);
1061 BT_ASSERT(ret == 0);
1062 ret = bt_component_class_source_set_message_iterator_init_method(
1063 component_class_source, bt_py3_component_class_source_message_iterator_init);
1064 BT_ASSERT(ret == 0);
1065 ret = bt_component_class_source_set_message_iterator_finalize_method(
1066 component_class_source, bt_py3_component_class_message_iterator_finalize);
1067 BT_ASSERT(ret == 0);
1069 register_cc_ptr_to_py_cls(component_class, py_cls);
1072 return component_class_source;
1076 bt_component_class_filter *bt_py3_component_class_filter_create(
1077 PyObject *py_cls, const char *name, const char *description,
1080 bt_component_class *component_class;
1081 bt_component_class_filter *component_class_filter;
1086 component_class_filter = bt_component_class_filter_create(name,
1087 bt_py3_component_class_message_iterator_next);
1088 if (!component_class_filter) {
1089 BT_LOGE_STR("Cannot create filter component class.");
1093 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1095 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1099 ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init);
1100 BT_ASSERT(ret == 0);
1101 ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize);
1102 BT_ASSERT(ret == 0);
1103 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1104 bt_py3_component_class_filter_input_port_connected);
1105 BT_ASSERT(ret == 0);
1106 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1107 bt_py3_component_class_filter_output_port_connected);
1108 BT_ASSERT(ret == 0);
1109 ret = bt_component_class_filter_set_query_method(component_class_filter, bt_py3_component_class_filter_query);
1110 BT_ASSERT(ret == 0);
1111 ret = bt_component_class_filter_set_message_iterator_init_method(
1112 component_class_filter, bt_py3_component_class_filter_message_iterator_init);
1113 BT_ASSERT(ret == 0);
1114 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1115 component_class_filter, bt_py3_component_class_message_iterator_finalize);
1116 BT_ASSERT(ret == 0);
1118 register_cc_ptr_to_py_cls(component_class, py_cls);
1121 return component_class_filter;
1125 bt_component_class_sink *bt_py3_component_class_sink_create(
1126 PyObject *py_cls, const char *name, const char *description,
1129 bt_component_class_sink *component_class_sink;
1130 bt_component_class *component_class;
1135 component_class_sink = bt_component_class_sink_create(name, bt_py3_component_class_sink_consume);
1137 if (!component_class_sink) {
1138 BT_LOGE_STR("Cannot create sink component class.");
1142 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1144 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1148 ret = bt_component_class_sink_set_init_method(component_class_sink, bt_py3_component_class_sink_init);
1149 BT_ASSERT(ret == 0);
1150 ret = bt_component_class_sink_set_finalize_method(component_class_sink, bt_py3_component_class_sink_finalize);
1151 BT_ASSERT(ret == 0);
1152 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1153 bt_py3_component_class_sink_input_port_connected);
1154 BT_ASSERT(ret == 0);
1155 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1156 bt_py3_component_class_sink_graph_is_configured);
1157 BT_ASSERT(ret == 0);
1158 ret = bt_component_class_sink_set_query_method(component_class_sink, bt_py3_component_class_sink_query);
1159 BT_ASSERT(ret == 0);
1161 register_cc_ptr_to_py_cls(component_class, py_cls);
1164 return component_class_sink;
1168 struct bt_component_class_source *bt_py3_component_class_source_create(
1169 PyObject *py_cls, const char *name, const char *description,
1171 struct bt_component_class_filter *bt_py3_component_class_filter_create(
1172 PyObject *py_cls, const char *name, const char *description,
1174 struct bt_component_class_sink *bt_py3_component_class_sink_create(
1175 PyObject *py_cls, const char *name, const char *description,
1177 void bt_py3_cc_init_from_bt2(void);
1178 void bt_py3_cc_exit_handler(void);