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"
26 #include "compat/glib.h"
29 * This hash table associates a BT component class object address to a
30 * user-defined Python class (PyObject *). The keys and values are NOT
31 * owned by this hash table. The Python class objects are owned by the
32 * Python module, which should not be unloaded until it is not possible
33 * to create a user Python component anyway.
35 * This hash table is written to when a user-defined Python component
36 * class is created by one of the bt_bt2_component_class_*_create()
39 * This function is read from when a user calls bt_component_create()
40 * with a component class pointer created by one of the functions above.
41 * In this case, the original Python class needs to be found to
42 * instantiate it and associate the created Python component object with
43 * a BT component object instance.
46 static GHashTable
*bt_cc_ptr_to_py_cls
;
49 void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class
*comp_cls
)
53 if (!bt_cc_ptr_to_py_cls
) {
57 existed
= g_hash_table_remove(bt_cc_ptr_to_py_cls
, comp_cls
);
62 void register_cc_ptr_to_py_cls(struct bt_component_class
*bt_cc
,
65 if (!bt_cc_ptr_to_py_cls
) {
67 * Lazy-initializing this GHashTable because GLib
68 * might not be initialized yet and it needs to be
69 * before we call g_hash_table_new()
71 BT_LOGD_STR("Creating native component class to Python component class hash table.");
72 bt_cc_ptr_to_py_cls
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
73 BT_ASSERT(bt_cc_ptr_to_py_cls
);
76 g_hash_table_insert(bt_cc_ptr_to_py_cls
, (gpointer
) bt_cc
,
81 PyObject
*lookup_cc_ptr_to_py_cls(const bt_component_class
*bt_cc
)
83 if (!bt_cc_ptr_to_py_cls
) {
84 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
85 "comp-cls-addr=%p", bt_cc
);
89 return (PyObject
*) g_hash_table_lookup(bt_cc_ptr_to_py_cls
,
90 (gconstpointer
) bt_cc
);
93 /* Library destructor */
95 __attribute__((destructor
))
97 void native_comp_class_dtor(void) {
98 /* Destroy component class association hash table */
99 if (bt_cc_ptr_to_py_cls
) {
100 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
101 g_hash_table_destroy(bt_cc_ptr_to_py_cls
);
102 bt_cc_ptr_to_py_cls
= NULL
;
107 int py_exc_to_status(bt_self_component_class
*self_component_class
,
108 bt_self_component
*self_component
,
109 bt_self_message_iterator
*self_message_iterator
,
110 const char *module_name
, int active_log_level
)
112 int status
= __BT_FUNC_STATUS_OK
;
113 PyObject
*exc
= PyErr_Occurred();
119 if (PyErr_GivenExceptionMatches(exc
,
120 py_mod_bt2_exc_try_again_type
)) {
121 status
= __BT_FUNC_STATUS_AGAIN
;
122 } else if (PyErr_GivenExceptionMatches(exc
,
123 py_mod_bt2_exc_stop_type
)) {
124 status
= __BT_FUNC_STATUS_END
;
125 } else if (PyErr_GivenExceptionMatches(exc
,
126 py_mod_bt2_exc_unknown_object_type
)) {
127 status
= __BT_FUNC_STATUS_UNKNOWN_OBJECT
;
130 * Unknown exception: convert to general error.
132 * Because we only want to fetch the log level when
133 * we actually get an exception, and not systematically
134 * when we call py_exc_to_status() (as py_exc_to_status()
135 * can return `__BT_FUNC_STATUS_OK`), we get it here
136 * depending on the actor's type.
138 if (self_component
) {
139 active_log_level
= get_self_component_log_level(
141 } else if (self_message_iterator
) {
142 active_log_level
= get_self_message_iterator_log_level(
143 self_message_iterator
);
146 BT_ASSERT(active_log_level
!= -1);
147 log_exception_and_maybe_append_error(BT_LOG_WARNING
,
148 active_log_level
, true,
149 self_component_class
, self_component
,
150 self_message_iterator
, module_name
);
152 if (PyErr_GivenExceptionMatches(exc
,
153 py_mod_bt2_exc_memory_error
)) {
154 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
156 status
= __BT_FUNC_STATUS_ERROR
;
166 int py_exc_to_status_component_class(
167 bt_self_component_class
*self_component_class
,
168 int active_log_level
)
170 return py_exc_to_status(self_component_class
, NULL
, NULL
, NULL
,
175 int py_exc_to_status_component(bt_self_component
*self_component
)
177 return py_exc_to_status(NULL
, self_component
, NULL
, NULL
, -1);
181 int py_exc_to_status_message_iterator(
182 bt_self_message_iterator
*self_message_iterator
)
184 return py_exc_to_status(NULL
, NULL
, self_message_iterator
, NULL
, -1);
188 bool bt_bt2_is_python_component_class(const bt_component_class
*comp_cls
)
190 return bt_g_hash_table_contains(bt_cc_ptr_to_py_cls
, comp_cls
);
193 /* Component class proxy methods (delegate to the attached Python object) */
196 bt_component_class_init_method_status
component_class_init(
197 bt_self_component
*self_component
,
198 void *self_component_v
,
199 swig_type_info
*self_comp_cls_type_swig_type
,
200 const bt_value
*params
,
201 void *init_method_data
)
203 const bt_component
*component
= bt_self_component_as_component(self_component
);
204 const bt_component_class
*component_class
= bt_component_borrow_class_const(component
);
205 bt_component_class_init_method_status status
= __BT_FUNC_STATUS_OK
;
206 PyObject
*py_cls
= NULL
;
207 PyObject
*py_comp
= NULL
;
208 PyObject
*py_params_ptr
= NULL
;
209 PyObject
*py_comp_ptr
= NULL
;
210 bt_logging_level log_level
= get_self_component_log_level(
213 BT_ASSERT(self_component
);
214 BT_ASSERT(self_component_v
);
215 BT_ASSERT(self_comp_cls_type_swig_type
);
218 * Get the user-defined Python class which created this
219 * component's class in the first place (borrowed
222 py_cls
= lookup_cc_ptr_to_py_cls(component_class
);
224 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
225 "Cannot find Python class associated to native component class: "
226 "comp-cls-addr=%p", component_class
);
230 /* Parameters pointer -> SWIG pointer Python object */
231 py_params_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(params
),
232 SWIGTYPE_p_bt_value
, 0);
233 if (!py_params_ptr
) {
234 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
235 "Failed to create a SWIG pointer object.");
239 py_comp_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v
),
240 self_comp_cls_type_swig_type
, 0);
242 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
243 "Failed to create a SWIG pointer object.");
248 * Do the equivalent of this:
250 * py_comp = py_cls._bt_init_from_native(py_comp_ptr,
251 * py_params_ptr, init_method_data ? init_method_data : Py_None)
253 * _UserComponentType._bt_init_from_native() calls the Python
254 * component object's __init__() function.
256 * We don't take any reference on `init_method_data` which, if
257 * not `NULL`, is assumed to be a `PyObject *`: the user's
258 * __init__() function will eventually take a reference if
259 * needed. If `init_method_data` is `NULL`, then we pass
260 * `Py_None` as the initialization's Python object.
262 py_comp
= PyObject_CallMethod(py_cls
,
263 "_bt_init_from_native", "(OOO)", py_comp_ptr
, py_params_ptr
,
264 init_method_data
? init_method_data
: Py_None
);
266 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING
, log_level
, self_component
,
267 "Failed to call Python class's _bt_init_from_native() method: "
268 "py-cls-addr=%p", py_cls
);
269 status
= py_exc_to_status_component(self_component
);
274 * Our user Python component object is now fully created and
275 * initialized by the user. Since we just created it, this
276 * native component is its only (persistent) owner.
278 bt_self_component_set_data(self_component
, py_comp
);
283 status
= __BT_FUNC_STATUS_ERROR
;
286 * Clear any exception: we're returning a bad status anyway. If
287 * this call originated from Python (creation from a plugin's
288 * component class, for example), then the user gets an
289 * appropriate creation error.
295 Py_XDECREF(py_params_ptr
);
296 Py_XDECREF(py_comp_ptr
);
301 bt_component_class_get_supported_mip_versions_method_status
302 component_class_get_supported_mip_versions(
303 const bt_component_class
*component_class
,
304 bt_self_component_class
*self_component_class
,
305 const bt_value
*params
, void *init_method_data
,
306 bt_logging_level log_level
,
307 bt_integer_range_set_unsigned
*supported_versions
)
310 PyObject
*py_cls
= NULL
;
311 PyObject
*py_params_ptr
= NULL
;
312 PyObject
*py_range_set_addr
= NULL
;
313 bt_integer_range_set_unsigned
*ret_range_set
= NULL
;
314 bt_component_class_get_supported_mip_versions_method_status status
=
317 py_cls
= lookup_cc_ptr_to_py_cls(component_class
);
319 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
320 "Cannot find Python class associated to native component class: "
321 "comp-cls-addr=%p", component_class
);
325 py_params_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(params
),
326 SWIGTYPE_p_bt_value
, 0);
327 if (!py_params_ptr
) {
328 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
329 "Failed to create a SWIG pointer object.");
334 * We don't take any reference on `init_method_data` which, if
335 * not `NULL`, is assumed to be a `PyObject *`: the user's
336 * _user_get_supported_mip_versions() function will eventually
337 * take a reference if needed. If `init_method_data` is `NULL`,
338 * then we pass `Py_None` as the initialization's Python object.
340 py_range_set_addr
= PyObject_CallMethod(py_cls
,
341 "_bt_get_supported_mip_versions_from_native", "(OOi)",
342 py_params_ptr
, init_method_data
? init_method_data
: Py_None
,
344 if (!py_range_set_addr
) {
345 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING
, log_level
, BT_LOG_TAG
,
346 "Failed to call Python class's _bt_get_supported_mip_versions_from_native() method: "
347 "py-cls-addr=%p", py_cls
);
348 status
= py_exc_to_status_component_class(self_component_class
,
354 * The returned object, on success, is an integer object
355 * (PyLong) containing the address of a BT unsigned integer
356 * range set object (new reference).
358 ret_range_set
= PyLong_AsVoidPtr(py_range_set_addr
);
359 BT_ASSERT(!PyErr_Occurred());
360 BT_ASSERT(ret_range_set
);
362 /* Copy returned ranges to input range set */
363 for (i
= 0; i
< bt_integer_range_set_get_range_count(
364 bt_integer_range_set_unsigned_as_range_set_const(ret_range_set
));
366 const bt_integer_range_unsigned
*range
=
367 bt_integer_range_set_unsigned_borrow_range_by_index_const(
369 bt_integer_range_set_add_range_status add_range_status
;
371 add_range_status
= bt_integer_range_set_unsigned_add_range(
373 bt_integer_range_unsigned_get_lower(range
),
374 bt_integer_range_unsigned_get_upper(range
));
375 if (add_range_status
) {
376 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
377 "Failed to add range to supported MIP versions range set.");
386 status
= __BT_FUNC_STATUS_ERROR
;
389 Py_XDECREF(py_params_ptr
);
390 Py_XDECREF(py_range_set_addr
);
391 bt_integer_range_set_unsigned_put_ref(ret_range_set
);
396 bt_component_class_get_supported_mip_versions_method_status
397 component_class_source_get_supported_mip_versions(
398 bt_self_component_class_source
*self_component_class_source
,
399 const bt_value
*params
, void *init_method_data
,
400 bt_logging_level log_level
,
401 bt_integer_range_set_unsigned
*supported_versions
)
403 const bt_component_class_source
*component_class_source
= bt_self_component_class_source_as_component_class_source(self_component_class_source
);
404 const bt_component_class
*component_class
= bt_component_class_source_as_component_class_const(component_class_source
);
405 bt_self_component_class
*self_component_class
= bt_self_component_class_source_as_self_component_class(self_component_class_source
);
407 return component_class_get_supported_mip_versions(
408 component_class
, self_component_class
,
409 params
, init_method_data
, log_level
, supported_versions
);
413 bt_component_class_get_supported_mip_versions_method_status
414 component_class_filter_get_supported_mip_versions(
415 bt_self_component_class_filter
*self_component_class_filter
,
416 const bt_value
*params
, void *init_method_data
,
417 bt_logging_level log_level
,
418 bt_integer_range_set_unsigned
*supported_versions
)
420 const bt_component_class_filter
*component_class_filter
= bt_self_component_class_filter_as_component_class_filter(self_component_class_filter
);
421 const bt_component_class
*component_class
= bt_component_class_filter_as_component_class_const(component_class_filter
);
422 bt_self_component_class
*self_component_class
= bt_self_component_class_filter_as_self_component_class(self_component_class_filter
);
424 return component_class_get_supported_mip_versions(
425 component_class
, self_component_class
,
426 params
, init_method_data
, log_level
, supported_versions
);
430 bt_component_class_get_supported_mip_versions_method_status
431 component_class_sink_get_supported_mip_versions(
432 bt_self_component_class_sink
*self_component_class_sink
,
433 const bt_value
*params
, void *init_method_data
,
434 bt_logging_level log_level
,
435 bt_integer_range_set_unsigned
*supported_versions
)
437 const bt_component_class_sink
*component_class_sink
= bt_self_component_class_sink_as_component_class_sink(self_component_class_sink
);
438 const bt_component_class
*component_class
= bt_component_class_sink_as_component_class_const(component_class_sink
);
439 bt_self_component_class
*self_component_class
= bt_self_component_class_sink_as_self_component_class(self_component_class_sink
);
441 return component_class_get_supported_mip_versions(
442 component_class
, self_component_class
,
443 params
, init_method_data
, log_level
, supported_versions
);
447 * Method of bt_component_class_source to initialize a bt_self_component_source
452 bt_component_class_init_method_status
component_class_source_init(
453 bt_self_component_source
*self_component_source
,
454 const bt_value
*params
, void *init_method_data
)
456 bt_self_component
*self_component
= bt_self_component_source_as_self_component(self_component_source
);
457 return component_class_init(
459 self_component_source
,
460 SWIGTYPE_p_bt_self_component_source
,
461 params
, init_method_data
);
465 bt_component_class_init_method_status
component_class_filter_init(
466 bt_self_component_filter
*self_component_filter
,
467 const bt_value
*params
, void *init_method_data
)
469 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
470 return component_class_init(
472 self_component_filter
,
473 SWIGTYPE_p_bt_self_component_filter
,
474 params
, init_method_data
);
478 bt_component_class_init_method_status
component_class_sink_init(
479 bt_self_component_sink
*self_component_sink
,
480 const bt_value
*params
, void *init_method_data
)
482 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
483 return component_class_init(
486 SWIGTYPE_p_bt_self_component_sink
,
487 params
, init_method_data
);
491 void component_class_finalize(bt_self_component
*self_component
)
493 PyObject
*py_comp
= bt_self_component_get_data(self_component
);
496 /* Call user's _user_finalize() method */
497 PyObject
*py_method_result
= PyObject_CallMethod(py_comp
,
498 "_user_finalize", NULL
);
500 if (PyErr_Occurred()) {
501 bt_logging_level log_level
= get_self_component_log_level(
504 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING
, log_level
, self_component
,
505 "User component's _user_finalize() method raised an exception: ignoring:");
506 logw_exception(log_level
);
510 * Ignore any exception raised by the _user_finalize() method
511 * because it won't change anything at this point: the component
512 * is being destroyed anyway.
515 Py_XDECREF(py_method_result
);
520 void component_class_source_finalize(bt_self_component_source
*self_component_source
)
522 bt_self_component
*self_component
= bt_self_component_source_as_self_component(self_component_source
);
523 component_class_finalize(self_component
);
527 void component_class_filter_finalize(bt_self_component_filter
*self_component_filter
)
529 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
530 component_class_finalize(self_component
);
534 void component_class_sink_finalize(bt_self_component_sink
*self_component_sink
)
536 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
537 component_class_finalize(self_component
);
541 bt_component_class_message_iterator_can_seek_beginning_method_status
542 component_class_can_seek_beginning(
543 bt_self_message_iterator
*self_message_iterator
, bt_bool
*can_seek
)
546 PyObject
*py_result
= NULL
;
547 bt_component_class_message_iterator_can_seek_beginning_method_status status
;
548 py_iter
= bt_self_message_iterator_get_data(self_message_iterator
);
551 py_result
= PyObject_GetAttrString(py_iter
, "_bt_can_seek_beginning_from_native");
553 BT_ASSERT(!py_result
|| PyBool_Check(py_result
));
556 *can_seek
= PyObject_IsTrue(py_result
);
557 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
;
559 status
= py_exc_to_status_message_iterator(self_message_iterator
);
560 BT_ASSERT(status
!= __BT_FUNC_STATUS_OK
);
563 Py_XDECREF(py_result
);
569 bt_component_class_message_iterator_seek_beginning_method_status
570 component_class_seek_beginning(bt_self_message_iterator
*self_message_iterator
)
574 bt_component_class_message_iterator_seek_beginning_method_status status
;
576 py_iter
= bt_self_message_iterator_get_data(self_message_iterator
);
578 py_result
= PyObject_CallMethod(py_iter
, "_bt_seek_beginning_from_native",
580 BT_ASSERT(!py_result
|| py_result
== Py_None
);
581 status
= py_exc_to_status_message_iterator(self_message_iterator
);
582 Py_XDECREF(py_result
);
587 bt_component_class_port_connected_method_status
component_class_port_connected(
588 bt_self_component
*self_component
,
589 void *self_component_port
,
590 swig_type_info
*self_component_port_swig_type
,
591 bt_port_type self_component_port_type
,
592 const void *other_port
,
593 swig_type_info
*other_port_swig_type
)
595 bt_component_class_port_connected_method_status status
;
596 PyObject
*py_comp
= NULL
;
597 PyObject
*py_self_port_ptr
= NULL
;
598 PyObject
*py_other_port_ptr
= NULL
;
599 PyObject
*py_method_result
= NULL
;
600 bt_logging_level log_level
= get_self_component_log_level(
603 py_comp
= bt_self_component_get_data(self_component
);
605 py_self_port_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port
),
606 self_component_port_swig_type
, 0);
607 if (!py_self_port_ptr
) {
608 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
609 "Failed to create a SWIG pointer object.");
610 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
614 py_other_port_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(other_port
),
615 other_port_swig_type
, 0);
616 if (!py_other_port_ptr
) {
617 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
618 "Failed to create a SWIG pointer object.");
619 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
622 py_method_result
= PyObject_CallMethod(py_comp
,
623 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr
,
624 self_component_port_type
, py_other_port_ptr
);
625 BT_ASSERT(!py_method_result
|| py_method_result
== Py_None
);
626 status
= py_exc_to_status_component(self_component
);
629 Py_XDECREF(py_self_port_ptr
);
630 Py_XDECREF(py_other_port_ptr
);
631 Py_XDECREF(py_method_result
);
636 bt_component_class_port_connected_method_status
637 component_class_source_output_port_connected(
638 bt_self_component_source
*self_component_source
,
639 bt_self_component_port_output
*self_component_port_output
,
640 const bt_port_input
*other_port_input
)
642 bt_self_component
*self_component
= bt_self_component_source_as_self_component(self_component_source
);
644 return component_class_port_connected(
646 self_component_port_output
,
647 SWIGTYPE_p_bt_self_component_port_output
,
650 SWIGTYPE_p_bt_port_input
);
654 bt_component_class_port_connected_method_status
655 component_class_filter_input_port_connected(
656 bt_self_component_filter
*self_component_filter
,
657 bt_self_component_port_input
*self_component_port_input
,
658 const bt_port_output
*other_port_output
)
660 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
662 return component_class_port_connected(
664 self_component_port_input
,
665 SWIGTYPE_p_bt_self_component_port_input
,
668 SWIGTYPE_p_bt_port_output
);
672 bt_component_class_port_connected_method_status
673 component_class_filter_output_port_connected(
674 bt_self_component_filter
*self_component_filter
,
675 bt_self_component_port_output
*self_component_port_output
,
676 const bt_port_input
*other_port_input
)
678 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
680 return component_class_port_connected(
682 self_component_port_output
,
683 SWIGTYPE_p_bt_self_component_port_output
,
686 SWIGTYPE_p_bt_port_input
);
690 bt_component_class_port_connected_method_status
691 component_class_sink_input_port_connected(
692 bt_self_component_sink
*self_component_sink
,
693 bt_self_component_port_input
*self_component_port_input
,
694 const bt_port_output
*other_port_output
)
696 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
698 return component_class_port_connected(
700 self_component_port_input
,
701 SWIGTYPE_p_bt_self_component_port_input
,
704 SWIGTYPE_p_bt_port_output
);
708 bt_component_class_sink_graph_is_configured_method_status
709 component_class_sink_graph_is_configured(
710 bt_self_component_sink
*self_component_sink
)
712 PyObject
*py_comp
= NULL
;
713 PyObject
*py_method_result
= NULL
;
714 bt_component_class_sink_graph_is_configured_method_status status
= __BT_FUNC_STATUS_OK
;
715 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
717 py_comp
= bt_self_component_get_data(self_component
);
718 py_method_result
= PyObject_CallMethod(py_comp
,
719 "_bt_graph_is_configured_from_native", NULL
);
720 BT_ASSERT(!py_method_result
|| py_method_result
== Py_None
);
721 status
= py_exc_to_status_component(self_component
);
722 Py_XDECREF(py_method_result
);
727 bt_component_class_query_method_status
component_class_query(
728 const bt_component_class
*component_class
,
729 bt_self_component_class
*self_component_class
,
730 bt_private_query_executor
*priv_query_executor
,
731 const char *object
, const bt_value
*params
, void *method_data
,
732 const bt_value
**result
)
734 PyObject
*py_cls
= NULL
;
735 PyObject
*py_params_ptr
= NULL
;
736 PyObject
*py_priv_query_exec_ptr
= NULL
;
737 PyObject
*py_query_func
= NULL
;
738 PyObject
*py_object
= NULL
;
739 PyObject
*py_results_addr
= NULL
;
740 bt_component_class_query_method_status status
= __BT_FUNC_STATUS_OK
;
741 const bt_query_executor
*query_exec
=
742 bt_private_query_executor_as_query_executor_const(
743 priv_query_executor
);
744 bt_logging_level log_level
=
745 bt_query_executor_get_logging_level(query_exec
);
748 * If there's any `method_data`, assume this component class is
749 * getting queried from Python, so that `method_data` is a
750 * Python object to pass to the user's _user_query() method.
752 BT_ASSERT(!method_data
||
753 bt_bt2_is_python_component_class(component_class
));
755 py_cls
= lookup_cc_ptr_to_py_cls(component_class
);
757 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
758 "Cannot find Python class associated to native component class: "
759 "comp-cls-addr=%p", component_class
);
763 py_params_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(params
),
764 SWIGTYPE_p_bt_value
, 0);
765 if (!py_params_ptr
) {
766 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
767 "Failed to create a SWIG pointer object.");
771 py_priv_query_exec_ptr
= SWIG_NewPointerObj(
772 SWIG_as_voidptr(priv_query_executor
),
773 SWIGTYPE_p_bt_private_query_executor
, 0);
774 if (!py_priv_query_exec_ptr
) {
775 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
776 "Failed to create a SWIG pointer object.");
780 py_object
= SWIG_FromCharPtr(object
);
782 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
783 "Failed to create a Python string.");
788 * We don't take any reference on `method_data` which, if not
789 * `NULL`, is assumed to be a `PyObject *`: the user's
790 * _user_query() function will eventually take a reference if
791 * needed. If `method_data` is `NULL`, then we pass `Py_None` as
792 * the initialization's Python object.
794 py_results_addr
= PyObject_CallMethod(py_cls
,
795 "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr
,
796 py_object
, py_params_ptr
,
797 method_data
? method_data
: Py_None
);
798 if (!py_results_addr
) {
799 status
= py_exc_to_status_component_class(self_component_class
,
802 static const char *fmt
=
803 "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p";
804 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING
, log_level
, BT_LOG_TAG
,
806 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
807 self_component_class
, fmt
, py_cls
);
813 * The returned object, on success, is an integer object
814 * (PyLong) containing the address of a BT value object (new
817 *result
= PyLong_AsVoidPtr(py_results_addr
);
818 BT_ASSERT(!PyErr_Occurred());
824 status
= __BT_FUNC_STATUS_ERROR
;
827 Py_XDECREF(py_params_ptr
);
828 Py_XDECREF(py_priv_query_exec_ptr
);
829 Py_XDECREF(py_query_func
);
830 Py_XDECREF(py_object
);
831 Py_XDECREF(py_results_addr
);
836 bt_component_class_query_method_status
component_class_source_query(
837 bt_self_component_class_source
*self_component_class_source
,
838 bt_private_query_executor
*priv_query_executor
,
839 const char *object
, const bt_value
*params
, void *method_data
,
840 const bt_value
**result
)
842 const bt_component_class_source
*component_class_source
= bt_self_component_class_source_as_component_class_source(self_component_class_source
);
843 const bt_component_class
*component_class
= bt_component_class_source_as_component_class_const(component_class_source
);
844 bt_self_component_class
*self_component_class
= bt_self_component_class_source_as_self_component_class(self_component_class_source
);
846 return component_class_query(component_class
, self_component_class
,
847 priv_query_executor
, object
, params
, method_data
, result
);
851 bt_component_class_query_method_status
component_class_filter_query(
852 bt_self_component_class_filter
*self_component_class_filter
,
853 bt_private_query_executor
*priv_query_executor
,
854 const char *object
, const bt_value
*params
, void *method_data
,
855 const bt_value
**result
)
857 const bt_component_class_filter
*component_class_filter
= bt_self_component_class_filter_as_component_class_filter(self_component_class_filter
);
858 const bt_component_class
*component_class
= bt_component_class_filter_as_component_class_const(component_class_filter
);
859 bt_self_component_class
*self_component_class
= bt_self_component_class_filter_as_self_component_class(self_component_class_filter
);
861 return component_class_query(component_class
, self_component_class
,
862 priv_query_executor
, object
, params
, method_data
, result
);
866 bt_component_class_query_method_status
component_class_sink_query(
867 bt_self_component_class_sink
*self_component_class_sink
,
868 bt_private_query_executor
*priv_query_executor
,
869 const char *object
, const bt_value
*params
, void *method_data
,
870 const bt_value
**result
)
872 const bt_component_class_sink
*component_class_sink
= bt_self_component_class_sink_as_component_class_sink(self_component_class_sink
);
873 const bt_component_class
*component_class
= bt_component_class_sink_as_component_class_const(component_class_sink
);
874 bt_self_component_class
*self_component_class
= bt_self_component_class_sink_as_self_component_class(self_component_class_sink
);
876 return component_class_query(component_class
, self_component_class
,
877 priv_query_executor
, object
, params
, method_data
, result
);
881 bt_component_class_message_iterator_init_method_status
882 component_class_message_iterator_init(
883 bt_self_message_iterator
*self_message_iterator
,
884 bt_self_component
*self_component
,
885 bt_self_component_port_output
*self_component_port_output
)
887 bt_component_class_message_iterator_init_method_status status
= __BT_FUNC_STATUS_OK
;
888 PyObject
*py_comp_cls
= NULL
;
889 PyObject
*py_iter_cls
= NULL
;
890 PyObject
*py_iter_ptr
= NULL
;
891 PyObject
*py_component_port_output_ptr
= NULL
;
892 PyObject
*py_init_method_result
= NULL
;
893 PyObject
*py_iter
= NULL
;
895 bt_logging_level log_level
= get_self_component_log_level(
898 py_comp
= bt_self_component_get_data(self_component
);
900 /* Find user's Python message iterator class */
901 py_comp_cls
= PyObject_GetAttrString(py_comp
, "__class__");
903 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
904 "Cannot get Python object's `__class__` attribute.");
908 py_iter_cls
= PyObject_GetAttrString(py_comp_cls
, "_iter_cls");
910 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
911 "Cannot get Python class's `_iter_cls` attribute.");
915 py_iter_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator
),
916 SWIGTYPE_p_bt_self_message_iterator
, 0);
918 const char *err
= "Failed to create a SWIG pointer object.";
920 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
922 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
923 self_message_iterator
, err
);
928 * Create object with borrowed native message iterator
931 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
933 py_iter
= PyObject_CallMethod(py_iter_cls
, "__new__",
934 "(OO)", py_iter_cls
, py_iter_ptr
);
936 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
937 "Failed to call Python class's __new__() method: "
938 "py-cls-addr=%p", py_iter_cls
);
945 * py_iter.__init__(self_output_port)
947 * through the _init_for_native helper static method.
949 * At this point, py_iter._ptr is set, so this initialization
950 * function has access to self._component (which gives it the
951 * user Python component object from which the iterator was
954 py_component_port_output_ptr
= SWIG_NewPointerObj(
955 SWIG_as_voidptr(self_component_port_output
),
956 SWIGTYPE_p_bt_self_component_port_output
, 0);
957 if (!py_component_port_output_ptr
) {
958 const char *err
= "Failed to create a SWIG pointer object.";
960 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
962 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
963 self_message_iterator
, err
);
967 py_init_method_result
= PyObject_CallMethod(py_iter
,
968 "_bt_init_from_native", "O", py_component_port_output_ptr
);
969 if (!py_init_method_result
) {
970 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_component
,
971 "User's __init__() method failed:");
976 * Since the Python code can never instantiate a user-defined
977 * message iterator class, the native message iterator
978 * object does NOT belong to a user Python message iterator
979 * object (borrowed reference). However this Python object is
980 * owned by this native message iterator object.
982 * In the Python world, the lifetime of the native message
983 * iterator is managed by a _GenericMessageIterator
986 * _GenericMessageIterator instance:
987 * owns a native bt_message_iterator object (iter)
988 * owns a _UserMessageIterator instance (py_iter)
989 * self._ptr is a borrowed reference to the
990 * native bt_private_connection_private_message_iterator
993 bt_self_message_iterator_set_data(self_message_iterator
, py_iter
);
998 /* Handling of errors that cause a Python exception to be set. */
999 status
= py_exc_to_status_message_iterator(self_message_iterator
);
1000 BT_ASSERT(status
!= __BT_FUNC_STATUS_OK
);
1004 /* Handling of errors that don't cause a Python exception to be set. */
1005 status
= __BT_FUNC_STATUS_ERROR
;
1008 BT_ASSERT(!PyErr_Occurred());
1010 Py_XDECREF(py_comp_cls
);
1011 Py_XDECREF(py_iter_cls
);
1012 Py_XDECREF(py_iter_ptr
);
1013 Py_XDECREF(py_component_port_output_ptr
);
1014 Py_XDECREF(py_init_method_result
);
1015 Py_XDECREF(py_iter
);
1020 bt_component_class_message_iterator_init_method_status
1021 component_class_source_message_iterator_init(
1022 bt_self_message_iterator
*self_message_iterator
,
1023 bt_self_component_source
*self_component_source
,
1024 bt_self_component_port_output
*self_component_port_output
)
1026 bt_self_component
*self_component
= bt_self_component_source_as_self_component(self_component_source
);
1028 return component_class_message_iterator_init(self_message_iterator
, self_component
, self_component_port_output
);
1032 bt_component_class_message_iterator_init_method_status
1033 component_class_filter_message_iterator_init(
1034 bt_self_message_iterator
*self_message_iterator
,
1035 bt_self_component_filter
*self_component_filter
,
1036 bt_self_component_port_output
*self_component_port_output
)
1038 bt_self_component
*self_component
= bt_self_component_filter_as_self_component(self_component_filter
);
1040 return component_class_message_iterator_init(self_message_iterator
, self_component
, self_component_port_output
);
1044 void component_class_message_iterator_finalize(
1045 bt_self_message_iterator
*message_iterator
)
1047 PyObject
*py_message_iter
= bt_self_message_iterator_get_data(
1049 PyObject
*py_method_result
= NULL
;
1051 BT_ASSERT(py_message_iter
);
1053 /* Call user's _user_finalize() method */
1054 py_method_result
= PyObject_CallMethod(py_message_iter
,
1055 "_user_finalize", NULL
);
1057 if (PyErr_Occurred()) {
1058 bt_self_component
*self_comp
=
1059 bt_self_message_iterator_borrow_component(
1061 bt_logging_level log_level
= get_self_component_log_level(
1064 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING
, log_level
, self_comp
,
1065 "User's _user_finalize() method raised an exception: ignoring:");
1066 logw_exception(get_self_message_iterator_log_level(
1071 * Ignore any exception raised by the _user_finalize() method
1072 * because it won't change anything at this point: the component
1073 * is being destroyed anyway.
1076 Py_XDECREF(py_method_result
);
1077 Py_DECREF(py_message_iter
);
1080 /* Valid for both sources and filters. */
1083 bt_component_class_message_iterator_next_method_status
1084 component_class_message_iterator_next(
1085 bt_self_message_iterator
*message_iterator
,
1086 bt_message_array_const msgs
, uint64_t capacity
,
1089 bt_component_class_message_iterator_next_method_status status
= __BT_FUNC_STATUS_OK
;
1090 PyObject
*py_message_iter
= bt_self_message_iterator_get_data(message_iterator
);
1091 PyObject
*py_method_result
= NULL
;
1093 BT_ASSERT(py_message_iter
);
1094 py_method_result
= PyObject_CallMethod(py_message_iter
,
1095 "_bt_next_from_native", NULL
);
1096 if (!py_method_result
) {
1097 status
= py_exc_to_status_message_iterator(message_iterator
);
1098 BT_ASSERT(status
!= __BT_FUNC_STATUS_OK
);
1103 * The returned object, on success, is an integer object
1104 * (PyLong) containing the address of a native message
1105 * object (which is now ours).
1107 msgs
[0] = PyLong_AsVoidPtr(py_method_result
);
1110 /* Clear potential overflow error; should never happen */
1111 BT_ASSERT(!PyErr_Occurred());
1115 Py_XDECREF(py_method_result
);
1120 bt_component_class_sink_consume_method_status
1121 component_class_sink_consume(bt_self_component_sink
*self_component_sink
)
1123 bt_self_component
*self_component
= bt_self_component_sink_as_self_component(self_component_sink
);
1124 PyObject
*py_comp
= bt_self_component_get_data(self_component
);
1125 PyObject
*py_method_result
= NULL
;
1126 bt_component_class_sink_consume_method_status status
;
1129 py_method_result
= PyObject_CallMethod(py_comp
,
1130 "_user_consume", NULL
);
1131 status
= py_exc_to_status_component(self_component
);
1132 BT_ASSERT(py_method_result
|| status
!= __BT_FUNC_STATUS_OK
);
1133 Py_XDECREF(py_method_result
);
1138 int component_class_set_help_and_desc(
1139 bt_component_class
*component_class
,
1140 const char *description
, const char *help
)
1145 ret
= bt_component_class_set_description(component_class
, description
);
1147 BT_LOGE("Cannot set component class's description: "
1148 "comp-cls-addr=%p", component_class
);
1154 ret
= bt_component_class_set_help(component_class
, help
);
1156 BT_LOGE("Cannot set component class's help text: "
1157 "comp-cls-addr=%p", component_class
);
1169 bt_component_class_source
*bt_bt2_component_class_source_create(
1170 PyObject
*py_cls
, const char *name
, const char *description
,
1173 bt_component_class_source
*component_class_source
;
1174 bt_component_class
*component_class
;
1178 component_class_source
= bt_component_class_source_create(name
,
1179 component_class_message_iterator_next
);
1180 if (!component_class_source
) {
1181 BT_LOGE_STR("Cannot create source component class.");
1185 component_class
= bt_component_class_source_as_component_class(component_class_source
);
1187 if (component_class_set_help_and_desc(component_class
, description
, help
)) {
1191 ret
= bt_component_class_source_set_init_method(component_class_source
, component_class_source_init
);
1192 BT_ASSERT(ret
== 0);
1193 ret
= bt_component_class_source_set_finalize_method(component_class_source
, component_class_source_finalize
);
1194 BT_ASSERT(ret
== 0);
1195 ret
= bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source
,
1196 component_class_can_seek_beginning
);
1197 BT_ASSERT(ret
== 0);
1198 ret
= bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source
,
1199 component_class_seek_beginning
);
1200 BT_ASSERT(ret
== 0);
1201 ret
= bt_component_class_source_set_output_port_connected_method(component_class_source
,
1202 component_class_source_output_port_connected
);
1203 BT_ASSERT(ret
== 0);
1204 ret
= bt_component_class_source_set_query_method(component_class_source
, component_class_source_query
);
1205 BT_ASSERT(ret
== 0);
1206 ret
= bt_component_class_source_set_get_supported_mip_versions_method(component_class_source
, component_class_source_get_supported_mip_versions
);
1207 BT_ASSERT(ret
== 0);
1208 ret
= bt_component_class_source_set_message_iterator_init_method(
1209 component_class_source
, component_class_source_message_iterator_init
);
1210 BT_ASSERT(ret
== 0);
1211 ret
= bt_component_class_source_set_message_iterator_finalize_method(
1212 component_class_source
, component_class_message_iterator_finalize
);
1213 BT_ASSERT(ret
== 0);
1214 register_cc_ptr_to_py_cls(component_class
, py_cls
);
1217 return component_class_source
;
1221 bt_component_class_filter
*bt_bt2_component_class_filter_create(
1222 PyObject
*py_cls
, const char *name
, const char *description
,
1225 bt_component_class
*component_class
;
1226 bt_component_class_filter
*component_class_filter
;
1230 component_class_filter
= bt_component_class_filter_create(name
,
1231 component_class_message_iterator_next
);
1232 if (!component_class_filter
) {
1233 BT_LOGE_STR("Cannot create filter component class.");
1237 component_class
= bt_component_class_filter_as_component_class(component_class_filter
);
1239 if (component_class_set_help_and_desc(component_class
, description
, help
)) {
1243 ret
= bt_component_class_filter_set_init_method(component_class_filter
, component_class_filter_init
);
1244 BT_ASSERT(ret
== 0);
1245 ret
= bt_component_class_filter_set_finalize_method (component_class_filter
, component_class_filter_finalize
);
1246 BT_ASSERT(ret
== 0);
1247 ret
= bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter
,
1248 component_class_can_seek_beginning
);
1249 BT_ASSERT(ret
== 0);
1250 ret
= bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter
,
1251 component_class_seek_beginning
);
1252 BT_ASSERT(ret
== 0);
1253 ret
= bt_component_class_filter_set_input_port_connected_method(component_class_filter
,
1254 component_class_filter_input_port_connected
);
1255 BT_ASSERT(ret
== 0);
1256 ret
= bt_component_class_filter_set_output_port_connected_method(component_class_filter
,
1257 component_class_filter_output_port_connected
);
1258 BT_ASSERT(ret
== 0);
1259 ret
= bt_component_class_filter_set_query_method(component_class_filter
, component_class_filter_query
);
1260 BT_ASSERT(ret
== 0);
1261 ret
= bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter
, component_class_filter_get_supported_mip_versions
);
1262 BT_ASSERT(ret
== 0);
1263 ret
= bt_component_class_filter_set_message_iterator_init_method(
1264 component_class_filter
, component_class_filter_message_iterator_init
);
1265 BT_ASSERT(ret
== 0);
1266 ret
= bt_component_class_filter_set_message_iterator_finalize_method(
1267 component_class_filter
, component_class_message_iterator_finalize
);
1268 BT_ASSERT(ret
== 0);
1269 register_cc_ptr_to_py_cls(component_class
, py_cls
);
1272 return component_class_filter
;
1276 bt_component_class_sink
*bt_bt2_component_class_sink_create(
1277 PyObject
*py_cls
, const char *name
, const char *description
,
1280 bt_component_class_sink
*component_class_sink
;
1281 bt_component_class
*component_class
;
1285 component_class_sink
= bt_component_class_sink_create(name
, component_class_sink_consume
);
1287 if (!component_class_sink
) {
1288 BT_LOGE_STR("Cannot create sink component class.");
1292 component_class
= bt_component_class_sink_as_component_class(component_class_sink
);
1294 if (component_class_set_help_and_desc(component_class
, description
, help
)) {
1298 ret
= bt_component_class_sink_set_init_method(component_class_sink
, component_class_sink_init
);
1299 BT_ASSERT(ret
== 0);
1300 ret
= bt_component_class_sink_set_finalize_method(component_class_sink
, component_class_sink_finalize
);
1301 BT_ASSERT(ret
== 0);
1302 ret
= bt_component_class_sink_set_input_port_connected_method(component_class_sink
,
1303 component_class_sink_input_port_connected
);
1304 BT_ASSERT(ret
== 0);
1305 ret
= bt_component_class_sink_set_graph_is_configured_method(component_class_sink
,
1306 component_class_sink_graph_is_configured
);
1307 BT_ASSERT(ret
== 0);
1308 ret
= bt_component_class_sink_set_query_method(component_class_sink
, component_class_sink_query
);
1309 BT_ASSERT(ret
== 0);
1310 ret
= bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink
, component_class_sink_get_supported_mip_versions
);
1311 BT_ASSERT(ret
== 0);
1312 register_cc_ptr_to_py_cls(component_class
, py_cls
);
1315 return component_class_sink
;