Move to kernel style SPDX license identifiers
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i.h
CommitLineData
4212232c 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
4212232c
PP
3 *
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4212232c
PP
5 */
6
7#include "logging/comp-logging.h"
66964f3f 8#include "compat/glib.h"
4212232c
PP
9
10/*
11 * This hash table associates a BT component class object address to a
12 * user-defined Python class (PyObject *). The keys and values are NOT
13 * owned by this hash table. The Python class objects are owned by the
14 * Python module, which should not be unloaded until it is not possible
15 * to create a user Python component anyway.
16 *
17 * This hash table is written to when a user-defined Python component
18 * class is created by one of the bt_bt2_component_class_*_create()
19 * functions.
20 *
21 * This function is read from when a user calls bt_component_create()
22 * with a component class pointer created by one of the functions above.
23 * In this case, the original Python class needs to be found to
24 * instantiate it and associate the created Python component object with
25 * a BT component object instance.
26 */
27
8b305066
SM
28#define BT_FMT_SWIG_ALLOC_FAILED "Failed to create a SWIG pointer object."
29
4212232c
PP
30static GHashTable *bt_cc_ptr_to_py_cls;
31
ab1cea3f
PP
32static
33void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class *comp_cls)
34{
35 gboolean existed;
36
37 if (!bt_cc_ptr_to_py_cls) {
38 return;
39 }
40
41 existed = g_hash_table_remove(bt_cc_ptr_to_py_cls, comp_cls);
42 BT_ASSERT(existed);
43}
44
4212232c
PP
45static
46void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
47 PyObject *py_cls)
48{
49 if (!bt_cc_ptr_to_py_cls) {
50 /*
51 * Lazy-initializing this GHashTable because GLib
52 * might not be initialized yet and it needs to be
53 * before we call g_hash_table_new()
54 */
55 BT_LOGD_STR("Creating native component class to Python component class hash table.");
56 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
57 BT_ASSERT(bt_cc_ptr_to_py_cls);
58 }
59
60 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
61 (gpointer) py_cls);
62}
63
64static
65PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
66{
67 if (!bt_cc_ptr_to_py_cls) {
68 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
69 "comp-cls-addr=%p", bt_cc);
70 return NULL;
71 }
72
73 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
74 (gconstpointer) bt_cc);
75}
76
4212232c
PP
77/* Library destructor */
78
79__attribute__((destructor))
80static
81void native_comp_class_dtor(void) {
82 /* Destroy component class association hash table */
83 if (bt_cc_ptr_to_py_cls) {
84 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
85 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
ab1cea3f 86 bt_cc_ptr_to_py_cls = NULL;
4212232c
PP
87 }
88}
89
4212232c 90static inline
84e438af
SM
91int py_exc_to_status_clear(
92 bt_self_component_class *self_component_class,
4212232c
PP
93 bt_self_component *self_component,
94 bt_self_message_iterator *self_message_iterator,
95 const char *module_name, int active_log_level)
96{
6dbc1938 97 int status;
4212232c
PP
98 PyObject *exc = PyErr_Occurred();
99
100 if (!exc) {
6dbc1938 101 status = __BT_FUNC_STATUS_OK;
4212232c
PP
102 goto end;
103 }
104
105 if (PyErr_GivenExceptionMatches(exc,
106 py_mod_bt2_exc_try_again_type)) {
107 status = __BT_FUNC_STATUS_AGAIN;
108 } else if (PyErr_GivenExceptionMatches(exc,
109 py_mod_bt2_exc_stop_type)) {
110 status = __BT_FUNC_STATUS_END;
111 } else if (PyErr_GivenExceptionMatches(exc,
112 py_mod_bt2_exc_unknown_object_type)) {
113 status = __BT_FUNC_STATUS_UNKNOWN_OBJECT;
114 } else {
115 /*
116 * Unknown exception: convert to general error.
117 *
118 * Because we only want to fetch the log level when
119 * we actually get an exception, and not systematically
120 * when we call py_exc_to_status() (as py_exc_to_status()
121 * can return `__BT_FUNC_STATUS_OK`), we get it here
122 * depending on the actor's type.
123 */
124 if (self_component) {
125 active_log_level = get_self_component_log_level(
126 self_component);
127 } else if (self_message_iterator) {
128 active_log_level = get_self_message_iterator_log_level(
129 self_message_iterator);
130 }
131
132 BT_ASSERT(active_log_level != -1);
84e438af 133 log_exception_and_maybe_append_cause(BT_LOG_WARNING,
4212232c
PP
134 active_log_level, true,
135 self_component_class, self_component,
136 self_message_iterator, module_name);
137
138 if (PyErr_GivenExceptionMatches(exc,
139 py_mod_bt2_exc_memory_error)) {
140 status = __BT_FUNC_STATUS_MEMORY_ERROR;
141 } else {
142 status = __BT_FUNC_STATUS_ERROR;
143 }
144 }
145
146end:
147 PyErr_Clear();
148 return status;
149}
150
151static
84e438af 152int py_exc_to_status_component_class_clear(
4212232c
PP
153 bt_self_component_class *self_component_class,
154 int active_log_level)
155{
84e438af 156 return py_exc_to_status_clear(self_component_class, NULL, NULL, NULL,
4212232c
PP
157 active_log_level);
158}
159
160static
84e438af 161int py_exc_to_status_component_clear(bt_self_component *self_component)
4212232c 162{
84e438af 163 return py_exc_to_status_clear(NULL, self_component, NULL, NULL, -1);
4212232c
PP
164}
165
166static
84e438af 167int py_exc_to_status_message_iterator_clear(
4212232c
PP
168 bt_self_message_iterator *self_message_iterator)
169{
84e438af 170 return py_exc_to_status_clear(NULL, NULL, self_message_iterator, NULL, -1);
4212232c
PP
171}
172
66964f3f
PP
173static
174bool bt_bt2_is_python_component_class(const bt_component_class *comp_cls)
175{
176 return bt_g_hash_table_contains(bt_cc_ptr_to_py_cls, comp_cls);
177}
178
4212232c
PP
179/* Component class proxy methods (delegate to the attached Python object) */
180
181static
21a9f056 182bt_component_class_initialize_method_status component_class_init(
4212232c
PP
183 bt_self_component *self_component,
184 void *self_component_v,
185 swig_type_info *self_comp_cls_type_swig_type,
186 const bt_value *params,
187 void *init_method_data)
188{
189 const bt_component *component = bt_self_component_as_component(self_component);
190 const bt_component_class *component_class = bt_component_borrow_class_const(component);
6dbc1938 191 bt_component_class_initialize_method_status status;
4212232c
PP
192 PyObject *py_cls = NULL;
193 PyObject *py_comp = NULL;
194 PyObject *py_params_ptr = NULL;
195 PyObject *py_comp_ptr = NULL;
196 bt_logging_level log_level = get_self_component_log_level(
197 self_component);
198
4212232c
PP
199 BT_ASSERT(self_component);
200 BT_ASSERT(self_component_v);
201 BT_ASSERT(self_comp_cls_type_swig_type);
202
203 /*
204 * Get the user-defined Python class which created this
205 * component's class in the first place (borrowed
206 * reference).
207 */
208 py_cls = lookup_cc_ptr_to_py_cls(component_class);
209 if (!py_cls) {
210 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
211 "Cannot find Python class associated to native component class: "
212 "comp-cls-addr=%p", component_class);
213 goto error;
214 }
215
216 /* Parameters pointer -> SWIG pointer Python object */
217 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
218 SWIGTYPE_p_bt_value, 0);
219 if (!py_params_ptr) {
220 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
8b305066 221 BT_FMT_SWIG_ALLOC_FAILED);
4212232c
PP
222 goto error;
223 }
224
225 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
226 self_comp_cls_type_swig_type, 0);
227 if (!py_comp_ptr) {
228 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
8b305066 229 BT_FMT_SWIG_ALLOC_FAILED);
4212232c
PP
230 goto error;
231 }
232
233 /*
234 * Do the equivalent of this:
235 *
66964f3f
PP
236 * py_comp = py_cls._bt_init_from_native(py_comp_ptr,
237 * py_params_ptr, init_method_data ? init_method_data : Py_None)
4212232c
PP
238 *
239 * _UserComponentType._bt_init_from_native() calls the Python
240 * component object's __init__() function.
66964f3f
PP
241 *
242 * We don't take any reference on `init_method_data` which, if
243 * not `NULL`, is assumed to be a `PyObject *`: the user's
244 * __init__() function will eventually take a reference if
245 * needed. If `init_method_data` is `NULL`, then we pass
246 * `Py_None` as the initialization's Python object.
4212232c
PP
247 */
248 py_comp = PyObject_CallMethod(py_cls,
66964f3f
PP
249 "_bt_init_from_native", "(OOO)", py_comp_ptr, py_params_ptr,
250 init_method_data ? init_method_data : Py_None);
4212232c
PP
251 if (!py_comp) {
252 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component,
253 "Failed to call Python class's _bt_init_from_native() method: "
254 "py-cls-addr=%p", py_cls);
84e438af 255 status = py_exc_to_status_component_clear(self_component);
4212232c
PP
256 goto end;
257 }
258
259 /*
260 * Our user Python component object is now fully created and
261 * initialized by the user. Since we just created it, this
262 * native component is its only (persistent) owner.
263 */
264 bt_self_component_set_data(self_component, py_comp);
265 py_comp = NULL;
6dbc1938
SM
266
267 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
268
4212232c
PP
269 goto end;
270
271error:
6dbc1938
SM
272 /* This error path is for non-Python errors only. */
273 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
4212232c
PP
274
275end:
6dbc1938 276 BT_ASSERT(!PyErr_Occurred());
4212232c
PP
277 Py_XDECREF(py_comp);
278 Py_XDECREF(py_params_ptr);
279 Py_XDECREF(py_comp_ptr);
280 return status;
281}
282
fd57054b
PP
283static
284bt_component_class_get_supported_mip_versions_method_status
285component_class_get_supported_mip_versions(
286 const bt_component_class *component_class,
287 bt_self_component_class *self_component_class,
288 const bt_value *params, void *init_method_data,
289 bt_logging_level log_level,
290 bt_integer_range_set_unsigned *supported_versions)
291{
292 uint64_t i;
293 PyObject *py_cls = NULL;
294 PyObject *py_params_ptr = NULL;
295 PyObject *py_range_set_addr = NULL;
296 bt_integer_range_set_unsigned *ret_range_set = NULL;
6dbc1938 297 bt_component_class_get_supported_mip_versions_method_status status;
fd57054b
PP
298
299 py_cls = lookup_cc_ptr_to_py_cls(component_class);
300 if (!py_cls) {
301 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
302 "Cannot find Python class associated to native component class: "
303 "comp-cls-addr=%p", component_class);
304 goto error;
305 }
306
307 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
308 SWIGTYPE_p_bt_value, 0);
309 if (!py_params_ptr) {
310 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
8b305066 311 BT_FMT_SWIG_ALLOC_FAILED);
fd57054b
PP
312 goto error;
313 }
314
315 /*
316 * We don't take any reference on `init_method_data` which, if
317 * not `NULL`, is assumed to be a `PyObject *`: the user's
318 * _user_get_supported_mip_versions() function will eventually
319 * take a reference if needed. If `init_method_data` is `NULL`,
320 * then we pass `Py_None` as the initialization's Python object.
321 */
322 py_range_set_addr = PyObject_CallMethod(py_cls,
323 "_bt_get_supported_mip_versions_from_native", "(OOi)",
324 py_params_ptr, init_method_data ? init_method_data : Py_None,
325 (int) log_level);
326 if (!py_range_set_addr) {
327 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
328 "Failed to call Python class's _bt_get_supported_mip_versions_from_native() method: "
329 "py-cls-addr=%p", py_cls);
84e438af 330 status = py_exc_to_status_component_class_clear(self_component_class,
fd57054b
PP
331 log_level);
332 goto end;
333 }
334
335 /*
336 * The returned object, on success, is an integer object
337 * (PyLong) containing the address of a BT unsigned integer
338 * range set object (new reference).
339 */
340 ret_range_set = PyLong_AsVoidPtr(py_range_set_addr);
341 BT_ASSERT(!PyErr_Occurred());
342 BT_ASSERT(ret_range_set);
343
344 /* Copy returned ranges to input range set */
345 for (i = 0; i < bt_integer_range_set_get_range_count(
346 bt_integer_range_set_unsigned_as_range_set_const(ret_range_set));
347 i++) {
348 const bt_integer_range_unsigned *range =
349 bt_integer_range_set_unsigned_borrow_range_by_index_const(
350 ret_range_set, i);
351 bt_integer_range_set_add_range_status add_range_status;
352
353 add_range_status = bt_integer_range_set_unsigned_add_range(
354 supported_versions,
355 bt_integer_range_unsigned_get_lower(range),
356 bt_integer_range_unsigned_get_upper(range));
357 if (add_range_status) {
358 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
359 "Failed to add range to supported MIP versions range set.");
360 goto error;
361 }
362 }
363
6dbc1938
SM
364 status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK;
365
fd57054b
PP
366 goto end;
367
368error:
6dbc1938
SM
369 /* This error path is for non-Python errors only. */
370 status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR;
fd57054b
PP
371
372end:
6dbc1938 373 BT_ASSERT(!PyErr_Occurred());
fd57054b
PP
374 Py_XDECREF(py_params_ptr);
375 Py_XDECREF(py_range_set_addr);
376 bt_integer_range_set_unsigned_put_ref(ret_range_set);
377 return status;
378}
379
380static
381bt_component_class_get_supported_mip_versions_method_status
382component_class_source_get_supported_mip_versions(
383 bt_self_component_class_source *self_component_class_source,
384 const bt_value *params, void *init_method_data,
385 bt_logging_level log_level,
386 bt_integer_range_set_unsigned *supported_versions)
387{
388 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
389 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
390 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
391
392 return component_class_get_supported_mip_versions(
393 component_class, self_component_class,
394 params, init_method_data, log_level, supported_versions);
395}
396
397static
398bt_component_class_get_supported_mip_versions_method_status
399component_class_filter_get_supported_mip_versions(
400 bt_self_component_class_filter *self_component_class_filter,
401 const bt_value *params, void *init_method_data,
402 bt_logging_level log_level,
403 bt_integer_range_set_unsigned *supported_versions)
404{
405 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
406 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
407 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
408
409 return component_class_get_supported_mip_versions(
410 component_class, self_component_class,
411 params, init_method_data, log_level, supported_versions);
412}
413
414static
415bt_component_class_get_supported_mip_versions_method_status
416component_class_sink_get_supported_mip_versions(
417 bt_self_component_class_sink *self_component_class_sink,
418 const bt_value *params, void *init_method_data,
419 bt_logging_level log_level,
420 bt_integer_range_set_unsigned *supported_versions)
421{
422 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
423 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
424 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
425
426 return component_class_get_supported_mip_versions(
427 component_class, self_component_class,
428 params, init_method_data, log_level, supported_versions);
429}
430
4212232c
PP
431/*
432 * Method of bt_component_class_source to initialize a bt_self_component_source
433 * of that class.
434 */
435
436static
21a9f056 437bt_component_class_initialize_method_status component_class_source_init(
4212232c 438 bt_self_component_source *self_component_source,
59225a3e 439 bt_self_component_source_configuration *config,
4212232c
PP
440 const bt_value *params, void *init_method_data)
441{
442 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
443 return component_class_init(
444 self_component,
445 self_component_source,
446 SWIGTYPE_p_bt_self_component_source,
447 params, init_method_data);
448}
449
450static
21a9f056 451bt_component_class_initialize_method_status component_class_filter_init(
4212232c 452 bt_self_component_filter *self_component_filter,
59225a3e 453 bt_self_component_filter_configuration *config,
4212232c
PP
454 const bt_value *params, void *init_method_data)
455{
456 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
457 return component_class_init(
458 self_component,
459 self_component_filter,
460 SWIGTYPE_p_bt_self_component_filter,
461 params, init_method_data);
462}
463
464static
21a9f056 465bt_component_class_initialize_method_status component_class_sink_init(
4212232c 466 bt_self_component_sink *self_component_sink,
59225a3e 467 bt_self_component_sink_configuration *config,
4212232c
PP
468 const bt_value *params, void *init_method_data)
469{
470 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
471 return component_class_init(
472 self_component,
473 self_component_sink,
474 SWIGTYPE_p_bt_self_component_sink,
475 params, init_method_data);
476}
477
478static
479void component_class_finalize(bt_self_component *self_component)
480{
481 PyObject *py_comp = bt_self_component_get_data(self_component);
6dbc1938
SM
482 PyObject *py_method_result;
483
4212232c
PP
484 BT_ASSERT(py_comp);
485
486 /* Call user's _user_finalize() method */
6dbc1938
SM
487 py_method_result = PyObject_CallMethod(py_comp, "_user_finalize", NULL);
488 if (!py_method_result) {
4212232c
PP
489 bt_logging_level log_level = get_self_component_log_level(
490 self_component);
491
6dbc1938
SM
492 /*
493 * Ignore any exception raised by the _user_finalize() method
494 * because it won't change anything at this point: the component
495 * is being destroyed anyway.
496 */
981f33dd
SM
497 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component,
498 "User component's _user_finalize() method raised an exception: ignoring:");
499 logw_exception_clear(log_level);
500
6dbc1938 501 goto end;
4212232c
PP
502 }
503
6dbc1938
SM
504 BT_ASSERT(py_method_result == Py_None);
505
506end:
4212232c
PP
507 Py_XDECREF(py_method_result);
508 Py_DECREF(py_comp);
509}
510
9675ad63
SM
511/* Decref the Python object in the user data associated to `port`. */
512
513static
514void delete_port_user_data(bt_self_component_port *port)
515{
516 Py_DECREF(bt_self_component_port_get_data(port));
517}
518
519static
520void delete_port_input_user_data(bt_self_component_port_input *port_input)
521{
522 bt_self_component_port *port =
523 bt_self_component_port_input_as_self_component_port(port_input);
524
525 delete_port_user_data(port);
526}
527
528static
529void delete_port_output_user_data(bt_self_component_port_output *port_output)
530{
531 bt_self_component_port *port =
532 bt_self_component_port_output_as_self_component_port(port_output);
533
534 delete_port_user_data(port);
535}
536
4212232c
PP
537static
538void component_class_source_finalize(bt_self_component_source *self_component_source)
539{
9675ad63
SM
540 uint64_t i;
541 bt_self_component *self_component;
542 const bt_component_source *component_source;
543
544 self_component = bt_self_component_source_as_self_component(
545 self_component_source);
546 component_source = bt_self_component_source_as_component_source(
547 self_component_source);
548
4212232c 549 component_class_finalize(self_component);
9675ad63
SM
550
551 /*
552 * Free the user data Python object attached to the port. The
553 * corresponding incref was done by the `void *` typemap in
554 * native_bt_port.i.
555 */
556 for (i = 0; i < bt_component_source_get_output_port_count(component_source); i++) {
557 bt_self_component_port_output *port_output;
558
559 port_output = bt_self_component_source_borrow_output_port_by_index(
560 self_component_source, i);
561
562 delete_port_output_user_data(port_output);
563 }
4212232c
PP
564}
565
566static
567void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
568{
9675ad63
SM
569 uint64_t i;
570 bt_self_component *self_component;
571 const bt_component_filter *component_filter;
572
573 self_component = bt_self_component_filter_as_self_component(
574 self_component_filter);
575 component_filter = bt_self_component_filter_as_component_filter(
576 self_component_filter);
577
4212232c 578 component_class_finalize(self_component);
9675ad63
SM
579
580 /*
581 * Free the user data Python object attached to the port. The
582 * corresponding incref was done by the `void *` typemap in
583 * native_bt_port.i.
584 */
585 for (i = 0; i < bt_component_filter_get_input_port_count(component_filter); i++) {
586 bt_self_component_port_input *port_input;
587
588 port_input = bt_self_component_filter_borrow_input_port_by_index(
589 self_component_filter, i);
590
591 delete_port_input_user_data(port_input);
592 }
593
594 for (i = 0; i < bt_component_filter_get_output_port_count(component_filter); i++) {
595 bt_self_component_port_output *port_output;
596
597 port_output = bt_self_component_filter_borrow_output_port_by_index(
598 self_component_filter, i);
599
600 delete_port_output_user_data(port_output);
601 }
4212232c
PP
602}
603
604static
605void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
606{
9675ad63
SM
607 uint64_t i;
608 bt_self_component *self_component;
609 const bt_component_sink *component_sink;
610
611 self_component = bt_self_component_sink_as_self_component(
612 self_component_sink);
613 component_sink = bt_self_component_sink_as_component_sink(
614 self_component_sink);
615
4212232c 616 component_class_finalize(self_component);
9675ad63
SM
617
618 /*
619 * Free the user data Python object attached to the port. The
620 * corresponding incref was done by the `void *` typemap in
621 * native_bt_port.i.
622 */
623 for (i = 0; i < bt_component_sink_get_input_port_count(component_sink); i++) {
624 bt_self_component_port_input *port_input;
625
626 port_input = bt_self_component_sink_borrow_input_port_by_index(
627 self_component_sink, i);
628
629 delete_port_input_user_data(port_input);
630 }
4212232c
PP
631}
632
633static
a3f0c7db 634bt_message_iterator_class_can_seek_beginning_method_status
f2fb1b32
SM
635component_class_can_seek_beginning(
636 bt_self_message_iterator *self_message_iterator, bt_bool *can_seek)
4212232c
PP
637{
638 PyObject *py_iter;
639 PyObject *py_result = NULL;
a3f0c7db 640 bt_message_iterator_class_can_seek_beginning_method_status status;
4212232c 641 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
6dbc1938 642
4212232c
PP
643 BT_ASSERT(py_iter);
644
14cfc8ce
SM
645 py_result = PyObject_CallMethod(py_iter,
646 "_bt_can_seek_beginning_from_native", NULL);
6dbc1938 647 if (!py_result) {
84e438af 648 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
6dbc1938 649 goto end;
4212232c
PP
650 }
651
6dbc1938
SM
652 BT_ASSERT(PyBool_Check(py_result));
653 *can_seek = PyObject_IsTrue(py_result);
654
a3f0c7db 655 status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
6dbc1938
SM
656
657end:
4212232c
PP
658 Py_XDECREF(py_result);
659
f2fb1b32 660 return status;
4212232c
PP
661}
662
663static
a3f0c7db 664bt_message_iterator_class_seek_beginning_method_status
4212232c
PP
665component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
666{
667 PyObject *py_iter;
668 PyObject *py_result;
a3f0c7db 669 bt_message_iterator_class_seek_beginning_method_status status;
4212232c
PP
670
671 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
672 BT_ASSERT(py_iter);
6dbc1938
SM
673
674 py_result = PyObject_CallMethod(py_iter,
675 "_bt_seek_beginning_from_native",
4212232c 676 NULL);
6dbc1938
SM
677 if (!py_result) {
678 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
679 goto end;
680 }
681
682 BT_ASSERT(py_result == Py_None);
683
a3f0c7db 684 status = BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK;
6dbc1938
SM
685
686end:
4212232c 687 Py_XDECREF(py_result);
6dbc1938 688
4212232c
PP
689 return status;
690}
691
c182d7dd 692static
a3f0c7db 693bt_message_iterator_class_can_seek_ns_from_origin_method_status
c182d7dd
SM
694component_class_can_seek_ns_from_origin(
695 bt_self_message_iterator *self_message_iterator,
696 int64_t ns_from_origin, bt_bool *can_seek)
697{
698 PyObject *py_iter;
699 PyObject *py_result = NULL;
a3f0c7db 700 bt_message_iterator_class_can_seek_ns_from_origin_method_status status;
6dbc1938 701
c182d7dd
SM
702 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
703 BT_ASSERT(py_iter);
704
705 py_result = PyObject_CallMethod(py_iter,
706 "_bt_can_seek_ns_from_origin_from_native", "L", ns_from_origin);
6dbc1938 707 if (!py_result) {
84e438af 708 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
6dbc1938 709 goto end;
c182d7dd
SM
710 }
711
6dbc1938
SM
712 BT_ASSERT(PyBool_Check(py_result));
713 *can_seek = PyObject_IsTrue(py_result);
714
a3f0c7db 715 status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
6dbc1938
SM
716
717end:
c182d7dd
SM
718 Py_XDECREF(py_result);
719
720 return status;
721}
722
723static
a3f0c7db 724bt_message_iterator_class_seek_ns_from_origin_method_status
c182d7dd
SM
725component_class_seek_ns_from_origin(
726 bt_self_message_iterator *self_message_iterator,
727 int64_t ns_from_origin)
728{
729 PyObject *py_iter;
730 PyObject *py_result;
a3f0c7db 731 bt_message_iterator_class_seek_ns_from_origin_method_status status;
c182d7dd
SM
732
733 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
734 BT_ASSERT(py_iter);
6dbc1938 735
c182d7dd
SM
736 py_result = PyObject_CallMethod(py_iter,
737 "_bt_seek_ns_from_origin_from_native", "L", ns_from_origin);
6dbc1938
SM
738 if (!py_result) {
739 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
740 goto end;
741 }
742
743
744 BT_ASSERT(py_result == Py_None);
745
a3f0c7db 746 status = BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
6dbc1938
SM
747
748end:
c182d7dd 749 Py_XDECREF(py_result);
6dbc1938 750
c182d7dd
SM
751 return status;
752}
753
4212232c
PP
754static
755bt_component_class_port_connected_method_status component_class_port_connected(
756 bt_self_component *self_component,
757 void *self_component_port,
758 swig_type_info *self_component_port_swig_type,
759 bt_port_type self_component_port_type,
760 const void *other_port,
761 swig_type_info *other_port_swig_type)
762{
763 bt_component_class_port_connected_method_status status;
764 PyObject *py_comp = NULL;
765 PyObject *py_self_port_ptr = NULL;
766 PyObject *py_other_port_ptr = NULL;
767 PyObject *py_method_result = NULL;
768 bt_logging_level log_level = get_self_component_log_level(
769 self_component);
770
771 py_comp = bt_self_component_get_data(self_component);
772 BT_ASSERT(py_comp);
773 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
774 self_component_port_swig_type, 0);
775 if (!py_self_port_ptr) {
776 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
8b305066 777 BT_FMT_SWIG_ALLOC_FAILED);
4212232c
PP
778 status = __BT_FUNC_STATUS_MEMORY_ERROR;
779 goto end;
780 }
781
782 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
783 other_port_swig_type, 0);
784 if (!py_other_port_ptr) {
785 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
8b305066 786 BT_FMT_SWIG_ALLOC_FAILED);
4212232c 787 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6dbc1938
SM
788 goto end;
789 }
4212232c
PP
790
791 py_method_result = PyObject_CallMethod(py_comp,
792 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
793 self_component_port_type, py_other_port_ptr);
6dbc1938
SM
794 if (!py_method_result) {
795 status = py_exc_to_status_component_clear(self_component);
796 goto end;
797 }
798
799 BT_ASSERT(py_method_result == Py_None);
800
801 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
4212232c
PP
802
803end:
804 Py_XDECREF(py_self_port_ptr);
805 Py_XDECREF(py_other_port_ptr);
806 Py_XDECREF(py_method_result);
6dbc1938 807
4212232c
PP
808 return status;
809}
810
811static
812bt_component_class_port_connected_method_status
813component_class_source_output_port_connected(
814 bt_self_component_source *self_component_source,
815 bt_self_component_port_output *self_component_port_output,
816 const bt_port_input *other_port_input)
817{
818 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
819
820 return component_class_port_connected(
821 self_component,
822 self_component_port_output,
823 SWIGTYPE_p_bt_self_component_port_output,
824 BT_PORT_TYPE_OUTPUT,
825 other_port_input,
826 SWIGTYPE_p_bt_port_input);
827}
828
829static
830bt_component_class_port_connected_method_status
831component_class_filter_input_port_connected(
832 bt_self_component_filter *self_component_filter,
833 bt_self_component_port_input *self_component_port_input,
834 const bt_port_output *other_port_output)
835{
836 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
837
838 return component_class_port_connected(
839 self_component,
840 self_component_port_input,
841 SWIGTYPE_p_bt_self_component_port_input,
842 BT_PORT_TYPE_INPUT,
843 other_port_output,
844 SWIGTYPE_p_bt_port_output);
845}
846
847static
848bt_component_class_port_connected_method_status
849component_class_filter_output_port_connected(
850 bt_self_component_filter *self_component_filter,
851 bt_self_component_port_output *self_component_port_output,
852 const bt_port_input *other_port_input)
853{
854 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
855
856 return component_class_port_connected(
857 self_component,
858 self_component_port_output,
859 SWIGTYPE_p_bt_self_component_port_output,
860 BT_PORT_TYPE_OUTPUT,
861 other_port_input,
862 SWIGTYPE_p_bt_port_input);
863}
864
865static
866bt_component_class_port_connected_method_status
867component_class_sink_input_port_connected(
868 bt_self_component_sink *self_component_sink,
869 bt_self_component_port_input *self_component_port_input,
870 const bt_port_output *other_port_output)
871{
872 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
873
874 return component_class_port_connected(
875 self_component,
876 self_component_port_input,
877 SWIGTYPE_p_bt_self_component_port_input,
878 BT_PORT_TYPE_INPUT,
879 other_port_output,
880 SWIGTYPE_p_bt_port_output);
881}
882
883static
884bt_component_class_sink_graph_is_configured_method_status
885component_class_sink_graph_is_configured(
886 bt_self_component_sink *self_component_sink)
887{
888 PyObject *py_comp = NULL;
889 PyObject *py_method_result = NULL;
6dbc1938 890 bt_component_class_sink_graph_is_configured_method_status status;
4212232c
PP
891 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
892
893 py_comp = bt_self_component_get_data(self_component);
6dbc1938 894
4212232c
PP
895 py_method_result = PyObject_CallMethod(py_comp,
896 "_bt_graph_is_configured_from_native", NULL);
6dbc1938
SM
897 if (!py_method_result) {
898 status = py_exc_to_status_component_clear(self_component);
899 goto end;
900 }
901
902 BT_ASSERT(py_method_result == Py_None);
903
904 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;;
905
906end:
4212232c
PP
907 Py_XDECREF(py_method_result);
908 return status;
909}
910
911static
912bt_component_class_query_method_status component_class_query(
913 const bt_component_class *component_class,
914 bt_self_component_class *self_component_class,
915 bt_private_query_executor *priv_query_executor,
7c14d641 916 const char *object, const bt_value *params, void *method_data,
4212232c
PP
917 const bt_value **result)
918{
919 PyObject *py_cls = NULL;
920 PyObject *py_params_ptr = NULL;
921 PyObject *py_priv_query_exec_ptr = NULL;
922 PyObject *py_query_func = NULL;
923 PyObject *py_object = NULL;
924 PyObject *py_results_addr = NULL;
925 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
926 const bt_query_executor *query_exec =
927 bt_private_query_executor_as_query_executor_const(
928 priv_query_executor);
929 bt_logging_level log_level =
930 bt_query_executor_get_logging_level(query_exec);
931
7c14d641
PP
932 /*
933 * If there's any `method_data`, assume this component class is
934 * getting queried from Python, so that `method_data` is a
935 * Python object to pass to the user's _user_query() method.
936 */
937 BT_ASSERT(!method_data ||
938 bt_bt2_is_python_component_class(component_class));
939
4212232c
PP
940 py_cls = lookup_cc_ptr_to_py_cls(component_class);
941 if (!py_cls) {
942 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
943 "Cannot find Python class associated to native component class: "
944 "comp-cls-addr=%p", component_class);
945 goto error;
946 }
947
948 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
949 SWIGTYPE_p_bt_value, 0);
950 if (!py_params_ptr) {
951 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
8b305066 952 BT_FMT_SWIG_ALLOC_FAILED);
4212232c
PP
953 goto error;
954 }
955
956 py_priv_query_exec_ptr = SWIG_NewPointerObj(
957 SWIG_as_voidptr(priv_query_executor),
958 SWIGTYPE_p_bt_private_query_executor, 0);
959 if (!py_priv_query_exec_ptr) {
960 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
8b305066 961 BT_FMT_SWIG_ALLOC_FAILED);
4212232c
PP
962 goto error;
963 }
964
965 py_object = SWIG_FromCharPtr(object);
966 if (!py_object) {
967 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
968 "Failed to create a Python string.");
969 goto error;
970 }
971
7c14d641
PP
972 /*
973 * We don't take any reference on `method_data` which, if not
974 * `NULL`, is assumed to be a `PyObject *`: the user's
975 * _user_query() function will eventually take a reference if
976 * needed. If `method_data` is `NULL`, then we pass `Py_None` as
977 * the initialization's Python object.
978 */
4212232c 979 py_results_addr = PyObject_CallMethod(py_cls,
7c14d641
PP
980 "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr,
981 py_object, py_params_ptr,
982 method_data ? method_data : Py_None);
4212232c 983 if (!py_results_addr) {
84e438af 984 status = py_exc_to_status_component_class_clear(self_component_class,
4212232c 985 log_level);
561e7049
SM
986 if (status < 0) {
987 static const char *fmt =
988 "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p";
989 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
990 fmt, py_cls);
991 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
992 self_component_class, fmt, py_cls);
993 }
4212232c
PP
994 goto end;
995 }
996
997 /*
998 * The returned object, on success, is an integer object
999 * (PyLong) containing the address of a BT value object (new
1000 * reference).
1001 */
1002 *result = PyLong_AsVoidPtr(py_results_addr);
1003 BT_ASSERT(!PyErr_Occurred());
1004 BT_ASSERT(*result);
1005 goto end;
1006
1007error:
1008 PyErr_Clear();
1009 status = __BT_FUNC_STATUS_ERROR;
1010
1011end:
1012 Py_XDECREF(py_params_ptr);
1013 Py_XDECREF(py_priv_query_exec_ptr);
1014 Py_XDECREF(py_query_func);
1015 Py_XDECREF(py_object);
1016 Py_XDECREF(py_results_addr);
1017 return status;
1018}
1019
1020static
1021bt_component_class_query_method_status component_class_source_query(
1022 bt_self_component_class_source *self_component_class_source,
1023 bt_private_query_executor *priv_query_executor,
7c14d641 1024 const char *object, const bt_value *params, void *method_data,
4212232c
PP
1025 const bt_value **result)
1026{
1027 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
1028 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
1029 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
1030
1031 return component_class_query(component_class, self_component_class,
7c14d641 1032 priv_query_executor, object, params, method_data, result);
4212232c
PP
1033}
1034
1035static
1036bt_component_class_query_method_status component_class_filter_query(
1037 bt_self_component_class_filter *self_component_class_filter,
1038 bt_private_query_executor *priv_query_executor,
7c14d641 1039 const char *object, const bt_value *params, void *method_data,
4212232c
PP
1040 const bt_value **result)
1041{
1042 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
1043 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
1044 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
1045
1046 return component_class_query(component_class, self_component_class,
7c14d641 1047 priv_query_executor, object, params, method_data, result);
4212232c
PP
1048}
1049
1050static
1051bt_component_class_query_method_status component_class_sink_query(
1052 bt_self_component_class_sink *self_component_class_sink,
1053 bt_private_query_executor *priv_query_executor,
7c14d641 1054 const char *object, const bt_value *params, void *method_data,
4212232c
PP
1055 const bt_value **result)
1056{
1057 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
1058 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
1059 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
1060
1061 return component_class_query(component_class, self_component_class,
7c14d641 1062 priv_query_executor, object, params, method_data, result);
4212232c
PP
1063}
1064
1065static
a3f0c7db 1066bt_message_iterator_class_initialize_method_status
4212232c
PP
1067component_class_message_iterator_init(
1068 bt_self_message_iterator *self_message_iterator,
8d8b141d 1069 bt_self_message_iterator_configuration *config,
4212232c
PP
1070 bt_self_component_port_output *self_component_port_output)
1071{
a3f0c7db 1072 bt_message_iterator_class_initialize_method_status status = __BT_FUNC_STATUS_OK;
4212232c
PP
1073 PyObject *py_comp_cls = NULL;
1074 PyObject *py_iter_cls = NULL;
1075 PyObject *py_iter_ptr = NULL;
8d8b141d 1076 PyObject *py_config_ptr = NULL;
4212232c
PP
1077 PyObject *py_component_port_output_ptr = NULL;
1078 PyObject *py_init_method_result = NULL;
1079 PyObject *py_iter = NULL;
1080 PyObject *py_comp;
f615b250
PP
1081 bt_self_component *self_component =
1082 bt_self_message_iterator_borrow_component(
1083 self_message_iterator);
4212232c
PP
1084 bt_logging_level log_level = get_self_component_log_level(
1085 self_component);
1086
1087 py_comp = bt_self_component_get_data(self_component);
1088
1089 /* Find user's Python message iterator class */
1090 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
1091 if (!py_comp_cls) {
1092 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1093 "Cannot get Python object's `__class__` attribute.");
1094 goto python_error;
1095 }
1096
1097 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
1098 if (!py_iter_cls) {
1099 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1100 "Cannot get Python class's `_iter_cls` attribute.");
1101 goto python_error;
1102 }
1103
1104 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
1105 SWIGTYPE_p_bt_self_message_iterator, 0);
1106 if (!py_iter_ptr) {
4212232c 1107 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
8b305066 1108 BT_FMT_SWIG_ALLOC_FAILED);
4212232c 1109 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
8b305066 1110 self_message_iterator, BT_FMT_SWIG_ALLOC_FAILED);
4212232c
PP
1111 goto error;
1112 }
1113
1114 /*
1115 * Create object with borrowed native message iterator
1116 * reference:
1117 *
1118 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
1119 */
1120 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
1121 "(OO)", py_iter_cls, py_iter_ptr);
1122 if (!py_iter) {
1123 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1124 "Failed to call Python class's __new__() method: "
1125 "py-cls-addr=%p", py_iter_cls);
1126 goto python_error;
1127 }
1128
1129 /*
1130 * Initialize object:
1131 *
8d8b141d 1132 * py_iter.__init__(config, self_output_port)
4212232c 1133 *
8d8b141d 1134 * through the _init_from_native helper static method.
4212232c
PP
1135 *
1136 * At this point, py_iter._ptr is set, so this initialization
1137 * function has access to self._component (which gives it the
1138 * user Python component object from which the iterator was
1139 * created).
1140 */
8d8b141d
SM
1141 py_config_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(config),
1142 SWIGTYPE_p_bt_self_message_iterator_configuration, 0);
1143 if (!py_config_ptr) {
8d8b141d 1144 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
8b305066 1145 BT_FMT_SWIG_ALLOC_FAILED);
8d8b141d 1146 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
8b305066 1147 self_message_iterator, BT_FMT_SWIG_ALLOC_FAILED);
8d8b141d
SM
1148 goto error;
1149 }
1150
4212232c
PP
1151 py_component_port_output_ptr = SWIG_NewPointerObj(
1152 SWIG_as_voidptr(self_component_port_output),
1153 SWIGTYPE_p_bt_self_component_port_output, 0);
1154 if (!py_component_port_output_ptr) {
4212232c 1155 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
8b305066 1156 "%s", BT_FMT_SWIG_ALLOC_FAILED);
4212232c 1157 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
8b305066 1158 self_message_iterator, BT_FMT_SWIG_ALLOC_FAILED);
4212232c
PP
1159 goto error;
1160 }
1161
1162 py_init_method_result = PyObject_CallMethod(py_iter,
8d8b141d
SM
1163 "_bt_init_from_native", "OO", py_config_ptr,
1164 py_component_port_output_ptr);
4212232c
PP
1165 if (!py_init_method_result) {
1166 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1167 "User's __init__() method failed:");
1168 goto python_error;
1169 }
1170
1171 /*
1172 * Since the Python code can never instantiate a user-defined
1173 * message iterator class, the native message iterator
1174 * object does NOT belong to a user Python message iterator
1175 * object (borrowed reference). However this Python object is
1176 * owned by this native message iterator object.
1177 *
1178 * In the Python world, the lifetime of the native message
1179 * iterator is managed by a _GenericMessageIterator
1180 * instance:
1181 *
1182 * _GenericMessageIterator instance:
1183 * owns a native bt_message_iterator object (iter)
1184 * owns a _UserMessageIterator instance (py_iter)
1185 * self._ptr is a borrowed reference to the
1186 * native bt_private_connection_private_message_iterator
1187 * object (iter)
1188 */
1189 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1190 py_iter = NULL;
1191 goto end;
1192
1193python_error:
1194 /* Handling of errors that cause a Python exception to be set. */
84e438af 1195 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
4212232c
PP
1196 goto end;
1197
1198error:
1199 /* Handling of errors that don't cause a Python exception to be set. */
1200 status = __BT_FUNC_STATUS_ERROR;
1201
1202end:
1203 BT_ASSERT(!PyErr_Occurred());
1204
1205 Py_XDECREF(py_comp_cls);
1206 Py_XDECREF(py_iter_cls);
1207 Py_XDECREF(py_iter_ptr);
1208 Py_XDECREF(py_component_port_output_ptr);
1209 Py_XDECREF(py_init_method_result);
1210 Py_XDECREF(py_iter);
1211 return status;
1212}
1213
4212232c
PP
1214static
1215void component_class_message_iterator_finalize(
1216 bt_self_message_iterator *message_iterator)
1217{
1218 PyObject *py_message_iter = bt_self_message_iterator_get_data(
1219 message_iterator);
1220 PyObject *py_method_result = NULL;
1221
1222 BT_ASSERT(py_message_iter);
1223
1224 /* Call user's _user_finalize() method */
1225 py_method_result = PyObject_CallMethod(py_message_iter,
1226 "_user_finalize", NULL);
6dbc1938 1227 if (!py_method_result) {
4212232c
PP
1228 bt_self_component *self_comp =
1229 bt_self_message_iterator_borrow_component(
1230 message_iterator);
1231 bt_logging_level log_level = get_self_component_log_level(
1232 self_comp);
1233
6dbc1938
SM
1234 /*
1235 * Ignore any exception raised by the _user_finalize() method
1236 * because it won't change anything at this point: the component
1237 * is being destroyed anyway.
1238 */
981f33dd
SM
1239 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp,
1240 "User's _user_finalize() method raised an exception: ignoring:");
1241 logw_exception_clear(get_self_message_iterator_log_level(
1242 message_iterator));
4212232c
PP
1243 }
1244
4212232c
PP
1245 Py_XDECREF(py_method_result);
1246 Py_DECREF(py_message_iter);
1247}
1248
1249/* Valid for both sources and filters. */
1250
1251static
a3f0c7db 1252bt_message_iterator_class_next_method_status
4212232c
PP
1253component_class_message_iterator_next(
1254 bt_self_message_iterator *message_iterator,
1255 bt_message_array_const msgs, uint64_t capacity,
1256 uint64_t *count)
1257{
a3f0c7db 1258 bt_message_iterator_class_next_method_status status;
4212232c
PP
1259 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1260 PyObject *py_method_result = NULL;
1261
98b15851 1262 BT_ASSERT_DBG(py_message_iter);
4212232c
PP
1263 py_method_result = PyObject_CallMethod(py_message_iter,
1264 "_bt_next_from_native", NULL);
1265 if (!py_method_result) {
84e438af 1266 status = py_exc_to_status_message_iterator_clear(message_iterator);
4212232c
PP
1267 goto end;
1268 }
1269
1270 /*
1271 * The returned object, on success, is an integer object
1272 * (PyLong) containing the address of a native message
1273 * object (which is now ours).
1274 */
1275 msgs[0] = PyLong_AsVoidPtr(py_method_result);
1276 *count = 1;
1277
6dbc1938 1278 /* Overflow errors should never happen. */
98b15851 1279 BT_ASSERT_DBG(!PyErr_Occurred());
6dbc1938 1280
a3f0c7db 1281 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
4212232c
PP
1282
1283end:
1284 Py_XDECREF(py_method_result);
1285 return status;
1286}
1287
1288static
1289bt_component_class_sink_consume_method_status
1290component_class_sink_consume(bt_self_component_sink *self_component_sink)
1291{
1292 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1293 PyObject *py_comp = bt_self_component_get_data(self_component);
1294 PyObject *py_method_result = NULL;
1295 bt_component_class_sink_consume_method_status status;
1296
98b15851 1297 BT_ASSERT_DBG(py_comp);
6dbc1938 1298
4212232c
PP
1299 py_method_result = PyObject_CallMethod(py_comp,
1300 "_user_consume", NULL);
6dbc1938
SM
1301 if (!py_method_result) {
1302 status = py_exc_to_status_component_clear(self_component);
1303 goto end;
1304 }
1305
1306 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
1307
1308end:
4212232c
PP
1309 Py_XDECREF(py_method_result);
1310 return status;
1311}
1312
1313static
1314int component_class_set_help_and_desc(
1315 bt_component_class *component_class,
1316 const char *description, const char *help)
1317{
1318 int ret;
1319
1320 if (description) {
1321 ret = bt_component_class_set_description(component_class, description);
1322 if (ret) {
1323 BT_LOGE("Cannot set component class's description: "
1324 "comp-cls-addr=%p", component_class);
1325 goto end;
1326 }
1327 }
1328
1329 if (help) {
1330 ret = bt_component_class_set_help(component_class, help);
1331 if (ret) {
1332 BT_LOGE("Cannot set component class's help text: "
1333 "comp-cls-addr=%p", component_class);
1334 goto end;
1335 }
1336 }
1337
1338 ret = 0;
1339
1340end:
1341 return ret;
1342}
1343
a3f0c7db
SM
1344static
1345bt_message_iterator_class *create_message_iterator_class(void)
1346{
1347 bt_message_iterator_class *message_iterator_class;
1348 bt_message_iterator_class_set_method_status ret;
1349
1350 message_iterator_class = bt_message_iterator_class_create(
1351 component_class_message_iterator_next);
1352 if (!message_iterator_class) {
1353 BT_LOGE_STR("Cannot create message iterator class.");
1354 goto end;
1355 }
1356
1357 ret = bt_message_iterator_class_set_seek_beginning_methods(
1358 message_iterator_class, component_class_seek_beginning,
1359 component_class_can_seek_beginning);
1360 BT_ASSERT(ret == 0);
1361 ret = bt_message_iterator_class_set_seek_ns_from_origin_methods(
1362 message_iterator_class, component_class_seek_ns_from_origin,
1363 component_class_can_seek_ns_from_origin);
1364 BT_ASSERT(ret == 0);
1365 ret = bt_message_iterator_class_set_initialize_method(
1366 message_iterator_class, component_class_message_iterator_init);
1367 BT_ASSERT(ret == 0);
1368 ret = bt_message_iterator_class_set_finalize_method(
1369 message_iterator_class, component_class_message_iterator_finalize);
1370 BT_ASSERT(ret == 0);
1371
1372end:
1373 return message_iterator_class;
1374}
1375
4212232c
PP
1376static
1377bt_component_class_source *bt_bt2_component_class_source_create(
1378 PyObject *py_cls, const char *name, const char *description,
1379 const char *help)
1380{
a3f0c7db
SM
1381 bt_component_class_source *component_class_source = NULL;
1382 bt_message_iterator_class *message_iterator_class;
4212232c
PP
1383 bt_component_class *component_class;
1384 int ret;
1385
1386 BT_ASSERT(py_cls);
a3f0c7db
SM
1387
1388 message_iterator_class = create_message_iterator_class();
1389 if (!message_iterator_class) {
1390 goto end;
1391 }
1392
4212232c 1393 component_class_source = bt_component_class_source_create(name,
a3f0c7db 1394 message_iterator_class);
4212232c
PP
1395 if (!component_class_source) {
1396 BT_LOGE_STR("Cannot create source component class.");
1397 goto end;
1398 }
1399
1400 component_class = bt_component_class_source_as_component_class(component_class_source);
1401
1402 if (component_class_set_help_and_desc(component_class, description, help)) {
1403 goto end;
1404 }
1405
21a9f056 1406 ret = bt_component_class_source_set_initialize_method(component_class_source, component_class_source_init);
4212232c
PP
1407 BT_ASSERT(ret == 0);
1408 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1409 BT_ASSERT(ret == 0);
4212232c
PP
1410 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1411 component_class_source_output_port_connected);
1412 BT_ASSERT(ret == 0);
1413 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1414 BT_ASSERT(ret == 0);
fd57054b
PP
1415 ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions);
1416 BT_ASSERT(ret == 0);
4212232c
PP
1417 register_cc_ptr_to_py_cls(component_class, py_cls);
1418
1419end:
a3f0c7db 1420 bt_message_iterator_class_put_ref(message_iterator_class);
4212232c
PP
1421 return component_class_source;
1422}
1423
1424static
1425bt_component_class_filter *bt_bt2_component_class_filter_create(
1426 PyObject *py_cls, const char *name, const char *description,
1427 const char *help)
1428{
a3f0c7db
SM
1429 bt_component_class_filter *component_class_filter = NULL;
1430 bt_message_iterator_class *message_iterator_class;
4212232c 1431 bt_component_class *component_class;
4212232c
PP
1432 int ret;
1433
1434 BT_ASSERT(py_cls);
a3f0c7db
SM
1435
1436 message_iterator_class = create_message_iterator_class();
1437 if (!message_iterator_class) {
1438 goto end;
1439 }
1440
4212232c 1441 component_class_filter = bt_component_class_filter_create(name,
a3f0c7db 1442 message_iterator_class);
4212232c
PP
1443 if (!component_class_filter) {
1444 BT_LOGE_STR("Cannot create filter component class.");
1445 goto end;
1446 }
1447
1448 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1449
1450 if (component_class_set_help_and_desc(component_class, description, help)) {
1451 goto end;
1452 }
1453
21a9f056 1454 ret = bt_component_class_filter_set_initialize_method(component_class_filter, component_class_filter_init);
4212232c
PP
1455 BT_ASSERT(ret == 0);
1456 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1457 BT_ASSERT(ret == 0);
4212232c
PP
1458 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1459 component_class_filter_input_port_connected);
1460 BT_ASSERT(ret == 0);
1461 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1462 component_class_filter_output_port_connected);
1463 BT_ASSERT(ret == 0);
1464 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1465 BT_ASSERT(ret == 0);
fd57054b
PP
1466 ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions);
1467 BT_ASSERT(ret == 0);
4212232c
PP
1468 register_cc_ptr_to_py_cls(component_class, py_cls);
1469
1470end:
a3f0c7db 1471 bt_message_iterator_class_put_ref(message_iterator_class);
4212232c
PP
1472 return component_class_filter;
1473}
1474
1475static
1476bt_component_class_sink *bt_bt2_component_class_sink_create(
1477 PyObject *py_cls, const char *name, const char *description,
1478 const char *help)
1479{
1480 bt_component_class_sink *component_class_sink;
1481 bt_component_class *component_class;
1482 int ret;
1483
1484 BT_ASSERT(py_cls);
1485 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1486
1487 if (!component_class_sink) {
1488 BT_LOGE_STR("Cannot create sink component class.");
1489 goto end;
1490 }
1491
1492 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1493
1494 if (component_class_set_help_and_desc(component_class, description, help)) {
1495 goto end;
1496 }
1497
21a9f056 1498 ret = bt_component_class_sink_set_initialize_method(component_class_sink, component_class_sink_init);
4212232c
PP
1499 BT_ASSERT(ret == 0);
1500 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1501 BT_ASSERT(ret == 0);
1502 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1503 component_class_sink_input_port_connected);
1504 BT_ASSERT(ret == 0);
1505 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1506 component_class_sink_graph_is_configured);
1507 BT_ASSERT(ret == 0);
1508 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1509 BT_ASSERT(ret == 0);
fd57054b
PP
1510 ret = bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink, component_class_sink_get_supported_mip_versions);
1511 BT_ASSERT(ret == 0);
4212232c
PP
1512 register_cc_ptr_to_py_cls(component_class, py_cls);
1513
1514end:
1515 return component_class_sink;
1516}
This page took 0.107162 seconds and 4 git commands to generate.