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 "logging/comp-logging.h"
28 * This hash table associates a BT component class object address to a
29 * user-defined Python class (PyObject *). The keys and values are NOT
30 * owned by this hash table. The Python class objects are owned by the
31 * Python module, which should not be unloaded until it is not possible
32 * to create a user Python component anyway.
34 * This hash table is written to when a user-defined Python component
35 * class is created by one of the bt_bt2_component_class_*_create()
38 * This function is read from when a user calls bt_component_create()
39 * with a component class pointer created by one of the functions above.
40 * In this case, the original Python class needs to be found to
41 * instantiate it and associate the created Python component object with
42 * a BT component object instance.
45 static GHashTable
*bt_cc_ptr_to_py_cls
;
48 void register_cc_ptr_to_py_cls(struct bt_component_class
*bt_cc
,
51 if (!bt_cc_ptr_to_py_cls
) {
53 * Lazy-initializing this GHashTable because GLib
54 * might not be initialized yet and it needs to be
55 * before we call g_hash_table_new()
57 BT_LOGD_STR("Creating native component class to Python component class hash table.");
58 bt_cc_ptr_to_py_cls
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
59 BT_ASSERT(bt_cc_ptr_to_py_cls
);
62 g_hash_table_insert(bt_cc_ptr_to_py_cls
, (gpointer
) bt_cc
,
67 PyObject
*lookup_cc_ptr_to_py_cls(const bt_component_class
*bt_cc
)
69 if (!bt_cc_ptr_to_py_cls
) {
70 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
71 "comp-cls-addr=%p", bt_cc
);
75 return (PyObject
*) g_hash_table_lookup(bt_cc_ptr_to_py_cls
,
76 (gconstpointer
) bt_cc
);
79 /* Library destructor */
81 __attribute__((destructor
))
83 void native_comp_class_dtor(void) {
84 /* Destroy component class association hash table */
85 if (bt_cc_ptr_to_py_cls
) {
86 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
87 g_hash_table_destroy(bt_cc_ptr_to_py_cls
);
92 int py_exc_to_status(bt_self_component_class
*self_component_class
,
93 bt_self_component
*self_component
,
94 bt_self_message_iterator
*self_message_iterator
,
95 const char *module_name
, int active_log_level
)
97 int status
= __BT_FUNC_STATUS_OK
;
98 PyObject
*exc
= PyErr_Occurred();
104 if (PyErr_GivenExceptionMatches(exc
,
105 py_mod_bt2_exc_try_again_type
)) {
106 status
= __BT_FUNC_STATUS_AGAIN
;
107 } else if (PyErr_GivenExceptionMatches(exc
,
108 py_mod_bt2_exc_stop_type
)) {
109 status
= __BT_FUNC_STATUS_END
;
110 } else if (PyErr_GivenExceptionMatches(exc
,
111 py_mod_bt2_exc_unknown_object_type
)) {
112 status
= __BT_FUNC_STATUS_UNKNOWN_OBJECT
;
115 * Unknown exception: convert to general error.
117 * Because we only want to fetch the log level when
118 * we actually get an exception, and not systematically
119 * when we call py_exc_to_status() (as py_exc_to_status()
120 * can return `__BT_FUNC_STATUS_OK`), we get it here
121 * depending on the actor's type.
123 if (self_component
) {
124 active_log_level
= get_self_component_log_level(
126 } else if (self_message_iterator
) {
127 active_log_level
= get_self_message_iterator_log_level(
128 self_message_iterator
);
131 BT_ASSERT(active_log_level
!= -1);
132 log_exception_and_maybe_append_error(BT_LOG_WARNING
,
133 active_log_level
, true,
134 self_component_class
, self_component
,
135 self_message_iterator
, module_name
);
137 if (PyErr_GivenExceptionMatches(exc
,
138 py_mod_bt2_exc_memory_error
)) {
139 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
141 status
= __BT_FUNC_STATUS_ERROR
;
151 int py_exc_to_status_component_class(
152 bt_self_component_class
*self_component_class
,
153 int active_log_level
)
155 return py_exc_to_status(self_component_class
, NULL
, NULL
, NULL
,
160 int py_exc_to_status_component(bt_self_component
*self_component
)
162 return py_exc_to_status(NULL
, self_component
, NULL
, NULL
, -1);
166 int py_exc_to_status_message_iterator(
167 bt_self_message_iterator
*self_message_iterator
)
169 return py_exc_to_status(NULL
, NULL
, self_message_iterator
, NULL
, -1);
172 /* Component class proxy methods (delegate to the attached Python object) */
175 bt_component_class_init_method_status
component_class_init(
176 bt_self_component
*self_component
,
177 void *self_component_v
,
178 swig_type_info
*self_comp_cls_type_swig_type
,
179 const bt_value
*params
,
180 void *init_method_data
)
182 const bt_component
*component
= bt_self_component_as_component(self_component
);
183 const bt_component_class
*component_class
= bt_component_borrow_class_const(component
);
184 bt_component_class_init_method_status status
= __BT_FUNC_STATUS_OK
;
185 PyObject
*py_cls
= NULL
;
186 PyObject
*py_comp
= NULL
;
187 PyObject
*py_params_ptr
= NULL
;
188 PyObject
*py_comp_ptr
= NULL
;
189 bt_logging_level log_level
= get_self_component_log_level(
192 (void) init_method_data
;
194 BT_ASSERT(self_component
);
195 BT_ASSERT(self_component_v
);
196 BT_ASSERT(self_comp_cls_type_swig_type
);
199 * Get the user-defined Python class which created this
200 * component's class in the first place (borrowed
203 py_cls
= lookup_cc_ptr_to_py_cls(component_class
);
205 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
206 "Cannot find Python class associated to native component class: "
207 "comp-cls-addr=%p", component_class
);
211 /* Parameters pointer -> SWIG pointer Python object */
212 py_params_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(params
),
213 SWIGTYPE_p_bt_value
, 0);
214 if (!py_params_ptr
) {
215 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
216 "Failed to create a SWIG pointer object.");
220 py_comp_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v
),
221 self_comp_cls_type_swig_type
, 0);
223 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
224 "Failed to create a SWIG pointer object.");
229 * Do the equivalent of this:
231 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
233 * _UserComponentType._bt_init_from_native() calls the Python
234 * component object's __init__() function.
236 py_comp
= PyObject_CallMethod(py_cls
,
237 "_bt_init_from_native", "(OO)", py_comp_ptr
, py_params_ptr
);
239 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING
, log_level
, self_component
,
240 "Failed to call Python class's _bt_init_from_native() method: "
241 "py-cls-addr=%p", py_cls
);
242 status
= py_exc_to_status_component(self_component
);
247 * Our user Python component object is now fully created and
248 * initialized by the user. Since we just created it, this
249 * native component is its only (persistent) owner.
251 bt_self_component_set_data(self_component
, py_comp
);
256 status
= __BT_FUNC_STATUS_ERROR
;
259 * Clear any exception: we're returning a bad status anyway. If
260 * this call originated from Python (creation from a plugin's
261 * component class, for example), then the user gets an
262 * appropriate creation error.
268 Py_XDECREF(py_params_ptr
);
269 Py_XDECREF(py_comp_ptr
);
274 * Method of bt_component_class_source to initialize a bt_self_component_source
279 bt_component_class_init_method_status
component_class_source_init(
280 bt_self_component_source
*self_component_source
,
281 const bt_value
*params
, void *init_method_data
)
283 bt_self_component
*self_component
= bt_self_component_source_as_self_component(self_component_source
);
284 return component_class_init(
286 self_component_source
,
287 SWIGTYPE_p_bt_self_component_source
,
288 params
, init_method_data
);
292 bt_component_class_init_method_status
component_class_filter_init(
293 bt_self_component_filter
*self_component_filter
,
294 const bt_value
*params
, void *init_method_data
)
296 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
297 return component_class_init(
299 self_component_filter
,
300 SWIGTYPE_p_bt_self_component_filter
,
301 params
, init_method_data
);
305 bt_component_class_init_method_status
component_class_sink_init(
306 bt_self_component_sink
*self_component_sink
,
307 const bt_value
*params
, void *init_method_data
)
309 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
310 return component_class_init(
313 SWIGTYPE_p_bt_self_component_sink
,
314 params
, init_method_data
);
318 void component_class_finalize(bt_self_component
*self_component
)
320 PyObject
*py_comp
= bt_self_component_get_data(self_component
);
323 /* Call user's _user_finalize() method */
324 PyObject
*py_method_result
= PyObject_CallMethod(py_comp
,
325 "_user_finalize", NULL
);
327 if (PyErr_Occurred()) {
328 bt_logging_level log_level
= get_self_component_log_level(
331 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING
, log_level
, self_component
,
332 "User component's _user_finalize() method raised an exception: ignoring:");
333 logw_exception(log_level
);
337 * Ignore any exception raised by the _user_finalize() method
338 * because it won't change anything at this point: the component
339 * is being destroyed anyway.
342 Py_XDECREF(py_method_result
);
347 void component_class_source_finalize(bt_self_component_source
*self_component_source
)
349 bt_self_component
*self_component
= bt_self_component_source_as_self_component(self_component_source
);
350 component_class_finalize(self_component
);
354 void component_class_filter_finalize(bt_self_component_filter
*self_component_filter
)
356 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
357 component_class_finalize(self_component
);
361 void component_class_sink_finalize(bt_self_component_sink
*self_component_sink
)
363 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
364 component_class_finalize(self_component
);
368 bt_bool
component_class_can_seek_beginning(
369 bt_self_message_iterator
*self_message_iterator
)
372 PyObject
*py_result
= NULL
;
373 bt_bool can_seek_beginning
= false;
375 py_iter
= bt_self_message_iterator_get_data(self_message_iterator
);
378 py_result
= PyObject_GetAttrString(py_iter
, "_bt_can_seek_beginning_from_native");
380 BT_ASSERT(!py_result
|| PyBool_Check(py_result
));
383 can_seek_beginning
= PyObject_IsTrue(py_result
);
386 * Once can_seek_beginning can report errors, convert the
387 * exception to a status. For now, log and return false;
389 loge_exception_message_iterator(self_message_iterator
);
393 Py_XDECREF(py_result
);
395 return can_seek_beginning
;
399 bt_component_class_message_iterator_seek_beginning_method_status
400 component_class_seek_beginning(bt_self_message_iterator
*self_message_iterator
)
404 bt_component_class_message_iterator_seek_beginning_method_status status
;
406 py_iter
= bt_self_message_iterator_get_data(self_message_iterator
);
408 py_result
= PyObject_CallMethod(py_iter
, "_bt_seek_beginning_from_native",
410 BT_ASSERT(!py_result
|| py_result
== Py_None
);
411 status
= py_exc_to_status_message_iterator(self_message_iterator
);
412 Py_XDECREF(py_result
);
417 bt_component_class_port_connected_method_status
component_class_port_connected(
418 bt_self_component
*self_component
,
419 void *self_component_port
,
420 swig_type_info
*self_component_port_swig_type
,
421 bt_port_type self_component_port_type
,
422 const void *other_port
,
423 swig_type_info
*other_port_swig_type
)
425 bt_component_class_port_connected_method_status status
;
426 PyObject
*py_comp
= NULL
;
427 PyObject
*py_self_port_ptr
= NULL
;
428 PyObject
*py_other_port_ptr
= NULL
;
429 PyObject
*py_method_result
= NULL
;
430 bt_logging_level log_level
= get_self_component_log_level(
433 py_comp
= bt_self_component_get_data(self_component
);
435 py_self_port_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port
),
436 self_component_port_swig_type
, 0);
437 if (!py_self_port_ptr
) {
438 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
439 "Failed to create a SWIG pointer object.");
440 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
444 py_other_port_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(other_port
),
445 other_port_swig_type
, 0);
446 if (!py_other_port_ptr
) {
447 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
448 "Failed to create a SWIG pointer object.");
449 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
452 py_method_result
= PyObject_CallMethod(py_comp
,
453 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr
,
454 self_component_port_type
, py_other_port_ptr
);
455 BT_ASSERT(!py_method_result
|| py_method_result
== Py_None
);
456 status
= py_exc_to_status_component(self_component
);
459 Py_XDECREF(py_self_port_ptr
);
460 Py_XDECREF(py_other_port_ptr
);
461 Py_XDECREF(py_method_result
);
466 bt_component_class_port_connected_method_status
467 component_class_source_output_port_connected(
468 bt_self_component_source
*self_component_source
,
469 bt_self_component_port_output
*self_component_port_output
,
470 const bt_port_input
*other_port_input
)
472 bt_self_component
*self_component
= bt_self_component_source_as_self_component(self_component_source
);
474 return component_class_port_connected(
476 self_component_port_output
,
477 SWIGTYPE_p_bt_self_component_port_output
,
480 SWIGTYPE_p_bt_port_input
);
484 bt_component_class_port_connected_method_status
485 component_class_filter_input_port_connected(
486 bt_self_component_filter
*self_component_filter
,
487 bt_self_component_port_input
*self_component_port_input
,
488 const bt_port_output
*other_port_output
)
490 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
492 return component_class_port_connected(
494 self_component_port_input
,
495 SWIGTYPE_p_bt_self_component_port_input
,
498 SWIGTYPE_p_bt_port_output
);
502 bt_component_class_port_connected_method_status
503 component_class_filter_output_port_connected(
504 bt_self_component_filter
*self_component_filter
,
505 bt_self_component_port_output
*self_component_port_output
,
506 const bt_port_input
*other_port_input
)
508 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
510 return component_class_port_connected(
512 self_component_port_output
,
513 SWIGTYPE_p_bt_self_component_port_output
,
516 SWIGTYPE_p_bt_port_input
);
520 bt_component_class_port_connected_method_status
521 component_class_sink_input_port_connected(
522 bt_self_component_sink
*self_component_sink
,
523 bt_self_component_port_input
*self_component_port_input
,
524 const bt_port_output
*other_port_output
)
526 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
528 return component_class_port_connected(
530 self_component_port_input
,
531 SWIGTYPE_p_bt_self_component_port_input
,
534 SWIGTYPE_p_bt_port_output
);
538 bt_component_class_sink_graph_is_configured_method_status
539 component_class_sink_graph_is_configured(
540 bt_self_component_sink
*self_component_sink
)
542 PyObject
*py_comp
= NULL
;
543 PyObject
*py_method_result
= NULL
;
544 bt_component_class_sink_graph_is_configured_method_status status
= __BT_FUNC_STATUS_OK
;
545 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
547 py_comp
= bt_self_component_get_data(self_component
);
548 py_method_result
= PyObject_CallMethod(py_comp
,
549 "_bt_graph_is_configured_from_native", NULL
);
550 BT_ASSERT(!py_method_result
|| py_method_result
== Py_None
);
551 status
= py_exc_to_status_component(self_component
);
552 Py_XDECREF(py_method_result
);
557 bt_component_class_query_method_status
component_class_query(
558 const bt_component_class
*component_class
,
559 bt_self_component_class
*self_component_class
,
560 bt_private_query_executor
*priv_query_executor
,
561 const char *object
, const bt_value
*params
,
562 const bt_value
**result
)
564 PyObject
*py_cls
= NULL
;
565 PyObject
*py_params_ptr
= NULL
;
566 PyObject
*py_priv_query_exec_ptr
= NULL
;
567 PyObject
*py_query_func
= NULL
;
568 PyObject
*py_object
= NULL
;
569 PyObject
*py_results_addr
= NULL
;
570 bt_component_class_query_method_status status
= __BT_FUNC_STATUS_OK
;
571 const bt_query_executor
*query_exec
=
572 bt_private_query_executor_as_query_executor_const(
573 priv_query_executor
);
574 bt_logging_level log_level
=
575 bt_query_executor_get_logging_level(query_exec
);
577 py_cls
= lookup_cc_ptr_to_py_cls(component_class
);
579 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
580 "Cannot find Python class associated to native component class: "
581 "comp-cls-addr=%p", component_class
);
585 py_params_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(params
),
586 SWIGTYPE_p_bt_value
, 0);
587 if (!py_params_ptr
) {
588 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
589 "Failed to create a SWIG pointer object.");
593 py_priv_query_exec_ptr
= SWIG_NewPointerObj(
594 SWIG_as_voidptr(priv_query_executor
),
595 SWIGTYPE_p_bt_private_query_executor
, 0);
596 if (!py_priv_query_exec_ptr
) {
597 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
598 "Failed to create a SWIG pointer object.");
602 py_object
= SWIG_FromCharPtr(object
);
604 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
605 "Failed to create a Python string.");
609 py_results_addr
= PyObject_CallMethod(py_cls
,
610 "_bt_query_from_native", "(OOO)", py_priv_query_exec_ptr
,
611 py_object
, py_params_ptr
);
612 if (!py_results_addr
) {
613 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING
, log_level
, BT_LOG_TAG
,
614 "Failed to call Python class's _bt_query_from_native() method: "
615 "py-cls-addr=%p", py_cls
);
616 status
= py_exc_to_status_component_class(self_component_class
,
622 * The returned object, on success, is an integer object
623 * (PyLong) containing the address of a BT value object (new
626 *result
= PyLong_AsVoidPtr(py_results_addr
);
627 BT_ASSERT(!PyErr_Occurred());
633 status
= __BT_FUNC_STATUS_ERROR
;
636 Py_XDECREF(py_params_ptr
);
637 Py_XDECREF(py_priv_query_exec_ptr
);
638 Py_XDECREF(py_query_func
);
639 Py_XDECREF(py_object
);
640 Py_XDECREF(py_results_addr
);
645 bt_component_class_query_method_status
component_class_source_query(
646 bt_self_component_class_source
*self_component_class_source
,
647 bt_private_query_executor
*priv_query_executor
,
648 const char *object
, const bt_value
*params
,
649 const bt_value
**result
)
651 const bt_component_class_source
*component_class_source
= bt_self_component_class_source_as_component_class_source(self_component_class_source
);
652 const bt_component_class
*component_class
= bt_component_class_source_as_component_class_const(component_class_source
);
653 bt_self_component_class
*self_component_class
= bt_self_component_class_source_as_self_component_class(self_component_class_source
);
655 return component_class_query(component_class
, self_component_class
,
656 priv_query_executor
, object
, params
, result
);
660 bt_component_class_query_method_status
component_class_filter_query(
661 bt_self_component_class_filter
*self_component_class_filter
,
662 bt_private_query_executor
*priv_query_executor
,
663 const char *object
, const bt_value
*params
,
664 const bt_value
**result
)
666 const bt_component_class_filter
*component_class_filter
= bt_self_component_class_filter_as_component_class_filter(self_component_class_filter
);
667 const bt_component_class
*component_class
= bt_component_class_filter_as_component_class_const(component_class_filter
);
668 bt_self_component_class
*self_component_class
= bt_self_component_class_filter_as_self_component_class(self_component_class_filter
);
670 return component_class_query(component_class
, self_component_class
,
671 priv_query_executor
, object
, params
, result
);
675 bt_component_class_query_method_status
component_class_sink_query(
676 bt_self_component_class_sink
*self_component_class_sink
,
677 bt_private_query_executor
*priv_query_executor
,
678 const char *object
, const bt_value
*params
,
679 const bt_value
**result
)
681 const bt_component_class_sink
*component_class_sink
= bt_self_component_class_sink_as_component_class_sink(self_component_class_sink
);
682 const bt_component_class
*component_class
= bt_component_class_sink_as_component_class_const(component_class_sink
);
683 bt_self_component_class
*self_component_class
= bt_self_component_class_sink_as_self_component_class(self_component_class_sink
);
685 return component_class_query(component_class
, self_component_class
,
686 priv_query_executor
, object
, params
, result
);
690 bt_component_class_message_iterator_init_method_status
691 component_class_message_iterator_init(
692 bt_self_message_iterator
*self_message_iterator
,
693 bt_self_component
*self_component
,
694 bt_self_component_port_output
*self_component_port_output
)
696 bt_component_class_message_iterator_init_method_status status
= __BT_FUNC_STATUS_OK
;
697 PyObject
*py_comp_cls
= NULL
;
698 PyObject
*py_iter_cls
= NULL
;
699 PyObject
*py_iter_ptr
= NULL
;
700 PyObject
*py_component_port_output_ptr
= NULL
;
701 PyObject
*py_init_method_result
= NULL
;
702 PyObject
*py_iter
= NULL
;
704 bt_logging_level log_level
= get_self_component_log_level(
707 py_comp
= bt_self_component_get_data(self_component
);
709 /* Find user's Python message iterator class */
710 py_comp_cls
= PyObject_GetAttrString(py_comp
, "__class__");
712 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
713 "Cannot get Python object's `__class__` attribute.");
717 py_iter_cls
= PyObject_GetAttrString(py_comp_cls
, "_iter_cls");
719 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
720 "Cannot get Python class's `_iter_cls` attribute.");
724 py_iter_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator
),
725 SWIGTYPE_p_bt_self_message_iterator
, 0);
727 const char *err
= "Failed to create a SWIG pointer object.";
729 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
731 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
732 self_message_iterator
, err
);
737 * Create object with borrowed native message iterator
740 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
742 py_iter
= PyObject_CallMethod(py_iter_cls
, "__new__",
743 "(OO)", py_iter_cls
, py_iter_ptr
);
745 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
746 "Failed to call Python class's __new__() method: "
747 "py-cls-addr=%p", py_iter_cls
);
754 * py_iter.__init__(self_output_port)
756 * through the _init_for_native helper static method.
758 * At this point, py_iter._ptr is set, so this initialization
759 * function has access to self._component (which gives it the
760 * user Python component object from which the iterator was
763 py_component_port_output_ptr
= SWIG_NewPointerObj(
764 SWIG_as_voidptr(self_component_port_output
),
765 SWIGTYPE_p_bt_self_component_port_output
, 0);
766 if (!py_component_port_output_ptr
) {
767 const char *err
= "Failed to create a SWIG pointer object.";
769 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
771 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
772 self_message_iterator
, err
);
776 py_init_method_result
= PyObject_CallMethod(py_iter
,
777 "_bt_init_from_native", "O", py_component_port_output_ptr
);
778 if (!py_init_method_result
) {
779 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
780 "User's __init__() method failed:");
785 * Since the Python code can never instantiate a user-defined
786 * message iterator class, the native message iterator
787 * object does NOT belong to a user Python message iterator
788 * object (borrowed reference). However this Python object is
789 * owned by this native message iterator object.
791 * In the Python world, the lifetime of the native message
792 * iterator is managed by a _GenericMessageIterator
795 * _GenericMessageIterator instance:
796 * owns a native bt_message_iterator object (iter)
797 * owns a _UserMessageIterator instance (py_iter)
798 * self._ptr is a borrowed reference to the
799 * native bt_private_connection_private_message_iterator
802 bt_self_message_iterator_set_data(self_message_iterator
, py_iter
);
807 /* Handling of errors that cause a Python exception to be set. */
808 status
= py_exc_to_status_message_iterator(self_message_iterator
);
809 BT_ASSERT(status
!= __BT_FUNC_STATUS_OK
);
813 /* Handling of errors that don't cause a Python exception to be set. */
814 status
= __BT_FUNC_STATUS_ERROR
;
817 BT_ASSERT(!PyErr_Occurred());
819 Py_XDECREF(py_comp_cls
);
820 Py_XDECREF(py_iter_cls
);
821 Py_XDECREF(py_iter_ptr
);
822 Py_XDECREF(py_component_port_output_ptr
);
823 Py_XDECREF(py_init_method_result
);
829 bt_component_class_message_iterator_init_method_status
830 component_class_source_message_iterator_init(
831 bt_self_message_iterator
*self_message_iterator
,
832 bt_self_component_source
*self_component_source
,
833 bt_self_component_port_output
*self_component_port_output
)
835 bt_self_component
*self_component
= bt_self_component_source_as_self_component(self_component_source
);
837 return component_class_message_iterator_init(self_message_iterator
, self_component
, self_component_port_output
);
841 bt_component_class_message_iterator_init_method_status
842 component_class_filter_message_iterator_init(
843 bt_self_message_iterator
*self_message_iterator
,
844 bt_self_component_filter
*self_component_filter
,
845 bt_self_component_port_output
*self_component_port_output
)
847 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
849 return component_class_message_iterator_init(self_message_iterator
, self_component
, self_component_port_output
);
853 void component_class_message_iterator_finalize(
854 bt_self_message_iterator
*message_iterator
)
856 PyObject
*py_message_iter
= bt_self_message_iterator_get_data(
858 PyObject
*py_method_result
= NULL
;
860 BT_ASSERT(py_message_iter
);
862 /* Call user's _user_finalize() method */
863 py_method_result
= PyObject_CallMethod(py_message_iter
,
864 "_user_finalize", NULL
);
866 if (PyErr_Occurred()) {
867 bt_self_component
*self_comp
=
868 bt_self_message_iterator_borrow_component(
870 bt_logging_level log_level
= get_self_component_log_level(
873 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING
, log_level
, self_comp
,
874 "User's _user_finalize() method raised an exception: ignoring:");
875 logw_exception(get_self_message_iterator_log_level(
880 * Ignore any exception raised by the _user_finalize() method
881 * because it won't change anything at this point: the component
882 * is being destroyed anyway.
885 Py_XDECREF(py_method_result
);
886 Py_DECREF(py_message_iter
);
889 /* Valid for both sources and filters. */
892 bt_component_class_message_iterator_next_method_status
893 component_class_message_iterator_next(
894 bt_self_message_iterator
*message_iterator
,
895 bt_message_array_const msgs
, uint64_t capacity
,
898 bt_component_class_message_iterator_next_method_status status
= __BT_FUNC_STATUS_OK
;
899 PyObject
*py_message_iter
= bt_self_message_iterator_get_data(message_iterator
);
900 PyObject
*py_method_result
= NULL
;
902 BT_ASSERT(py_message_iter
);
903 py_method_result
= PyObject_CallMethod(py_message_iter
,
904 "_bt_next_from_native", NULL
);
905 if (!py_method_result
) {
906 status
= py_exc_to_status_message_iterator(message_iterator
);
907 BT_ASSERT(status
!= __BT_FUNC_STATUS_OK
);
912 * The returned object, on success, is an integer object
913 * (PyLong) containing the address of a native message
914 * object (which is now ours).
916 msgs
[0] = PyLong_AsVoidPtr(py_method_result
);
919 /* Clear potential overflow error; should never happen */
920 BT_ASSERT(!PyErr_Occurred());
924 Py_XDECREF(py_method_result
);
929 bt_component_class_sink_consume_method_status
930 component_class_sink_consume(bt_self_component_sink
*self_component_sink
)
932 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
933 PyObject
*py_comp
= bt_self_component_get_data(self_component
);
934 PyObject
*py_method_result
= NULL
;
935 bt_component_class_sink_consume_method_status status
;
938 py_method_result
= PyObject_CallMethod(py_comp
,
939 "_user_consume", NULL
);
940 status
= py_exc_to_status_component(self_component
);
941 BT_ASSERT(py_method_result
|| status
!= __BT_FUNC_STATUS_OK
);
942 Py_XDECREF(py_method_result
);
947 int component_class_set_help_and_desc(
948 bt_component_class
*component_class
,
949 const char *description
, const char *help
)
954 ret
= bt_component_class_set_description(component_class
, description
);
956 BT_LOGE("Cannot set component class's description: "
957 "comp-cls-addr=%p", component_class
);
963 ret
= bt_component_class_set_help(component_class
, help
);
965 BT_LOGE("Cannot set component class's help text: "
966 "comp-cls-addr=%p", component_class
);
978 bt_component_class_source
*bt_bt2_component_class_source_create(
979 PyObject
*py_cls
, const char *name
, const char *description
,
982 bt_component_class_source
*component_class_source
;
983 bt_component_class
*component_class
;
987 component_class_source
= bt_component_class_source_create(name
,
988 component_class_message_iterator_next
);
989 if (!component_class_source
) {
990 BT_LOGE_STR("Cannot create source component class.");
994 component_class
= bt_component_class_source_as_component_class(component_class_source
);
996 if (component_class_set_help_and_desc(component_class
, description
, help
)) {
1000 ret
= bt_component_class_source_set_init_method(component_class_source
, component_class_source_init
);
1001 BT_ASSERT(ret
== 0);
1002 ret
= bt_component_class_source_set_finalize_method(component_class_source
, component_class_source_finalize
);
1003 BT_ASSERT(ret
== 0);
1004 ret
= bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source
,
1005 component_class_can_seek_beginning
);
1006 BT_ASSERT(ret
== 0);
1007 ret
= bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source
,
1008 component_class_seek_beginning
);
1009 BT_ASSERT(ret
== 0);
1010 ret
= bt_component_class_source_set_output_port_connected_method(component_class_source
,
1011 component_class_source_output_port_connected
);
1012 BT_ASSERT(ret
== 0);
1013 ret
= bt_component_class_source_set_query_method(component_class_source
, component_class_source_query
);
1014 BT_ASSERT(ret
== 0);
1015 ret
= bt_component_class_source_set_message_iterator_init_method(
1016 component_class_source
, component_class_source_message_iterator_init
);
1017 BT_ASSERT(ret
== 0);
1018 ret
= bt_component_class_source_set_message_iterator_finalize_method(
1019 component_class_source
, component_class_message_iterator_finalize
);
1020 BT_ASSERT(ret
== 0);
1021 register_cc_ptr_to_py_cls(component_class
, py_cls
);
1024 return component_class_source
;
1028 bt_component_class_filter
*bt_bt2_component_class_filter_create(
1029 PyObject
*py_cls
, const char *name
, const char *description
,
1032 bt_component_class
*component_class
;
1033 bt_component_class_filter
*component_class_filter
;
1037 component_class_filter
= bt_component_class_filter_create(name
,
1038 component_class_message_iterator_next
);
1039 if (!component_class_filter
) {
1040 BT_LOGE_STR("Cannot create filter component class.");
1044 component_class
= bt_component_class_filter_as_component_class(component_class_filter
);
1046 if (component_class_set_help_and_desc(component_class
, description
, help
)) {
1050 ret
= bt_component_class_filter_set_init_method(component_class_filter
, component_class_filter_init
);
1051 BT_ASSERT(ret
== 0);
1052 ret
= bt_component_class_filter_set_finalize_method (component_class_filter
, component_class_filter_finalize
);
1053 BT_ASSERT(ret
== 0);
1054 ret
= bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter
,
1055 component_class_can_seek_beginning
);
1056 BT_ASSERT(ret
== 0);
1057 ret
= bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter
,
1058 component_class_seek_beginning
);
1059 BT_ASSERT(ret
== 0);
1060 ret
= bt_component_class_filter_set_input_port_connected_method(component_class_filter
,
1061 component_class_filter_input_port_connected
);
1062 BT_ASSERT(ret
== 0);
1063 ret
= bt_component_class_filter_set_output_port_connected_method(component_class_filter
,
1064 component_class_filter_output_port_connected
);
1065 BT_ASSERT(ret
== 0);
1066 ret
= bt_component_class_filter_set_query_method(component_class_filter
, component_class_filter_query
);
1067 BT_ASSERT(ret
== 0);
1068 ret
= bt_component_class_filter_set_message_iterator_init_method(
1069 component_class_filter
, component_class_filter_message_iterator_init
);
1070 BT_ASSERT(ret
== 0);
1071 ret
= bt_component_class_filter_set_message_iterator_finalize_method(
1072 component_class_filter
, component_class_message_iterator_finalize
);
1073 BT_ASSERT(ret
== 0);
1074 register_cc_ptr_to_py_cls(component_class
, py_cls
);
1077 return component_class_filter
;
1081 bt_component_class_sink
*bt_bt2_component_class_sink_create(
1082 PyObject
*py_cls
, const char *name
, const char *description
,
1085 bt_component_class_sink
*component_class_sink
;
1086 bt_component_class
*component_class
;
1090 component_class_sink
= bt_component_class_sink_create(name
, component_class_sink_consume
);
1092 if (!component_class_sink
) {
1093 BT_LOGE_STR("Cannot create sink component class.");
1097 component_class
= bt_component_class_sink_as_component_class(component_class_sink
);
1099 if (component_class_set_help_and_desc(component_class
, description
, help
)) {
1103 ret
= bt_component_class_sink_set_init_method(component_class_sink
, component_class_sink_init
);
1104 BT_ASSERT(ret
== 0);
1105 ret
= bt_component_class_sink_set_finalize_method(component_class_sink
, component_class_sink_finalize
);
1106 BT_ASSERT(ret
== 0);
1107 ret
= bt_component_class_sink_set_input_port_connected_method(component_class_sink
,
1108 component_class_sink_input_port_connected
);
1109 BT_ASSERT(ret
== 0);
1110 ret
= bt_component_class_sink_set_graph_is_configured_method(component_class_sink
,
1111 component_class_sink_graph_is_configured
);
1112 BT_ASSERT(ret
== 0);
1113 ret
= bt_component_class_sink_set_query_method(component_class_sink
, component_class_sink_query
);
1114 BT_ASSERT(ret
== 0);
1115 register_cc_ptr_to_py_cls(component_class
, py_cls
);
1118 return component_class_sink
;