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