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