bt2: honor self component or query log level when logging
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i
CommitLineData
6fa6bfa1
SM
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
fb25b9e3 25%include <babeltrace2/graph/component-class.h>
e4b56bb9
SM
26%include <babeltrace2/graph/component-class-const.h>
27%include <babeltrace2/graph/component-class-source-const.h>
28%include <babeltrace2/graph/component-class-source.h>
29%include <babeltrace2/graph/component-class-filter-const.h>
30%include <babeltrace2/graph/component-class-filter.h>
31%include <babeltrace2/graph/component-class-sink-const.h>
32%include <babeltrace2/graph/component-class-sink.h>
33%include <babeltrace2/graph/self-component-class-source.h>
34%include <babeltrace2/graph/self-component-class-filter.h>
35%include <babeltrace2/graph/self-component-class-sink.h>
6fa6bfa1
SM
36
37%{
8d6ef9df
PP
38#include "logging/comp-logging.h"
39
6fa6bfa1
SM
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
fb25b9e3 48 * class is created by one of the bt_bt2_component_class_*_create()
6fa6bfa1
SM
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
58static GHashTable *bt_cc_ptr_to_py_cls;
59
efb177a7
SM
60static
61void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
6fa6bfa1
SM
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
efb177a7
SM
79static
80PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
6fa6bfa1
SM
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
97static PyObject *py_mod_bt2 = NULL;
98static PyObject *py_mod_bt2_exc_error_type = NULL;
493428a0 99static PyObject *py_mod_bt2_exc_memory_error = NULL;
6fa6bfa1
SM
100static PyObject *py_mod_bt2_exc_try_again_type = NULL;
101static PyObject *py_mod_bt2_exc_stop_type = NULL;
7dd3e712 102static PyObject *py_mod_bt2_exc_unknown_object_type = NULL;
6fa6bfa1 103
efb177a7 104static
fb25b9e3 105void bt_bt2_cc_init_from_bt2(void)
6fa6bfa1
SM
106{
107 /*
108 * This is called once the bt2 package is loaded.
109 *
110 * Those modules and functions are needed while the package is
111 * used. Loading them here is safe because we know the bt2
112 * package is imported, and we know that the user cannot use the
113 * code here without importing bt2 first.
114 */
115 py_mod_bt2 = PyImport_ImportModule("bt2");
116 BT_ASSERT(py_mod_bt2);
117 py_mod_bt2_exc_error_type =
614743a5 118 PyObject_GetAttrString(py_mod_bt2, "_Error");
6fa6bfa1 119 BT_ASSERT(py_mod_bt2_exc_error_type);
493428a0 120 py_mod_bt2_exc_memory_error =
614743a5 121 PyObject_GetAttrString(py_mod_bt2, "_MemoryError");
493428a0 122 BT_ASSERT(py_mod_bt2_exc_memory_error);
6fa6bfa1
SM
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);
7dd3e712
SM
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);
6fa6bfa1
SM
132}
133
efb177a7 134static
fb25b9e3 135void bt_bt2_cc_exit_handler(void)
6fa6bfa1
SM
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
fb25b9e3 141 * bt_bt2_cc_init_from_bt2() here. The global variables continue
6fa6bfa1
SM
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);
7dd3e712 154 Py_XDECREF(py_mod_bt2_exc_unknown_object_type);
6fa6bfa1
SM
155}
156
157
158/* Library destructor */
159
160__attribute__((destructor))
efb177a7 161static
fb25b9e3 162void native_comp_class_dtor(void) {
6fa6bfa1
SM
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
3b2be708
SM
170static
171void restore_current_thread_error_and_append_exception_chain_recursive(
8d6ef9df 172 int active_log_level, PyObject *py_exc_value,
3b2be708
SM
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(
8d6ef9df
PP
187 active_log_level, py_exc_cause_value,
188 self_component_class, self_component,
189 self_message_iterator, module_name);
3b2be708
SM
190 }
191
192 /*
614743a5 193 * If the raised exception is a bt2._Error, restore the wrapped error.
3b2be708
SM
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 /*
614743a5 201 * We never raise a bt2._Error with a cause: it should be the
3b2be708
SM
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,
8d6ef9df 229 py_exc_tb, active_log_level, false);
3b2be708
SM
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
249end:
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()
614743a5 265 * except bt2._Error as e1:
3b2be708
SM
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 *
614743a5 272 * TypeError -> ValueError -> bt2._Error
3b2be708
SM
273 *
274 * Where the TypeError is the current exception (obtained from PyErr_Fetch).
275 *
614743a5 276 * The bt2._Error contains a `struct bt_error *` that used to be the current
3b2be708
SM
277 * thread's error, at the moment the exception was raised.
278 *
614743a5 279 * This function gets to the bt2._Error and restores the wrapped
3b2be708
SM
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 */
285static
286void restore_bt_error_and_append_current_exception_chain(
8d6ef9df 287 int active_log_level,
3b2be708
SM
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
8d6ef9df
PP
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);
3b2be708
SM
317
318 PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb);
319}
320
fb25b9e3 321static inline
8d6ef9df
PP
322void log_exception_and_maybe_append_error(int func_log_level,
323 int active_log_level, bool append_error,
f8d71b59
SM
324 bt_self_component_class *self_component_class,
325 bt_self_component *self_component,
3b2be708
SM
326 bt_self_message_iterator *self_message_iterator,
327 const char *module_name)
6fa6bfa1 328{
2d7aa9b6 329 GString *gstr;
6fa6bfa1 330
8e01f2d9 331 BT_ASSERT(PyErr_Occurred());
8d6ef9df 332 gstr = bt_py_common_format_current_exception(active_log_level);
2d7aa9b6 333 if (!gstr) {
f33c35fb 334 /* bt_py_common_format_current_exception() logs errors */
6fa6bfa1
SM
335 goto end;
336 }
337
8d6ef9df
PP
338 BT_COMP_LOG_CUR_LVL(func_log_level, active_log_level, self_component,
339 "%s", gstr->str);
6fa6bfa1 340
f8d71b59 341 if (append_error) {
3b2be708 342 restore_bt_error_and_append_current_exception_chain(
8d6ef9df 343 active_log_level, self_component_class, self_component,
3b2be708
SM
344 self_message_iterator, module_name);
345
f8d71b59
SM
346 }
347
6fa6bfa1 348end:
2d7aa9b6
PP
349 if (gstr) {
350 g_string_free(gstr, TRUE);
6fa6bfa1 351 }
6fa6bfa1
SM
352}
353
8d6ef9df
PP
354static
355bt_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
361static
362bt_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
fb25b9e3 371static inline
8d6ef9df 372void loge_exception(const char *module_name, int active_log_level)
6fa6bfa1 373{
8d6ef9df
PP
374 log_exception_and_maybe_append_error(BT_LOG_ERROR, active_log_level,
375 true, NULL, NULL, NULL, module_name);
f8d71b59
SM
376}
377
378static
379void loge_exception_message_iterator(
380 bt_self_message_iterator *self_message_iterator)
381{
8d6ef9df
PP
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);
6fa6bfa1
SM
387}
388
fb25b9e3 389static inline
8d6ef9df 390void logw_exception(int active_log_level)
6fa6bfa1 391{
8d6ef9df
PP
392 log_exception_and_maybe_append_error(BT_LOG_WARNING, active_log_level,
393 false, NULL, NULL, NULL, NULL);
6fa6bfa1
SM
394}
395
fb25b9e3 396static inline
f8d71b59
SM
397int py_exc_to_status(bt_self_component_class *self_component_class,
398 bt_self_component *self_component,
3b2be708 399 bt_self_message_iterator *self_message_iterator,
8d6ef9df 400 const char *module_name, int active_log_level)
6fa6bfa1 401{
fb25b9e3 402 int status = __BT_FUNC_STATUS_OK;
6fa6bfa1
SM
403 PyObject *exc = PyErr_Occurred();
404
405 if (!exc) {
406 goto end;
407 }
408
409 if (PyErr_GivenExceptionMatches(exc,
fb25b9e3
PP
410 py_mod_bt2_exc_try_again_type)) {
411 status = __BT_FUNC_STATUS_AGAIN;
6fa6bfa1 412 } else if (PyErr_GivenExceptionMatches(exc,
fb25b9e3
PP
413 py_mod_bt2_exc_stop_type)) {
414 status = __BT_FUNC_STATUS_END;
6fa6bfa1 415 } else if (PyErr_GivenExceptionMatches(exc,
7dd3e712
SM
416 py_mod_bt2_exc_unknown_object_type)) {
417 status = __BT_FUNC_STATUS_UNKNOWN_OBJECT;
6fa6bfa1 418 } else {
8d6ef9df
PP
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,
f8d71b59 439 self_component_class, self_component,
3b2be708 440 self_message_iterator, module_name);
493428a0
SM
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 }
6fa6bfa1
SM
448 }
449
450end:
451 PyErr_Clear();
452 return status;
453}
454
f8d71b59 455static
8d6ef9df
PP
456int py_exc_to_status_component_class(
457 bt_self_component_class *self_component_class,
458 int active_log_level)
f8d71b59 459{
8d6ef9df
PP
460 return py_exc_to_status(self_component_class, NULL, NULL, NULL,
461 active_log_level);
f8d71b59
SM
462}
463
464static
465int py_exc_to_status_component(bt_self_component *self_component)
466{
8d6ef9df 467 return py_exc_to_status(NULL, self_component, NULL, NULL, -1);
f8d71b59
SM
468}
469
470static
471int py_exc_to_status_message_iterator(
472 bt_self_message_iterator *self_message_iterator)
473{
8d6ef9df 474 return py_exc_to_status(NULL, NULL, self_message_iterator, NULL, -1);
f8d71b59
SM
475}
476
fb25b9e3
PP
477/* Component class proxy methods (delegate to the attached Python object) */
478
efb177a7 479static
fb25b9e3 480bt_component_class_init_method_status component_class_init(
6fa6bfa1
SM
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);
fb25b9e3 489 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
6fa6bfa1
SM
490 PyObject *py_cls = NULL;
491 PyObject *py_comp = NULL;
492 PyObject *py_params_ptr = NULL;
493 PyObject *py_comp_ptr = NULL;
8d6ef9df
PP
494 bt_logging_level log_level = get_self_component_log_level(
495 self_component);
6fa6bfa1
SM
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) {
8d6ef9df
PP
510 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
511 "Cannot find Python class associated to native component class: "
6fa6bfa1
SM
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) {
8d6ef9df
PP
520 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
521 "Failed to create a SWIG pointer object.");
6fa6bfa1
SM
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) {
8d6ef9df
PP
528 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
529 "Failed to create a SWIG pointer object.");
6fa6bfa1
SM
530 goto error;
531 }
532
533 /*
534 * Do the equivalent of this:
535 *
deec48a6 536 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
6fa6bfa1 537 *
deec48a6 538 * _UserComponentType._bt_init_from_native() calls the Python
6fa6bfa1
SM
539 * component object's __init__() function.
540 */
541 py_comp = PyObject_CallMethod(py_cls,
deec48a6 542 "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
6fa6bfa1 543 if (!py_comp) {
8d6ef9df
PP
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: "
6fa6bfa1 546 "py-cls-addr=%p", py_cls);
f8d71b59
SM
547 status = py_exc_to_status_component(self_component);
548 goto end;
6fa6bfa1
SM
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
560error:
fb25b9e3 561 status = __BT_FUNC_STATUS_ERROR;
6fa6bfa1
SM
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
571end:
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
efb177a7 583static
fb25b9e3 584bt_component_class_init_method_status component_class_source_init(
efb177a7 585 bt_self_component_source *self_component_source,
6fa6bfa1
SM
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);
fb25b9e3 589 return component_class_init(
6fa6bfa1
SM
590 self_component,
591 self_component_source,
592 SWIGTYPE_p_bt_self_component_source,
593 params, init_method_data);
594}
595
efb177a7 596static
fb25b9e3 597bt_component_class_init_method_status component_class_filter_init(
efb177a7 598 bt_self_component_filter *self_component_filter,
6fa6bfa1
SM
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);
fb25b9e3 602 return component_class_init(
6fa6bfa1
SM
603 self_component,
604 self_component_filter,
605 SWIGTYPE_p_bt_self_component_filter,
606 params, init_method_data);
607}
608
efb177a7 609static
fb25b9e3 610bt_component_class_init_method_status component_class_sink_init(
efb177a7 611 bt_self_component_sink *self_component_sink,
6fa6bfa1
SM
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);
fb25b9e3 615 return component_class_init(
6fa6bfa1
SM
616 self_component,
617 self_component_sink,
618 SWIGTYPE_p_bt_self_component_sink,
619 params, init_method_data);
620}
621
efb177a7 622static
fb25b9e3 623void component_class_finalize(bt_self_component *self_component)
6fa6bfa1
SM
624{
625 PyObject *py_comp = bt_self_component_get_data(self_component);
626 BT_ASSERT(py_comp);
627
819d0ae7 628 /* Call user's _user_finalize() method */
6fa6bfa1 629 PyObject *py_method_result = PyObject_CallMethod(py_comp,
819d0ae7 630 "_user_finalize", NULL);
6fa6bfa1
SM
631
632 if (PyErr_Occurred()) {
8d6ef9df
PP
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);
6fa6bfa1
SM
639 }
640
641 /*
819d0ae7
PP
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.
6fa6bfa1
SM
645 */
646 PyErr_Clear();
647 Py_XDECREF(py_method_result);
648 Py_DECREF(py_comp);
649}
650
efb177a7 651static
fb25b9e3 652void component_class_source_finalize(bt_self_component_source *self_component_source)
6fa6bfa1
SM
653{
654 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
fb25b9e3 655 component_class_finalize(self_component);
6fa6bfa1
SM
656}
657
efb177a7 658static
fb25b9e3 659void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
6fa6bfa1
SM
660{
661 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
fb25b9e3 662 component_class_finalize(self_component);
6fa6bfa1
SM
663}
664
efb177a7 665static
fb25b9e3 666void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
6fa6bfa1
SM
667{
668 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
fb25b9e3 669 component_class_finalize(self_component);
6fa6bfa1
SM
670}
671
39ddfa44 672static
fb25b9e3 673bt_bool component_class_can_seek_beginning(
39ddfa44
SM
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
deec48a6 683 py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
39ddfa44
SM
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 */
f8d71b59 694 loge_exception_message_iterator(self_message_iterator);
39ddfa44
SM
695 PyErr_Clear();
696 }
697
698 Py_XDECREF(py_result);
699
700 return can_seek_beginning;
701}
702
703static
fb25b9e3
PP
704bt_component_class_message_iterator_seek_beginning_method_status
705component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
39ddfa44
SM
706{
707 PyObject *py_iter;
708 PyObject *py_result;
fb25b9e3 709 bt_component_class_message_iterator_seek_beginning_method_status status;
39ddfa44
SM
710
711 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
712 BT_ASSERT(py_iter);
deec48a6 713 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
fb25b9e3 714 NULL);
39ddfa44 715 BT_ASSERT(!py_result || py_result == Py_None);
3b2be708 716 status = py_exc_to_status_message_iterator(self_message_iterator);
39ddfa44 717 Py_XDECREF(py_result);
39ddfa44
SM
718 return status;
719}
720
efb177a7 721static
fb25b9e3 722bt_component_class_port_connected_method_status component_class_port_connected(
6fa6bfa1
SM
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{
fb25b9e3 730 bt_component_class_port_connected_method_status status;
6fa6bfa1
SM
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;
8d6ef9df
PP
735 bt_logging_level log_level = get_self_component_log_level(
736 self_component);
6fa6bfa1
SM
737
738 py_comp = bt_self_component_get_data(self_component);
739 BT_ASSERT(py_comp);
6fa6bfa1
SM
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) {
8d6ef9df
PP
743 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
744 "Failed to create a SWIG pointer object.");
fb25b9e3 745 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6fa6bfa1
SM
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) {
8d6ef9df
PP
752 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
753 "Failed to create a SWIG pointer object.");
fb25b9e3 754 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6fa6bfa1
SM
755 goto end; }
756
757 py_method_result = PyObject_CallMethod(py_comp,
deec48a6 758 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
6fa6bfa1 759 self_component_port_type, py_other_port_ptr);
6fa6bfa1 760 BT_ASSERT(!py_method_result || py_method_result == Py_None);
f8d71b59 761 status = py_exc_to_status_component(self_component);
6fa6bfa1
SM
762
763end:
764 Py_XDECREF(py_self_port_ptr);
765 Py_XDECREF(py_other_port_ptr);
766 Py_XDECREF(py_method_result);
6fa6bfa1
SM
767 return status;
768}
769
efb177a7 770static
fb25b9e3
PP
771bt_component_class_port_connected_method_status
772component_class_source_output_port_connected(
6fa6bfa1
SM
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
fb25b9e3 779 return component_class_port_connected(
6fa6bfa1
SM
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
efb177a7 788static
fb25b9e3
PP
789bt_component_class_port_connected_method_status
790component_class_filter_input_port_connected(
6fa6bfa1
SM
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
fb25b9e3 797 return component_class_port_connected(
6fa6bfa1
SM
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
efb177a7 806static
fb25b9e3
PP
807bt_component_class_port_connected_method_status
808component_class_filter_output_port_connected(
6fa6bfa1
SM
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
fb25b9e3 815 return component_class_port_connected(
6fa6bfa1
SM
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
efb177a7 824static
fb25b9e3
PP
825bt_component_class_port_connected_method_status
826component_class_sink_input_port_connected(
6fa6bfa1
SM
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
fb25b9e3 833 return component_class_port_connected(
6fa6bfa1
SM
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
efb177a7 842static
fb25b9e3
PP
843bt_component_class_sink_graph_is_configured_method_status
844component_class_sink_graph_is_configured(
efb177a7 845 bt_self_component_sink *self_component_sink)
6fa6bfa1
SM
846{
847 PyObject *py_comp = NULL;
848 PyObject *py_method_result = NULL;
fb25b9e3 849 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
6fa6bfa1
SM
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,
deec48a6 854 "_bt_graph_is_configured_from_native", NULL);
6fa6bfa1 855 BT_ASSERT(!py_method_result || py_method_result == Py_None);
f8d71b59 856 status = py_exc_to_status_component(self_component);
6fa6bfa1 857 Py_XDECREF(py_method_result);
6fa6bfa1
SM
858 return status;
859}
860
efb177a7 861static
fb25b9e3 862bt_component_class_query_method_status component_class_query(
6fa6bfa1 863 const bt_component_class *component_class,
f8d71b59 864 bt_self_component_class *self_component_class,
6fa6bfa1
SM
865 const bt_query_executor *query_executor,
866 const char *object, const bt_value *params,
83da519a 867 bt_logging_level log_level,
6fa6bfa1
SM
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;
fb25b9e3 876 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
6fa6bfa1
SM
877
878 py_cls = lookup_cc_ptr_to_py_cls(component_class);
879 if (!py_cls) {
8d6ef9df
PP
880 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
881 "Cannot find Python class associated to native component class: "
6fa6bfa1
SM
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) {
8d6ef9df
PP
889 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
890 "Failed to create a SWIG pointer object.");
6fa6bfa1
SM
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) {
8d6ef9df
PP
897 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
898 "Failed to create a SWIG pointer object.");
6fa6bfa1
SM
899 goto error;
900 }
901
902 py_object = SWIG_FromCharPtr(object);
903 if (!py_object) {
8d6ef9df
PP
904 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
905 "Failed to create a Python string.");
6fa6bfa1
SM
906 goto error;
907 }
908
909 py_results_addr = PyObject_CallMethod(py_cls,
deec48a6 910 "_bt_query_from_native", "(OOOi)", py_query_exec_ptr,
83da519a 911 py_object, py_params_ptr, (int) log_level);
6fa6bfa1 912 if (!py_results_addr) {
8d6ef9df
PP
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: "
6fa6bfa1 915 "py-cls-addr=%p", py_cls);
8d6ef9df
PP
916 status = py_exc_to_status_component_class(self_component_class,
917 log_level);
6fa6bfa1
SM
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 */
c240a5d8 926 *result = PyLong_AsVoidPtr(py_results_addr);
6fa6bfa1
SM
927 BT_ASSERT(!PyErr_Occurred());
928 BT_ASSERT(*result);
929 goto end;
930
931error:
932 PyErr_Clear();
fb25b9e3 933 status = __BT_FUNC_STATUS_ERROR;
6fa6bfa1
SM
934
935end:
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
efb177a7 944static
fb25b9e3 945bt_component_class_query_method_status component_class_source_query(
6fa6bfa1
SM
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,
83da519a 949 bt_logging_level log_level,
6fa6bfa1
SM
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);
f8d71b59 954 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
fb25b9e3 955
f8d71b59 956 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
6fa6bfa1
SM
957}
958
efb177a7 959static
fb25b9e3 960bt_component_class_query_method_status component_class_filter_query(
6fa6bfa1
SM
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,
83da519a 964 bt_logging_level log_level,
6fa6bfa1
SM
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);
f8d71b59 969 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
fb25b9e3 970
f8d71b59 971 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
6fa6bfa1
SM
972}
973
efb177a7 974static
fb25b9e3 975bt_component_class_query_method_status component_class_sink_query(
6fa6bfa1
SM
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,
83da519a 979 bt_logging_level log_level,
6fa6bfa1
SM
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);
f8d71b59 984 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
fb25b9e3 985
f8d71b59 986 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
6fa6bfa1
SM
987}
988
efb177a7 989static
fb25b9e3
PP
990bt_component_class_message_iterator_init_method_status
991component_class_message_iterator_init(
6fa6bfa1
SM
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{
fb25b9e3 996 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
6fa6bfa1
SM
997 PyObject *py_comp_cls = NULL;
998 PyObject *py_iter_cls = NULL;
999 PyObject *py_iter_ptr = NULL;
a4dcfa96 1000 PyObject *py_component_port_output_ptr = NULL;
6fa6bfa1
SM
1001 PyObject *py_init_method_result = NULL;
1002 PyObject *py_iter = NULL;
1003 PyObject *py_comp;
8d6ef9df
PP
1004 bt_logging_level log_level = get_self_component_log_level(
1005 self_component);
6fa6bfa1
SM
1006
1007 py_comp = bt_self_component_get_data(self_component);
1008
fa4c33e3 1009 /* Find user's Python message iterator class */
6fa6bfa1
SM
1010 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
1011 if (!py_comp_cls) {
8d6ef9df
PP
1012 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1013 "Cannot get Python object's `__class__` attribute.");
f8d71b59 1014 goto python_error;
6fa6bfa1
SM
1015 }
1016
1017 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
1018 if (!py_iter_cls) {
8d6ef9df
PP
1019 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1020 "Cannot get Python class's `_iter_cls` attribute.");
f8d71b59 1021 goto python_error;
6fa6bfa1
SM
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) {
f8d71b59
SM
1027 const char *err = "Failed to create a SWIG pointer object.";
1028
8d6ef9df
PP
1029 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1030 "%s", err);
f8d71b59
SM
1031 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1032 self_message_iterator, err);
6fa6bfa1
SM
1033 goto error;
1034 }
1035
1036 /*
fa4c33e3 1037 * Create object with borrowed native message iterator
6fa6bfa1
SM
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) {
8d6ef9df
PP
1045 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1046 "Failed to call Python class's __new__() method: "
6fa6bfa1 1047 "py-cls-addr=%p", py_iter_cls);
f8d71b59 1048 goto python_error;
6fa6bfa1
SM
1049 }
1050
1051 /*
1052 * Initialize object:
1053 *
a4dcfa96
SM
1054 * py_iter.__init__(self_output_port)
1055 *
3e2b0fcf 1056 * through the _init_for_native helper static method.
6fa6bfa1
SM
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 */
3e2b0fcf
SM
1063 py_component_port_output_ptr = SWIG_NewPointerObj(
1064 SWIG_as_voidptr(self_component_port_output),
a4dcfa96
SM
1065 SWIGTYPE_p_bt_self_component_port_output, 0);
1066 if (!py_component_port_output_ptr) {
f8d71b59
SM
1067 const char *err = "Failed to create a SWIG pointer object.";
1068
8d6ef9df
PP
1069 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1070 "%s", err);
f8d71b59
SM
1071 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1072 self_message_iterator, err);
a4dcfa96
SM
1073 goto error;
1074 }
1075
fb25b9e3 1076 py_init_method_result = PyObject_CallMethod(py_iter,
deec48a6 1077 "_bt_init_from_native", "O", py_component_port_output_ptr);
6fa6bfa1 1078 if (!py_init_method_result) {
8d6ef9df
PP
1079 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1080 "User's __init__() method failed:");
f8d71b59 1081 goto python_error;
6fa6bfa1
SM
1082 }
1083
1084 /*
1085 * Since the Python code can never instantiate a user-defined
fa4c33e3
SM
1086 * message iterator class, the native message iterator
1087 * object does NOT belong to a user Python message iterator
6fa6bfa1 1088 * object (borrowed reference). However this Python object is
fa4c33e3 1089 * owned by this native message iterator object.
6fa6bfa1 1090 *
fa4c33e3
SM
1091 * In the Python world, the lifetime of the native message
1092 * iterator is managed by a _GenericMessageIterator
6fa6bfa1
SM
1093 * instance:
1094 *
fa4c33e3
SM
1095 * _GenericMessageIterator instance:
1096 * owns a native bt_message_iterator object (iter)
6fa6bfa1
SM
1097 * owns a _UserMessageIterator instance (py_iter)
1098 * self._ptr is a borrowed reference to the
fa4c33e3 1099 * native bt_private_connection_private_message_iterator
6fa6bfa1
SM
1100 * object (iter)
1101 */
1102 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1103 py_iter = NULL;
1104 goto end;
1105
f8d71b59
SM
1106python_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;
6fa6bfa1 1111
f8d71b59
SM
1112error:
1113 /* Handling of errors that don't cause a Python exception to be set. */
1114 status = __BT_FUNC_STATUS_ERROR;
6fa6bfa1
SM
1115
1116end:
f8d71b59
SM
1117 BT_ASSERT(!PyErr_Occurred());
1118
6fa6bfa1
SM
1119 Py_XDECREF(py_comp_cls);
1120 Py_XDECREF(py_iter_cls);
1121 Py_XDECREF(py_iter_ptr);
a4dcfa96 1122 Py_XDECREF(py_component_port_output_ptr);
6fa6bfa1
SM
1123 Py_XDECREF(py_init_method_result);
1124 Py_XDECREF(py_iter);
1125 return status;
1126}
1127
efb177a7 1128static
fb25b9e3
PP
1129bt_component_class_message_iterator_init_method_status
1130component_class_source_message_iterator_init(
6fa6bfa1
SM
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);
fb25b9e3
PP
1136
1137 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6fa6bfa1
SM
1138}
1139
efb177a7 1140static
fb25b9e3
PP
1141bt_component_class_message_iterator_init_method_status
1142component_class_filter_message_iterator_init(
6fa6bfa1
SM
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);
fb25b9e3
PP
1148
1149 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6fa6bfa1
SM
1150}
1151
efb177a7 1152static
fb25b9e3 1153void component_class_message_iterator_finalize(
6fa6bfa1
SM
1154 bt_self_message_iterator *message_iterator)
1155{
8d6ef9df
PP
1156 PyObject *py_message_iter = bt_self_message_iterator_get_data(
1157 message_iterator);
6fa6bfa1
SM
1158 PyObject *py_method_result = NULL;
1159
1160 BT_ASSERT(py_message_iter);
1161
819d0ae7 1162 /* Call user's _user_finalize() method */
6fa6bfa1 1163 py_method_result = PyObject_CallMethod(py_message_iter,
819d0ae7 1164 "_user_finalize", NULL);
6fa6bfa1
SM
1165
1166 if (PyErr_Occurred()) {
8d6ef9df
PP
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));
6fa6bfa1
SM
1177 }
1178
1179 /*
819d0ae7
PP
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.
6fa6bfa1
SM
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
efb177a7 1191static
fb25b9e3
PP
1192bt_component_class_message_iterator_next_method_status
1193component_class_message_iterator_next(
efb177a7
SM
1194 bt_self_message_iterator *message_iterator,
1195 bt_message_array_const msgs, uint64_t capacity,
1196 uint64_t *count)
6fa6bfa1 1197{
fb25b9e3 1198 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
6fa6bfa1
SM
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,
deec48a6 1204 "_bt_next_from_native", NULL);
6fa6bfa1 1205 if (!py_method_result) {
f8d71b59 1206 status = py_exc_to_status_message_iterator(message_iterator);
fb25b9e3 1207 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
6fa6bfa1
SM
1208 goto end;
1209 }
1210
1211 /*
1212 * The returned object, on success, is an integer object
fa4c33e3 1213 * (PyLong) containing the address of a native message
6fa6bfa1
SM
1214 * object (which is now ours).
1215 */
c240a5d8 1216 msgs[0] = PyLong_AsVoidPtr(py_method_result);
6fa6bfa1
SM
1217 *count = 1;
1218
1219 /* Clear potential overflow error; should never happen */
1220 BT_ASSERT(!PyErr_Occurred());
1221 goto end;
1222
1223end:
1224 Py_XDECREF(py_method_result);
1225 return status;
1226}
1227
efb177a7 1228static
fb25b9e3
PP
1229bt_component_class_sink_consume_method_status
1230component_class_sink_consume(bt_self_component_sink *self_component_sink)
6fa6bfa1
SM
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;
fb25b9e3 1235 bt_component_class_sink_consume_method_status status;
6fa6bfa1
SM
1236
1237 BT_ASSERT(py_comp);
1238 py_method_result = PyObject_CallMethod(py_comp,
819d0ae7 1239 "_user_consume", NULL);
f8d71b59 1240 status = py_exc_to_status_component(self_component);
8d6ef9df 1241 BT_ASSERT(py_method_result || status != __BT_FUNC_STATUS_OK);
6fa6bfa1
SM
1242 Py_XDECREF(py_method_result);
1243 return status;
1244}
1245
1246static
fb25b9e3 1247int component_class_set_help_and_desc(
6fa6bfa1
SM
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
1273end:
1274 return ret;
1275}
1276
1277static
fb25b9e3 1278bt_component_class_source *bt_bt2_component_class_source_create(
6fa6bfa1
SM
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);
6fa6bfa1 1287 component_class_source = bt_component_class_source_create(name,
fb25b9e3 1288 component_class_message_iterator_next);
6fa6bfa1
SM
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
fb25b9e3 1296 if (component_class_set_help_and_desc(component_class, description, help)) {
6fa6bfa1
SM
1297 goto end;
1298 }
1299
fb25b9e3 1300 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
6fa6bfa1 1301 BT_ASSERT(ret == 0);
fb25b9e3 1302 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
39ddfa44
SM
1303 BT_ASSERT(ret == 0);
1304 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
fb25b9e3 1305 component_class_can_seek_beginning);
39ddfa44
SM
1306 BT_ASSERT(ret == 0);
1307 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
fb25b9e3 1308 component_class_seek_beginning);
6fa6bfa1 1309 BT_ASSERT(ret == 0);
6fa6bfa1 1310 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
fb25b9e3 1311 component_class_source_output_port_connected);
6fa6bfa1 1312 BT_ASSERT(ret == 0);
fb25b9e3 1313 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
6fa6bfa1
SM
1314 BT_ASSERT(ret == 0);
1315 ret = bt_component_class_source_set_message_iterator_init_method(
fb25b9e3 1316 component_class_source, component_class_source_message_iterator_init);
6fa6bfa1
SM
1317 BT_ASSERT(ret == 0);
1318 ret = bt_component_class_source_set_message_iterator_finalize_method(
fb25b9e3 1319 component_class_source, component_class_message_iterator_finalize);
6fa6bfa1 1320 BT_ASSERT(ret == 0);
6fa6bfa1
SM
1321 register_cc_ptr_to_py_cls(component_class, py_cls);
1322
1323end:
1324 return component_class_source;
1325}
1326
1327static
fb25b9e3 1328bt_component_class_filter *bt_bt2_component_class_filter_create(
6fa6bfa1
SM
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);
6fa6bfa1 1337 component_class_filter = bt_component_class_filter_create(name,
fb25b9e3 1338 component_class_message_iterator_next);
6fa6bfa1
SM
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
fb25b9e3 1346 if (component_class_set_help_and_desc(component_class, description, help)) {
6fa6bfa1
SM
1347 goto end;
1348 }
1349
fb25b9e3 1350 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
6fa6bfa1 1351 BT_ASSERT(ret == 0);
fb25b9e3 1352 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
6fa6bfa1 1353 BT_ASSERT(ret == 0);
39ddfa44 1354 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
fb25b9e3 1355 component_class_can_seek_beginning);
39ddfa44
SM
1356 BT_ASSERT(ret == 0);
1357 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
fb25b9e3 1358 component_class_seek_beginning);
39ddfa44 1359 BT_ASSERT(ret == 0);
6fa6bfa1 1360 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
fb25b9e3 1361 component_class_filter_input_port_connected);
6fa6bfa1
SM
1362 BT_ASSERT(ret == 0);
1363 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
fb25b9e3 1364 component_class_filter_output_port_connected);
6fa6bfa1 1365 BT_ASSERT(ret == 0);
fb25b9e3 1366 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
6fa6bfa1
SM
1367 BT_ASSERT(ret == 0);
1368 ret = bt_component_class_filter_set_message_iterator_init_method(
fb25b9e3 1369 component_class_filter, component_class_filter_message_iterator_init);
6fa6bfa1
SM
1370 BT_ASSERT(ret == 0);
1371 ret = bt_component_class_filter_set_message_iterator_finalize_method(
fb25b9e3 1372 component_class_filter, component_class_message_iterator_finalize);
6fa6bfa1 1373 BT_ASSERT(ret == 0);
6fa6bfa1
SM
1374 register_cc_ptr_to_py_cls(component_class, py_cls);
1375
1376end:
1377 return component_class_filter;
1378}
1379
1380static
fb25b9e3 1381bt_component_class_sink *bt_bt2_component_class_sink_create(
6fa6bfa1
SM
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);
fb25b9e3 1390 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
6fa6bfa1
SM
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
fb25b9e3 1399 if (component_class_set_help_and_desc(component_class, description, help)) {
6fa6bfa1
SM
1400 goto end;
1401 }
1402
fb25b9e3 1403 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
6fa6bfa1 1404 BT_ASSERT(ret == 0);
fb25b9e3 1405 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
6fa6bfa1 1406 BT_ASSERT(ret == 0);
6fa6bfa1 1407 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
fb25b9e3 1408 component_class_sink_input_port_connected);
6fa6bfa1
SM
1409 BT_ASSERT(ret == 0);
1410 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
fb25b9e3 1411 component_class_sink_graph_is_configured);
6fa6bfa1 1412 BT_ASSERT(ret == 0);
fb25b9e3 1413 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
6fa6bfa1 1414 BT_ASSERT(ret == 0);
6fa6bfa1
SM
1415 register_cc_ptr_to_py_cls(component_class, py_cls);
1416
1417end:
1418 return component_class_sink;
1419}
1420%}
1421
fb25b9e3 1422struct bt_component_class_source *bt_bt2_component_class_source_create(
6fa6bfa1
SM
1423 PyObject *py_cls, const char *name, const char *description,
1424 const char *help);
fb25b9e3 1425struct bt_component_class_filter *bt_bt2_component_class_filter_create(
6fa6bfa1
SM
1426 PyObject *py_cls, const char *name, const char *description,
1427 const char *help);
fb25b9e3 1428struct bt_component_class_sink *bt_bt2_component_class_sink_create(
6fa6bfa1
SM
1429 PyObject *py_cls, const char *name, const char *description,
1430 const char *help);
fb25b9e3
PP
1431void bt_bt2_cc_init_from_bt2(void);
1432void bt_bt2_cc_exit_handler(void);
This page took 0.102265 seconds and 4 git commands to generate.