tests: reorganize CLI's `convert` tests
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i
CommitLineData
6945df9a
SM
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
d24d5663 25%include <babeltrace2/graph/component-class.h>
d6bb425c
SM
26%include <babeltrace2/graph/component-class-const.h>
27%include <babeltrace2/graph/component-class-source-const.h>
28%include <babeltrace2/graph/component-class-source.h>
29%include <babeltrace2/graph/component-class-filter-const.h>
30%include <babeltrace2/graph/component-class-filter.h>
31%include <babeltrace2/graph/component-class-sink-const.h>
32%include <babeltrace2/graph/component-class-sink.h>
33%include <babeltrace2/graph/self-component-class-source.h>
34%include <babeltrace2/graph/self-component-class-filter.h>
35%include <babeltrace2/graph/self-component-class-sink.h>
6945df9a
SM
36
37%{
12cd4490
PP
38#include "logging/comp-logging.h"
39
6945df9a
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
d24d5663 48 * class is created by one of the bt_bt2_component_class_*_create()
6945df9a
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
7744859c
SM
60static
61void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
6945df9a
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
7744859c
SM
79static
80PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
6945df9a
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;
4acc866e 99static PyObject *py_mod_bt2_exc_memory_error = NULL;
6945df9a
SM
100static PyObject *py_mod_bt2_exc_try_again_type = NULL;
101static PyObject *py_mod_bt2_exc_stop_type = NULL;
76b6c2f7 102static PyObject *py_mod_bt2_exc_unknown_object_type = NULL;
6945df9a 103
7744859c 104static
d24d5663 105void bt_bt2_cc_init_from_bt2(void)
6945df9a
SM
106{
107 /*
108 * This is called once the bt2 package is loaded.
109 *
110 * Those modules and functions are needed while the package is
111 * used. Loading them here is safe because we know the bt2
112 * package is imported, and we know that the user cannot use the
113 * code here without importing bt2 first.
114 */
115 py_mod_bt2 = PyImport_ImportModule("bt2");
116 BT_ASSERT(py_mod_bt2);
117 py_mod_bt2_exc_error_type =
694c792b 118 PyObject_GetAttrString(py_mod_bt2, "_Error");
6945df9a 119 BT_ASSERT(py_mod_bt2_exc_error_type);
4acc866e 120 py_mod_bt2_exc_memory_error =
694c792b 121 PyObject_GetAttrString(py_mod_bt2, "_MemoryError");
4acc866e 122 BT_ASSERT(py_mod_bt2_exc_memory_error);
6945df9a
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);
76b6c2f7
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);
6945df9a
SM
132}
133
7744859c 134static
d24d5663 135void bt_bt2_cc_exit_handler(void)
6945df9a
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
d24d5663 141 * bt_bt2_cc_init_from_bt2() here. The global variables continue
6945df9a
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);
76b6c2f7 154 Py_XDECREF(py_mod_bt2_exc_unknown_object_type);
6945df9a
SM
155}
156
157
158/* Library destructor */
159
160__attribute__((destructor))
7744859c 161static
d24d5663 162void native_comp_class_dtor(void) {
6945df9a
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
ce4923b0
SM
170static
171void restore_current_thread_error_and_append_exception_chain_recursive(
12cd4490 172 int active_log_level, PyObject *py_exc_value,
ce4923b0
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(
12cd4490
PP
187 active_log_level, py_exc_cause_value,
188 self_component_class, self_component,
189 self_message_iterator, module_name);
ce4923b0
SM
190 }
191
192 /*
694c792b 193 * If the raised exception is a bt2._Error, restore the wrapped error.
ce4923b0
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 /*
694c792b 201 * We never raise a bt2._Error with a cause: it should be the
ce4923b0
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,
12cd4490 229 py_exc_tb, active_log_level, false);
ce4923b0
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()
694c792b 265 * except bt2._Error as e1:
ce4923b0
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 *
694c792b 272 * TypeError -> ValueError -> bt2._Error
ce4923b0
SM
273 *
274 * Where the TypeError is the current exception (obtained from PyErr_Fetch).
275 *
694c792b 276 * The bt2._Error contains a `struct bt_error *` that used to be the current
ce4923b0
SM
277 * thread's error, at the moment the exception was raised.
278 *
694c792b 279 * This function gets to the bt2._Error and restores the wrapped
ce4923b0
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(
12cd4490 287 int active_log_level,
ce4923b0
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
12cd4490
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);
ce4923b0
SM
317
318 PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb);
319}
320
d24d5663 321static inline
12cd4490
PP
322void log_exception_and_maybe_append_error(int func_log_level,
323 int active_log_level, bool append_error,
ce95fb26
SM
324 bt_self_component_class *self_component_class,
325 bt_self_component *self_component,
ce4923b0
SM
326 bt_self_message_iterator *self_message_iterator,
327 const char *module_name)
6945df9a 328{
7085eeaa 329 GString *gstr;
6945df9a 330
5084732e 331 BT_ASSERT(PyErr_Occurred());
12cd4490 332 gstr = bt_py_common_format_current_exception(active_log_level);
7085eeaa 333 if (!gstr) {
cacd0713 334 /* bt_py_common_format_current_exception() logs errors */
6945df9a
SM
335 goto end;
336 }
337
12cd4490
PP
338 BT_COMP_LOG_CUR_LVL(func_log_level, active_log_level, self_component,
339 "%s", gstr->str);
6945df9a 340
ce95fb26 341 if (append_error) {
ce4923b0 342 restore_bt_error_and_append_current_exception_chain(
12cd4490 343 active_log_level, self_component_class, self_component,
ce4923b0
SM
344 self_message_iterator, module_name);
345
ce95fb26
SM
346 }
347
6945df9a 348end:
7085eeaa
PP
349 if (gstr) {
350 g_string_free(gstr, TRUE);
6945df9a 351 }
6945df9a
SM
352}
353
12cd4490
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
d24d5663 371static inline
12cd4490 372void loge_exception(const char *module_name, int active_log_level)
6945df9a 373{
12cd4490
PP
374 log_exception_and_maybe_append_error(BT_LOG_ERROR, active_log_level,
375 true, NULL, NULL, NULL, module_name);
ce95fb26
SM
376}
377
378static
379void loge_exception_message_iterator(
380 bt_self_message_iterator *self_message_iterator)
381{
12cd4490
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);
6945df9a
SM
387}
388
d24d5663 389static inline
12cd4490 390void logw_exception(int active_log_level)
6945df9a 391{
12cd4490
PP
392 log_exception_and_maybe_append_error(BT_LOG_WARNING, active_log_level,
393 false, NULL, NULL, NULL, NULL);
6945df9a
SM
394}
395
d24d5663 396static inline
ce95fb26
SM
397int py_exc_to_status(bt_self_component_class *self_component_class,
398 bt_self_component *self_component,
ce4923b0 399 bt_self_message_iterator *self_message_iterator,
12cd4490 400 const char *module_name, int active_log_level)
6945df9a 401{
d24d5663 402 int status = __BT_FUNC_STATUS_OK;
6945df9a
SM
403 PyObject *exc = PyErr_Occurred();
404
405 if (!exc) {
406 goto end;
407 }
408
409 if (PyErr_GivenExceptionMatches(exc,
d24d5663
PP
410 py_mod_bt2_exc_try_again_type)) {
411 status = __BT_FUNC_STATUS_AGAIN;
6945df9a 412 } else if (PyErr_GivenExceptionMatches(exc,
d24d5663
PP
413 py_mod_bt2_exc_stop_type)) {
414 status = __BT_FUNC_STATUS_END;
6945df9a 415 } else if (PyErr_GivenExceptionMatches(exc,
76b6c2f7
SM
416 py_mod_bt2_exc_unknown_object_type)) {
417 status = __BT_FUNC_STATUS_UNKNOWN_OBJECT;
6945df9a 418 } else {
12cd4490
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,
ce95fb26 439 self_component_class, self_component,
ce4923b0 440 self_message_iterator, module_name);
4acc866e
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 }
6945df9a
SM
448 }
449
450end:
451 PyErr_Clear();
452 return status;
453}
454
ce95fb26 455static
12cd4490
PP
456int py_exc_to_status_component_class(
457 bt_self_component_class *self_component_class,
458 int active_log_level)
ce95fb26 459{
12cd4490
PP
460 return py_exc_to_status(self_component_class, NULL, NULL, NULL,
461 active_log_level);
ce95fb26
SM
462}
463
464static
465int py_exc_to_status_component(bt_self_component *self_component)
466{
12cd4490 467 return py_exc_to_status(NULL, self_component, NULL, NULL, -1);
ce95fb26
SM
468}
469
470static
471int py_exc_to_status_message_iterator(
472 bt_self_message_iterator *self_message_iterator)
473{
12cd4490 474 return py_exc_to_status(NULL, NULL, self_message_iterator, NULL, -1);
ce95fb26
SM
475}
476
d24d5663
PP
477/* Component class proxy methods (delegate to the attached Python object) */
478
7744859c 479static
d24d5663 480bt_component_class_init_method_status component_class_init(
6945df9a
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);
d24d5663 489 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
490 PyObject *py_cls = NULL;
491 PyObject *py_comp = NULL;
492 PyObject *py_params_ptr = NULL;
493 PyObject *py_comp_ptr = NULL;
12cd4490
PP
494 bt_logging_level log_level = get_self_component_log_level(
495 self_component);
6945df9a
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) {
12cd4490
PP
510 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
511 "Cannot find Python class associated to native component class: "
6945df9a
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) {
12cd4490
PP
520 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
521 "Failed to create a SWIG pointer object.");
6945df9a
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) {
12cd4490
PP
528 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
529 "Failed to create a SWIG pointer object.");
6945df9a
SM
530 goto error;
531 }
532
533 /*
534 * Do the equivalent of this:
535 *
85906b6b 536 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
6945df9a 537 *
85906b6b 538 * _UserComponentType._bt_init_from_native() calls the Python
6945df9a
SM
539 * component object's __init__() function.
540 */
541 py_comp = PyObject_CallMethod(py_cls,
85906b6b 542 "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
6945df9a 543 if (!py_comp) {
12cd4490
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: "
6945df9a 546 "py-cls-addr=%p", py_cls);
ce95fb26
SM
547 status = py_exc_to_status_component(self_component);
548 goto end;
6945df9a
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:
d24d5663 561 status = __BT_FUNC_STATUS_ERROR;
6945df9a
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
7744859c 583static
d24d5663 584bt_component_class_init_method_status component_class_source_init(
7744859c 585 bt_self_component_source *self_component_source,
6945df9a
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);
d24d5663 589 return component_class_init(
6945df9a
SM
590 self_component,
591 self_component_source,
592 SWIGTYPE_p_bt_self_component_source,
593 params, init_method_data);
594}
595
7744859c 596static
d24d5663 597bt_component_class_init_method_status component_class_filter_init(
7744859c 598 bt_self_component_filter *self_component_filter,
6945df9a
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);
d24d5663 602 return component_class_init(
6945df9a
SM
603 self_component,
604 self_component_filter,
605 SWIGTYPE_p_bt_self_component_filter,
606 params, init_method_data);
607}
608
7744859c 609static
d24d5663 610bt_component_class_init_method_status component_class_sink_init(
7744859c 611 bt_self_component_sink *self_component_sink,
6945df9a
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);
d24d5663 615 return component_class_init(
6945df9a
SM
616 self_component,
617 self_component_sink,
618 SWIGTYPE_p_bt_self_component_sink,
619 params, init_method_data);
620}
621
7744859c 622static
d24d5663 623void component_class_finalize(bt_self_component *self_component)
6945df9a
SM
624{
625 PyObject *py_comp = bt_self_component_get_data(self_component);
626 BT_ASSERT(py_comp);
627
6a91742b 628 /* Call user's _user_finalize() method */
6945df9a 629 PyObject *py_method_result = PyObject_CallMethod(py_comp,
6a91742b 630 "_user_finalize", NULL);
6945df9a
SM
631
632 if (PyErr_Occurred()) {
12cd4490
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);
6945df9a
SM
639 }
640
641 /*
6a91742b
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.
6945df9a
SM
645 */
646 PyErr_Clear();
647 Py_XDECREF(py_method_result);
648 Py_DECREF(py_comp);
649}
650
7744859c 651static
d24d5663 652void component_class_source_finalize(bt_self_component_source *self_component_source)
6945df9a
SM
653{
654 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
d24d5663 655 component_class_finalize(self_component);
6945df9a
SM
656}
657
7744859c 658static
d24d5663 659void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
6945df9a
SM
660{
661 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
d24d5663 662 component_class_finalize(self_component);
6945df9a
SM
663}
664
7744859c 665static
d24d5663 666void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
6945df9a
SM
667{
668 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
d24d5663 669 component_class_finalize(self_component);
6945df9a
SM
670}
671
f00b8d40 672static
d24d5663 673bt_bool component_class_can_seek_beginning(
f00b8d40
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
85906b6b 683 py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
f00b8d40
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 */
ce95fb26 694 loge_exception_message_iterator(self_message_iterator);
f00b8d40
SM
695 PyErr_Clear();
696 }
697
698 Py_XDECREF(py_result);
699
700 return can_seek_beginning;
701}
702
703static
d24d5663
PP
704bt_component_class_message_iterator_seek_beginning_method_status
705component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
f00b8d40
SM
706{
707 PyObject *py_iter;
708 PyObject *py_result;
d24d5663 709 bt_component_class_message_iterator_seek_beginning_method_status status;
f00b8d40
SM
710
711 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
712 BT_ASSERT(py_iter);
85906b6b 713 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
d24d5663 714 NULL);
f00b8d40 715 BT_ASSERT(!py_result || py_result == Py_None);
ce4923b0 716 status = py_exc_to_status_message_iterator(self_message_iterator);
f00b8d40 717 Py_XDECREF(py_result);
f00b8d40
SM
718 return status;
719}
720
7744859c 721static
d24d5663 722bt_component_class_port_connected_method_status component_class_port_connected(
6945df9a
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{
d24d5663 730 bt_component_class_port_connected_method_status status;
6945df9a
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;
12cd4490
PP
735 bt_logging_level log_level = get_self_component_log_level(
736 self_component);
6945df9a
SM
737
738 py_comp = bt_self_component_get_data(self_component);
739 BT_ASSERT(py_comp);
6945df9a
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) {
12cd4490
PP
743 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
744 "Failed to create a SWIG pointer object.");
d24d5663 745 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6945df9a
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) {
12cd4490
PP
752 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
753 "Failed to create a SWIG pointer object.");
d24d5663 754 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6945df9a
SM
755 goto end; }
756
757 py_method_result = PyObject_CallMethod(py_comp,
85906b6b 758 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
6945df9a 759 self_component_port_type, py_other_port_ptr);
6945df9a 760 BT_ASSERT(!py_method_result || py_method_result == Py_None);
ce95fb26 761 status = py_exc_to_status_component(self_component);
6945df9a
SM
762
763end:
764 Py_XDECREF(py_self_port_ptr);
765 Py_XDECREF(py_other_port_ptr);
766 Py_XDECREF(py_method_result);
6945df9a
SM
767 return status;
768}
769
7744859c 770static
d24d5663
PP
771bt_component_class_port_connected_method_status
772component_class_source_output_port_connected(
6945df9a
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
d24d5663 779 return component_class_port_connected(
6945df9a
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
7744859c 788static
d24d5663
PP
789bt_component_class_port_connected_method_status
790component_class_filter_input_port_connected(
6945df9a
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
d24d5663 797 return component_class_port_connected(
6945df9a
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
7744859c 806static
d24d5663
PP
807bt_component_class_port_connected_method_status
808component_class_filter_output_port_connected(
6945df9a
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
d24d5663 815 return component_class_port_connected(
6945df9a
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
7744859c 824static
d24d5663
PP
825bt_component_class_port_connected_method_status
826component_class_sink_input_port_connected(
6945df9a
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
d24d5663 833 return component_class_port_connected(
6945df9a
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
7744859c 842static
d24d5663
PP
843bt_component_class_sink_graph_is_configured_method_status
844component_class_sink_graph_is_configured(
7744859c 845 bt_self_component_sink *self_component_sink)
6945df9a
SM
846{
847 PyObject *py_comp = NULL;
848 PyObject *py_method_result = NULL;
d24d5663 849 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
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,
85906b6b 854 "_bt_graph_is_configured_from_native", NULL);
6945df9a 855 BT_ASSERT(!py_method_result || py_method_result == Py_None);
ce95fb26 856 status = py_exc_to_status_component(self_component);
6945df9a 857 Py_XDECREF(py_method_result);
6945df9a
SM
858 return status;
859}
860
7744859c 861static
d24d5663 862bt_component_class_query_method_status component_class_query(
6945df9a 863 const bt_component_class *component_class,
ce95fb26 864 bt_self_component_class *self_component_class,
3c729b9a 865 bt_private_query_executor *priv_query_executor,
6945df9a
SM
866 const char *object, const bt_value *params,
867 const bt_value **result)
868{
869 PyObject *py_cls = NULL;
870 PyObject *py_params_ptr = NULL;
3c729b9a 871 PyObject *py_priv_query_exec_ptr = NULL;
6945df9a
SM
872 PyObject *py_query_func = NULL;
873 PyObject *py_object = NULL;
874 PyObject *py_results_addr = NULL;
d24d5663 875 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
3c729b9a
PP
876 const bt_query_executor *query_exec =
877 bt_private_query_executor_as_query_executor_const(
878 priv_query_executor);
879 bt_logging_level log_level =
880 bt_query_executor_get_logging_level(query_exec);
6945df9a
SM
881
882 py_cls = lookup_cc_ptr_to_py_cls(component_class);
883 if (!py_cls) {
12cd4490
PP
884 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
885 "Cannot find Python class associated to native component class: "
6945df9a
SM
886 "comp-cls-addr=%p", component_class);
887 goto error;
888 }
889
890 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
891 SWIGTYPE_p_bt_value, 0);
892 if (!py_params_ptr) {
12cd4490
PP
893 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
894 "Failed to create a SWIG pointer object.");
6945df9a
SM
895 goto error;
896 }
897
3c729b9a
PP
898 py_priv_query_exec_ptr = SWIG_NewPointerObj(
899 SWIG_as_voidptr(priv_query_executor),
900 SWIGTYPE_p_bt_private_query_executor, 0);
901 if (!py_priv_query_exec_ptr) {
12cd4490
PP
902 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
903 "Failed to create a SWIG pointer object.");
6945df9a
SM
904 goto error;
905 }
906
907 py_object = SWIG_FromCharPtr(object);
908 if (!py_object) {
12cd4490
PP
909 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
910 "Failed to create a Python string.");
6945df9a
SM
911 goto error;
912 }
913
914 py_results_addr = PyObject_CallMethod(py_cls,
3c729b9a
PP
915 "_bt_query_from_native", "(OOO)", py_priv_query_exec_ptr,
916 py_object, py_params_ptr);
6945df9a 917 if (!py_results_addr) {
12cd4490
PP
918 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
919 "Failed to call Python class's _bt_query_from_native() method: "
6945df9a 920 "py-cls-addr=%p", py_cls);
12cd4490
PP
921 status = py_exc_to_status_component_class(self_component_class,
922 log_level);
6945df9a
SM
923 goto end;
924 }
925
926 /*
927 * The returned object, on success, is an integer object
928 * (PyLong) containing the address of a BT value object (new
929 * reference).
930 */
babe0791 931 *result = PyLong_AsVoidPtr(py_results_addr);
6945df9a
SM
932 BT_ASSERT(!PyErr_Occurred());
933 BT_ASSERT(*result);
934 goto end;
935
936error:
937 PyErr_Clear();
d24d5663 938 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
939
940end:
941 Py_XDECREF(py_params_ptr);
3c729b9a 942 Py_XDECREF(py_priv_query_exec_ptr);
6945df9a
SM
943 Py_XDECREF(py_query_func);
944 Py_XDECREF(py_object);
945 Py_XDECREF(py_results_addr);
946 return status;
947}
948
7744859c 949static
d24d5663 950bt_component_class_query_method_status component_class_source_query(
6945df9a 951 bt_self_component_class_source *self_component_class_source,
3c729b9a 952 bt_private_query_executor *priv_query_executor,
6945df9a
SM
953 const char *object, const bt_value *params,
954 const bt_value **result)
955{
956 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
957 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
ce95fb26 958 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
d24d5663 959
3c729b9a
PP
960 return component_class_query(component_class, self_component_class,
961 priv_query_executor, object, params, result);
6945df9a
SM
962}
963
7744859c 964static
d24d5663 965bt_component_class_query_method_status component_class_filter_query(
6945df9a 966 bt_self_component_class_filter *self_component_class_filter,
3c729b9a 967 bt_private_query_executor *priv_query_executor,
6945df9a
SM
968 const char *object, const bt_value *params,
969 const bt_value **result)
970{
971 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
972 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
ce95fb26 973 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
d24d5663 974
3c729b9a
PP
975 return component_class_query(component_class, self_component_class,
976 priv_query_executor, object, params, result);
6945df9a
SM
977}
978
7744859c 979static
d24d5663 980bt_component_class_query_method_status component_class_sink_query(
6945df9a 981 bt_self_component_class_sink *self_component_class_sink,
3c729b9a 982 bt_private_query_executor *priv_query_executor,
6945df9a
SM
983 const char *object, const bt_value *params,
984 const bt_value **result)
985{
986 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
987 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
ce95fb26 988 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
d24d5663 989
3c729b9a
PP
990 return component_class_query(component_class, self_component_class,
991 priv_query_executor, object, params, result);
6945df9a
SM
992}
993
7744859c 994static
d24d5663
PP
995bt_component_class_message_iterator_init_method_status
996component_class_message_iterator_init(
6945df9a
SM
997 bt_self_message_iterator *self_message_iterator,
998 bt_self_component *self_component,
999 bt_self_component_port_output *self_component_port_output)
1000{
d24d5663 1001 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
1002 PyObject *py_comp_cls = NULL;
1003 PyObject *py_iter_cls = NULL;
1004 PyObject *py_iter_ptr = NULL;
c5f330cd 1005 PyObject *py_component_port_output_ptr = NULL;
6945df9a
SM
1006 PyObject *py_init_method_result = NULL;
1007 PyObject *py_iter = NULL;
1008 PyObject *py_comp;
12cd4490
PP
1009 bt_logging_level log_level = get_self_component_log_level(
1010 self_component);
6945df9a
SM
1011
1012 py_comp = bt_self_component_get_data(self_component);
1013
5602ef81 1014 /* Find user's Python message iterator class */
6945df9a
SM
1015 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
1016 if (!py_comp_cls) {
12cd4490
PP
1017 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1018 "Cannot get Python object's `__class__` attribute.");
ce95fb26 1019 goto python_error;
6945df9a
SM
1020 }
1021
1022 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
1023 if (!py_iter_cls) {
12cd4490
PP
1024 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1025 "Cannot get Python class's `_iter_cls` attribute.");
ce95fb26 1026 goto python_error;
6945df9a
SM
1027 }
1028
1029 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
1030 SWIGTYPE_p_bt_self_message_iterator, 0);
1031 if (!py_iter_ptr) {
ce95fb26
SM
1032 const char *err = "Failed to create a SWIG pointer object.";
1033
12cd4490
PP
1034 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1035 "%s", err);
ce95fb26
SM
1036 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1037 self_message_iterator, err);
6945df9a
SM
1038 goto error;
1039 }
1040
1041 /*
5602ef81 1042 * Create object with borrowed native message iterator
6945df9a
SM
1043 * reference:
1044 *
1045 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
1046 */
1047 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
1048 "(OO)", py_iter_cls, py_iter_ptr);
1049 if (!py_iter) {
12cd4490
PP
1050 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1051 "Failed to call Python class's __new__() method: "
6945df9a 1052 "py-cls-addr=%p", py_iter_cls);
ce95fb26 1053 goto python_error;
6945df9a
SM
1054 }
1055
1056 /*
1057 * Initialize object:
1058 *
c5f330cd
SM
1059 * py_iter.__init__(self_output_port)
1060 *
51f97fcc 1061 * through the _init_for_native helper static method.
6945df9a
SM
1062 *
1063 * At this point, py_iter._ptr is set, so this initialization
1064 * function has access to self._component (which gives it the
1065 * user Python component object from which the iterator was
1066 * created).
1067 */
51f97fcc
SM
1068 py_component_port_output_ptr = SWIG_NewPointerObj(
1069 SWIG_as_voidptr(self_component_port_output),
c5f330cd
SM
1070 SWIGTYPE_p_bt_self_component_port_output, 0);
1071 if (!py_component_port_output_ptr) {
ce95fb26
SM
1072 const char *err = "Failed to create a SWIG pointer object.";
1073
12cd4490
PP
1074 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1075 "%s", err);
ce95fb26
SM
1076 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1077 self_message_iterator, err);
c5f330cd
SM
1078 goto error;
1079 }
1080
d24d5663 1081 py_init_method_result = PyObject_CallMethod(py_iter,
85906b6b 1082 "_bt_init_from_native", "O", py_component_port_output_ptr);
6945df9a 1083 if (!py_init_method_result) {
12cd4490
PP
1084 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1085 "User's __init__() method failed:");
ce95fb26 1086 goto python_error;
6945df9a
SM
1087 }
1088
1089 /*
1090 * Since the Python code can never instantiate a user-defined
5602ef81
SM
1091 * message iterator class, the native message iterator
1092 * object does NOT belong to a user Python message iterator
6945df9a 1093 * object (borrowed reference). However this Python object is
5602ef81 1094 * owned by this native message iterator object.
6945df9a 1095 *
5602ef81
SM
1096 * In the Python world, the lifetime of the native message
1097 * iterator is managed by a _GenericMessageIterator
6945df9a
SM
1098 * instance:
1099 *
5602ef81
SM
1100 * _GenericMessageIterator instance:
1101 * owns a native bt_message_iterator object (iter)
6945df9a
SM
1102 * owns a _UserMessageIterator instance (py_iter)
1103 * self._ptr is a borrowed reference to the
5602ef81 1104 * native bt_private_connection_private_message_iterator
6945df9a
SM
1105 * object (iter)
1106 */
1107 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1108 py_iter = NULL;
1109 goto end;
1110
ce95fb26
SM
1111python_error:
1112 /* Handling of errors that cause a Python exception to be set. */
1113 status = py_exc_to_status_message_iterator(self_message_iterator);
1114 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
1115 goto end;
6945df9a 1116
ce95fb26
SM
1117error:
1118 /* Handling of errors that don't cause a Python exception to be set. */
1119 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
1120
1121end:
ce95fb26
SM
1122 BT_ASSERT(!PyErr_Occurred());
1123
6945df9a
SM
1124 Py_XDECREF(py_comp_cls);
1125 Py_XDECREF(py_iter_cls);
1126 Py_XDECREF(py_iter_ptr);
c5f330cd 1127 Py_XDECREF(py_component_port_output_ptr);
6945df9a
SM
1128 Py_XDECREF(py_init_method_result);
1129 Py_XDECREF(py_iter);
1130 return status;
1131}
1132
7744859c 1133static
d24d5663
PP
1134bt_component_class_message_iterator_init_method_status
1135component_class_source_message_iterator_init(
6945df9a
SM
1136 bt_self_message_iterator *self_message_iterator,
1137 bt_self_component_source *self_component_source,
1138 bt_self_component_port_output *self_component_port_output)
1139{
1140 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
d24d5663
PP
1141
1142 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6945df9a
SM
1143}
1144
7744859c 1145static
d24d5663
PP
1146bt_component_class_message_iterator_init_method_status
1147component_class_filter_message_iterator_init(
6945df9a
SM
1148 bt_self_message_iterator *self_message_iterator,
1149 bt_self_component_filter *self_component_filter,
1150 bt_self_component_port_output *self_component_port_output)
1151{
1152 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
d24d5663
PP
1153
1154 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6945df9a
SM
1155}
1156
7744859c 1157static
d24d5663 1158void component_class_message_iterator_finalize(
6945df9a
SM
1159 bt_self_message_iterator *message_iterator)
1160{
12cd4490
PP
1161 PyObject *py_message_iter = bt_self_message_iterator_get_data(
1162 message_iterator);
6945df9a
SM
1163 PyObject *py_method_result = NULL;
1164
1165 BT_ASSERT(py_message_iter);
1166
6a91742b 1167 /* Call user's _user_finalize() method */
6945df9a 1168 py_method_result = PyObject_CallMethod(py_message_iter,
6a91742b 1169 "_user_finalize", NULL);
6945df9a
SM
1170
1171 if (PyErr_Occurred()) {
12cd4490
PP
1172 bt_self_component *self_comp =
1173 bt_self_message_iterator_borrow_component(
1174 message_iterator);
1175 bt_logging_level log_level = get_self_component_log_level(
1176 self_comp);
1177
1178 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp,
1179 "User's _user_finalize() method raised an exception: ignoring:");
1180 logw_exception(get_self_message_iterator_log_level(
1181 message_iterator));
6945df9a
SM
1182 }
1183
1184 /*
6a91742b
PP
1185 * Ignore any exception raised by the _user_finalize() method
1186 * because it won't change anything at this point: the component
1187 * is being destroyed anyway.
6945df9a
SM
1188 */
1189 PyErr_Clear();
1190 Py_XDECREF(py_method_result);
1191 Py_DECREF(py_message_iter);
1192}
1193
1194/* Valid for both sources and filters. */
1195
7744859c 1196static
d24d5663
PP
1197bt_component_class_message_iterator_next_method_status
1198component_class_message_iterator_next(
7744859c
SM
1199 bt_self_message_iterator *message_iterator,
1200 bt_message_array_const msgs, uint64_t capacity,
1201 uint64_t *count)
6945df9a 1202{
d24d5663 1203 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
1204 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1205 PyObject *py_method_result = NULL;
1206
1207 BT_ASSERT(py_message_iter);
1208 py_method_result = PyObject_CallMethod(py_message_iter,
85906b6b 1209 "_bt_next_from_native", NULL);
6945df9a 1210 if (!py_method_result) {
ce95fb26 1211 status = py_exc_to_status_message_iterator(message_iterator);
d24d5663 1212 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
6945df9a
SM
1213 goto end;
1214 }
1215
1216 /*
1217 * The returned object, on success, is an integer object
5602ef81 1218 * (PyLong) containing the address of a native message
6945df9a
SM
1219 * object (which is now ours).
1220 */
babe0791 1221 msgs[0] = PyLong_AsVoidPtr(py_method_result);
6945df9a
SM
1222 *count = 1;
1223
1224 /* Clear potential overflow error; should never happen */
1225 BT_ASSERT(!PyErr_Occurred());
1226 goto end;
1227
1228end:
1229 Py_XDECREF(py_method_result);
1230 return status;
1231}
1232
7744859c 1233static
d24d5663
PP
1234bt_component_class_sink_consume_method_status
1235component_class_sink_consume(bt_self_component_sink *self_component_sink)
6945df9a
SM
1236{
1237 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1238 PyObject *py_comp = bt_self_component_get_data(self_component);
1239 PyObject *py_method_result = NULL;
d24d5663 1240 bt_component_class_sink_consume_method_status status;
6945df9a
SM
1241
1242 BT_ASSERT(py_comp);
1243 py_method_result = PyObject_CallMethod(py_comp,
6a91742b 1244 "_user_consume", NULL);
ce95fb26 1245 status = py_exc_to_status_component(self_component);
12cd4490 1246 BT_ASSERT(py_method_result || status != __BT_FUNC_STATUS_OK);
6945df9a
SM
1247 Py_XDECREF(py_method_result);
1248 return status;
1249}
1250
1251static
d24d5663 1252int component_class_set_help_and_desc(
6945df9a
SM
1253 bt_component_class *component_class,
1254 const char *description, const char *help)
1255{
1256 int ret;
1257
1258 if (description) {
1259 ret = bt_component_class_set_description(component_class, description);
1260 if (ret) {
1261 BT_LOGE("Cannot set component class's description: "
1262 "comp-cls-addr=%p", component_class);
1263 goto end;
1264 }
1265 }
1266
1267 if (help) {
1268 ret = bt_component_class_set_help(component_class, help);
1269 if (ret) {
1270 BT_LOGE("Cannot set component class's help text: "
1271 "comp-cls-addr=%p", component_class);
1272 goto end;
1273 }
1274 }
1275
1276 ret = 0;
1277
1278end:
1279 return ret;
1280}
1281
1282static
d24d5663 1283bt_component_class_source *bt_bt2_component_class_source_create(
6945df9a
SM
1284 PyObject *py_cls, const char *name, const char *description,
1285 const char *help)
1286{
1287 bt_component_class_source *component_class_source;
1288 bt_component_class *component_class;
1289 int ret;
1290
1291 BT_ASSERT(py_cls);
6945df9a 1292 component_class_source = bt_component_class_source_create(name,
d24d5663 1293 component_class_message_iterator_next);
6945df9a
SM
1294 if (!component_class_source) {
1295 BT_LOGE_STR("Cannot create source component class.");
1296 goto end;
1297 }
1298
1299 component_class = bt_component_class_source_as_component_class(component_class_source);
1300
d24d5663 1301 if (component_class_set_help_and_desc(component_class, description, help)) {
6945df9a
SM
1302 goto end;
1303 }
1304
d24d5663 1305 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
6945df9a 1306 BT_ASSERT(ret == 0);
d24d5663 1307 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
f00b8d40
SM
1308 BT_ASSERT(ret == 0);
1309 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
d24d5663 1310 component_class_can_seek_beginning);
f00b8d40
SM
1311 BT_ASSERT(ret == 0);
1312 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
d24d5663 1313 component_class_seek_beginning);
6945df9a 1314 BT_ASSERT(ret == 0);
6945df9a 1315 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
d24d5663 1316 component_class_source_output_port_connected);
6945df9a 1317 BT_ASSERT(ret == 0);
d24d5663 1318 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
6945df9a
SM
1319 BT_ASSERT(ret == 0);
1320 ret = bt_component_class_source_set_message_iterator_init_method(
d24d5663 1321 component_class_source, component_class_source_message_iterator_init);
6945df9a
SM
1322 BT_ASSERT(ret == 0);
1323 ret = bt_component_class_source_set_message_iterator_finalize_method(
d24d5663 1324 component_class_source, component_class_message_iterator_finalize);
6945df9a 1325 BT_ASSERT(ret == 0);
6945df9a
SM
1326 register_cc_ptr_to_py_cls(component_class, py_cls);
1327
1328end:
1329 return component_class_source;
1330}
1331
1332static
d24d5663 1333bt_component_class_filter *bt_bt2_component_class_filter_create(
6945df9a
SM
1334 PyObject *py_cls, const char *name, const char *description,
1335 const char *help)
1336{
1337 bt_component_class *component_class;
1338 bt_component_class_filter *component_class_filter;
1339 int ret;
1340
1341 BT_ASSERT(py_cls);
6945df9a 1342 component_class_filter = bt_component_class_filter_create(name,
d24d5663 1343 component_class_message_iterator_next);
6945df9a
SM
1344 if (!component_class_filter) {
1345 BT_LOGE_STR("Cannot create filter component class.");
1346 goto end;
1347 }
1348
1349 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1350
d24d5663 1351 if (component_class_set_help_and_desc(component_class, description, help)) {
6945df9a
SM
1352 goto end;
1353 }
1354
d24d5663 1355 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
6945df9a 1356 BT_ASSERT(ret == 0);
d24d5663 1357 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
6945df9a 1358 BT_ASSERT(ret == 0);
f00b8d40 1359 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
d24d5663 1360 component_class_can_seek_beginning);
f00b8d40
SM
1361 BT_ASSERT(ret == 0);
1362 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
d24d5663 1363 component_class_seek_beginning);
f00b8d40 1364 BT_ASSERT(ret == 0);
6945df9a 1365 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
d24d5663 1366 component_class_filter_input_port_connected);
6945df9a
SM
1367 BT_ASSERT(ret == 0);
1368 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
d24d5663 1369 component_class_filter_output_port_connected);
6945df9a 1370 BT_ASSERT(ret == 0);
d24d5663 1371 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
6945df9a
SM
1372 BT_ASSERT(ret == 0);
1373 ret = bt_component_class_filter_set_message_iterator_init_method(
d24d5663 1374 component_class_filter, component_class_filter_message_iterator_init);
6945df9a
SM
1375 BT_ASSERT(ret == 0);
1376 ret = bt_component_class_filter_set_message_iterator_finalize_method(
d24d5663 1377 component_class_filter, component_class_message_iterator_finalize);
6945df9a 1378 BT_ASSERT(ret == 0);
6945df9a
SM
1379 register_cc_ptr_to_py_cls(component_class, py_cls);
1380
1381end:
1382 return component_class_filter;
1383}
1384
1385static
d24d5663 1386bt_component_class_sink *bt_bt2_component_class_sink_create(
6945df9a
SM
1387 PyObject *py_cls, const char *name, const char *description,
1388 const char *help)
1389{
1390 bt_component_class_sink *component_class_sink;
1391 bt_component_class *component_class;
1392 int ret;
1393
1394 BT_ASSERT(py_cls);
d24d5663 1395 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
6945df9a
SM
1396
1397 if (!component_class_sink) {
1398 BT_LOGE_STR("Cannot create sink component class.");
1399 goto end;
1400 }
1401
1402 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1403
d24d5663 1404 if (component_class_set_help_and_desc(component_class, description, help)) {
6945df9a
SM
1405 goto end;
1406 }
1407
d24d5663 1408 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
6945df9a 1409 BT_ASSERT(ret == 0);
d24d5663 1410 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
6945df9a 1411 BT_ASSERT(ret == 0);
6945df9a 1412 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
d24d5663 1413 component_class_sink_input_port_connected);
6945df9a
SM
1414 BT_ASSERT(ret == 0);
1415 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
d24d5663 1416 component_class_sink_graph_is_configured);
6945df9a 1417 BT_ASSERT(ret == 0);
d24d5663 1418 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
6945df9a 1419 BT_ASSERT(ret == 0);
6945df9a
SM
1420 register_cc_ptr_to_py_cls(component_class, py_cls);
1421
1422end:
1423 return component_class_sink;
1424}
1425%}
1426
d24d5663 1427struct bt_component_class_source *bt_bt2_component_class_source_create(
6945df9a
SM
1428 PyObject *py_cls, const char *name, const char *description,
1429 const char *help);
d24d5663 1430struct bt_component_class_filter *bt_bt2_component_class_filter_create(
6945df9a
SM
1431 PyObject *py_cls, const char *name, const char *description,
1432 const char *help);
d24d5663 1433struct bt_component_class_sink *bt_bt2_component_class_sink_create(
6945df9a
SM
1434 PyObject *py_cls, const char *name, const char *description,
1435 const char *help);
d24d5663
PP
1436void bt_bt2_cc_init_from_bt2(void);
1437void bt_bt2_cc_exit_handler(void);
This page took 0.100961 seconds and 4 git commands to generate.