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