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
26 struct bt_component_class;
29 enum bt_component_class_type {
30 BT_COMPONENT_CLASS_TYPE_UNKNOWN = -1,
31 BT_COMPONENT_CLASS_TYPE_SOURCE = 0,
32 BT_COMPONENT_CLASS_TYPE_SINK = 1,
33 BT_COMPONENT_CLASS_TYPE_FILTER = 2,
36 /* General functions */
37 const char *bt_component_class_get_name(
38 struct bt_component_class *component_class);
39 const char *bt_component_class_get_description(
40 struct bt_component_class *component_class);
41 const char *bt_component_class_get_help(
42 struct bt_component_class *component_class);
43 struct bt_value *bt_component_class_query(
44 struct bt_component_class *component_class,
45 const char *object, struct bt_value *params);
46 enum bt_component_class_type bt_component_class_get_type(
47 struct bt_component_class *component_class);
51 * This hash table associates a BT component class object address to a
52 * user-defined Python class (PyObject *). The keys and values are NOT
53 * owned by this hash table. The Python class objects are owned by the
54 * Python module, which should not be unloaded until it is not possible
55 * to create a user Python component anyway.
57 * This hash table is written to when a user-defined Python component
58 * class is created by one of the bt_py3_component_class_*_create()
61 * This function is read from when a user calls bt_component_create()
62 * with a component class pointer created by one of the functions above.
63 * In this case, the original Python class needs to be found to
64 * instantiate it and associate the created Python component object with
65 * a BT component object instance.
68 static GHashTable *bt_cc_ptr_to_py_cls;
70 static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
73 if (!bt_cc_ptr_to_py_cls) {
75 * Lazy-initializing this GHashTable because GLib
76 * might not be initialized yet and it needs to be
77 * before we call g_hash_table_new()
79 BT_LOGD_STR("Creating native component class to Python component class hash table.");
80 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
81 assert(bt_cc_ptr_to_py_cls);
84 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
88 static PyObject *lookup_cc_ptr_to_py_cls(struct bt_component_class *bt_cc)
90 if (!bt_cc_ptr_to_py_cls) {
91 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
92 "comp-cls-addr=%p", bt_cc);
96 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
97 (gconstpointer) bt_cc);
102 * Useful Python objects.
105 static PyObject *py_mod_bt2 = NULL;
106 static PyObject *py_mod_bt2_exc_error_type = NULL;
107 static PyObject *py_mod_bt2_exc_unsupported_feature_type = NULL;
108 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
109 static PyObject *py_mod_bt2_exc_stop_type = NULL;
110 static PyObject *py_mod_bt2_exc_port_connection_refused_type = NULL;
111 static PyObject *py_mod_bt2_exc_graph_canceled_type = NULL;
112 static PyObject *py_mod_bt2_exc_notif_iter_canceled_type = NULL;
113 static PyObject *py_mod_bt2_exc_connection_ended_type = NULL;
115 static void bt_py3_cc_init_from_bt2(void)
118 * This is called once the bt2 package is loaded.
120 * Those modules and functions are needed while the package is
121 * used. Loading them here is safe because we know the bt2
122 * package is imported, and we know that the user cannot use the
123 * code here without importing bt2 first.
125 py_mod_bt2 = PyImport_ImportModule("bt2");
127 py_mod_bt2_exc_error_type =
128 PyObject_GetAttrString(py_mod_bt2, "Error");
129 assert(py_mod_bt2_exc_error_type);
130 py_mod_bt2_exc_unsupported_feature_type =
131 PyObject_GetAttrString(py_mod_bt2, "UnsupportedFeature");
132 py_mod_bt2_exc_try_again_type =
133 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
134 py_mod_bt2_exc_stop_type =
135 PyObject_GetAttrString(py_mod_bt2, "Stop");
136 py_mod_bt2_exc_port_connection_refused_type =
137 PyObject_GetAttrString(py_mod_bt2, "PortConnectionRefused");
138 py_mod_bt2_exc_graph_canceled_type =
139 PyObject_GetAttrString(py_mod_bt2, "GraphCanceled");
140 py_mod_bt2_exc_connection_ended_type =
141 PyObject_GetAttrString(py_mod_bt2, "ConnectionEnded");
142 assert(py_mod_bt2_exc_stop_type);
145 static void bt_py3_cc_exit_handler(void)
148 * This is an exit handler (set by the bt2 package).
150 * We only give back the references that we took in
151 * bt_py3_cc_init_from_bt2() here. The global variables continue
152 * to exist for the code of this file, but they are now borrowed
153 * references. If this code is executed, it means that somehow
154 * the modules are still loaded, so it should be safe to use
155 * them even without a strong reference.
157 * We cannot do this in the library's destructor because it
158 * gets executed once Python is already finalized.
160 Py_XDECREF(py_mod_bt2);
161 Py_XDECREF(py_mod_bt2_exc_error_type);
162 Py_XDECREF(py_mod_bt2_exc_unsupported_feature_type);
163 Py_XDECREF(py_mod_bt2_exc_try_again_type);
164 Py_XDECREF(py_mod_bt2_exc_stop_type);
165 Py_XDECREF(py_mod_bt2_exc_port_connection_refused_type);
166 Py_XDECREF(py_mod_bt2_exc_graph_canceled_type);
167 Py_XDECREF(py_mod_bt2_exc_notif_iter_canceled_type);
168 Py_XDECREF(py_mod_bt2_exc_connection_ended_type);
172 /* Library destructor */
174 __attribute__((destructor))
175 static void bt_py3_native_comp_class_dtor(void) {
176 /* Destroy component class association hash table */
177 if (bt_cc_ptr_to_py_cls) {
178 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
179 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
184 /* Component class proxy methods (delegate to the attached Python object) */
186 static enum bt_notification_iterator_status bt_py3_exc_to_notif_iter_status(void)
188 enum bt_notification_iterator_status status =
189 BT_NOTIFICATION_ITERATOR_STATUS_OK;
190 PyObject *exc = PyErr_Occurred();
196 if (PyErr_GivenExceptionMatches(exc,
197 py_mod_bt2_exc_unsupported_feature_type)) {
198 status = BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED;
199 } else if (PyErr_GivenExceptionMatches(exc,
200 py_mod_bt2_exc_stop_type)) {
201 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
202 } else if (PyErr_GivenExceptionMatches(exc,
203 py_mod_bt2_exc_try_again_type)) {
204 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
206 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
214 static enum bt_component_status bt_py3_exc_to_component_status(void)
216 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
217 PyObject *exc = PyErr_Occurred();
223 if (PyErr_GivenExceptionMatches(exc,
224 py_mod_bt2_exc_unsupported_feature_type)) {
225 status = BT_COMPONENT_STATUS_UNSUPPORTED;
226 } else if (PyErr_GivenExceptionMatches(exc,
227 py_mod_bt2_exc_try_again_type)) {
228 status = BT_COMPONENT_STATUS_AGAIN;
229 } else if (PyErr_GivenExceptionMatches(exc,
230 py_mod_bt2_exc_stop_type)) {
231 status = BT_COMPONENT_STATUS_END;
232 } else if (PyErr_GivenExceptionMatches(exc,
233 py_mod_bt2_exc_port_connection_refused_type)) {
234 status = BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION;
236 status = BT_COMPONENT_STATUS_ERROR;
244 static enum bt_component_status bt_py3_cc_init(
245 struct bt_private_component *priv_comp,
246 struct bt_value *params, void *init_method_data)
248 struct bt_component *comp =
249 bt_component_from_private_component(priv_comp);
250 struct bt_component_class *comp_cls = bt_component_get_class(comp);
251 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
252 PyObject *py_cls = NULL;
253 PyObject *py_comp = NULL;
254 PyObject *py_params_ptr = NULL;
255 PyObject *py_comp_ptr = NULL;
257 (void) init_method_data;
262 * Get the user-defined Python class which created this
263 * component's class in the first place (borrowed
266 py_cls = lookup_cc_ptr_to_py_cls(comp_cls);
268 BT_LOGE("Cannot find Python class associated to native component class: "
269 "comp-cls-addr=%p", comp_cls);
273 /* Parameters pointer -> SWIG pointer Python object */
274 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
275 SWIGTYPE_p_bt_value, 0);
276 if (!py_params_ptr) {
277 BT_LOGE_STR("Failed to create a SWIG pointer object.");
281 /* Private component pointer -> SWIG pointer Python object */
282 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(priv_comp),
283 SWIGTYPE_p_bt_private_component, 0);
285 BT_LOGE_STR("Failed to create a SWIG pointer object.");
290 * Do the equivalent of this:
292 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
294 * _UserComponentType._init_from_native() calls the Python
295 * component object's __init__() function.
297 py_comp = PyObject_CallMethod(py_cls,
298 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
300 BT_LOGE("Failed to call Python class's _init_from_native() method: "
301 "py-cls-addr=%p", py_cls);
306 * Our user Python component object is now fully created and
307 * initialized by the user. Since we just created it, this
308 * native component is its only (persistent) owner.
310 bt_private_component_set_user_data(priv_comp, py_comp);
315 status = BT_COMPONENT_STATUS_ERROR;
318 * Clear any exception: we're returning a bad status anyway. If
319 * this call originated from Python (creation from a plugin's
320 * component class, for example), then the user gets an
321 * appropriate creation error.
329 Py_XDECREF(py_params_ptr);
330 Py_XDECREF(py_comp_ptr);
334 static void bt_py3_cc_finalize(struct bt_private_component *priv_comp)
336 PyObject *py_comp = bt_private_component_get_user_data(priv_comp);
337 PyObject *py_method_result = NULL;
341 /* Call user's _finalize() method */
342 py_method_result = PyObject_CallMethod(py_comp,
345 if (PyErr_Occurred()) {
346 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
350 * Ignore any exception raised by the _finalize() method because
351 * it won't change anything at this point: the component is
352 * being destroyed anyway.
355 Py_XDECREF(py_method_result);
359 static enum bt_component_status bt_py3_cc_accept_port_connection(
360 struct bt_private_component *priv_comp,
361 struct bt_private_port *self_priv_port,
362 struct bt_port *other_port)
364 enum bt_component_status status;
365 PyObject *py_comp = NULL;
366 PyObject *py_self_port_ptr = NULL;
367 PyObject *py_other_port_ptr = NULL;
368 PyObject *py_method_result = NULL;
370 py_comp = bt_private_component_get_user_data(priv_comp);
372 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_priv_port),
373 SWIGTYPE_p_bt_private_port, 0);
374 if (!py_self_port_ptr) {
375 BT_LOGE_STR("Failed to create a SWIG pointer object.");
379 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
380 SWIGTYPE_p_bt_port, 0);
381 if (!py_other_port_ptr) {
382 BT_LOGE_STR("Failed to create a SWIG pointer object.");
386 py_method_result = PyObject_CallMethod(py_comp,
387 "_accept_port_connection_from_native", "(OO)", py_self_port_ptr,
389 status = bt_py3_exc_to_component_status();
390 if (!py_method_result && status == BT_COMPONENT_STATUS_OK) {
391 /* Pretty sure this should never happen, but just in case */
392 BT_LOGE("User's _accept_port_connection() method failed without raising an exception: "
393 "status=%d", status);
397 if (status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
399 * Looks like the user method raised
400 * PortConnectionRefused: accept this like if it
404 } else if (status != BT_COMPONENT_STATUS_OK) {
405 BT_LOGE("User's _accept_port_connection() raised an unexpected exception: "
406 "status=%d", status);
410 assert(PyBool_Check(py_method_result));
412 if (py_method_result == Py_True) {
413 status = BT_COMPONENT_STATUS_OK;
415 status = BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION;
421 status = BT_COMPONENT_STATUS_ERROR;
424 * Clear any exception: we're returning a bad status anyway. If
425 * this call originated from Python, then the user gets an
431 Py_XDECREF(py_self_port_ptr);
432 Py_XDECREF(py_other_port_ptr);
433 Py_XDECREF(py_method_result);
437 static void bt_py3_cc_port_connected(
438 struct bt_private_component *priv_comp,
439 struct bt_private_port *self_priv_port,
440 struct bt_port *other_port)
442 PyObject *py_comp = NULL;
443 PyObject *py_self_port_ptr = NULL;
444 PyObject *py_other_port_ptr = NULL;
445 PyObject *py_method_result = NULL;
447 py_comp = bt_private_component_get_user_data(priv_comp);
449 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_priv_port),
450 SWIGTYPE_p_bt_private_port, 0);
451 if (!py_self_port_ptr) {
452 BT_LOGF_STR("Failed to create a SWIG pointer object.");
456 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
457 SWIGTYPE_p_bt_port, 0);
458 if (!py_other_port_ptr) {
459 BT_LOGF_STR("Failed to create a SWIG pointer object.");
463 py_method_result = PyObject_CallMethod(py_comp,
464 "_port_connected_from_native", "(OO)", py_self_port_ptr,
466 assert(py_method_result == Py_None);
467 Py_XDECREF(py_self_port_ptr);
468 Py_XDECREF(py_other_port_ptr);
469 Py_XDECREF(py_method_result);
472 static void bt_py3_cc_port_disconnected(
473 struct bt_private_component *priv_comp,
474 struct bt_private_port *priv_port)
476 PyObject *py_comp = NULL;
477 PyObject *py_port_ptr = NULL;
478 PyObject *py_method_result = NULL;
480 py_comp = bt_private_component_get_user_data(priv_comp);
482 py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(priv_port),
483 SWIGTYPE_p_bt_private_port, 0);
485 BT_LOGF_STR("Failed to create a SWIG pointer object.");
489 py_method_result = PyObject_CallMethod(py_comp,
490 "_port_disconnected_from_native", "(O)", py_port_ptr);
491 assert(py_method_result == Py_None);
492 Py_XDECREF(py_port_ptr);
493 Py_XDECREF(py_method_result);
496 static struct bt_value *bt_py3_cc_query(
497 struct bt_component_class *comp_cls,
498 const char *object, struct bt_value *params)
500 PyObject *py_cls = NULL;
501 PyObject *py_params_ptr = NULL;
502 PyObject *py_query_func = NULL;
503 PyObject *py_object = NULL;
504 PyObject *py_results_addr = NULL;
505 struct bt_value *results = NULL;
507 py_cls = lookup_cc_ptr_to_py_cls(comp_cls);
509 BT_LOGE("Cannot find Python class associated to native component class: "
510 "comp-cls-addr=%p", comp_cls);
514 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
515 SWIGTYPE_p_bt_value, 0);
516 if (!py_params_ptr) {
517 BT_LOGE_STR("Failed to create a SWIG pointer object.");
521 py_object = SWIG_FromCharPtr(object);
523 BT_LOGE_STR("Failed to create a Python string.");
527 py_results_addr = PyObject_CallMethod(py_cls,
528 "_query_from_native", "(OO)", py_object, py_params_ptr);
529 if (!py_results_addr || py_results_addr == Py_None) {
530 BT_LOGE_STR("User's _query() method failed.");
534 if (py_results_addr == Py_NotImplemented) {
535 BT_LOGE_STR("User's _query() method is not implemented.");
540 * The returned object, on success, is an integer object
541 * (PyLong) containing the address of a BT value object (new
544 results = (void *) PyLong_AsUnsignedLongLong(py_results_addr);
545 assert(!PyErr_Occurred());
554 Py_XDECREF(py_params_ptr);
555 Py_XDECREF(py_query_func);
556 Py_XDECREF(py_object);
557 Py_XDECREF(py_results_addr);
561 static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_init(
562 struct bt_private_notification_iterator *priv_notif_iter,
563 struct bt_private_port *priv_port)
565 enum bt_notification_iterator_status status =
566 BT_NOTIFICATION_ITERATOR_STATUS_OK;
567 PyObject *py_comp_cls = NULL;
568 PyObject *py_iter_cls = NULL;
569 PyObject *py_iter_ptr = NULL;
570 PyObject *py_init_method_result = NULL;
571 PyObject *py_iter = NULL;
572 struct bt_private_component *priv_comp =
573 bt_private_notification_iterator_get_private_component(
578 py_comp = bt_private_component_get_user_data(priv_comp);
580 /* Find user's Python notification iterator class */
581 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
583 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
587 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
589 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
593 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(priv_notif_iter),
594 SWIGTYPE_p_bt_private_notification_iterator, 0);
596 BT_LOGE_STR("Failed to create a SWIG pointer object.");
601 * Create object with borrowed native notification iterator
604 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
606 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
607 "(OO)", py_iter_cls, py_iter_ptr);
609 BT_LOGE("Failed to call Python class's __new__() method: "
610 "py-cls-addr=%p", py_iter_cls);
619 * At this point, py_iter._ptr is set, so this initialization
620 * function has access to self._component (which gives it the
621 * user Python component object from which the iterator was
624 py_init_method_result = PyObject_CallMethod(py_iter, "__init__", NULL);
625 if (!py_init_method_result) {
626 BT_LOGE_STR("User's __init__() method failed.");
631 * Since the Python code can never instantiate a user-defined
632 * notification iterator class, the native notification iterator
633 * object does NOT belong to a user Python notification iterator
634 * object (borrowed reference). However this Python object is
635 * owned by this native notification iterator object.
637 * In the Python world, the lifetime of the native notification
638 * iterator is managed by a _GenericNotificationIterator
641 * _GenericNotificationIterator instance:
642 * owns a native bt_notification_iterator object (iter)
643 * owns a _UserNotificationIterator instance (py_iter)
644 * self._ptr is a borrowed reference to the
645 * native bt_private_notification_iterator
648 bt_private_notification_iterator_set_user_data(priv_notif_iter,
654 status = bt_py3_exc_to_notif_iter_status();
655 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
657 * Looks like there wasn't any exception from the Python
658 * side, but we're still in an error state here.
660 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
664 * Clear any exception: we're returning a bad status anyway. If
665 * this call originated from Python, then the user gets an
666 * appropriate creation error.
672 Py_XDECREF(py_comp_cls);
673 Py_XDECREF(py_iter_cls);
674 Py_XDECREF(py_iter_ptr);
675 Py_XDECREF(py_init_method_result);
680 static void bt_py3_cc_notification_iterator_finalize(
681 struct bt_private_notification_iterator *priv_notif_iter)
683 PyObject *py_notif_iter =
684 bt_private_notification_iterator_get_user_data(priv_notif_iter);
685 PyObject *py_method_result = NULL;
687 assert(py_notif_iter);
689 /* Call user's _finalize() method */
690 py_method_result = PyObject_CallMethod(py_notif_iter,
693 if (PyErr_Occurred()) {
694 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
698 * Ignore any exception raised by the _finalize() method because
699 * it won't change anything at this point: the component is
700 * being destroyed anyway.
703 Py_XDECREF(py_method_result);
704 Py_DECREF(py_notif_iter);
707 static struct bt_notification_iterator_next_return
708 bt_py3_cc_notification_iterator_next(
709 struct bt_private_notification_iterator *priv_notif_iter)
711 struct bt_notification_iterator_next_return next_ret = {
712 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
713 .notification = NULL,
715 PyObject *py_notif_iter =
716 bt_private_notification_iterator_get_user_data(priv_notif_iter);
717 PyObject *py_method_result = NULL;
719 assert(py_notif_iter);
720 py_method_result = PyObject_CallMethod(py_notif_iter,
721 "_next_from_native", NULL);
722 if (!py_method_result) {
723 next_ret.status = bt_py3_exc_to_notif_iter_status();
724 assert(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK);
729 * The returned object, on success, is an integer object
730 * (PyLong) containing the address of a native notification
731 * object (which is now ours).
733 next_ret.notification =
734 (struct bt_notification *) PyLong_AsUnsignedLongLong(
737 /* Clear potential overflow error; should never happen */
738 assert(!PyErr_Occurred());
739 assert(next_ret.notification);
743 Py_XDECREF(py_method_result);
747 static enum bt_component_status bt_py3_cc_sink_consume(
748 struct bt_private_component *priv_comp)
750 PyObject *py_comp = bt_private_component_get_user_data(priv_comp);
751 PyObject *py_method_result = NULL;
752 enum bt_component_status status;
755 py_method_result = PyObject_CallMethod(py_comp,
757 status = bt_py3_exc_to_component_status();
758 if (!py_method_result && status == BT_COMPONENT_STATUS_OK) {
759 /* Pretty sure this should never happen, but just in case */
760 BT_LOGE("User's _consume() method failed without raising an exception: "
761 "status=%d", status);
762 status = BT_COMPONENT_STATUS_ERROR;
765 Py_XDECREF(py_method_result);
770 /* Component class creation functions (called from Python module) */
772 static int bt_py3_cc_set_optional_attrs_methods(struct bt_component_class *cc,
773 const char *description, const char *help)
778 ret = bt_component_class_set_description(cc, description);
780 BT_LOGE("Cannot set component class's description: "
781 "comp-cls-addr=%p", cc);
787 ret = bt_component_class_set_help(cc, help);
789 BT_LOGE("Cannot set component class's help text: "
790 "comp-cls-addr=%p", cc);
795 ret = bt_component_class_set_init_method(cc, bt_py3_cc_init);
797 ret = bt_component_class_set_finalize_method(cc, bt_py3_cc_finalize);
799 ret = bt_component_class_set_accept_port_connection_method(cc,
800 bt_py3_cc_accept_port_connection);
802 ret = bt_component_class_set_port_connected_method(cc,
803 bt_py3_cc_port_connected);
805 ret = bt_component_class_set_port_disconnected_method(cc,
806 bt_py3_cc_port_disconnected);
808 ret = bt_component_class_set_query_method(cc, bt_py3_cc_query);
815 static void bt_py3_cc_set_optional_iter_methods(struct bt_component_class *cc,
816 int (*set_notif_iter_init_method)(struct bt_component_class *, bt_component_class_notification_iterator_init_method),
817 int (*set_notif_iter_finalize_method)(struct bt_component_class *, bt_component_class_notification_iterator_finalize_method))
819 int ret __attribute__((unused));
821 ret = set_notif_iter_init_method(
822 cc, bt_py3_cc_notification_iterator_init);
824 ret = set_notif_iter_finalize_method(
825 cc, bt_py3_cc_notification_iterator_finalize);
829 static struct bt_component_class *bt_py3_component_class_source_create(
830 PyObject *py_cls, const char *name, const char *description,
833 struct bt_component_class *cc;
837 cc = bt_component_class_source_create(name,
838 bt_py3_cc_notification_iterator_next);
840 BT_LOGE_STR("Cannot create source component class.");
844 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
846 BT_LOGE_STR("Cannot set source component class's optional attributes and methods.");
851 bt_py3_cc_set_optional_iter_methods(cc,
852 bt_component_class_source_set_notification_iterator_init_method,
853 bt_component_class_source_set_notification_iterator_finalize_method);
854 register_cc_ptr_to_py_cls(cc, py_cls);
855 bt_component_class_freeze(cc);
861 static struct bt_component_class *bt_py3_component_class_filter_create(
862 PyObject *py_cls, const char *name, const char *description,
865 struct bt_component_class *cc;
869 cc = bt_component_class_filter_create(name,
870 bt_py3_cc_notification_iterator_next);
872 BT_LOGE_STR("Cannot create filter component class.");
876 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
878 BT_LOGE_STR("Cannot set filter component class's optional attributes and methods.");
883 bt_py3_cc_set_optional_iter_methods(cc,
884 bt_component_class_filter_set_notification_iterator_init_method,
885 bt_component_class_filter_set_notification_iterator_finalize_method);
886 register_cc_ptr_to_py_cls(cc, py_cls);
887 bt_component_class_freeze(cc);
893 static struct bt_component_class *bt_py3_component_class_sink_create(
894 PyObject *py_cls, const char *name, const char *description,
897 struct bt_component_class *cc;
901 cc = bt_component_class_sink_create(name, bt_py3_cc_sink_consume);
903 BT_LOGE_STR("Cannot create sink component class.");
907 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
909 BT_LOGE_STR("Cannot set sink component class's optional attributes and methods.");
914 register_cc_ptr_to_py_cls(cc, py_cls);
915 bt_component_class_freeze(cc);
922 struct bt_component_class *bt_py3_component_class_source_create(
923 PyObject *py_cls, const char *name, const char *description,
925 struct bt_component_class *bt_py3_component_class_filter_create(
926 PyObject *py_cls, const char *name, const char *description,
928 struct bt_component_class *bt_py3_component_class_sink_create(
929 PyObject *py_cls, const char *name, const char *description,
931 void bt_py3_cc_init_from_bt2(void);
932 void bt_py3_cc_exit_handler(void);