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