bt2: report errors from Python component and component class callbacks
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i
CommitLineData
6945df9a
SM
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
d24d5663 25%include <babeltrace2/graph/component-class.h>
d6bb425c
SM
26%include <babeltrace2/graph/component-class-const.h>
27%include <babeltrace2/graph/component-class-source-const.h>
28%include <babeltrace2/graph/component-class-source.h>
29%include <babeltrace2/graph/component-class-filter-const.h>
30%include <babeltrace2/graph/component-class-filter.h>
31%include <babeltrace2/graph/component-class-sink-const.h>
32%include <babeltrace2/graph/component-class-sink.h>
33%include <babeltrace2/graph/self-component-class-source.h>
34%include <babeltrace2/graph/self-component-class-filter.h>
35%include <babeltrace2/graph/self-component-class-sink.h>
6945df9a
SM
36
37%{
38/*
39 * This hash table associates a BT component class object address to a
40 * user-defined Python class (PyObject *). The keys and values are NOT
41 * owned by this hash table. The Python class objects are owned by the
42 * Python module, which should not be unloaded until it is not possible
43 * to create a user Python component anyway.
44 *
45 * This hash table is written to when a user-defined Python component
d24d5663 46 * class is created by one of the bt_bt2_component_class_*_create()
6945df9a
SM
47 * functions.
48 *
49 * This function is read from when a user calls bt_component_create()
50 * with a component class pointer created by one of the functions above.
51 * In this case, the original Python class needs to be found to
52 * instantiate it and associate the created Python component object with
53 * a BT component object instance.
54 */
55
56static GHashTable *bt_cc_ptr_to_py_cls;
57
7744859c
SM
58static
59void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
6945df9a
SM
60 PyObject *py_cls)
61{
62 if (!bt_cc_ptr_to_py_cls) {
63 /*
64 * Lazy-initializing this GHashTable because GLib
65 * might not be initialized yet and it needs to be
66 * before we call g_hash_table_new()
67 */
68 BT_LOGD_STR("Creating native component class to Python component class hash table.");
69 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
70 BT_ASSERT(bt_cc_ptr_to_py_cls);
71 }
72
73 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
74 (gpointer) py_cls);
75}
76
7744859c
SM
77static
78PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
6945df9a
SM
79{
80 if (!bt_cc_ptr_to_py_cls) {
81 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
82 "comp-cls-addr=%p", bt_cc);
83 return NULL;
84 }
85
86 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
87 (gconstpointer) bt_cc);
88}
89
90
91/*
92 * Useful Python objects.
93 */
94
95static PyObject *py_mod_bt2 = NULL;
96static PyObject *py_mod_bt2_exc_error_type = NULL;
97static PyObject *py_mod_bt2_exc_try_again_type = NULL;
98static PyObject *py_mod_bt2_exc_stop_type = NULL;
5602ef81 99static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
d24d5663
PP
100static PyObject *py_mod_bt2_exc_invalid_object_type = NULL;
101static PyObject *py_mod_bt2_exc_invalid_params_type = NULL;
102static PyObject *py_mod_bt2_exc_unsupported_type = NULL;
6945df9a 103
7744859c 104static
d24d5663 105void bt_bt2_cc_init_from_bt2(void)
6945df9a
SM
106{
107 /*
108 * This is called once the bt2 package is loaded.
109 *
110 * Those modules and functions are needed while the package is
111 * used. Loading them here is safe because we know the bt2
112 * package is imported, and we know that the user cannot use the
113 * code here without importing bt2 first.
114 */
115 py_mod_bt2 = PyImport_ImportModule("bt2");
116 BT_ASSERT(py_mod_bt2);
117 py_mod_bt2_exc_error_type =
118 PyObject_GetAttrString(py_mod_bt2, "Error");
119 BT_ASSERT(py_mod_bt2_exc_error_type);
120 py_mod_bt2_exc_try_again_type =
121 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
122 BT_ASSERT(py_mod_bt2_exc_try_again_type);
123 py_mod_bt2_exc_stop_type =
124 PyObject_GetAttrString(py_mod_bt2, "Stop");
125 BT_ASSERT(py_mod_bt2_exc_stop_type);
d24d5663
PP
126 py_mod_bt2_exc_invalid_object_type =
127 PyObject_GetAttrString(py_mod_bt2, "InvalidObject");
128 BT_ASSERT(py_mod_bt2_exc_invalid_object_type);
129 py_mod_bt2_exc_invalid_params_type =
130 PyObject_GetAttrString(py_mod_bt2, "InvalidParams");
131 BT_ASSERT(py_mod_bt2_exc_invalid_params_type);
132 py_mod_bt2_exc_unsupported_type =
133 PyObject_GetAttrString(py_mod_bt2, "Unsupported");
134 BT_ASSERT(py_mod_bt2_exc_unsupported_type);
6945df9a
SM
135}
136
7744859c 137static
d24d5663 138void bt_bt2_cc_exit_handler(void)
6945df9a
SM
139{
140 /*
141 * This is an exit handler (set by the bt2 package).
142 *
143 * We only give back the references that we took in
d24d5663 144 * bt_bt2_cc_init_from_bt2() here. The global variables continue
6945df9a
SM
145 * to exist for the code of this file, but they are now borrowed
146 * references. If this code is executed, it means that somehow
147 * the modules are still loaded, so it should be safe to use
148 * them even without a strong reference.
149 *
150 * We cannot do this in the library's destructor because it
151 * gets executed once Python is already finalized.
152 */
153 Py_XDECREF(py_mod_bt2);
154 Py_XDECREF(py_mod_bt2_exc_error_type);
155 Py_XDECREF(py_mod_bt2_exc_try_again_type);
156 Py_XDECREF(py_mod_bt2_exc_stop_type);
5602ef81 157 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
d24d5663
PP
158 Py_XDECREF(py_mod_bt2_exc_invalid_object_type);
159 Py_XDECREF(py_mod_bt2_exc_invalid_params_type);
6945df9a
SM
160}
161
162
163/* Library destructor */
164
165__attribute__((destructor))
7744859c 166static
d24d5663 167void native_comp_class_dtor(void) {
6945df9a
SM
168 /* Destroy component class association hash table */
169 if (bt_cc_ptr_to_py_cls) {
170 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
171 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
172 }
173}
174
d24d5663 175static inline
ce95fb26
SM
176void log_exception_and_maybe_append_error(int log_level,
177 bool append_error,
178 bt_self_component_class *self_component_class,
179 bt_self_component *self_component,
180 bt_self_message_iterator *self_message_iterator)
6945df9a 181{
7085eeaa 182 GString *gstr;
6945df9a
SM
183
184 BT_ASSERT(PyErr_Occurred() != NULL);
7085eeaa
PP
185 gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL);
186 if (!gstr) {
187 /* bt_py_common_format_exception() logs errors */
6945df9a
SM
188 goto end;
189 }
190
d24d5663 191 BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str);
6945df9a 192
ce95fb26
SM
193 if (append_error) {
194 if (self_component_class) {
195 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
196 self_component_class, "%s", gstr->str);
197 } else if (self_component) {
198 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
199 self_component, "%s", gstr->str);
200 } else if (self_message_iterator) {
201 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
202 self_message_iterator, "%s", gstr->str);
203 } else {
204 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
205 "Python", "%s", gstr->str);
206 }
207 }
208
6945df9a 209end:
7085eeaa
PP
210 if (gstr) {
211 g_string_free(gstr, TRUE);
6945df9a 212 }
6945df9a
SM
213}
214
d24d5663
PP
215static inline
216void loge_exception(void)
6945df9a 217{
ce95fb26
SM
218 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL, NULL);
219}
220
221static
222void loge_exception_message_iterator(
223 bt_self_message_iterator *self_message_iterator)
224{
225 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL, self_message_iterator);
6945df9a
SM
226}
227
d24d5663
PP
228static inline
229void logw_exception(void)
6945df9a 230{
ce95fb26 231 log_exception_and_maybe_append_error(BT_LOG_WARNING, false, NULL, NULL, NULL);
6945df9a
SM
232}
233
d24d5663 234static inline
ce95fb26
SM
235int py_exc_to_status(bt_self_component_class *self_component_class,
236 bt_self_component *self_component,
237 bt_self_message_iterator *self_message_iterator)
6945df9a 238{
d24d5663 239 int status = __BT_FUNC_STATUS_OK;
6945df9a
SM
240 PyObject *exc = PyErr_Occurred();
241
242 if (!exc) {
243 goto end;
244 }
245
246 if (PyErr_GivenExceptionMatches(exc,
d24d5663
PP
247 py_mod_bt2_exc_try_again_type)) {
248 status = __BT_FUNC_STATUS_AGAIN;
6945df9a 249 } else if (PyErr_GivenExceptionMatches(exc,
d24d5663
PP
250 py_mod_bt2_exc_stop_type)) {
251 status = __BT_FUNC_STATUS_END;
6945df9a 252 } else if (PyErr_GivenExceptionMatches(exc,
d24d5663
PP
253 py_mod_bt2_exc_invalid_object_type)) {
254 status = __BT_FUNC_STATUS_INVALID_OBJECT;
255 } else if (PyErr_GivenExceptionMatches(exc,
256 py_mod_bt2_exc_invalid_params_type)) {
257 status = __BT_FUNC_STATUS_INVALID_PARAMS;
258 } else if (PyErr_GivenExceptionMatches(exc,
259 py_mod_bt2_exc_unsupported_type)) {
260 status = __BT_FUNC_STATUS_UNSUPPORTED;
6945df9a 261 } else {
d24d5663 262 /* Unknown exception: convert to general error */
ce95fb26
SM
263 log_exception_and_maybe_append_error(BT_LOG_WARNING, true,
264 self_component_class, self_component,
265 self_message_iterator);
d24d5663 266 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
267 }
268
269end:
270 PyErr_Clear();
271 return status;
272}
273
ce95fb26
SM
274static
275int py_exc_to_status_component_class(bt_self_component_class *self_component_class)
276{
277 return py_exc_to_status(self_component_class, NULL, NULL);
278}
279
280static
281int py_exc_to_status_component(bt_self_component *self_component)
282{
283 return py_exc_to_status(NULL, self_component, NULL);
284}
285
286static
287int py_exc_to_status_message_iterator(
288 bt_self_message_iterator *self_message_iterator)
289{
290 return py_exc_to_status(NULL, NULL, self_message_iterator);
291}
292
d24d5663
PP
293/* Component class proxy methods (delegate to the attached Python object) */
294
7744859c 295static
d24d5663 296bt_component_class_init_method_status component_class_init(
6945df9a
SM
297 bt_self_component *self_component,
298 void *self_component_v,
299 swig_type_info *self_comp_cls_type_swig_type,
300 const bt_value *params,
301 void *init_method_data)
302{
303 const bt_component *component = bt_self_component_as_component(self_component);
304 const bt_component_class *component_class = bt_component_borrow_class_const(component);
d24d5663 305 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
306 PyObject *py_cls = NULL;
307 PyObject *py_comp = NULL;
308 PyObject *py_params_ptr = NULL;
309 PyObject *py_comp_ptr = NULL;
310
311 (void) init_method_data;
312
313 BT_ASSERT(self_component);
314 BT_ASSERT(self_component_v);
315 BT_ASSERT(self_comp_cls_type_swig_type);
316
317 /*
318 * Get the user-defined Python class which created this
319 * component's class in the first place (borrowed
320 * reference).
321 */
322 py_cls = lookup_cc_ptr_to_py_cls(component_class);
323 if (!py_cls) {
324 BT_LOGE("Cannot find Python class associated to native component class: "
325 "comp-cls-addr=%p", component_class);
326 goto error;
327 }
328
329 /* Parameters pointer -> SWIG pointer Python object */
330 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
331 SWIGTYPE_p_bt_value, 0);
332 if (!py_params_ptr) {
333 BT_LOGE_STR("Failed to create a SWIG pointer object.");
334 goto error;
335 }
336
337 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
338 self_comp_cls_type_swig_type, 0);
339 if (!py_comp_ptr) {
340 BT_LOGE_STR("Failed to create a SWIG pointer object.");
341 goto error;
342 }
343
344 /*
345 * Do the equivalent of this:
346 *
85906b6b 347 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
6945df9a 348 *
85906b6b 349 * _UserComponentType._bt_init_from_native() calls the Python
6945df9a
SM
350 * component object's __init__() function.
351 */
352 py_comp = PyObject_CallMethod(py_cls,
85906b6b 353 "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
6945df9a 354 if (!py_comp) {
85906b6b 355 BT_LOGW("Failed to call Python class's _bt_init_from_native() method: "
6945df9a 356 "py-cls-addr=%p", py_cls);
ce95fb26
SM
357 status = py_exc_to_status_component(self_component);
358 goto end;
6945df9a
SM
359 }
360
361 /*
362 * Our user Python component object is now fully created and
363 * initialized by the user. Since we just created it, this
364 * native component is its only (persistent) owner.
365 */
366 bt_self_component_set_data(self_component, py_comp);
367 py_comp = NULL;
368 goto end;
369
370error:
d24d5663 371 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
372
373 /*
374 * Clear any exception: we're returning a bad status anyway. If
375 * this call originated from Python (creation from a plugin's
376 * component class, for example), then the user gets an
377 * appropriate creation error.
378 */
379 PyErr_Clear();
380
381end:
382 Py_XDECREF(py_comp);
383 Py_XDECREF(py_params_ptr);
384 Py_XDECREF(py_comp_ptr);
385 return status;
386}
387
388/*
389 * Method of bt_component_class_source to initialize a bt_self_component_source
390 * of that class.
391 */
392
7744859c 393static
d24d5663 394bt_component_class_init_method_status component_class_source_init(
7744859c 395 bt_self_component_source *self_component_source,
6945df9a
SM
396 const bt_value *params, void *init_method_data)
397{
398 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
d24d5663 399 return component_class_init(
6945df9a
SM
400 self_component,
401 self_component_source,
402 SWIGTYPE_p_bt_self_component_source,
403 params, init_method_data);
404}
405
7744859c 406static
d24d5663 407bt_component_class_init_method_status component_class_filter_init(
7744859c 408 bt_self_component_filter *self_component_filter,
6945df9a
SM
409 const bt_value *params, void *init_method_data)
410{
411 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
d24d5663 412 return component_class_init(
6945df9a
SM
413 self_component,
414 self_component_filter,
415 SWIGTYPE_p_bt_self_component_filter,
416 params, init_method_data);
417}
418
7744859c 419static
d24d5663 420bt_component_class_init_method_status component_class_sink_init(
7744859c 421 bt_self_component_sink *self_component_sink,
6945df9a
SM
422 const bt_value *params, void *init_method_data)
423{
424 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
d24d5663 425 return component_class_init(
6945df9a
SM
426 self_component,
427 self_component_sink,
428 SWIGTYPE_p_bt_self_component_sink,
429 params, init_method_data);
430}
431
7744859c 432static
d24d5663 433void component_class_finalize(bt_self_component *self_component)
6945df9a
SM
434{
435 PyObject *py_comp = bt_self_component_get_data(self_component);
436 BT_ASSERT(py_comp);
437
438 /* Call user's _finalize() method */
439 PyObject *py_method_result = PyObject_CallMethod(py_comp,
440 "_finalize", NULL);
441
442 if (PyErr_Occurred()) {
d24d5663
PP
443 BT_LOGW("User component's _finalize() method raised an exception: ignoring:");
444 logw_exception();
6945df9a
SM
445 }
446
447 /*
448 * Ignore any exception raised by the _finalize() method because
449 * it won't change anything at this point: the component is
450 * being destroyed anyway.
451 */
452 PyErr_Clear();
453 Py_XDECREF(py_method_result);
454 Py_DECREF(py_comp);
455}
456
7744859c 457static
d24d5663 458void component_class_source_finalize(bt_self_component_source *self_component_source)
6945df9a
SM
459{
460 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
d24d5663 461 component_class_finalize(self_component);
6945df9a
SM
462}
463
7744859c 464static
d24d5663 465void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
6945df9a
SM
466{
467 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
d24d5663 468 component_class_finalize(self_component);
6945df9a
SM
469}
470
7744859c 471static
d24d5663 472void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
6945df9a
SM
473{
474 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
d24d5663 475 component_class_finalize(self_component);
6945df9a
SM
476}
477
f00b8d40 478static
d24d5663 479bt_bool component_class_can_seek_beginning(
f00b8d40
SM
480 bt_self_message_iterator *self_message_iterator)
481{
482 PyObject *py_iter;
483 PyObject *py_result = NULL;
484 bt_bool can_seek_beginning = false;
485
486 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
487 BT_ASSERT(py_iter);
488
85906b6b 489 py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
f00b8d40
SM
490
491 BT_ASSERT(!py_result || PyBool_Check(py_result));
492
493 if (py_result) {
494 can_seek_beginning = PyObject_IsTrue(py_result);
495 } else {
496 /*
497 * Once can_seek_beginning can report errors, convert the
498 * exception to a status. For now, log and return false;
499 */
ce95fb26 500 loge_exception_message_iterator(self_message_iterator);
f00b8d40
SM
501 PyErr_Clear();
502 }
503
504 Py_XDECREF(py_result);
505
506 return can_seek_beginning;
507}
508
509static
d24d5663
PP
510bt_component_class_message_iterator_seek_beginning_method_status
511component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
f00b8d40
SM
512{
513 PyObject *py_iter;
514 PyObject *py_result;
d24d5663 515 bt_component_class_message_iterator_seek_beginning_method_status status;
f00b8d40
SM
516
517 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
518 BT_ASSERT(py_iter);
85906b6b 519 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
d24d5663 520 NULL);
f00b8d40 521 BT_ASSERT(!py_result || py_result == Py_None);
ce95fb26 522 status = py_exc_to_status_message_iterator(self_message_iterator);
f00b8d40 523 Py_XDECREF(py_result);
f00b8d40
SM
524 return status;
525}
526
7744859c 527static
d24d5663 528bt_component_class_port_connected_method_status component_class_port_connected(
6945df9a
SM
529 bt_self_component *self_component,
530 void *self_component_port,
531 swig_type_info *self_component_port_swig_type,
532 bt_port_type self_component_port_type,
533 const void *other_port,
534 swig_type_info *other_port_swig_type)
535{
d24d5663 536 bt_component_class_port_connected_method_status status;
6945df9a
SM
537 PyObject *py_comp = NULL;
538 PyObject *py_self_port_ptr = NULL;
539 PyObject *py_other_port_ptr = NULL;
540 PyObject *py_method_result = NULL;
541
542 py_comp = bt_self_component_get_data(self_component);
543 BT_ASSERT(py_comp);
6945df9a
SM
544 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
545 self_component_port_swig_type, 0);
546 if (!py_self_port_ptr) {
547 BT_LOGF_STR("Failed to create a SWIG pointer object.");
d24d5663 548 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6945df9a
SM
549 goto end;
550 }
551
552 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
553 other_port_swig_type, 0);
554 if (!py_other_port_ptr) {
555 BT_LOGF_STR("Failed to create a SWIG pointer object.");
d24d5663 556 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6945df9a
SM
557 goto end; }
558
559 py_method_result = PyObject_CallMethod(py_comp,
85906b6b 560 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
6945df9a 561 self_component_port_type, py_other_port_ptr);
6945df9a 562 BT_ASSERT(!py_method_result || py_method_result == Py_None);
ce95fb26 563 status = py_exc_to_status_component(self_component);
6945df9a
SM
564
565end:
566 Py_XDECREF(py_self_port_ptr);
567 Py_XDECREF(py_other_port_ptr);
568 Py_XDECREF(py_method_result);
6945df9a
SM
569 return status;
570}
571
7744859c 572static
d24d5663
PP
573bt_component_class_port_connected_method_status
574component_class_source_output_port_connected(
6945df9a
SM
575 bt_self_component_source *self_component_source,
576 bt_self_component_port_output *self_component_port_output,
577 const bt_port_input *other_port_input)
578{
579 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
580
d24d5663 581 return component_class_port_connected(
6945df9a
SM
582 self_component,
583 self_component_port_output,
584 SWIGTYPE_p_bt_self_component_port_output,
585 BT_PORT_TYPE_OUTPUT,
586 other_port_input,
587 SWIGTYPE_p_bt_port_input);
588}
589
7744859c 590static
d24d5663
PP
591bt_component_class_port_connected_method_status
592component_class_filter_input_port_connected(
6945df9a
SM
593 bt_self_component_filter *self_component_filter,
594 bt_self_component_port_input *self_component_port_input,
595 const bt_port_output *other_port_output)
596{
597 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
598
d24d5663 599 return component_class_port_connected(
6945df9a
SM
600 self_component,
601 self_component_port_input,
602 SWIGTYPE_p_bt_self_component_port_input,
603 BT_PORT_TYPE_INPUT,
604 other_port_output,
605 SWIGTYPE_p_bt_port_output);
606}
607
7744859c 608static
d24d5663
PP
609bt_component_class_port_connected_method_status
610component_class_filter_output_port_connected(
6945df9a
SM
611 bt_self_component_filter *self_component_filter,
612 bt_self_component_port_output *self_component_port_output,
613 const bt_port_input *other_port_input)
614{
615 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
616
d24d5663 617 return component_class_port_connected(
6945df9a
SM
618 self_component,
619 self_component_port_output,
620 SWIGTYPE_p_bt_self_component_port_output,
621 BT_PORT_TYPE_OUTPUT,
622 other_port_input,
623 SWIGTYPE_p_bt_port_input);
624}
625
7744859c 626static
d24d5663
PP
627bt_component_class_port_connected_method_status
628component_class_sink_input_port_connected(
6945df9a
SM
629 bt_self_component_sink *self_component_sink,
630 bt_self_component_port_input *self_component_port_input,
631 const bt_port_output *other_port_output)
632{
633 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
634
d24d5663 635 return component_class_port_connected(
6945df9a
SM
636 self_component,
637 self_component_port_input,
638 SWIGTYPE_p_bt_self_component_port_input,
639 BT_PORT_TYPE_INPUT,
640 other_port_output,
641 SWIGTYPE_p_bt_port_output);
642}
643
7744859c 644static
d24d5663
PP
645bt_component_class_sink_graph_is_configured_method_status
646component_class_sink_graph_is_configured(
7744859c 647 bt_self_component_sink *self_component_sink)
6945df9a
SM
648{
649 PyObject *py_comp = NULL;
650 PyObject *py_method_result = NULL;
d24d5663 651 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
652 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
653
654 py_comp = bt_self_component_get_data(self_component);
655 py_method_result = PyObject_CallMethod(py_comp,
85906b6b 656 "_bt_graph_is_configured_from_native", NULL);
6945df9a 657 BT_ASSERT(!py_method_result || py_method_result == Py_None);
ce95fb26 658 status = py_exc_to_status_component(self_component);
6945df9a 659 Py_XDECREF(py_method_result);
6945df9a
SM
660 return status;
661}
662
7744859c 663static
d24d5663 664bt_component_class_query_method_status component_class_query(
6945df9a 665 const bt_component_class *component_class,
ce95fb26 666 bt_self_component_class *self_component_class,
6945df9a
SM
667 const bt_query_executor *query_executor,
668 const char *object, const bt_value *params,
f4e38e70 669 bt_logging_level log_level,
6945df9a
SM
670 const bt_value **result)
671{
672 PyObject *py_cls = NULL;
673 PyObject *py_params_ptr = NULL;
674 PyObject *py_query_exec_ptr = NULL;
675 PyObject *py_query_func = NULL;
676 PyObject *py_object = NULL;
677 PyObject *py_results_addr = NULL;
d24d5663 678 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
679
680 py_cls = lookup_cc_ptr_to_py_cls(component_class);
681 if (!py_cls) {
682 BT_LOGE("Cannot find Python class associated to native component class: "
683 "comp-cls-addr=%p", component_class);
684 goto error;
685 }
686
687 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
688 SWIGTYPE_p_bt_value, 0);
689 if (!py_params_ptr) {
690 BT_LOGE_STR("Failed to create a SWIG pointer object.");
691 goto error;
692 }
693
694 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
695 SWIGTYPE_p_bt_query_executor, 0);
696 if (!py_query_exec_ptr) {
697 BT_LOGE_STR("Failed to create a SWIG pointer object.");
698 goto error;
699 }
700
701 py_object = SWIG_FromCharPtr(object);
702 if (!py_object) {
703 BT_LOGE_STR("Failed to create a Python string.");
704 goto error;
705 }
706
707 py_results_addr = PyObject_CallMethod(py_cls,
85906b6b 708 "_bt_query_from_native", "(OOOi)", py_query_exec_ptr,
f4e38e70 709 py_object, py_params_ptr, (int) log_level);
6945df9a 710 if (!py_results_addr) {
85906b6b 711 BT_LOGW("Failed to call Python class's _bt_query_from_native() method: "
6945df9a 712 "py-cls-addr=%p", py_cls);
ce95fb26 713 status = py_exc_to_status_component_class(self_component_class);
6945df9a
SM
714 goto end;
715 }
716
717 /*
718 * The returned object, on success, is an integer object
719 * (PyLong) containing the address of a BT value object (new
720 * reference).
721 */
babe0791 722 *result = PyLong_AsVoidPtr(py_results_addr);
6945df9a
SM
723 BT_ASSERT(!PyErr_Occurred());
724 BT_ASSERT(*result);
725 goto end;
726
727error:
728 PyErr_Clear();
d24d5663 729 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
730
731end:
732 Py_XDECREF(py_params_ptr);
733 Py_XDECREF(py_query_exec_ptr);
734 Py_XDECREF(py_query_func);
735 Py_XDECREF(py_object);
736 Py_XDECREF(py_results_addr);
737 return status;
738}
739
7744859c 740static
d24d5663 741bt_component_class_query_method_status component_class_source_query(
6945df9a
SM
742 bt_self_component_class_source *self_component_class_source,
743 const bt_query_executor *query_executor,
744 const char *object, const bt_value *params,
f4e38e70 745 bt_logging_level log_level,
6945df9a
SM
746 const bt_value **result)
747{
748 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
749 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
ce95fb26 750 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
d24d5663 751
ce95fb26 752 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
6945df9a
SM
753}
754
7744859c 755static
d24d5663 756bt_component_class_query_method_status component_class_filter_query(
6945df9a
SM
757 bt_self_component_class_filter *self_component_class_filter,
758 const bt_query_executor *query_executor,
759 const char *object, const bt_value *params,
f4e38e70 760 bt_logging_level log_level,
6945df9a
SM
761 const bt_value **result)
762{
763 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
764 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
ce95fb26 765 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
d24d5663 766
ce95fb26 767 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
6945df9a
SM
768}
769
7744859c 770static
d24d5663 771bt_component_class_query_method_status component_class_sink_query(
6945df9a
SM
772 bt_self_component_class_sink *self_component_class_sink,
773 const bt_query_executor *query_executor,
774 const char *object, const bt_value *params,
f4e38e70 775 bt_logging_level log_level,
6945df9a
SM
776 const bt_value **result)
777{
778 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
779 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
ce95fb26 780 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
d24d5663 781
ce95fb26 782 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
6945df9a
SM
783}
784
7744859c 785static
d24d5663
PP
786bt_component_class_message_iterator_init_method_status
787component_class_message_iterator_init(
6945df9a
SM
788 bt_self_message_iterator *self_message_iterator,
789 bt_self_component *self_component,
790 bt_self_component_port_output *self_component_port_output)
791{
d24d5663 792 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
793 PyObject *py_comp_cls = NULL;
794 PyObject *py_iter_cls = NULL;
795 PyObject *py_iter_ptr = NULL;
c5f330cd 796 PyObject *py_component_port_output_ptr = NULL;
6945df9a
SM
797 PyObject *py_init_method_result = NULL;
798 PyObject *py_iter = NULL;
799 PyObject *py_comp;
800
801 py_comp = bt_self_component_get_data(self_component);
802
5602ef81 803 /* Find user's Python message iterator class */
6945df9a
SM
804 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
805 if (!py_comp_cls) {
806 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
ce95fb26 807 goto python_error;
6945df9a
SM
808 }
809
810 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
811 if (!py_iter_cls) {
812 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
ce95fb26 813 goto python_error;
6945df9a
SM
814 }
815
816 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
817 SWIGTYPE_p_bt_self_message_iterator, 0);
818 if (!py_iter_ptr) {
ce95fb26
SM
819 const char *err = "Failed to create a SWIG pointer object.";
820
821 BT_LOGE_STR(err);
822 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
823 self_message_iterator, err);
6945df9a
SM
824 goto error;
825 }
826
827 /*
5602ef81 828 * Create object with borrowed native message iterator
6945df9a
SM
829 * reference:
830 *
831 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
832 */
833 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
834 "(OO)", py_iter_cls, py_iter_ptr);
835 if (!py_iter) {
836 BT_LOGE("Failed to call Python class's __new__() method: "
837 "py-cls-addr=%p", py_iter_cls);
ce95fb26 838 goto python_error;
6945df9a
SM
839 }
840
841 /*
842 * Initialize object:
843 *
c5f330cd
SM
844 * py_iter.__init__(self_output_port)
845 *
846 * through the _init_for_native helper static method.
6945df9a
SM
847 *
848 * At this point, py_iter._ptr is set, so this initialization
849 * function has access to self._component (which gives it the
850 * user Python component object from which the iterator was
851 * created).
852 */
d24d5663
PP
853 py_component_port_output_ptr = SWIG_NewPointerObj(
854 SWIG_as_voidptr(self_component_port_output),
c5f330cd
SM
855 SWIGTYPE_p_bt_self_component_port_output, 0);
856 if (!py_component_port_output_ptr) {
ce95fb26
SM
857 const char *err = "Failed to create a SWIG pointer object.";
858
859 BT_LOGE_STR(err);
860 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
861 self_message_iterator, err);
c5f330cd
SM
862 goto error;
863 }
864
d24d5663 865 py_init_method_result = PyObject_CallMethod(py_iter,
85906b6b 866 "_bt_init_from_native", "O", py_component_port_output_ptr);
6945df9a 867 if (!py_init_method_result) {
ce95fb26
SM
868 BT_LOGE_STR("User's __init__() method failed:");
869 goto python_error;
6945df9a
SM
870 }
871
872 /*
873 * Since the Python code can never instantiate a user-defined
5602ef81
SM
874 * message iterator class, the native message iterator
875 * object does NOT belong to a user Python message iterator
6945df9a 876 * object (borrowed reference). However this Python object is
5602ef81 877 * owned by this native message iterator object.
6945df9a 878 *
5602ef81
SM
879 * In the Python world, the lifetime of the native message
880 * iterator is managed by a _GenericMessageIterator
6945df9a
SM
881 * instance:
882 *
5602ef81
SM
883 * _GenericMessageIterator instance:
884 * owns a native bt_message_iterator object (iter)
6945df9a
SM
885 * owns a _UserMessageIterator instance (py_iter)
886 * self._ptr is a borrowed reference to the
5602ef81 887 * native bt_private_connection_private_message_iterator
6945df9a
SM
888 * object (iter)
889 */
890 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
891 py_iter = NULL;
892 goto end;
893
ce95fb26
SM
894python_error:
895 /* Handling of errors that cause a Python exception to be set. */
896 status = py_exc_to_status_message_iterator(self_message_iterator);
897 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
898 goto end;
6945df9a 899
ce95fb26
SM
900error:
901 /* Handling of errors that don't cause a Python exception to be set. */
902 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
903
904end:
ce95fb26
SM
905 BT_ASSERT(!PyErr_Occurred());
906
6945df9a
SM
907 Py_XDECREF(py_comp_cls);
908 Py_XDECREF(py_iter_cls);
909 Py_XDECREF(py_iter_ptr);
c5f330cd 910 Py_XDECREF(py_component_port_output_ptr);
6945df9a
SM
911 Py_XDECREF(py_init_method_result);
912 Py_XDECREF(py_iter);
913 return status;
914}
915
7744859c 916static
d24d5663
PP
917bt_component_class_message_iterator_init_method_status
918component_class_source_message_iterator_init(
6945df9a
SM
919 bt_self_message_iterator *self_message_iterator,
920 bt_self_component_source *self_component_source,
921 bt_self_component_port_output *self_component_port_output)
922{
923 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
d24d5663
PP
924
925 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6945df9a
SM
926}
927
7744859c 928static
d24d5663
PP
929bt_component_class_message_iterator_init_method_status
930component_class_filter_message_iterator_init(
6945df9a
SM
931 bt_self_message_iterator *self_message_iterator,
932 bt_self_component_filter *self_component_filter,
933 bt_self_component_port_output *self_component_port_output)
934{
935 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
d24d5663
PP
936
937 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6945df9a
SM
938}
939
7744859c 940static
d24d5663 941void component_class_message_iterator_finalize(
6945df9a
SM
942 bt_self_message_iterator *message_iterator)
943{
944 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
945 PyObject *py_method_result = NULL;
946
947 BT_ASSERT(py_message_iter);
948
949 /* Call user's _finalize() method */
950 py_method_result = PyObject_CallMethod(py_message_iter,
951 "_finalize", NULL);
952
953 if (PyErr_Occurred()) {
d24d5663
PP
954 BT_LOGW("User's _finalize() method raised an exception: ignoring:");
955 logw_exception();
6945df9a
SM
956 }
957
958 /*
959 * Ignore any exception raised by the _finalize() method because
960 * it won't change anything at this point: the component is
961 * being destroyed anyway.
962 */
963 PyErr_Clear();
964 Py_XDECREF(py_method_result);
965 Py_DECREF(py_message_iter);
966}
967
968/* Valid for both sources and filters. */
969
7744859c 970static
d24d5663
PP
971bt_component_class_message_iterator_next_method_status
972component_class_message_iterator_next(
7744859c
SM
973 bt_self_message_iterator *message_iterator,
974 bt_message_array_const msgs, uint64_t capacity,
975 uint64_t *count)
6945df9a 976{
d24d5663 977 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
978 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
979 PyObject *py_method_result = NULL;
980
981 BT_ASSERT(py_message_iter);
982 py_method_result = PyObject_CallMethod(py_message_iter,
85906b6b 983 "_bt_next_from_native", NULL);
6945df9a 984 if (!py_method_result) {
ce95fb26 985 status = py_exc_to_status_message_iterator(message_iterator);
d24d5663 986 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
6945df9a
SM
987 goto end;
988 }
989
990 /*
991 * The returned object, on success, is an integer object
5602ef81 992 * (PyLong) containing the address of a native message
6945df9a
SM
993 * object (which is now ours).
994 */
babe0791 995 msgs[0] = PyLong_AsVoidPtr(py_method_result);
6945df9a
SM
996 *count = 1;
997
998 /* Clear potential overflow error; should never happen */
999 BT_ASSERT(!PyErr_Occurred());
1000 goto end;
1001
1002end:
1003 Py_XDECREF(py_method_result);
1004 return status;
1005}
1006
7744859c 1007static
d24d5663
PP
1008bt_component_class_sink_consume_method_status
1009component_class_sink_consume(bt_self_component_sink *self_component_sink)
6945df9a
SM
1010{
1011 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1012 PyObject *py_comp = bt_self_component_get_data(self_component);
1013 PyObject *py_method_result = NULL;
d24d5663 1014 bt_component_class_sink_consume_method_status status;
6945df9a
SM
1015
1016 BT_ASSERT(py_comp);
1017 py_method_result = PyObject_CallMethod(py_comp,
1018 "_consume", NULL);
ce95fb26 1019 status = py_exc_to_status_component(self_component);
d24d5663 1020 if (!py_method_result && status == __BT_FUNC_STATUS_OK) {
6945df9a
SM
1021 /* Pretty sure this should never happen, but just in case */
1022 BT_LOGE("User's _consume() method failed without raising an exception: "
1023 "status=%d", status);
d24d5663 1024 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
1025 }
1026
1027 Py_XDECREF(py_method_result);
1028 return status;
1029}
1030
1031static
d24d5663 1032int component_class_set_help_and_desc(
6945df9a
SM
1033 bt_component_class *component_class,
1034 const char *description, const char *help)
1035{
1036 int ret;
1037
1038 if (description) {
1039 ret = bt_component_class_set_description(component_class, description);
1040 if (ret) {
1041 BT_LOGE("Cannot set component class's description: "
1042 "comp-cls-addr=%p", component_class);
1043 goto end;
1044 }
1045 }
1046
1047 if (help) {
1048 ret = bt_component_class_set_help(component_class, help);
1049 if (ret) {
1050 BT_LOGE("Cannot set component class's help text: "
1051 "comp-cls-addr=%p", component_class);
1052 goto end;
1053 }
1054 }
1055
1056 ret = 0;
1057
1058end:
1059 return ret;
1060}
1061
1062static
d24d5663 1063bt_component_class_source *bt_bt2_component_class_source_create(
6945df9a
SM
1064 PyObject *py_cls, const char *name, const char *description,
1065 const char *help)
1066{
1067 bt_component_class_source *component_class_source;
1068 bt_component_class *component_class;
1069 int ret;
1070
1071 BT_ASSERT(py_cls);
6945df9a 1072 component_class_source = bt_component_class_source_create(name,
d24d5663 1073 component_class_message_iterator_next);
6945df9a
SM
1074 if (!component_class_source) {
1075 BT_LOGE_STR("Cannot create source component class.");
1076 goto end;
1077 }
1078
1079 component_class = bt_component_class_source_as_component_class(component_class_source);
1080
d24d5663 1081 if (component_class_set_help_and_desc(component_class, description, help)) {
6945df9a
SM
1082 goto end;
1083 }
1084
d24d5663 1085 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
6945df9a 1086 BT_ASSERT(ret == 0);
d24d5663 1087 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
f00b8d40
SM
1088 BT_ASSERT(ret == 0);
1089 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
d24d5663 1090 component_class_can_seek_beginning);
f00b8d40
SM
1091 BT_ASSERT(ret == 0);
1092 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
d24d5663 1093 component_class_seek_beginning);
6945df9a 1094 BT_ASSERT(ret == 0);
6945df9a 1095 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
d24d5663 1096 component_class_source_output_port_connected);
6945df9a 1097 BT_ASSERT(ret == 0);
d24d5663 1098 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
6945df9a
SM
1099 BT_ASSERT(ret == 0);
1100 ret = bt_component_class_source_set_message_iterator_init_method(
d24d5663 1101 component_class_source, component_class_source_message_iterator_init);
6945df9a
SM
1102 BT_ASSERT(ret == 0);
1103 ret = bt_component_class_source_set_message_iterator_finalize_method(
d24d5663 1104 component_class_source, component_class_message_iterator_finalize);
6945df9a 1105 BT_ASSERT(ret == 0);
6945df9a
SM
1106 register_cc_ptr_to_py_cls(component_class, py_cls);
1107
1108end:
1109 return component_class_source;
1110}
1111
1112static
d24d5663 1113bt_component_class_filter *bt_bt2_component_class_filter_create(
6945df9a
SM
1114 PyObject *py_cls, const char *name, const char *description,
1115 const char *help)
1116{
1117 bt_component_class *component_class;
1118 bt_component_class_filter *component_class_filter;
1119 int ret;
1120
1121 BT_ASSERT(py_cls);
6945df9a 1122 component_class_filter = bt_component_class_filter_create(name,
d24d5663 1123 component_class_message_iterator_next);
6945df9a
SM
1124 if (!component_class_filter) {
1125 BT_LOGE_STR("Cannot create filter component class.");
1126 goto end;
1127 }
1128
1129 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1130
d24d5663 1131 if (component_class_set_help_and_desc(component_class, description, help)) {
6945df9a
SM
1132 goto end;
1133 }
1134
d24d5663 1135 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
6945df9a 1136 BT_ASSERT(ret == 0);
d24d5663 1137 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
6945df9a 1138 BT_ASSERT(ret == 0);
f00b8d40 1139 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
d24d5663 1140 component_class_can_seek_beginning);
f00b8d40
SM
1141 BT_ASSERT(ret == 0);
1142 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
d24d5663 1143 component_class_seek_beginning);
f00b8d40 1144 BT_ASSERT(ret == 0);
6945df9a 1145 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
d24d5663 1146 component_class_filter_input_port_connected);
6945df9a
SM
1147 BT_ASSERT(ret == 0);
1148 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
d24d5663 1149 component_class_filter_output_port_connected);
6945df9a 1150 BT_ASSERT(ret == 0);
d24d5663 1151 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
6945df9a
SM
1152 BT_ASSERT(ret == 0);
1153 ret = bt_component_class_filter_set_message_iterator_init_method(
d24d5663 1154 component_class_filter, component_class_filter_message_iterator_init);
6945df9a
SM
1155 BT_ASSERT(ret == 0);
1156 ret = bt_component_class_filter_set_message_iterator_finalize_method(
d24d5663 1157 component_class_filter, component_class_message_iterator_finalize);
6945df9a 1158 BT_ASSERT(ret == 0);
6945df9a
SM
1159 register_cc_ptr_to_py_cls(component_class, py_cls);
1160
1161end:
1162 return component_class_filter;
1163}
1164
1165static
d24d5663 1166bt_component_class_sink *bt_bt2_component_class_sink_create(
6945df9a
SM
1167 PyObject *py_cls, const char *name, const char *description,
1168 const char *help)
1169{
1170 bt_component_class_sink *component_class_sink;
1171 bt_component_class *component_class;
1172 int ret;
1173
1174 BT_ASSERT(py_cls);
d24d5663 1175 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
6945df9a
SM
1176
1177 if (!component_class_sink) {
1178 BT_LOGE_STR("Cannot create sink component class.");
1179 goto end;
1180 }
1181
1182 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1183
d24d5663 1184 if (component_class_set_help_and_desc(component_class, description, help)) {
6945df9a
SM
1185 goto end;
1186 }
1187
d24d5663 1188 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
6945df9a 1189 BT_ASSERT(ret == 0);
d24d5663 1190 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
6945df9a 1191 BT_ASSERT(ret == 0);
6945df9a 1192 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
d24d5663 1193 component_class_sink_input_port_connected);
6945df9a
SM
1194 BT_ASSERT(ret == 0);
1195 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
d24d5663 1196 component_class_sink_graph_is_configured);
6945df9a 1197 BT_ASSERT(ret == 0);
d24d5663 1198 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
6945df9a 1199 BT_ASSERT(ret == 0);
6945df9a
SM
1200 register_cc_ptr_to_py_cls(component_class, py_cls);
1201
1202end:
1203 return component_class_sink;
1204}
1205%}
1206
d24d5663 1207struct bt_component_class_source *bt_bt2_component_class_source_create(
6945df9a
SM
1208 PyObject *py_cls, const char *name, const char *description,
1209 const char *help);
d24d5663 1210struct bt_component_class_filter *bt_bt2_component_class_filter_create(
6945df9a
SM
1211 PyObject *py_cls, const char *name, const char *description,
1212 const char *help);
d24d5663 1213struct bt_component_class_sink *bt_bt2_component_class_sink_create(
6945df9a
SM
1214 PyObject *py_cls, const char *name, const char *description,
1215 const char *help);
d24d5663
PP
1216void bt_bt2_cc_init_from_bt2(void);
1217void bt_bt2_cc_exit_handler(void);
This page took 0.087305 seconds and 4 git commands to generate.