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;
58 void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
61 if (!bt_cc_ptr_to_py_cls) {
63 * Lazy-initializing this GHashTable because GLib
64 * might not be initialized yet and it needs to be
65 * before we call g_hash_table_new()
67 BT_LOGD_STR("Creating native component class to Python component class hash table.");
68 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
69 BT_ASSERT(bt_cc_ptr_to_py_cls);
72 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
77 PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
79 if (!bt_cc_ptr_to_py_cls) {
80 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
81 "comp-cls-addr=%p", bt_cc);
85 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
86 (gconstpointer) bt_cc);
91 * Useful Python objects.
94 static PyObject *py_mod_bt2 = NULL;
95 static PyObject *py_mod_bt2_exc_error_type = NULL;
96 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
97 static PyObject *py_mod_bt2_exc_stop_type = NULL;
98 static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
99 static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL;
100 static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL;
103 void bt_py3_cc_init_from_bt2(void)
106 * This is called once the bt2 package is loaded.
108 * Those modules and functions are needed while the package is
109 * used. Loading them here is safe because we know the bt2
110 * package is imported, and we know that the user cannot use the
111 * code here without importing bt2 first.
113 py_mod_bt2 = PyImport_ImportModule("bt2");
114 BT_ASSERT(py_mod_bt2);
115 py_mod_bt2_exc_error_type =
116 PyObject_GetAttrString(py_mod_bt2, "Error");
117 BT_ASSERT(py_mod_bt2_exc_error_type);
118 py_mod_bt2_exc_try_again_type =
119 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
120 BT_ASSERT(py_mod_bt2_exc_try_again_type);
121 py_mod_bt2_exc_stop_type =
122 PyObject_GetAttrString(py_mod_bt2, "Stop");
123 BT_ASSERT(py_mod_bt2_exc_stop_type);
124 py_mod_bt2_exc_invalid_query_object_type =
125 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject");
126 BT_ASSERT(py_mod_bt2_exc_invalid_query_object_type);
127 py_mod_bt2_exc_invalid_query_params_type =
128 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams");
129 BT_ASSERT(py_mod_bt2_exc_invalid_query_params_type);
133 void bt_py3_cc_exit_handler(void)
136 * This is an exit handler (set by the bt2 package).
138 * We only give back the references that we took in
139 * bt_py3_cc_init_from_bt2() here. The global variables continue
140 * to exist for the code of this file, but they are now borrowed
141 * references. If this code is executed, it means that somehow
142 * the modules are still loaded, so it should be safe to use
143 * them even without a strong reference.
145 * We cannot do this in the library's destructor because it
146 * gets executed once Python is already finalized.
148 Py_XDECREF(py_mod_bt2);
149 Py_XDECREF(py_mod_bt2_exc_error_type);
150 Py_XDECREF(py_mod_bt2_exc_try_again_type);
151 Py_XDECREF(py_mod_bt2_exc_stop_type);
152 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
153 Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type);
154 Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type);
158 /* Library destructor */
160 __attribute__((destructor))
162 void bt_py3_native_comp_class_dtor(void) {
163 /* Destroy component class association hash table */
164 if (bt_cc_ptr_to_py_cls) {
165 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
166 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
171 void bt2_py_loge_exception(void)
175 BT_ASSERT(PyErr_Occurred() != NULL);
176 gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL);
178 /* bt_py_common_format_exception() logs errors */
182 BT_LOGE_STR(gstr->str);
186 g_string_free(gstr, TRUE);
191 bt_self_component_status bt_py3_exc_to_self_component_status(void)
193 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
194 PyObject *exc = PyErr_Occurred();
200 if (PyErr_GivenExceptionMatches(exc,
201 py_mod_bt2_exc_try_again_type)) {
202 status = BT_SELF_COMPONENT_STATUS_AGAIN;
203 } else if (PyErr_GivenExceptionMatches(exc,
204 py_mod_bt2_exc_stop_type)) {
205 status = BT_SELF_COMPONENT_STATUS_END;
207 bt2_py_loge_exception();
208 status = BT_SELF_COMPONENT_STATUS_ERROR;
216 /* Component class proxy methods (delegate to the attached Python object) */
219 bt_self_message_iterator_status bt_py3_exc_to_self_message_iterator_status(void)
221 enum bt_self_message_iterator_status status =
222 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
223 PyObject *exc = PyErr_Occurred();
229 if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) {
230 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
231 } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) {
232 status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN;
234 bt2_py_loge_exception();
235 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
244 enum bt_query_status bt_py3_exc_to_query_status(void)
246 enum bt_query_status status = BT_QUERY_STATUS_OK;
247 PyObject *exc = PyErr_Occurred();
253 if (PyErr_GivenExceptionMatches(exc,
254 py_mod_bt2_exc_invalid_query_object_type)) {
255 status = BT_QUERY_STATUS_INVALID_OBJECT;
256 } else if (PyErr_GivenExceptionMatches(exc,
257 py_mod_bt2_exc_invalid_query_params_type)) {
258 status = BT_QUERY_STATUS_INVALID_PARAMS;
259 } else if (PyErr_GivenExceptionMatches(exc,
260 py_mod_bt2_exc_try_again_type)) {
261 status = BT_QUERY_STATUS_AGAIN;
263 bt2_py_loge_exception();
264 status = BT_QUERY_STATUS_ERROR;
273 bt_self_component_status bt_py3_component_class_init(
274 bt_self_component *self_component,
275 void *self_component_v,
276 swig_type_info *self_comp_cls_type_swig_type,
277 const bt_value *params,
278 void *init_method_data)
280 const bt_component *component = bt_self_component_as_component(self_component);
281 const bt_component_class *component_class = bt_component_borrow_class_const(component);
282 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
283 PyObject *py_cls = NULL;
284 PyObject *py_comp = NULL;
285 PyObject *py_params_ptr = NULL;
286 PyObject *py_comp_ptr = NULL;
288 (void) init_method_data;
290 BT_ASSERT(self_component);
291 BT_ASSERT(self_component_v);
292 BT_ASSERT(self_comp_cls_type_swig_type);
295 * Get the user-defined Python class which created this
296 * component's class in the first place (borrowed
299 py_cls = lookup_cc_ptr_to_py_cls(component_class);
301 BT_LOGE("Cannot find Python class associated to native component class: "
302 "comp-cls-addr=%p", component_class);
306 /* Parameters pointer -> SWIG pointer Python object */
307 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
308 SWIGTYPE_p_bt_value, 0);
309 if (!py_params_ptr) {
310 BT_LOGE_STR("Failed to create a SWIG pointer object.");
314 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
315 self_comp_cls_type_swig_type, 0);
317 BT_LOGE_STR("Failed to create a SWIG pointer object.");
322 * Do the equivalent of this:
324 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
326 * _UserComponentType._init_from_native() calls the Python
327 * component object's __init__() function.
329 py_comp = PyObject_CallMethod(py_cls,
330 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
332 bt2_py_loge_exception();
333 BT_LOGE("Failed to call Python class's _init_from_native() method: "
334 "py-cls-addr=%p", py_cls);
340 * Our user Python component object is now fully created and
341 * initialized by the user. Since we just created it, this
342 * native component is its only (persistent) owner.
344 bt_self_component_set_data(self_component, py_comp);
349 status = BT_SELF_COMPONENT_STATUS_ERROR;
352 * Clear any exception: we're returning a bad status anyway. If
353 * this call originated from Python (creation from a plugin's
354 * component class, for example), then the user gets an
355 * appropriate creation error.
361 Py_XDECREF(py_params_ptr);
362 Py_XDECREF(py_comp_ptr);
367 * Method of bt_component_class_source to initialize a bt_self_component_source
372 bt_self_component_status bt_py3_component_class_source_init(
373 bt_self_component_source *self_component_source,
374 const bt_value *params, void *init_method_data)
376 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
377 return bt_py3_component_class_init(
379 self_component_source,
380 SWIGTYPE_p_bt_self_component_source,
381 params, init_method_data);
385 bt_self_component_status bt_py3_component_class_filter_init(
386 bt_self_component_filter *self_component_filter,
387 const bt_value *params, void *init_method_data)
389 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
390 return bt_py3_component_class_init(
392 self_component_filter,
393 SWIGTYPE_p_bt_self_component_filter,
394 params, init_method_data);
398 bt_self_component_status bt_py3_component_class_sink_init(
399 bt_self_component_sink *self_component_sink,
400 const bt_value *params, void *init_method_data)
402 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
403 return bt_py3_component_class_init(
406 SWIGTYPE_p_bt_self_component_sink,
407 params, init_method_data);
411 void bt_py3_component_class_finalize(bt_self_component *self_component)
413 PyObject *py_comp = bt_self_component_get_data(self_component);
416 /* Call user's _finalize() method */
417 PyObject *py_method_result = PyObject_CallMethod(py_comp,
420 if (PyErr_Occurred()) {
421 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
425 * Ignore any exception raised by the _finalize() method because
426 * it won't change anything at this point: the component is
427 * being destroyed anyway.
430 Py_XDECREF(py_method_result);
435 void bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source)
437 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
438 bt_py3_component_class_finalize(self_component);
442 void bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter)
444 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
445 bt_py3_component_class_finalize(self_component);
449 void bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink)
451 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
452 bt_py3_component_class_finalize(self_component);
456 bt_self_component_status bt_py3_component_class_port_connected(
457 bt_self_component *self_component,
458 void *self_component_port,
459 swig_type_info *self_component_port_swig_type,
460 bt_port_type self_component_port_type,
461 const void *other_port,
462 swig_type_info *other_port_swig_type)
464 bt_self_component_status status;
465 PyObject *py_comp = NULL;
466 PyObject *py_self_port_ptr = NULL;
467 PyObject *py_other_port_ptr = NULL;
468 PyObject *py_method_result = NULL;
470 py_comp = bt_self_component_get_data(self_component);
473 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
474 self_component_port_swig_type, 0);
475 if (!py_self_port_ptr) {
476 BT_LOGF_STR("Failed to create a SWIG pointer object.");
477 status = BT_SELF_COMPONENT_STATUS_NOMEM;
481 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
482 other_port_swig_type, 0);
483 if (!py_other_port_ptr) {
484 BT_LOGF_STR("Failed to create a SWIG pointer object.");
485 status = BT_SELF_COMPONENT_STATUS_NOMEM;
488 py_method_result = PyObject_CallMethod(py_comp,
489 "_port_connected_from_native", "(OiO)", py_self_port_ptr,
490 self_component_port_type, py_other_port_ptr);
492 BT_ASSERT(!py_method_result || py_method_result == Py_None);
494 status = bt_py3_exc_to_self_component_status();
497 Py_XDECREF(py_self_port_ptr);
498 Py_XDECREF(py_other_port_ptr);
499 Py_XDECREF(py_method_result);
505 bt_self_component_status bt_py3_component_class_source_output_port_connected(
506 bt_self_component_source *self_component_source,
507 bt_self_component_port_output *self_component_port_output,
508 const bt_port_input *other_port_input)
510 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
512 return bt_py3_component_class_port_connected(
514 self_component_port_output,
515 SWIGTYPE_p_bt_self_component_port_output,
518 SWIGTYPE_p_bt_port_input);
522 bt_self_component_status bt_py3_component_class_filter_input_port_connected(
523 bt_self_component_filter *self_component_filter,
524 bt_self_component_port_input *self_component_port_input,
525 const bt_port_output *other_port_output)
527 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
529 return bt_py3_component_class_port_connected(
531 self_component_port_input,
532 SWIGTYPE_p_bt_self_component_port_input,
535 SWIGTYPE_p_bt_port_output);
539 bt_self_component_status bt_py3_component_class_filter_output_port_connected(
540 bt_self_component_filter *self_component_filter,
541 bt_self_component_port_output *self_component_port_output,
542 const bt_port_input *other_port_input)
544 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
546 return bt_py3_component_class_port_connected(
548 self_component_port_output,
549 SWIGTYPE_p_bt_self_component_port_output,
552 SWIGTYPE_p_bt_port_input);
556 bt_self_component_status bt_py3_component_class_sink_input_port_connected(
557 bt_self_component_sink *self_component_sink,
558 bt_self_component_port_input *self_component_port_input,
559 const bt_port_output *other_port_output)
561 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
563 return bt_py3_component_class_port_connected(
565 self_component_port_input,
566 SWIGTYPE_p_bt_self_component_port_input,
569 SWIGTYPE_p_bt_port_output);
573 bt_self_component_status bt_py3_component_class_sink_graph_is_configured(
574 bt_self_component_sink *self_component_sink)
576 PyObject *py_comp = NULL;
577 PyObject *py_method_result = NULL;
578 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
579 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
581 py_comp = bt_self_component_get_data(self_component);
582 py_method_result = PyObject_CallMethod(py_comp,
583 "_graph_is_configured_from_native", NULL);
585 BT_ASSERT(!py_method_result || py_method_result == Py_None);
587 status = bt_py3_exc_to_self_component_status();
589 Py_XDECREF(py_method_result);
595 bt_query_status bt_py3_component_class_query(
596 const bt_component_class *component_class,
597 const bt_query_executor *query_executor,
598 const char *object, const bt_value *params,
599 bt_logging_level log_level,
600 const bt_value **result)
602 PyObject *py_cls = NULL;
603 PyObject *py_params_ptr = NULL;
604 PyObject *py_query_exec_ptr = NULL;
605 PyObject *py_query_func = NULL;
606 PyObject *py_object = NULL;
607 PyObject *py_results_addr = NULL;
608 bt_query_status status = BT_QUERY_STATUS_OK;
610 py_cls = lookup_cc_ptr_to_py_cls(component_class);
612 BT_LOGE("Cannot find Python class associated to native component class: "
613 "comp-cls-addr=%p", component_class);
617 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
618 SWIGTYPE_p_bt_value, 0);
619 if (!py_params_ptr) {
620 BT_LOGE_STR("Failed to create a SWIG pointer object.");
624 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
625 SWIGTYPE_p_bt_query_executor, 0);
626 if (!py_query_exec_ptr) {
627 BT_LOGE_STR("Failed to create a SWIG pointer object.");
631 py_object = SWIG_FromCharPtr(object);
633 BT_LOGE_STR("Failed to create a Python string.");
637 py_results_addr = PyObject_CallMethod(py_cls,
638 "_query_from_native", "(OOOi)", py_query_exec_ptr,
639 py_object, py_params_ptr, (int) log_level);
641 if (!py_results_addr) {
642 BT_LOGE("Failed to call Python class's _query_from_native() method: "
643 "py-cls-addr=%p", py_cls);
644 status = bt_py3_exc_to_query_status();
649 * The returned object, on success, is an integer object
650 * (PyLong) containing the address of a BT value object (new
653 *result = PyLong_AsVoidPtr(py_results_addr);
654 BT_ASSERT(!PyErr_Occurred());
660 status = BT_QUERY_STATUS_ERROR;
663 Py_XDECREF(py_params_ptr);
664 Py_XDECREF(py_query_exec_ptr);
665 Py_XDECREF(py_query_func);
666 Py_XDECREF(py_object);
667 Py_XDECREF(py_results_addr);
672 bt_query_status bt_py3_component_class_source_query(
673 bt_self_component_class_source *self_component_class_source,
674 const bt_query_executor *query_executor,
675 const char *object, const bt_value *params,
676 bt_logging_level log_level,
677 const bt_value **result)
679 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
680 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
681 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
685 bt_query_status bt_py3_component_class_filter_query(
686 bt_self_component_class_filter *self_component_class_filter,
687 const bt_query_executor *query_executor,
688 const char *object, const bt_value *params,
689 bt_logging_level log_level,
690 const bt_value **result)
692 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
693 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
694 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
698 bt_query_status bt_py3_component_class_sink_query(
699 bt_self_component_class_sink *self_component_class_sink,
700 const bt_query_executor *query_executor,
701 const char *object, const bt_value *params,
702 bt_logging_level log_level,
703 const bt_value **result)
705 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
706 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
707 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
711 bt_self_message_iterator_status bt_py3_component_class_message_iterator_init(
712 bt_self_message_iterator *self_message_iterator,
713 bt_self_component *self_component,
714 bt_self_component_port_output *self_component_port_output)
716 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
717 PyObject *py_comp_cls = NULL;
718 PyObject *py_iter_cls = NULL;
719 PyObject *py_iter_ptr = NULL;
720 PyObject *py_component_port_output_ptr = NULL;
721 PyObject *py_init_method_result = NULL;
722 PyObject *py_iter = NULL;
725 py_comp = bt_self_component_get_data(self_component);
727 /* Find user's Python message iterator class */
728 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
730 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
734 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
736 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
740 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
741 SWIGTYPE_p_bt_self_message_iterator, 0);
743 BT_LOGE_STR("Failed to create a SWIG pointer object.");
748 * Create object with borrowed native message iterator
751 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
753 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
754 "(OO)", py_iter_cls, py_iter_ptr);
756 BT_LOGE("Failed to call Python class's __new__() method: "
757 "py-cls-addr=%p", py_iter_cls);
758 bt2_py_loge_exception();
765 * py_iter.__init__(self_output_port)
767 * through the _init_for_native helper static method.
769 * At this point, py_iter._ptr is set, so this initialization
770 * function has access to self._component (which gives it the
771 * user Python component object from which the iterator was
774 py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output),
775 SWIGTYPE_p_bt_self_component_port_output, 0);
776 if (!py_component_port_output_ptr) {
777 BT_LOGE_STR("Failed to create a SWIG pointer object.");
781 py_init_method_result = PyObject_CallMethod(py_iter, "_init_from_native", "O", py_component_port_output_ptr);
782 if (!py_init_method_result) {
783 BT_LOGE_STR("User's __init__() method failed.");
784 bt2_py_loge_exception();
789 * Since the Python code can never instantiate a user-defined
790 * message iterator class, the native message iterator
791 * object does NOT belong to a user Python message iterator
792 * object (borrowed reference). However this Python object is
793 * owned by this native message iterator object.
795 * In the Python world, the lifetime of the native message
796 * iterator is managed by a _GenericMessageIterator
799 * _GenericMessageIterator instance:
800 * owns a native bt_message_iterator object (iter)
801 * owns a _UserMessageIterator instance (py_iter)
802 * self._ptr is a borrowed reference to the
803 * native bt_private_connection_private_message_iterator
806 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
811 status = bt_py3_exc_to_self_message_iterator_status();
812 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
814 * Looks like there wasn't any exception from the Python
815 * side, but we're still in an error state here.
817 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
821 * Clear any exception: we're returning a bad status anyway. If
822 * this call originated from Python, then the user gets an
823 * appropriate creation error.
828 Py_XDECREF(py_comp_cls);
829 Py_XDECREF(py_iter_cls);
830 Py_XDECREF(py_iter_ptr);
831 Py_XDECREF(py_component_port_output_ptr);
832 Py_XDECREF(py_init_method_result);
838 bt_self_message_iterator_status bt_py3_component_class_source_message_iterator_init(
839 bt_self_message_iterator *self_message_iterator,
840 bt_self_component_source *self_component_source,
841 bt_self_component_port_output *self_component_port_output)
843 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
844 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
848 bt_self_message_iterator_status bt_py3_component_class_filter_message_iterator_init(
849 bt_self_message_iterator *self_message_iterator,
850 bt_self_component_filter *self_component_filter,
851 bt_self_component_port_output *self_component_port_output)
853 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
854 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
858 void bt_py3_component_class_message_iterator_finalize(
859 bt_self_message_iterator *message_iterator)
861 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
862 PyObject *py_method_result = NULL;
864 BT_ASSERT(py_message_iter);
866 /* Call user's _finalize() method */
867 py_method_result = PyObject_CallMethod(py_message_iter,
870 if (PyErr_Occurred()) {
871 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
875 * Ignore any exception raised by the _finalize() method because
876 * it won't change anything at this point: the component is
877 * being destroyed anyway.
880 Py_XDECREF(py_method_result);
881 Py_DECREF(py_message_iter);
884 /* Valid for both sources and filters. */
887 bt_self_message_iterator_status bt_py3_component_class_message_iterator_next(
888 bt_self_message_iterator *message_iterator,
889 bt_message_array_const msgs, uint64_t capacity,
892 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
893 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
894 PyObject *py_method_result = NULL;
896 BT_ASSERT(py_message_iter);
897 py_method_result = PyObject_CallMethod(py_message_iter,
898 "_next_from_native", NULL);
899 if (!py_method_result) {
900 status = bt_py3_exc_to_self_message_iterator_status();
901 BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK);
906 * The returned object, on success, is an integer object
907 * (PyLong) containing the address of a native message
908 * object (which is now ours).
910 msgs[0] = PyLong_AsVoidPtr(py_method_result);
913 /* Clear potential overflow error; should never happen */
914 BT_ASSERT(!PyErr_Occurred());
918 Py_XDECREF(py_method_result);
923 bt_self_component_status bt_py3_component_class_sink_consume(
924 bt_self_component_sink *self_component_sink)
926 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
927 PyObject *py_comp = bt_self_component_get_data(self_component);
928 PyObject *py_method_result = NULL;
929 bt_self_component_status status;
932 py_method_result = PyObject_CallMethod(py_comp,
935 status = bt_py3_exc_to_self_component_status();
936 if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) {
937 /* Pretty sure this should never happen, but just in case */
938 BT_LOGE("User's _consume() method failed without raising an exception: "
939 "status=%d", status);
940 status = BT_SELF_COMPONENT_STATUS_ERROR;
943 Py_XDECREF(py_method_result);
948 int bt_py3_component_class_set_help_and_desc(
949 bt_component_class *component_class,
950 const char *description, const char *help)
955 ret = bt_component_class_set_description(component_class, description);
957 BT_LOGE("Cannot set component class's description: "
958 "comp-cls-addr=%p", component_class);
964 ret = bt_component_class_set_help(component_class, help);
966 BT_LOGE("Cannot set component class's help text: "
967 "comp-cls-addr=%p", component_class);
979 bt_component_class_source *bt_py3_component_class_source_create(
980 PyObject *py_cls, const char *name, const char *description,
983 bt_component_class_source *component_class_source;
984 bt_component_class *component_class;
989 component_class_source = bt_component_class_source_create(name,
990 bt_py3_component_class_message_iterator_next);
991 if (!component_class_source) {
992 BT_LOGE_STR("Cannot create source component class.");
996 component_class = bt_component_class_source_as_component_class(component_class_source);
998 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1002 ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init);
1003 BT_ASSERT(ret == 0);
1004 ret = bt_component_class_source_set_finalize_method (component_class_source, bt_py3_component_class_source_finalize);
1005 BT_ASSERT(ret == 0);
1006 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1007 bt_py3_component_class_source_output_port_connected);
1008 BT_ASSERT(ret == 0);
1009 ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query);
1010 BT_ASSERT(ret == 0);
1011 ret = bt_component_class_source_set_message_iterator_init_method(
1012 component_class_source, bt_py3_component_class_source_message_iterator_init);
1013 BT_ASSERT(ret == 0);
1014 ret = bt_component_class_source_set_message_iterator_finalize_method(
1015 component_class_source, bt_py3_component_class_message_iterator_finalize);
1016 BT_ASSERT(ret == 0);
1018 register_cc_ptr_to_py_cls(component_class, py_cls);
1021 return component_class_source;
1025 bt_component_class_filter *bt_py3_component_class_filter_create(
1026 PyObject *py_cls, const char *name, const char *description,
1029 bt_component_class *component_class;
1030 bt_component_class_filter *component_class_filter;
1035 component_class_filter = bt_component_class_filter_create(name,
1036 bt_py3_component_class_message_iterator_next);
1037 if (!component_class_filter) {
1038 BT_LOGE_STR("Cannot create filter component class.");
1042 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1044 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1048 ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init);
1049 BT_ASSERT(ret == 0);
1050 ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize);
1051 BT_ASSERT(ret == 0);
1052 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1053 bt_py3_component_class_filter_input_port_connected);
1054 BT_ASSERT(ret == 0);
1055 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1056 bt_py3_component_class_filter_output_port_connected);
1057 BT_ASSERT(ret == 0);
1058 ret = bt_component_class_filter_set_query_method(component_class_filter, bt_py3_component_class_filter_query);
1059 BT_ASSERT(ret == 0);
1060 ret = bt_component_class_filter_set_message_iterator_init_method(
1061 component_class_filter, bt_py3_component_class_filter_message_iterator_init);
1062 BT_ASSERT(ret == 0);
1063 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1064 component_class_filter, bt_py3_component_class_message_iterator_finalize);
1065 BT_ASSERT(ret == 0);
1067 register_cc_ptr_to_py_cls(component_class, py_cls);
1070 return component_class_filter;
1074 bt_component_class_sink *bt_py3_component_class_sink_create(
1075 PyObject *py_cls, const char *name, const char *description,
1078 bt_component_class_sink *component_class_sink;
1079 bt_component_class *component_class;
1084 component_class_sink = bt_component_class_sink_create(name, bt_py3_component_class_sink_consume);
1086 if (!component_class_sink) {
1087 BT_LOGE_STR("Cannot create sink component class.");
1091 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1093 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1097 ret = bt_component_class_sink_set_init_method(component_class_sink, bt_py3_component_class_sink_init);
1098 BT_ASSERT(ret == 0);
1099 ret = bt_component_class_sink_set_finalize_method(component_class_sink, bt_py3_component_class_sink_finalize);
1100 BT_ASSERT(ret == 0);
1101 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1102 bt_py3_component_class_sink_input_port_connected);
1103 BT_ASSERT(ret == 0);
1104 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1105 bt_py3_component_class_sink_graph_is_configured);
1106 BT_ASSERT(ret == 0);
1107 ret = bt_component_class_sink_set_query_method(component_class_sink, bt_py3_component_class_sink_query);
1108 BT_ASSERT(ret == 0);
1110 register_cc_ptr_to_py_cls(component_class, py_cls);
1113 return component_class_sink;
1117 struct bt_component_class_source *bt_py3_component_class_source_create(
1118 PyObject *py_cls, const char *name, const char *description,
1120 struct bt_component_class_filter *bt_py3_component_class_filter_create(
1121 PyObject *py_cls, const char *name, const char *description,
1123 struct bt_component_class_sink *bt_py3_component_class_sink_create(
1124 PyObject *py_cls, const char *name, const char *description,
1126 void bt_py3_cc_init_from_bt2(void);
1127 void bt_py3_cc_exit_handler(void);