lib: prepare the ground for stateful query operations
[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,
bf403eb2 865 bt_private_query_executor *priv_query_executor,
6fa6bfa1
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;
bf403eb2 871 PyObject *py_priv_query_exec_ptr = NULL;
6fa6bfa1
SM
872 PyObject *py_query_func = NULL;
873 PyObject *py_object = NULL;
874 PyObject *py_results_addr = NULL;
fb25b9e3 875 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
bf403eb2
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);
6fa6bfa1
SM
881
882 py_cls = lookup_cc_ptr_to_py_cls(component_class);
883 if (!py_cls) {
8d6ef9df
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: "
6fa6bfa1
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) {
8d6ef9df
PP
893 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
894 "Failed to create a SWIG pointer object.");
6fa6bfa1
SM
895 goto error;
896 }
897
bf403eb2
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) {
8d6ef9df
PP
902 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
903 "Failed to create a SWIG pointer object.");
6fa6bfa1
SM
904 goto error;
905 }
906
907 py_object = SWIG_FromCharPtr(object);
908 if (!py_object) {
8d6ef9df
PP
909 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
910 "Failed to create a Python string.");
6fa6bfa1
SM
911 goto error;
912 }
913
914 py_results_addr = PyObject_CallMethod(py_cls,
bf403eb2
PP
915 "_bt_query_from_native", "(OOO)", py_priv_query_exec_ptr,
916 py_object, py_params_ptr);
6fa6bfa1 917 if (!py_results_addr) {
8d6ef9df
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: "
6fa6bfa1 920 "py-cls-addr=%p", py_cls);
8d6ef9df
PP
921 status = py_exc_to_status_component_class(self_component_class,
922 log_level);
6fa6bfa1
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 */
c240a5d8 931 *result = PyLong_AsVoidPtr(py_results_addr);
6fa6bfa1
SM
932 BT_ASSERT(!PyErr_Occurred());
933 BT_ASSERT(*result);
934 goto end;
935
936error:
937 PyErr_Clear();
fb25b9e3 938 status = __BT_FUNC_STATUS_ERROR;
6fa6bfa1
SM
939
940end:
941 Py_XDECREF(py_params_ptr);
bf403eb2 942 Py_XDECREF(py_priv_query_exec_ptr);
6fa6bfa1
SM
943 Py_XDECREF(py_query_func);
944 Py_XDECREF(py_object);
945 Py_XDECREF(py_results_addr);
946 return status;
947}
948
efb177a7 949static
fb25b9e3 950bt_component_class_query_method_status component_class_source_query(
6fa6bfa1 951 bt_self_component_class_source *self_component_class_source,
bf403eb2 952 bt_private_query_executor *priv_query_executor,
6fa6bfa1
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);
f8d71b59 958 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
fb25b9e3 959
bf403eb2
PP
960 return component_class_query(component_class, self_component_class,
961 priv_query_executor, object, params, result);
6fa6bfa1
SM
962}
963
efb177a7 964static
fb25b9e3 965bt_component_class_query_method_status component_class_filter_query(
6fa6bfa1 966 bt_self_component_class_filter *self_component_class_filter,
bf403eb2 967 bt_private_query_executor *priv_query_executor,
6fa6bfa1
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);
f8d71b59 973 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
fb25b9e3 974
bf403eb2
PP
975 return component_class_query(component_class, self_component_class,
976 priv_query_executor, object, params, result);
6fa6bfa1
SM
977}
978
efb177a7 979static
fb25b9e3 980bt_component_class_query_method_status component_class_sink_query(
6fa6bfa1 981 bt_self_component_class_sink *self_component_class_sink,
bf403eb2 982 bt_private_query_executor *priv_query_executor,
6fa6bfa1
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);
f8d71b59 988 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
fb25b9e3 989
bf403eb2
PP
990 return component_class_query(component_class, self_component_class,
991 priv_query_executor, object, params, result);
6fa6bfa1
SM
992}
993
efb177a7 994static
fb25b9e3
PP
995bt_component_class_message_iterator_init_method_status
996component_class_message_iterator_init(
6fa6bfa1
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{
fb25b9e3 1001 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
6fa6bfa1
SM
1002 PyObject *py_comp_cls = NULL;
1003 PyObject *py_iter_cls = NULL;
1004 PyObject *py_iter_ptr = NULL;
a4dcfa96 1005 PyObject *py_component_port_output_ptr = NULL;
6fa6bfa1
SM
1006 PyObject *py_init_method_result = NULL;
1007 PyObject *py_iter = NULL;
1008 PyObject *py_comp;
8d6ef9df
PP
1009 bt_logging_level log_level = get_self_component_log_level(
1010 self_component);
6fa6bfa1
SM
1011
1012 py_comp = bt_self_component_get_data(self_component);
1013
fa4c33e3 1014 /* Find user's Python message iterator class */
6fa6bfa1
SM
1015 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
1016 if (!py_comp_cls) {
8d6ef9df
PP
1017 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1018 "Cannot get Python object's `__class__` attribute.");
f8d71b59 1019 goto python_error;
6fa6bfa1
SM
1020 }
1021
1022 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
1023 if (!py_iter_cls) {
8d6ef9df
PP
1024 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1025 "Cannot get Python class's `_iter_cls` attribute.");
f8d71b59 1026 goto python_error;
6fa6bfa1
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) {
f8d71b59
SM
1032 const char *err = "Failed to create a SWIG pointer object.";
1033
8d6ef9df
PP
1034 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1035 "%s", err);
f8d71b59
SM
1036 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1037 self_message_iterator, err);
6fa6bfa1
SM
1038 goto error;
1039 }
1040
1041 /*
fa4c33e3 1042 * Create object with borrowed native message iterator
6fa6bfa1
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) {
8d6ef9df
PP
1050 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1051 "Failed to call Python class's __new__() method: "
6fa6bfa1 1052 "py-cls-addr=%p", py_iter_cls);
f8d71b59 1053 goto python_error;
6fa6bfa1
SM
1054 }
1055
1056 /*
1057 * Initialize object:
1058 *
a4dcfa96
SM
1059 * py_iter.__init__(self_output_port)
1060 *
3e2b0fcf 1061 * through the _init_for_native helper static method.
6fa6bfa1
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 */
3e2b0fcf
SM
1068 py_component_port_output_ptr = SWIG_NewPointerObj(
1069 SWIG_as_voidptr(self_component_port_output),
a4dcfa96
SM
1070 SWIGTYPE_p_bt_self_component_port_output, 0);
1071 if (!py_component_port_output_ptr) {
f8d71b59
SM
1072 const char *err = "Failed to create a SWIG pointer object.";
1073
8d6ef9df
PP
1074 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1075 "%s", err);
f8d71b59
SM
1076 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1077 self_message_iterator, err);
a4dcfa96
SM
1078 goto error;
1079 }
1080
fb25b9e3 1081 py_init_method_result = PyObject_CallMethod(py_iter,
deec48a6 1082 "_bt_init_from_native", "O", py_component_port_output_ptr);
6fa6bfa1 1083 if (!py_init_method_result) {
8d6ef9df
PP
1084 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1085 "User's __init__() method failed:");
f8d71b59 1086 goto python_error;
6fa6bfa1
SM
1087 }
1088
1089 /*
1090 * Since the Python code can never instantiate a user-defined
fa4c33e3
SM
1091 * message iterator class, the native message iterator
1092 * object does NOT belong to a user Python message iterator
6fa6bfa1 1093 * object (borrowed reference). However this Python object is
fa4c33e3 1094 * owned by this native message iterator object.
6fa6bfa1 1095 *
fa4c33e3
SM
1096 * In the Python world, the lifetime of the native message
1097 * iterator is managed by a _GenericMessageIterator
6fa6bfa1
SM
1098 * instance:
1099 *
fa4c33e3
SM
1100 * _GenericMessageIterator instance:
1101 * owns a native bt_message_iterator object (iter)
6fa6bfa1
SM
1102 * owns a _UserMessageIterator instance (py_iter)
1103 * self._ptr is a borrowed reference to the
fa4c33e3 1104 * native bt_private_connection_private_message_iterator
6fa6bfa1
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
f8d71b59
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;
6fa6bfa1 1116
f8d71b59
SM
1117error:
1118 /* Handling of errors that don't cause a Python exception to be set. */
1119 status = __BT_FUNC_STATUS_ERROR;
6fa6bfa1
SM
1120
1121end:
f8d71b59
SM
1122 BT_ASSERT(!PyErr_Occurred());
1123
6fa6bfa1
SM
1124 Py_XDECREF(py_comp_cls);
1125 Py_XDECREF(py_iter_cls);
1126 Py_XDECREF(py_iter_ptr);
a4dcfa96 1127 Py_XDECREF(py_component_port_output_ptr);
6fa6bfa1
SM
1128 Py_XDECREF(py_init_method_result);
1129 Py_XDECREF(py_iter);
1130 return status;
1131}
1132
efb177a7 1133static
fb25b9e3
PP
1134bt_component_class_message_iterator_init_method_status
1135component_class_source_message_iterator_init(
6fa6bfa1
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);
fb25b9e3
PP
1141
1142 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6fa6bfa1
SM
1143}
1144
efb177a7 1145static
fb25b9e3
PP
1146bt_component_class_message_iterator_init_method_status
1147component_class_filter_message_iterator_init(
6fa6bfa1
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);
fb25b9e3
PP
1153
1154 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6fa6bfa1
SM
1155}
1156
efb177a7 1157static
fb25b9e3 1158void component_class_message_iterator_finalize(
6fa6bfa1
SM
1159 bt_self_message_iterator *message_iterator)
1160{
8d6ef9df
PP
1161 PyObject *py_message_iter = bt_self_message_iterator_get_data(
1162 message_iterator);
6fa6bfa1
SM
1163 PyObject *py_method_result = NULL;
1164
1165 BT_ASSERT(py_message_iter);
1166
819d0ae7 1167 /* Call user's _user_finalize() method */
6fa6bfa1 1168 py_method_result = PyObject_CallMethod(py_message_iter,
819d0ae7 1169 "_user_finalize", NULL);
6fa6bfa1
SM
1170
1171 if (PyErr_Occurred()) {
8d6ef9df
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));
6fa6bfa1
SM
1182 }
1183
1184 /*
819d0ae7
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.
6fa6bfa1
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
efb177a7 1196static
fb25b9e3
PP
1197bt_component_class_message_iterator_next_method_status
1198component_class_message_iterator_next(
efb177a7
SM
1199 bt_self_message_iterator *message_iterator,
1200 bt_message_array_const msgs, uint64_t capacity,
1201 uint64_t *count)
6fa6bfa1 1202{
fb25b9e3 1203 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
6fa6bfa1
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,
deec48a6 1209 "_bt_next_from_native", NULL);
6fa6bfa1 1210 if (!py_method_result) {
f8d71b59 1211 status = py_exc_to_status_message_iterator(message_iterator);
fb25b9e3 1212 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
6fa6bfa1
SM
1213 goto end;
1214 }
1215
1216 /*
1217 * The returned object, on success, is an integer object
fa4c33e3 1218 * (PyLong) containing the address of a native message
6fa6bfa1
SM
1219 * object (which is now ours).
1220 */
c240a5d8 1221 msgs[0] = PyLong_AsVoidPtr(py_method_result);
6fa6bfa1
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
efb177a7 1233static
fb25b9e3
PP
1234bt_component_class_sink_consume_method_status
1235component_class_sink_consume(bt_self_component_sink *self_component_sink)
6fa6bfa1
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;
fb25b9e3 1240 bt_component_class_sink_consume_method_status status;
6fa6bfa1
SM
1241
1242 BT_ASSERT(py_comp);
1243 py_method_result = PyObject_CallMethod(py_comp,
819d0ae7 1244 "_user_consume", NULL);
f8d71b59 1245 status = py_exc_to_status_component(self_component);
8d6ef9df 1246 BT_ASSERT(py_method_result || status != __BT_FUNC_STATUS_OK);
6fa6bfa1
SM
1247 Py_XDECREF(py_method_result);
1248 return status;
1249}
1250
1251static
fb25b9e3 1252int component_class_set_help_and_desc(
6fa6bfa1
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
fb25b9e3 1283bt_component_class_source *bt_bt2_component_class_source_create(
6fa6bfa1
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);
6fa6bfa1 1292 component_class_source = bt_component_class_source_create(name,
fb25b9e3 1293 component_class_message_iterator_next);
6fa6bfa1
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
fb25b9e3 1301 if (component_class_set_help_and_desc(component_class, description, help)) {
6fa6bfa1
SM
1302 goto end;
1303 }
1304
fb25b9e3 1305 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
6fa6bfa1 1306 BT_ASSERT(ret == 0);
fb25b9e3 1307 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
39ddfa44
SM
1308 BT_ASSERT(ret == 0);
1309 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
fb25b9e3 1310 component_class_can_seek_beginning);
39ddfa44
SM
1311 BT_ASSERT(ret == 0);
1312 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
fb25b9e3 1313 component_class_seek_beginning);
6fa6bfa1 1314 BT_ASSERT(ret == 0);
6fa6bfa1 1315 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
fb25b9e3 1316 component_class_source_output_port_connected);
6fa6bfa1 1317 BT_ASSERT(ret == 0);
fb25b9e3 1318 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
6fa6bfa1
SM
1319 BT_ASSERT(ret == 0);
1320 ret = bt_component_class_source_set_message_iterator_init_method(
fb25b9e3 1321 component_class_source, component_class_source_message_iterator_init);
6fa6bfa1
SM
1322 BT_ASSERT(ret == 0);
1323 ret = bt_component_class_source_set_message_iterator_finalize_method(
fb25b9e3 1324 component_class_source, component_class_message_iterator_finalize);
6fa6bfa1 1325 BT_ASSERT(ret == 0);
6fa6bfa1
SM
1326 register_cc_ptr_to_py_cls(component_class, py_cls);
1327
1328end:
1329 return component_class_source;
1330}
1331
1332static
fb25b9e3 1333bt_component_class_filter *bt_bt2_component_class_filter_create(
6fa6bfa1
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);
6fa6bfa1 1342 component_class_filter = bt_component_class_filter_create(name,
fb25b9e3 1343 component_class_message_iterator_next);
6fa6bfa1
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
fb25b9e3 1351 if (component_class_set_help_and_desc(component_class, description, help)) {
6fa6bfa1
SM
1352 goto end;
1353 }
1354
fb25b9e3 1355 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
6fa6bfa1 1356 BT_ASSERT(ret == 0);
fb25b9e3 1357 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
6fa6bfa1 1358 BT_ASSERT(ret == 0);
39ddfa44 1359 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
fb25b9e3 1360 component_class_can_seek_beginning);
39ddfa44
SM
1361 BT_ASSERT(ret == 0);
1362 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
fb25b9e3 1363 component_class_seek_beginning);
39ddfa44 1364 BT_ASSERT(ret == 0);
6fa6bfa1 1365 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
fb25b9e3 1366 component_class_filter_input_port_connected);
6fa6bfa1
SM
1367 BT_ASSERT(ret == 0);
1368 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
fb25b9e3 1369 component_class_filter_output_port_connected);
6fa6bfa1 1370 BT_ASSERT(ret == 0);
fb25b9e3 1371 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
6fa6bfa1
SM
1372 BT_ASSERT(ret == 0);
1373 ret = bt_component_class_filter_set_message_iterator_init_method(
fb25b9e3 1374 component_class_filter, component_class_filter_message_iterator_init);
6fa6bfa1
SM
1375 BT_ASSERT(ret == 0);
1376 ret = bt_component_class_filter_set_message_iterator_finalize_method(
fb25b9e3 1377 component_class_filter, component_class_message_iterator_finalize);
6fa6bfa1 1378 BT_ASSERT(ret == 0);
6fa6bfa1
SM
1379 register_cc_ptr_to_py_cls(component_class, py_cls);
1380
1381end:
1382 return component_class_filter;
1383}
1384
1385static
fb25b9e3 1386bt_component_class_sink *bt_bt2_component_class_sink_create(
6fa6bfa1
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);
fb25b9e3 1395 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
6fa6bfa1
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
fb25b9e3 1404 if (component_class_set_help_and_desc(component_class, description, help)) {
6fa6bfa1
SM
1405 goto end;
1406 }
1407
fb25b9e3 1408 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
6fa6bfa1 1409 BT_ASSERT(ret == 0);
fb25b9e3 1410 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
6fa6bfa1 1411 BT_ASSERT(ret == 0);
6fa6bfa1 1412 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
fb25b9e3 1413 component_class_sink_input_port_connected);
6fa6bfa1
SM
1414 BT_ASSERT(ret == 0);
1415 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
fb25b9e3 1416 component_class_sink_graph_is_configured);
6fa6bfa1 1417 BT_ASSERT(ret == 0);
fb25b9e3 1418 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
6fa6bfa1 1419 BT_ASSERT(ret == 0);
6fa6bfa1
SM
1420 register_cc_ptr_to_py_cls(component_class, py_cls);
1421
1422end:
1423 return component_class_sink;
1424}
1425%}
1426
fb25b9e3 1427struct bt_component_class_source *bt_bt2_component_class_source_create(
6fa6bfa1
SM
1428 PyObject *py_cls, const char *name, const char *description,
1429 const char *help);
fb25b9e3 1430struct bt_component_class_filter *bt_bt2_component_class_filter_create(
6fa6bfa1
SM
1431 PyObject *py_cls, const char *name, const char *description,
1432 const char *help);
fb25b9e3 1433struct bt_component_class_sink *bt_bt2_component_class_sink_create(
6fa6bfa1
SM
1434 PyObject *py_cls, const char *name, const char *description,
1435 const char *help);
fb25b9e3
PP
1436void bt_bt2_cc_init_from_bt2(void);
1437void bt_bt2_cc_exit_handler(void);
This page took 0.100502 seconds and 4 git commands to generate.