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 /* From component-class-const.h */
27 typedef enum bt_component_class_status {
28 BT_COMPONENT_CLASS_STATUS_OK = 0,
29 BT_COMPONENT_CLASS_STATUS_NOMEM = -12,
30 } bt_component_class_status;
32 typedef enum bt_component_class_type {
33 BT_COMPONENT_CLASS_TYPE_SOURCE = 0,
34 BT_COMPONENT_CLASS_TYPE_FILTER = 1,
35 BT_COMPONENT_CLASS_TYPE_SINK = 2,
36 } bt_component_class_type;
38 extern const char *bt_component_class_get_name(
39 const bt_component_class *component_class);
41 extern const char *bt_component_class_get_description(
42 const bt_component_class *component_class);
44 extern const char *bt_component_class_get_help(
45 const bt_component_class *component_class);
47 extern bt_component_class_type bt_component_class_get_type(
48 const bt_component_class *component_class);
50 bt_bool bt_component_class_is_source(
51 const bt_component_class *component_class);
53 bt_bool bt_component_class_is_filter(
54 const bt_component_class *component_class);
56 bt_bool bt_component_class_is_sink(
57 const bt_component_class *component_class);
59 extern void bt_component_class_get_ref(
60 const bt_component_class *component_class);
62 extern void bt_component_class_put_ref(
63 const bt_component_class *component_class);
65 /* From component-class-source-const.h */
67 const bt_component_class *
68 bt_component_class_source_as_component_class_const(
69 const bt_component_class_source *comp_cls_source);
71 extern void bt_component_class_source_get_ref(
72 const bt_component_class_source *component_class_source);
74 extern void bt_component_class_source_put_ref(
75 const bt_component_class_source *component_class_source);
77 /* From component-class-source.h */
79 typedef bt_self_component_status
80 (*bt_component_class_source_init_method)(
81 bt_self_component_source *self_component,
82 const bt_value *params, void *init_method_data);
84 typedef void (*bt_component_class_source_finalize_method)(
85 bt_self_component_source *self_component);
87 typedef bt_self_message_iterator_status
88 (*bt_component_class_source_message_iterator_init_method)(
89 bt_self_message_iterator *message_iterator,
90 bt_self_component_source *self_component,
91 bt_self_component_port_output *port);
94 (*bt_component_class_source_message_iterator_finalize_method)(
95 bt_self_message_iterator *message_iterator);
97 typedef bt_self_message_iterator_status
98 (*bt_component_class_source_message_iterator_next_method)(
99 bt_self_message_iterator *message_iterator,
100 bt_message_array_const msgs, uint64_t capacity,
103 typedef bt_self_message_iterator_status
104 (*bt_component_class_source_message_iterator_seek_ns_from_origin_method)(
105 bt_self_message_iterator *message_iterator,
106 int64_t ns_from_origin);
108 typedef bt_self_message_iterator_status
109 (*bt_component_class_source_message_iterator_seek_beginning_method)(
110 bt_self_message_iterator *message_iterator);
113 (*bt_component_class_source_message_iterator_can_seek_ns_from_origin_method)(
114 bt_self_message_iterator *message_iterator,
115 int64_t ns_from_origin);
118 (*bt_component_class_source_message_iterator_can_seek_beginning_method)(
119 bt_self_message_iterator *message_iterator);
121 typedef bt_query_status (*bt_component_class_source_query_method)(
122 bt_self_component_class_source *comp_class,
123 const bt_query_executor *query_executor,
124 const char *object, const bt_value *params,
125 bt_logging_level log_level,
126 const bt_value **result);
128 typedef bt_self_component_status
129 (*bt_component_class_source_output_port_connected_method)(
130 bt_self_component_source *self_component,
131 bt_self_component_port_output *self_port,
132 const bt_port_input *other_port);
134 bt_component_class *bt_component_class_source_as_component_class(
135 bt_component_class_source *comp_cls_source);
138 bt_component_class_source *bt_component_class_source_create(
140 bt_component_class_source_message_iterator_next_method method);
142 extern bt_component_class_status
143 bt_component_class_source_set_init_method(
144 bt_component_class_source *comp_class,
145 bt_component_class_source_init_method method);
147 extern bt_component_class_status
148 bt_component_class_source_set_finalize_method(
149 bt_component_class_source *comp_class,
150 bt_component_class_source_finalize_method method);
152 extern bt_component_class_status
153 bt_component_class_source_set_output_port_connected_method(
154 bt_component_class_source *comp_class,
155 bt_component_class_source_output_port_connected_method method);
157 extern bt_component_class_status
158 bt_component_class_source_set_query_method(
159 bt_component_class_source *comp_class,
160 bt_component_class_source_query_method method);
162 extern bt_component_class_status
163 bt_component_class_source_set_message_iterator_init_method(
164 bt_component_class_source *comp_class,
165 bt_component_class_source_message_iterator_init_method method);
167 extern bt_component_class_status
168 bt_component_class_source_set_message_iterator_finalize_method(
169 bt_component_class_source *comp_class,
170 bt_component_class_source_message_iterator_finalize_method method);
172 extern bt_component_class_status
173 bt_component_class_source_set_message_iterator_seek_ns_from_origin_method(
174 bt_component_class_source *comp_class,
175 bt_component_class_source_message_iterator_seek_ns_from_origin_method method);
177 extern bt_component_class_status
178 bt_component_class_source_set_message_iterator_seek_beginning_method(
179 bt_component_class_source *comp_class,
180 bt_component_class_source_message_iterator_seek_beginning_method method);
183 bt_component_class_source_set_message_iterator_can_seek_ns_from_origin_method(
184 bt_component_class_source *comp_class,
185 bt_component_class_source_message_iterator_can_seek_ns_from_origin_method method);
188 bt_component_class_source_set_message_iterator_can_seek_beginning_method(
189 bt_component_class_source *comp_class,
190 bt_component_class_source_message_iterator_can_seek_beginning_method method);
192 /* From component-class-filter-const.h */
194 const bt_component_class *
195 bt_component_class_filter_as_component_class_const(
196 const bt_component_class_filter *comp_cls_filter);
198 extern void bt_component_class_filter_get_ref(
199 const bt_component_class_filter *component_class_filter);
201 extern void bt_component_class_filter_put_ref(
202 const bt_component_class_filter *component_class_filter);
204 /* From component-class-filter.h */
206 typedef bt_self_component_status
207 (*bt_component_class_filter_init_method)(
208 bt_self_component_filter *self_component,
209 const bt_value *params, void *init_method_data);
211 typedef void (*bt_component_class_filter_finalize_method)(
212 bt_self_component_filter *self_component);
214 typedef bt_self_message_iterator_status
215 (*bt_component_class_filter_message_iterator_init_method)(
216 bt_self_message_iterator *message_iterator,
217 bt_self_component_filter *self_component,
218 bt_self_component_port_output *port);
221 (*bt_component_class_filter_message_iterator_finalize_method)(
222 bt_self_message_iterator *message_iterator);
224 typedef bt_self_message_iterator_status
225 (*bt_component_class_filter_message_iterator_next_method)(
226 bt_self_message_iterator *message_iterator,
227 bt_message_array_const msgs, uint64_t capacity,
230 typedef bt_self_message_iterator_status
231 (*bt_component_class_filter_message_iterator_seek_ns_from_origin_method)(
232 bt_self_message_iterator *message_iterator,
233 int64_t ns_from_origin);
235 typedef bt_self_message_iterator_status
236 (*bt_component_class_filter_message_iterator_seek_beginning_method)(
237 bt_self_message_iterator *message_iterator);
240 (*bt_component_class_filter_message_iterator_can_seek_ns_from_origin_method)(
241 bt_self_message_iterator *message_iterator,
242 int64_t ns_from_origin);
245 (*bt_component_class_filter_message_iterator_can_seek_beginning_method)(
246 bt_self_message_iterator *message_iterator);
248 typedef bt_query_status
249 (*bt_component_class_filter_query_method)(
250 bt_self_component_class_filter *comp_class,
251 const bt_query_executor *query_executor,
252 const char *object, const bt_value *params,
253 bt_logging_level log_level,
254 const bt_value **result);
256 typedef bt_self_component_status
257 (*bt_component_class_filter_input_port_connected_method)(
258 bt_self_component_filter *self_component,
259 bt_self_component_port_input *self_port,
260 const bt_port_output *other_port);
262 typedef bt_self_component_status
263 (*bt_component_class_filter_output_port_connected_method)(
264 bt_self_component_filter *self_component,
265 bt_self_component_port_output *self_port,
266 const bt_port_input *other_port);
268 bt_component_class *bt_component_class_filter_as_component_class(
269 bt_component_class_filter *comp_cls_filter);
272 bt_component_class_filter *bt_component_class_filter_create(
274 bt_component_class_filter_message_iterator_next_method method);
276 extern bt_component_class_status
277 bt_component_class_filter_set_init_method(
278 bt_component_class_filter *comp_class,
279 bt_component_class_filter_init_method method);
281 extern bt_component_class_status
282 bt_component_class_filter_set_finalize_method(
283 bt_component_class_filter *comp_class,
284 bt_component_class_filter_finalize_method method);
286 extern bt_component_class_status
287 bt_component_class_filter_set_input_port_connected_method(
288 bt_component_class_filter *comp_class,
289 bt_component_class_filter_input_port_connected_method method);
291 extern bt_component_class_status
292 bt_component_class_filter_set_output_port_connected_method(
293 bt_component_class_filter *comp_class,
294 bt_component_class_filter_output_port_connected_method method);
296 extern bt_component_class_status
297 bt_component_class_filter_set_query_method(
298 bt_component_class_filter *comp_class,
299 bt_component_class_filter_query_method method);
301 extern bt_component_class_status
302 bt_component_class_filter_set_message_iterator_init_method(
303 bt_component_class_filter *comp_class,
304 bt_component_class_filter_message_iterator_init_method method);
306 extern bt_component_class_status
307 bt_component_class_filter_set_message_iterator_finalize_method(
308 bt_component_class_filter *comp_class,
309 bt_component_class_filter_message_iterator_finalize_method method);
311 extern bt_component_class_status
312 bt_component_class_filter_set_message_iterator_seek_ns_from_origin_method(
313 bt_component_class_filter *comp_class,
314 bt_component_class_filter_message_iterator_seek_ns_from_origin_method method);
316 extern bt_component_class_status
317 bt_component_class_filter_set_message_iterator_seek_beginning_method(
318 bt_component_class_filter *comp_class,
319 bt_component_class_filter_message_iterator_seek_beginning_method method);
322 bt_component_class_filter_set_message_iterator_can_seek_ns_from_origin_method(
323 bt_component_class_filter *comp_class,
324 bt_component_class_filter_message_iterator_can_seek_ns_from_origin_method method);
327 bt_component_class_filter_set_message_iterator_can_seek_beginning_method(
328 bt_component_class_filter *comp_class,
329 bt_component_class_filter_message_iterator_can_seek_beginning_method method);
331 /* From component-class-sink-const.h */
333 const bt_component_class *
334 bt_component_class_sink_as_component_class_const(
335 const bt_component_class_sink *comp_cls_sink);
337 extern void bt_component_class_sink_get_ref(
338 const bt_component_class_sink *component_class_sink);
340 extern void bt_component_class_sink_put_ref(
341 const bt_component_class_sink *component_class_sink);
343 /* From component-class-sink.h */
345 typedef bt_self_component_status (*bt_component_class_sink_init_method)(
346 bt_self_component_sink *self_component,
347 const bt_value *params, void *init_method_data);
349 typedef void (*bt_component_class_sink_finalize_method)(
350 bt_self_component_sink *self_component);
352 typedef bt_query_status
353 (*bt_component_class_sink_query_method)(
354 bt_self_component_class_sink *comp_class,
355 const bt_query_executor *query_executor,
356 const char *object, const bt_value *params,
357 bt_logging_level log_level,
358 const bt_value **result);
360 typedef bt_self_component_status
361 (*bt_component_class_sink_input_port_connected_method)(
362 bt_self_component_sink *self_component,
363 bt_self_component_port_input *self_port,
364 const bt_port_output *other_port);
366 typedef bt_self_component_status
367 (*bt_component_class_sink_graph_is_configured_method)(
368 bt_self_component_sink *self_component);
370 typedef bt_self_component_status (*bt_component_class_sink_consume_method)(
371 bt_self_component_sink *self_component);
373 bt_component_class *bt_component_class_sink_as_component_class(
374 bt_component_class_sink *comp_cls_sink);
377 bt_component_class_sink *bt_component_class_sink_create(
379 bt_component_class_sink_consume_method method);
381 extern bt_component_class_status bt_component_class_sink_set_init_method(
382 bt_component_class_sink *comp_class,
383 bt_component_class_sink_init_method method);
385 extern bt_component_class_status bt_component_class_sink_set_finalize_method(
386 bt_component_class_sink *comp_class,
387 bt_component_class_sink_finalize_method method);
389 extern bt_component_class_status
390 bt_component_class_sink_set_input_port_connected_method(
391 bt_component_class_sink *comp_class,
392 bt_component_class_sink_input_port_connected_method method);
394 extern bt_component_class_status
395 bt_component_class_sink_set_graph_is_configured_method(
396 bt_component_class_sink *comp_class,
397 bt_component_class_sink_graph_is_configured_method method);
399 extern bt_component_class_status bt_component_class_sink_set_query_method(
400 bt_component_class_sink *comp_class,
401 bt_component_class_sink_query_method method);
403 /* From self-component-class-source.h */
405 const bt_component_class_source *
406 bt_self_component_class_source_as_component_class_source(
407 bt_self_component_class_source *self_comp_cls_source);
409 /* From self-component-class-filter.h */
411 const bt_component_class_filter *
412 bt_self_component_class_filter_as_component_class_filter(
413 bt_self_component_class_filter *self_comp_cls_filter);
415 /* From self-component-class-sink.h */
417 const bt_component_class_sink *
418 bt_self_component_class_sink_as_component_class_sink(
419 bt_self_component_class_sink *self_comp_cls_sink);
423 * This hash table associates a BT component class object address to a
424 * user-defined Python class (PyObject *). The keys and values are NOT
425 * owned by this hash table. The Python class objects are owned by the
426 * Python module, which should not be unloaded until it is not possible
427 * to create a user Python component anyway.
429 * This hash table is written to when a user-defined Python component
430 * class is created by one of the bt_py3_component_class_*_create()
433 * This function is read from when a user calls bt_component_create()
434 * with a component class pointer created by one of the functions above.
435 * In this case, the original Python class needs to be found to
436 * instantiate it and associate the created Python component object with
437 * a BT component object instance.
440 static GHashTable *bt_cc_ptr_to_py_cls;
442 static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
445 if (!bt_cc_ptr_to_py_cls) {
447 * Lazy-initializing this GHashTable because GLib
448 * might not be initialized yet and it needs to be
449 * before we call g_hash_table_new()
451 BT_LOGD_STR("Creating native component class to Python component class hash table.");
452 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
453 BT_ASSERT(bt_cc_ptr_to_py_cls);
456 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
460 static PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
462 if (!bt_cc_ptr_to_py_cls) {
463 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
464 "comp-cls-addr=%p", bt_cc);
468 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
469 (gconstpointer) bt_cc);
474 * Useful Python objects.
477 static PyObject *py_mod_bt2 = NULL;
478 static PyObject *py_mod_bt2_exc_error_type = NULL;
479 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
480 static PyObject *py_mod_bt2_exc_stop_type = NULL;
481 static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
482 static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL;
483 static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL;
485 static void bt_py3_cc_init_from_bt2(void)
488 * This is called once the bt2 package is loaded.
490 * Those modules and functions are needed while the package is
491 * used. Loading them here is safe because we know the bt2
492 * package is imported, and we know that the user cannot use the
493 * code here without importing bt2 first.
495 py_mod_bt2 = PyImport_ImportModule("bt2");
496 BT_ASSERT(py_mod_bt2);
497 py_mod_bt2_exc_error_type =
498 PyObject_GetAttrString(py_mod_bt2, "Error");
499 BT_ASSERT(py_mod_bt2_exc_error_type);
500 py_mod_bt2_exc_try_again_type =
501 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
502 BT_ASSERT(py_mod_bt2_exc_try_again_type);
503 py_mod_bt2_exc_stop_type =
504 PyObject_GetAttrString(py_mod_bt2, "Stop");
505 BT_ASSERT(py_mod_bt2_exc_stop_type);
506 py_mod_bt2_exc_invalid_query_object_type =
507 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject");
508 BT_ASSERT(py_mod_bt2_exc_invalid_query_object_type);
509 py_mod_bt2_exc_invalid_query_params_type =
510 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams");
511 BT_ASSERT(py_mod_bt2_exc_invalid_query_params_type);
514 static void bt_py3_cc_exit_handler(void)
517 * This is an exit handler (set by the bt2 package).
519 * We only give back the references that we took in
520 * bt_py3_cc_init_from_bt2() here. The global variables continue
521 * to exist for the code of this file, but they are now borrowed
522 * references. If this code is executed, it means that somehow
523 * the modules are still loaded, so it should be safe to use
524 * them even without a strong reference.
526 * We cannot do this in the library's destructor because it
527 * gets executed once Python is already finalized.
529 Py_XDECREF(py_mod_bt2);
530 Py_XDECREF(py_mod_bt2_exc_error_type);
531 Py_XDECREF(py_mod_bt2_exc_try_again_type);
532 Py_XDECREF(py_mod_bt2_exc_stop_type);
533 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
534 Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type);
535 Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type);
539 /* Library destructor */
541 __attribute__((destructor))
542 static void bt_py3_native_comp_class_dtor(void) {
543 /* Destroy component class association hash table */
544 if (bt_cc_ptr_to_py_cls) {
545 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
546 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
551 // TODO: maybe we can wrap code in the Python methods (e.g. _query_from_native)
552 // in a try catch and print the error there instead, it would be simpler.
554 void bt2_py_loge_exception(void)
556 PyObject *type = NULL;
557 PyObject *value = NULL;
558 PyObject *traceback = NULL;
559 PyObject *traceback_module = NULL;
560 PyObject *format_exception_func = NULL;
561 PyObject *exc_str_list = NULL;
562 GString *msg_buf = NULL;
565 BT_ASSERT(PyErr_Occurred() != NULL);
567 PyErr_Fetch(&type, &value, &traceback);
569 BT_ASSERT(type != NULL);
572 * traceback can be NULL, when we fail to call a Python function from the
573 * native code (there is no Python stack at that point). E.g.:
575 * TypeError: _query_from_native() takes 5 positional arguments but 8 were given
579 /* Make sure `value` is what we expected - an instance of `type`. */
580 PyErr_NormalizeException(&type, &value, &traceback);
582 traceback_module = PyImport_ImportModule("traceback");
583 if (!traceback_module) {
584 BT_LOGE_STR("Failed to log Python exception (could not import traceback module).");
588 format_exception_func = PyObject_GetAttrString(traceback_module,
589 traceback ? "format_exception" : "format_exception_only");
590 if (!format_exception_func) {
591 BT_LOGE_STR("Failed to log Python exception (could not find format_exception).");
595 if (!PyCallable_Check(format_exception_func)) {
596 BT_LOGE_STR("Failed to log Python exception (format_exception is not callable).");
600 exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, type, value, traceback, NULL);
603 BT_LOGE_STR("Failed to log Python exception (call to format_exception failed).");
607 msg_buf = g_string_new(NULL);
609 for (i = 0; i < PyList_Size(exc_str_list); i++) {
610 PyObject *exc_str = PyList_GetItem(exc_str_list, i);
611 const char *str = PyUnicode_AsUTF8(exc_str);
613 BT_LOGE_STR("Failed to log Python exception (failed to convert exception to string).");
617 g_string_append(msg_buf, str);
620 BT_LOGE_STR(msg_buf->str);
624 g_string_free(msg_buf, TRUE);
626 Py_XDECREF(exc_str_list);
627 Py_XDECREF(format_exception_func);
628 Py_XDECREF(traceback_module);
630 /* PyErr_Restore takes our references. */
631 PyErr_Restore(type, value, traceback);
634 static bt_self_component_status bt_py3_exc_to_self_component_status(void)
636 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
637 PyObject *exc = PyErr_Occurred();
643 if (PyErr_GivenExceptionMatches(exc,
644 py_mod_bt2_exc_try_again_type)) {
645 status = BT_SELF_COMPONENT_STATUS_AGAIN;
646 } else if (PyErr_GivenExceptionMatches(exc,
647 py_mod_bt2_exc_stop_type)) {
648 status = BT_SELF_COMPONENT_STATUS_END;
650 bt2_py_loge_exception();
651 status = BT_SELF_COMPONENT_STATUS_ERROR;
659 /* Component class proxy methods (delegate to the attached Python object) */
661 static bt_self_message_iterator_status
662 bt_py3_exc_to_self_message_iterator_status(void)
664 enum bt_self_message_iterator_status status =
665 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
666 PyObject *exc = PyErr_Occurred();
672 if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) {
673 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
674 } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) {
675 status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN;
677 bt2_py_loge_exception();
678 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
686 static enum bt_query_status bt_py3_exc_to_query_status(void)
688 enum bt_query_status status = BT_QUERY_STATUS_OK;
689 PyObject *exc = PyErr_Occurred();
695 if (PyErr_GivenExceptionMatches(exc,
696 py_mod_bt2_exc_invalid_query_object_type)) {
697 status = BT_QUERY_STATUS_INVALID_OBJECT;
698 } else if (PyErr_GivenExceptionMatches(exc,
699 py_mod_bt2_exc_invalid_query_params_type)) {
700 status = BT_QUERY_STATUS_INVALID_PARAMS;
701 } else if (PyErr_GivenExceptionMatches(exc,
702 py_mod_bt2_exc_try_again_type)) {
703 status = BT_QUERY_STATUS_AGAIN;
705 bt2_py_loge_exception();
706 status = BT_QUERY_STATUS_ERROR;
714 static bt_self_component_status
715 bt_py3_component_class_init(
716 bt_self_component *self_component,
717 void *self_component_v,
718 swig_type_info *self_comp_cls_type_swig_type,
719 const bt_value *params,
720 void *init_method_data)
722 const bt_component *component = bt_self_component_as_component(self_component);
723 const bt_component_class *component_class = bt_component_borrow_class_const(component);
724 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
725 PyObject *py_cls = NULL;
726 PyObject *py_comp = NULL;
727 PyObject *py_params_ptr = NULL;
728 PyObject *py_comp_ptr = NULL;
730 (void) init_method_data;
732 BT_ASSERT(self_component);
733 BT_ASSERT(self_component_v);
734 BT_ASSERT(self_comp_cls_type_swig_type);
737 * Get the user-defined Python class which created this
738 * component's class in the first place (borrowed
741 py_cls = lookup_cc_ptr_to_py_cls(component_class);
743 BT_LOGE("Cannot find Python class associated to native component class: "
744 "comp-cls-addr=%p", component_class);
748 /* Parameters pointer -> SWIG pointer Python object */
749 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
750 SWIGTYPE_p_bt_value, 0);
751 if (!py_params_ptr) {
752 BT_LOGE_STR("Failed to create a SWIG pointer object.");
756 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
757 self_comp_cls_type_swig_type, 0);
759 BT_LOGE_STR("Failed to create a SWIG pointer object.");
764 * Do the equivalent of this:
766 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
768 * _UserComponentType._init_from_native() calls the Python
769 * component object's __init__() function.
771 py_comp = PyObject_CallMethod(py_cls,
772 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
774 bt2_py_loge_exception();
775 BT_LOGE("Failed to call Python class's _init_from_native() method: "
776 "py-cls-addr=%p", py_cls);
782 * Our user Python component object is now fully created and
783 * initialized by the user. Since we just created it, this
784 * native component is its only (persistent) owner.
786 bt_self_component_set_data(self_component, py_comp);
791 status = BT_SELF_COMPONENT_STATUS_ERROR;
794 * Clear any exception: we're returning a bad status anyway. If
795 * this call originated from Python (creation from a plugin's
796 * component class, for example), then the user gets an
797 * appropriate creation error.
803 Py_XDECREF(py_params_ptr);
804 Py_XDECREF(py_comp_ptr);
809 * Method of bt_component_class_source to initialize a bt_self_component_source
813 static bt_self_component_status
814 bt_py3_component_class_source_init(bt_self_component_source *self_component_source,
815 const bt_value *params, void *init_method_data)
817 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
818 return bt_py3_component_class_init(
820 self_component_source,
821 SWIGTYPE_p_bt_self_component_source,
822 params, init_method_data);
825 static bt_self_component_status
826 bt_py3_component_class_filter_init(bt_self_component_filter *self_component_filter,
827 const bt_value *params, void *init_method_data)
829 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
830 return bt_py3_component_class_init(
832 self_component_filter,
833 SWIGTYPE_p_bt_self_component_filter,
834 params, init_method_data);
837 static bt_self_component_status
838 bt_py3_component_class_sink_init(bt_self_component_sink *self_component_sink,
839 const bt_value *params, void *init_method_data)
841 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
842 return bt_py3_component_class_init(
845 SWIGTYPE_p_bt_self_component_sink,
846 params, init_method_data);
849 static void bt_py3_component_class_finalize(bt_self_component *self_component)
851 PyObject *py_comp = bt_self_component_get_data(self_component);
854 /* Call user's _finalize() method */
855 PyObject *py_method_result = PyObject_CallMethod(py_comp,
858 if (PyErr_Occurred()) {
859 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
863 * Ignore any exception raised by the _finalize() method because
864 * it won't change anything at this point: the component is
865 * being destroyed anyway.
868 Py_XDECREF(py_method_result);
873 bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source)
875 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
876 bt_py3_component_class_finalize(self_component);
880 bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter)
882 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
883 bt_py3_component_class_finalize(self_component);
887 bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink)
889 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
890 bt_py3_component_class_finalize(self_component);
893 static bt_self_component_status
894 bt_py3_component_class_port_connected(
895 bt_self_component *self_component,
896 void *self_component_port,
897 swig_type_info *self_component_port_swig_type,
898 bt_port_type self_component_port_type,
899 const void *other_port,
900 swig_type_info *other_port_swig_type)
902 bt_self_component_status status;
903 PyObject *py_comp = NULL;
904 PyObject *py_self_port_ptr = NULL;
905 PyObject *py_other_port_ptr = NULL;
906 PyObject *py_method_result = NULL;
908 py_comp = bt_self_component_get_data(self_component);
911 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
912 self_component_port_swig_type, 0);
913 if (!py_self_port_ptr) {
914 BT_LOGF_STR("Failed to create a SWIG pointer object.");
915 status = BT_SELF_COMPONENT_STATUS_NOMEM;
919 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
920 other_port_swig_type, 0);
921 if (!py_other_port_ptr) {
922 BT_LOGF_STR("Failed to create a SWIG pointer object.");
923 status = BT_SELF_COMPONENT_STATUS_NOMEM;
926 py_method_result = PyObject_CallMethod(py_comp,
927 "_port_connected_from_native", "(OiO)", py_self_port_ptr,
928 self_component_port_type, py_other_port_ptr);
930 BT_ASSERT(!py_method_result || py_method_result == Py_None);
932 status = bt_py3_exc_to_self_component_status();
935 Py_XDECREF(py_self_port_ptr);
936 Py_XDECREF(py_other_port_ptr);
937 Py_XDECREF(py_method_result);
942 static bt_self_component_status
943 bt_py3_component_class_source_output_port_connected(
944 bt_self_component_source *self_component_source,
945 bt_self_component_port_output *self_component_port_output,
946 const bt_port_input *other_port_input)
948 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
950 return bt_py3_component_class_port_connected(
952 self_component_port_output,
953 SWIGTYPE_p_bt_self_component_port_output,
956 SWIGTYPE_p_bt_port_input);
959 static bt_self_component_status
960 bt_py3_component_class_filter_input_port_connected(
961 bt_self_component_filter *self_component_filter,
962 bt_self_component_port_input *self_component_port_input,
963 const bt_port_output *other_port_output)
965 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
967 return bt_py3_component_class_port_connected(
969 self_component_port_input,
970 SWIGTYPE_p_bt_self_component_port_input,
973 SWIGTYPE_p_bt_port_output);
976 static bt_self_component_status
977 bt_py3_component_class_filter_output_port_connected(
978 bt_self_component_filter *self_component_filter,
979 bt_self_component_port_output *self_component_port_output,
980 const bt_port_input *other_port_input)
982 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
984 return bt_py3_component_class_port_connected(
986 self_component_port_output,
987 SWIGTYPE_p_bt_self_component_port_output,
990 SWIGTYPE_p_bt_port_input);
993 static bt_self_component_status
994 bt_py3_component_class_sink_input_port_connected(
995 bt_self_component_sink *self_component_sink,
996 bt_self_component_port_input *self_component_port_input,
997 const bt_port_output *other_port_output)
999 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1001 return bt_py3_component_class_port_connected(
1003 self_component_port_input,
1004 SWIGTYPE_p_bt_self_component_port_input,
1007 SWIGTYPE_p_bt_port_output);
1010 static bt_self_component_status
1011 bt_py3_component_class_sink_graph_is_configured(bt_self_component_sink *self_component_sink)
1013 PyObject *py_comp = NULL;
1014 PyObject *py_method_result = NULL;
1015 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
1016 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1018 py_comp = bt_self_component_get_data(self_component);
1019 py_method_result = PyObject_CallMethod(py_comp,
1020 "_graph_is_configured_from_native", NULL);
1022 BT_ASSERT(!py_method_result || py_method_result == Py_None);
1024 status = bt_py3_exc_to_self_component_status();
1026 Py_XDECREF(py_method_result);
1031 static bt_query_status
1032 bt_py3_component_class_query(
1033 const bt_component_class *component_class,
1034 const bt_query_executor *query_executor,
1035 const char *object, const bt_value *params,
1036 bt_logging_level log_level,
1037 const bt_value **result)
1039 PyObject *py_cls = NULL;
1040 PyObject *py_params_ptr = NULL;
1041 PyObject *py_query_exec_ptr = NULL;
1042 PyObject *py_query_func = NULL;
1043 PyObject *py_object = NULL;
1044 PyObject *py_results_addr = NULL;
1045 bt_query_status status = BT_QUERY_STATUS_OK;
1047 py_cls = lookup_cc_ptr_to_py_cls(component_class);
1049 BT_LOGE("Cannot find Python class associated to native component class: "
1050 "comp-cls-addr=%p", component_class);
1054 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
1055 SWIGTYPE_p_bt_value, 0);
1056 if (!py_params_ptr) {
1057 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1061 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
1062 SWIGTYPE_p_bt_query_executor, 0);
1063 if (!py_query_exec_ptr) {
1064 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1068 py_object = SWIG_FromCharPtr(object);
1070 BT_LOGE_STR("Failed to create a Python string.");
1074 py_results_addr = PyObject_CallMethod(py_cls,
1075 "_query_from_native", "(OOOi)", py_query_exec_ptr,
1076 py_object, py_params_ptr, (int) log_level);
1078 if (!py_results_addr) {
1079 BT_LOGE("Failed to call Python class's _query_from_native() method: "
1080 "py-cls-addr=%p", py_cls);
1081 status = bt_py3_exc_to_query_status();
1086 * The returned object, on success, is an integer object
1087 * (PyLong) containing the address of a BT value object (new
1090 *result = PyLong_AsVoidPtr(py_results_addr);
1091 BT_ASSERT(!PyErr_Occurred());
1097 status = BT_QUERY_STATUS_ERROR;
1100 Py_XDECREF(py_params_ptr);
1101 Py_XDECREF(py_query_exec_ptr);
1102 Py_XDECREF(py_query_func);
1103 Py_XDECREF(py_object);
1104 Py_XDECREF(py_results_addr);
1108 static bt_query_status
1109 bt_py3_component_class_source_query(
1110 bt_self_component_class_source *self_component_class_source,
1111 const bt_query_executor *query_executor,
1112 const char *object, const bt_value *params,
1113 bt_logging_level log_level,
1114 const bt_value **result)
1116 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
1117 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
1118 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
1121 static bt_query_status
1122 bt_py3_component_class_filter_query(
1123 bt_self_component_class_filter *self_component_class_filter,
1124 const bt_query_executor *query_executor,
1125 const char *object, const bt_value *params,
1126 bt_logging_level log_level,
1127 const bt_value **result)
1129 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
1130 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
1131 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
1134 static bt_query_status
1135 bt_py3_component_class_sink_query(
1136 bt_self_component_class_sink *self_component_class_sink,
1137 const bt_query_executor *query_executor,
1138 const char *object, const bt_value *params,
1139 bt_logging_level log_level,
1140 const bt_value **result)
1142 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
1143 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
1144 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
1147 static bt_self_message_iterator_status
1148 bt_py3_component_class_message_iterator_init(
1149 bt_self_message_iterator *self_message_iterator,
1150 bt_self_component *self_component,
1151 bt_self_component_port_output *self_component_port_output)
1153 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
1154 PyObject *py_comp_cls = NULL;
1155 PyObject *py_iter_cls = NULL;
1156 PyObject *py_iter_ptr = NULL;
1157 PyObject *py_component_port_output_ptr = NULL;
1158 PyObject *py_init_method_result = NULL;
1159 PyObject *py_iter = NULL;
1162 py_comp = bt_self_component_get_data(self_component);
1164 /* Find user's Python message iterator class */
1165 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
1167 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
1171 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
1173 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
1177 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
1178 SWIGTYPE_p_bt_self_message_iterator, 0);
1180 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1185 * Create object with borrowed native message iterator
1188 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
1190 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
1191 "(OO)", py_iter_cls, py_iter_ptr);
1193 BT_LOGE("Failed to call Python class's __new__() method: "
1194 "py-cls-addr=%p", py_iter_cls);
1195 bt2_py_loge_exception();
1200 * Initialize object:
1202 * py_iter.__init__(self_output_port)
1204 * through the _init_for_native helper static method.
1206 * At this point, py_iter._ptr is set, so this initialization
1207 * function has access to self._component (which gives it the
1208 * user Python component object from which the iterator was
1211 py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output),
1212 SWIGTYPE_p_bt_self_component_port_output, 0);
1213 if (!py_component_port_output_ptr) {
1214 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1218 py_init_method_result = PyObject_CallMethod(py_iter, "_init_from_native", "O", py_component_port_output_ptr);
1219 if (!py_init_method_result) {
1220 BT_LOGE_STR("User's __init__() method failed.");
1221 bt2_py_loge_exception();
1226 * Since the Python code can never instantiate a user-defined
1227 * message iterator class, the native message iterator
1228 * object does NOT belong to a user Python message iterator
1229 * object (borrowed reference). However this Python object is
1230 * owned by this native message iterator object.
1232 * In the Python world, the lifetime of the native message
1233 * iterator is managed by a _GenericMessageIterator
1236 * _GenericMessageIterator instance:
1237 * owns a native bt_message_iterator object (iter)
1238 * owns a _UserMessageIterator instance (py_iter)
1239 * self._ptr is a borrowed reference to the
1240 * native bt_private_connection_private_message_iterator
1243 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1248 status = bt_py3_exc_to_self_message_iterator_status();
1249 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
1251 * Looks like there wasn't any exception from the Python
1252 * side, but we're still in an error state here.
1254 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
1258 * Clear any exception: we're returning a bad status anyway. If
1259 * this call originated from Python, then the user gets an
1260 * appropriate creation error.
1265 Py_XDECREF(py_comp_cls);
1266 Py_XDECREF(py_iter_cls);
1267 Py_XDECREF(py_iter_ptr);
1268 Py_XDECREF(py_component_port_output_ptr);
1269 Py_XDECREF(py_init_method_result);
1270 Py_XDECREF(py_iter);
1274 static bt_self_message_iterator_status
1275 bt_py3_component_class_source_message_iterator_init(
1276 bt_self_message_iterator *self_message_iterator,
1277 bt_self_component_source *self_component_source,
1278 bt_self_component_port_output *self_component_port_output)
1280 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
1281 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1284 static bt_self_message_iterator_status
1285 bt_py3_component_class_filter_message_iterator_init(
1286 bt_self_message_iterator *self_message_iterator,
1287 bt_self_component_filter *self_component_filter,
1288 bt_self_component_port_output *self_component_port_output)
1290 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
1291 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1295 bt_py3_component_class_message_iterator_finalize(
1296 bt_self_message_iterator *message_iterator)
1298 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1299 PyObject *py_method_result = NULL;
1301 BT_ASSERT(py_message_iter);
1303 /* Call user's _finalize() method */
1304 py_method_result = PyObject_CallMethod(py_message_iter,
1307 if (PyErr_Occurred()) {
1308 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
1312 * Ignore any exception raised by the _finalize() method because
1313 * it won't change anything at this point: the component is
1314 * being destroyed anyway.
1317 Py_XDECREF(py_method_result);
1318 Py_DECREF(py_message_iter);
1321 /* Valid for both sources and filters. */
1323 static bt_self_message_iterator_status
1324 bt_py3_component_class_message_iterator_next(
1325 bt_self_message_iterator *message_iterator,
1326 bt_message_array_const msgs, uint64_t capacity,
1329 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
1330 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1331 PyObject *py_method_result = NULL;
1333 BT_ASSERT(py_message_iter);
1334 py_method_result = PyObject_CallMethod(py_message_iter,
1335 "_next_from_native", NULL);
1336 if (!py_method_result) {
1337 status = bt_py3_exc_to_self_message_iterator_status();
1338 BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK);
1343 * The returned object, on success, is an integer object
1344 * (PyLong) containing the address of a native message
1345 * object (which is now ours).
1347 msgs[0] = PyLong_AsVoidPtr(py_method_result);
1350 /* Clear potential overflow error; should never happen */
1351 BT_ASSERT(!PyErr_Occurred());
1355 Py_XDECREF(py_method_result);
1359 static bt_self_component_status
1360 bt_py3_component_class_sink_consume(bt_self_component_sink *self_component_sink)
1362 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1363 PyObject *py_comp = bt_self_component_get_data(self_component);
1364 PyObject *py_method_result = NULL;
1365 bt_self_component_status status;
1368 py_method_result = PyObject_CallMethod(py_comp,
1371 status = bt_py3_exc_to_self_component_status();
1372 if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) {
1373 /* Pretty sure this should never happen, but just in case */
1374 BT_LOGE("User's _consume() method failed without raising an exception: "
1375 "status=%d", status);
1376 status = BT_SELF_COMPONENT_STATUS_ERROR;
1379 Py_XDECREF(py_method_result);
1384 int bt_py3_component_class_set_help_and_desc(
1385 bt_component_class *component_class,
1386 const char *description, const char *help)
1391 ret = bt_component_class_set_description(component_class, description);
1393 BT_LOGE("Cannot set component class's description: "
1394 "comp-cls-addr=%p", component_class);
1400 ret = bt_component_class_set_help(component_class, help);
1402 BT_LOGE("Cannot set component class's help text: "
1403 "comp-cls-addr=%p", component_class);
1415 bt_component_class_source *bt_py3_component_class_source_create(
1416 PyObject *py_cls, const char *name, const char *description,
1419 bt_component_class_source *component_class_source;
1420 bt_component_class *component_class;
1425 component_class_source = bt_component_class_source_create(name,
1426 bt_py3_component_class_message_iterator_next);
1427 if (!component_class_source) {
1428 BT_LOGE_STR("Cannot create source component class.");
1432 component_class = bt_component_class_source_as_component_class(component_class_source);
1434 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1438 ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init);
1439 BT_ASSERT(ret == 0);
1440 ret = bt_component_class_source_set_finalize_method (component_class_source, bt_py3_component_class_source_finalize);
1441 BT_ASSERT(ret == 0);
1442 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1443 bt_py3_component_class_source_output_port_connected);
1444 BT_ASSERT(ret == 0);
1445 ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query);
1446 BT_ASSERT(ret == 0);
1447 ret = bt_component_class_source_set_message_iterator_init_method(
1448 component_class_source, bt_py3_component_class_source_message_iterator_init);
1449 BT_ASSERT(ret == 0);
1450 ret = bt_component_class_source_set_message_iterator_finalize_method(
1451 component_class_source, bt_py3_component_class_message_iterator_finalize);
1452 BT_ASSERT(ret == 0);
1454 register_cc_ptr_to_py_cls(component_class, py_cls);
1457 return component_class_source;
1461 bt_component_class_filter *bt_py3_component_class_filter_create(
1462 PyObject *py_cls, const char *name, const char *description,
1465 bt_component_class *component_class;
1466 bt_component_class_filter *component_class_filter;
1471 component_class_filter = bt_component_class_filter_create(name,
1472 bt_py3_component_class_message_iterator_next);
1473 if (!component_class_filter) {
1474 BT_LOGE_STR("Cannot create filter component class.");
1478 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1480 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1484 ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init);
1485 BT_ASSERT(ret == 0);
1486 ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize);
1487 BT_ASSERT(ret == 0);
1488 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1489 bt_py3_component_class_filter_input_port_connected);
1490 BT_ASSERT(ret == 0);
1491 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1492 bt_py3_component_class_filter_output_port_connected);
1493 BT_ASSERT(ret == 0);
1494 ret = bt_component_class_filter_set_query_method(component_class_filter, bt_py3_component_class_filter_query);
1495 BT_ASSERT(ret == 0);
1496 ret = bt_component_class_filter_set_message_iterator_init_method(
1497 component_class_filter, bt_py3_component_class_filter_message_iterator_init);
1498 BT_ASSERT(ret == 0);
1499 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1500 component_class_filter, bt_py3_component_class_message_iterator_finalize);
1501 BT_ASSERT(ret == 0);
1503 register_cc_ptr_to_py_cls(component_class, py_cls);
1506 return component_class_filter;
1510 bt_component_class_sink *bt_py3_component_class_sink_create(
1511 PyObject *py_cls, const char *name, const char *description,
1514 bt_component_class_sink *component_class_sink;
1515 bt_component_class *component_class;
1520 component_class_sink = bt_component_class_sink_create(name, bt_py3_component_class_sink_consume);
1522 if (!component_class_sink) {
1523 BT_LOGE_STR("Cannot create sink component class.");
1527 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1529 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1533 ret = bt_component_class_sink_set_init_method(component_class_sink, bt_py3_component_class_sink_init);
1534 BT_ASSERT(ret == 0);
1535 ret = bt_component_class_sink_set_finalize_method(component_class_sink, bt_py3_component_class_sink_finalize);
1536 BT_ASSERT(ret == 0);
1537 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1538 bt_py3_component_class_sink_input_port_connected);
1539 BT_ASSERT(ret == 0);
1540 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1541 bt_py3_component_class_sink_graph_is_configured);
1542 BT_ASSERT(ret == 0);
1543 ret = bt_component_class_sink_set_query_method(component_class_sink, bt_py3_component_class_sink_query);
1544 BT_ASSERT(ret == 0);
1546 register_cc_ptr_to_py_cls(component_class, py_cls);
1549 return component_class_sink;
1553 struct bt_component_class_source *bt_py3_component_class_source_create(
1554 PyObject *py_cls, const char *name, const char *description,
1556 struct bt_component_class_filter *bt_py3_component_class_filter_create(
1557 PyObject *py_cls, const char *name, const char *description,
1559 struct bt_component_class_sink *bt_py3_component_class_sink_create(
1560 PyObject *py_cls, const char *name, const char *description,
1562 void bt_py3_cc_init_from_bt2(void);
1563 void bt_py3_cc_exit_handler(void);