lib: introduce bt_message_iterator_class
[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
9675ad63
SM
527/* Decref the Python object in the user data associated to `port`. */
528
529static
530void delete_port_user_data(bt_self_component_port *port)
531{
532 Py_DECREF(bt_self_component_port_get_data(port));
533}
534
535static
536void delete_port_input_user_data(bt_self_component_port_input *port_input)
537{
538 bt_self_component_port *port =
539 bt_self_component_port_input_as_self_component_port(port_input);
540
541 delete_port_user_data(port);
542}
543
544static
545void delete_port_output_user_data(bt_self_component_port_output *port_output)
546{
547 bt_self_component_port *port =
548 bt_self_component_port_output_as_self_component_port(port_output);
549
550 delete_port_user_data(port);
551}
552
4212232c
PP
553static
554void component_class_source_finalize(bt_self_component_source *self_component_source)
555{
9675ad63
SM
556 uint64_t i;
557 bt_self_component *self_component;
558 const bt_component_source *component_source;
559
560 self_component = bt_self_component_source_as_self_component(
561 self_component_source);
562 component_source = bt_self_component_source_as_component_source(
563 self_component_source);
564
4212232c 565 component_class_finalize(self_component);
9675ad63
SM
566
567 /*
568 * Free the user data Python object attached to the port. The
569 * corresponding incref was done by the `void *` typemap in
570 * native_bt_port.i.
571 */
572 for (i = 0; i < bt_component_source_get_output_port_count(component_source); i++) {
573 bt_self_component_port_output *port_output;
574
575 port_output = bt_self_component_source_borrow_output_port_by_index(
576 self_component_source, i);
577
578 delete_port_output_user_data(port_output);
579 }
4212232c
PP
580}
581
582static
583void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
584{
9675ad63
SM
585 uint64_t i;
586 bt_self_component *self_component;
587 const bt_component_filter *component_filter;
588
589 self_component = bt_self_component_filter_as_self_component(
590 self_component_filter);
591 component_filter = bt_self_component_filter_as_component_filter(
592 self_component_filter);
593
4212232c 594 component_class_finalize(self_component);
9675ad63
SM
595
596 /*
597 * Free the user data Python object attached to the port. The
598 * corresponding incref was done by the `void *` typemap in
599 * native_bt_port.i.
600 */
601 for (i = 0; i < bt_component_filter_get_input_port_count(component_filter); i++) {
602 bt_self_component_port_input *port_input;
603
604 port_input = bt_self_component_filter_borrow_input_port_by_index(
605 self_component_filter, i);
606
607 delete_port_input_user_data(port_input);
608 }
609
610 for (i = 0; i < bt_component_filter_get_output_port_count(component_filter); i++) {
611 bt_self_component_port_output *port_output;
612
613 port_output = bt_self_component_filter_borrow_output_port_by_index(
614 self_component_filter, i);
615
616 delete_port_output_user_data(port_output);
617 }
4212232c
PP
618}
619
620static
621void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
622{
9675ad63
SM
623 uint64_t i;
624 bt_self_component *self_component;
625 const bt_component_sink *component_sink;
626
627 self_component = bt_self_component_sink_as_self_component(
628 self_component_sink);
629 component_sink = bt_self_component_sink_as_component_sink(
630 self_component_sink);
631
4212232c 632 component_class_finalize(self_component);
9675ad63
SM
633
634 /*
635 * Free the user data Python object attached to the port. The
636 * corresponding incref was done by the `void *` typemap in
637 * native_bt_port.i.
638 */
639 for (i = 0; i < bt_component_sink_get_input_port_count(component_sink); i++) {
640 bt_self_component_port_input *port_input;
641
642 port_input = bt_self_component_sink_borrow_input_port_by_index(
643 self_component_sink, i);
644
645 delete_port_input_user_data(port_input);
646 }
4212232c
PP
647}
648
649static
a3f0c7db 650bt_message_iterator_class_can_seek_beginning_method_status
f2fb1b32
SM
651component_class_can_seek_beginning(
652 bt_self_message_iterator *self_message_iterator, bt_bool *can_seek)
4212232c
PP
653{
654 PyObject *py_iter;
655 PyObject *py_result = NULL;
a3f0c7db 656 bt_message_iterator_class_can_seek_beginning_method_status status;
4212232c 657 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
6dbc1938 658
4212232c
PP
659 BT_ASSERT(py_iter);
660
14cfc8ce
SM
661 py_result = PyObject_CallMethod(py_iter,
662 "_bt_can_seek_beginning_from_native", NULL);
6dbc1938 663 if (!py_result) {
84e438af 664 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
6dbc1938 665 goto end;
4212232c
PP
666 }
667
6dbc1938
SM
668 BT_ASSERT(PyBool_Check(py_result));
669 *can_seek = PyObject_IsTrue(py_result);
670
a3f0c7db 671 status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
6dbc1938
SM
672
673end:
4212232c
PP
674 Py_XDECREF(py_result);
675
f2fb1b32 676 return status;
4212232c
PP
677}
678
679static
a3f0c7db 680bt_message_iterator_class_seek_beginning_method_status
4212232c
PP
681component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
682{
683 PyObject *py_iter;
684 PyObject *py_result;
a3f0c7db 685 bt_message_iterator_class_seek_beginning_method_status status;
4212232c
PP
686
687 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
688 BT_ASSERT(py_iter);
6dbc1938
SM
689
690 py_result = PyObject_CallMethod(py_iter,
691 "_bt_seek_beginning_from_native",
4212232c 692 NULL);
6dbc1938
SM
693 if (!py_result) {
694 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
695 goto end;
696 }
697
698 BT_ASSERT(py_result == Py_None);
699
a3f0c7db 700 status = BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK;
6dbc1938
SM
701
702end:
4212232c 703 Py_XDECREF(py_result);
6dbc1938 704
4212232c
PP
705 return status;
706}
707
c182d7dd 708static
a3f0c7db 709bt_message_iterator_class_can_seek_ns_from_origin_method_status
c182d7dd
SM
710component_class_can_seek_ns_from_origin(
711 bt_self_message_iterator *self_message_iterator,
712 int64_t ns_from_origin, bt_bool *can_seek)
713{
714 PyObject *py_iter;
715 PyObject *py_result = NULL;
a3f0c7db 716 bt_message_iterator_class_can_seek_ns_from_origin_method_status status;
6dbc1938 717
c182d7dd
SM
718 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
719 BT_ASSERT(py_iter);
720
721 py_result = PyObject_CallMethod(py_iter,
722 "_bt_can_seek_ns_from_origin_from_native", "L", ns_from_origin);
6dbc1938 723 if (!py_result) {
84e438af 724 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
6dbc1938 725 goto end;
c182d7dd
SM
726 }
727
6dbc1938
SM
728 BT_ASSERT(PyBool_Check(py_result));
729 *can_seek = PyObject_IsTrue(py_result);
730
a3f0c7db 731 status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
6dbc1938
SM
732
733end:
c182d7dd
SM
734 Py_XDECREF(py_result);
735
736 return status;
737}
738
739static
a3f0c7db 740bt_message_iterator_class_seek_ns_from_origin_method_status
c182d7dd
SM
741component_class_seek_ns_from_origin(
742 bt_self_message_iterator *self_message_iterator,
743 int64_t ns_from_origin)
744{
745 PyObject *py_iter;
746 PyObject *py_result;
a3f0c7db 747 bt_message_iterator_class_seek_ns_from_origin_method_status status;
c182d7dd
SM
748
749 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
750 BT_ASSERT(py_iter);
6dbc1938 751
c182d7dd
SM
752 py_result = PyObject_CallMethod(py_iter,
753 "_bt_seek_ns_from_origin_from_native", "L", ns_from_origin);
6dbc1938
SM
754 if (!py_result) {
755 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
756 goto end;
757 }
758
759
760 BT_ASSERT(py_result == Py_None);
761
a3f0c7db 762 status = BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
6dbc1938
SM
763
764end:
c182d7dd 765 Py_XDECREF(py_result);
6dbc1938 766
c182d7dd
SM
767 return status;
768}
769
4212232c
PP
770static
771bt_component_class_port_connected_method_status component_class_port_connected(
772 bt_self_component *self_component,
773 void *self_component_port,
774 swig_type_info *self_component_port_swig_type,
775 bt_port_type self_component_port_type,
776 const void *other_port,
777 swig_type_info *other_port_swig_type)
778{
779 bt_component_class_port_connected_method_status status;
780 PyObject *py_comp = NULL;
781 PyObject *py_self_port_ptr = NULL;
782 PyObject *py_other_port_ptr = NULL;
783 PyObject *py_method_result = NULL;
784 bt_logging_level log_level = get_self_component_log_level(
785 self_component);
786
787 py_comp = bt_self_component_get_data(self_component);
788 BT_ASSERT(py_comp);
789 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
790 self_component_port_swig_type, 0);
791 if (!py_self_port_ptr) {
792 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
793 "Failed to create a SWIG pointer object.");
794 status = __BT_FUNC_STATUS_MEMORY_ERROR;
795 goto end;
796 }
797
798 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
799 other_port_swig_type, 0);
800 if (!py_other_port_ptr) {
801 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
802 "Failed to create a SWIG pointer object.");
803 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6dbc1938
SM
804 goto end;
805 }
4212232c
PP
806
807 py_method_result = PyObject_CallMethod(py_comp,
808 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
809 self_component_port_type, py_other_port_ptr);
6dbc1938
SM
810 if (!py_method_result) {
811 status = py_exc_to_status_component_clear(self_component);
812 goto end;
813 }
814
815 BT_ASSERT(py_method_result == Py_None);
816
817 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
4212232c
PP
818
819end:
820 Py_XDECREF(py_self_port_ptr);
821 Py_XDECREF(py_other_port_ptr);
822 Py_XDECREF(py_method_result);
6dbc1938 823
4212232c
PP
824 return status;
825}
826
827static
828bt_component_class_port_connected_method_status
829component_class_source_output_port_connected(
830 bt_self_component_source *self_component_source,
831 bt_self_component_port_output *self_component_port_output,
832 const bt_port_input *other_port_input)
833{
834 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
835
836 return component_class_port_connected(
837 self_component,
838 self_component_port_output,
839 SWIGTYPE_p_bt_self_component_port_output,
840 BT_PORT_TYPE_OUTPUT,
841 other_port_input,
842 SWIGTYPE_p_bt_port_input);
843}
844
845static
846bt_component_class_port_connected_method_status
847component_class_filter_input_port_connected(
848 bt_self_component_filter *self_component_filter,
849 bt_self_component_port_input *self_component_port_input,
850 const bt_port_output *other_port_output)
851{
852 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
853
854 return component_class_port_connected(
855 self_component,
856 self_component_port_input,
857 SWIGTYPE_p_bt_self_component_port_input,
858 BT_PORT_TYPE_INPUT,
859 other_port_output,
860 SWIGTYPE_p_bt_port_output);
861}
862
863static
864bt_component_class_port_connected_method_status
865component_class_filter_output_port_connected(
866 bt_self_component_filter *self_component_filter,
867 bt_self_component_port_output *self_component_port_output,
868 const bt_port_input *other_port_input)
869{
870 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
871
872 return component_class_port_connected(
873 self_component,
874 self_component_port_output,
875 SWIGTYPE_p_bt_self_component_port_output,
876 BT_PORT_TYPE_OUTPUT,
877 other_port_input,
878 SWIGTYPE_p_bt_port_input);
879}
880
881static
882bt_component_class_port_connected_method_status
883component_class_sink_input_port_connected(
884 bt_self_component_sink *self_component_sink,
885 bt_self_component_port_input *self_component_port_input,
886 const bt_port_output *other_port_output)
887{
888 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
889
890 return component_class_port_connected(
891 self_component,
892 self_component_port_input,
893 SWIGTYPE_p_bt_self_component_port_input,
894 BT_PORT_TYPE_INPUT,
895 other_port_output,
896 SWIGTYPE_p_bt_port_output);
897}
898
899static
900bt_component_class_sink_graph_is_configured_method_status
901component_class_sink_graph_is_configured(
902 bt_self_component_sink *self_component_sink)
903{
904 PyObject *py_comp = NULL;
905 PyObject *py_method_result = NULL;
6dbc1938 906 bt_component_class_sink_graph_is_configured_method_status status;
4212232c
PP
907 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
908
909 py_comp = bt_self_component_get_data(self_component);
6dbc1938 910
4212232c
PP
911 py_method_result = PyObject_CallMethod(py_comp,
912 "_bt_graph_is_configured_from_native", NULL);
6dbc1938
SM
913 if (!py_method_result) {
914 status = py_exc_to_status_component_clear(self_component);
915 goto end;
916 }
917
918 BT_ASSERT(py_method_result == Py_None);
919
920 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;;
921
922end:
4212232c
PP
923 Py_XDECREF(py_method_result);
924 return status;
925}
926
927static
928bt_component_class_query_method_status component_class_query(
929 const bt_component_class *component_class,
930 bt_self_component_class *self_component_class,
931 bt_private_query_executor *priv_query_executor,
7c14d641 932 const char *object, const bt_value *params, void *method_data,
4212232c
PP
933 const bt_value **result)
934{
935 PyObject *py_cls = NULL;
936 PyObject *py_params_ptr = NULL;
937 PyObject *py_priv_query_exec_ptr = NULL;
938 PyObject *py_query_func = NULL;
939 PyObject *py_object = NULL;
940 PyObject *py_results_addr = NULL;
941 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
942 const bt_query_executor *query_exec =
943 bt_private_query_executor_as_query_executor_const(
944 priv_query_executor);
945 bt_logging_level log_level =
946 bt_query_executor_get_logging_level(query_exec);
947
7c14d641
PP
948 /*
949 * If there's any `method_data`, assume this component class is
950 * getting queried from Python, so that `method_data` is a
951 * Python object to pass to the user's _user_query() method.
952 */
953 BT_ASSERT(!method_data ||
954 bt_bt2_is_python_component_class(component_class));
955
4212232c
PP
956 py_cls = lookup_cc_ptr_to_py_cls(component_class);
957 if (!py_cls) {
958 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
959 "Cannot find Python class associated to native component class: "
960 "comp-cls-addr=%p", component_class);
961 goto error;
962 }
963
964 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
965 SWIGTYPE_p_bt_value, 0);
966 if (!py_params_ptr) {
967 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
968 "Failed to create a SWIG pointer object.");
969 goto error;
970 }
971
972 py_priv_query_exec_ptr = SWIG_NewPointerObj(
973 SWIG_as_voidptr(priv_query_executor),
974 SWIGTYPE_p_bt_private_query_executor, 0);
975 if (!py_priv_query_exec_ptr) {
976 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
977 "Failed to create a SWIG pointer object.");
978 goto error;
979 }
980
981 py_object = SWIG_FromCharPtr(object);
982 if (!py_object) {
983 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
984 "Failed to create a Python string.");
985 goto error;
986 }
987
7c14d641
PP
988 /*
989 * We don't take any reference on `method_data` which, if not
990 * `NULL`, is assumed to be a `PyObject *`: the user's
991 * _user_query() function will eventually take a reference if
992 * needed. If `method_data` is `NULL`, then we pass `Py_None` as
993 * the initialization's Python object.
994 */
4212232c 995 py_results_addr = PyObject_CallMethod(py_cls,
7c14d641
PP
996 "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr,
997 py_object, py_params_ptr,
998 method_data ? method_data : Py_None);
4212232c 999 if (!py_results_addr) {
84e438af 1000 status = py_exc_to_status_component_class_clear(self_component_class,
4212232c 1001 log_level);
561e7049
SM
1002 if (status < 0) {
1003 static const char *fmt =
1004 "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p";
1005 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
1006 fmt, py_cls);
1007 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
1008 self_component_class, fmt, py_cls);
1009 }
4212232c
PP
1010 goto end;
1011 }
1012
1013 /*
1014 * The returned object, on success, is an integer object
1015 * (PyLong) containing the address of a BT value object (new
1016 * reference).
1017 */
1018 *result = PyLong_AsVoidPtr(py_results_addr);
1019 BT_ASSERT(!PyErr_Occurred());
1020 BT_ASSERT(*result);
1021 goto end;
1022
1023error:
1024 PyErr_Clear();
1025 status = __BT_FUNC_STATUS_ERROR;
1026
1027end:
1028 Py_XDECREF(py_params_ptr);
1029 Py_XDECREF(py_priv_query_exec_ptr);
1030 Py_XDECREF(py_query_func);
1031 Py_XDECREF(py_object);
1032 Py_XDECREF(py_results_addr);
1033 return status;
1034}
1035
1036static
1037bt_component_class_query_method_status component_class_source_query(
1038 bt_self_component_class_source *self_component_class_source,
1039 bt_private_query_executor *priv_query_executor,
7c14d641 1040 const char *object, const bt_value *params, void *method_data,
4212232c
PP
1041 const bt_value **result)
1042{
1043 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
1044 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
1045 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
1046
1047 return component_class_query(component_class, self_component_class,
7c14d641 1048 priv_query_executor, object, params, method_data, result);
4212232c
PP
1049}
1050
1051static
1052bt_component_class_query_method_status component_class_filter_query(
1053 bt_self_component_class_filter *self_component_class_filter,
1054 bt_private_query_executor *priv_query_executor,
7c14d641 1055 const char *object, const bt_value *params, void *method_data,
4212232c
PP
1056 const bt_value **result)
1057{
1058 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
1059 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
1060 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
1061
1062 return component_class_query(component_class, self_component_class,
7c14d641 1063 priv_query_executor, object, params, method_data, result);
4212232c
PP
1064}
1065
1066static
1067bt_component_class_query_method_status component_class_sink_query(
1068 bt_self_component_class_sink *self_component_class_sink,
1069 bt_private_query_executor *priv_query_executor,
7c14d641 1070 const char *object, const bt_value *params, void *method_data,
4212232c
PP
1071 const bt_value **result)
1072{
1073 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
1074 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
1075 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
1076
1077 return component_class_query(component_class, self_component_class,
7c14d641 1078 priv_query_executor, object, params, method_data, result);
4212232c
PP
1079}
1080
1081static
a3f0c7db 1082bt_message_iterator_class_initialize_method_status
4212232c
PP
1083component_class_message_iterator_init(
1084 bt_self_message_iterator *self_message_iterator,
8d8b141d 1085 bt_self_message_iterator_configuration *config,
4212232c
PP
1086 bt_self_component *self_component,
1087 bt_self_component_port_output *self_component_port_output)
1088{
a3f0c7db 1089 bt_message_iterator_class_initialize_method_status status = __BT_FUNC_STATUS_OK;
4212232c
PP
1090 PyObject *py_comp_cls = NULL;
1091 PyObject *py_iter_cls = NULL;
1092 PyObject *py_iter_ptr = NULL;
8d8b141d 1093 PyObject *py_config_ptr = NULL;
4212232c
PP
1094 PyObject *py_component_port_output_ptr = NULL;
1095 PyObject *py_init_method_result = NULL;
1096 PyObject *py_iter = NULL;
1097 PyObject *py_comp;
1098 bt_logging_level log_level = get_self_component_log_level(
1099 self_component);
1100
1101 py_comp = bt_self_component_get_data(self_component);
1102
1103 /* Find user's Python message iterator class */
1104 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
1105 if (!py_comp_cls) {
1106 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1107 "Cannot get Python object's `__class__` attribute.");
1108 goto python_error;
1109 }
1110
1111 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
1112 if (!py_iter_cls) {
1113 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1114 "Cannot get Python class's `_iter_cls` attribute.");
1115 goto python_error;
1116 }
1117
1118 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
1119 SWIGTYPE_p_bt_self_message_iterator, 0);
1120 if (!py_iter_ptr) {
1121 const char *err = "Failed to create a SWIG pointer object.";
1122
1123 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1124 "%s", err);
1125 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1126 self_message_iterator, err);
1127 goto error;
1128 }
1129
1130 /*
1131 * Create object with borrowed native message iterator
1132 * reference:
1133 *
1134 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
1135 */
1136 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
1137 "(OO)", py_iter_cls, py_iter_ptr);
1138 if (!py_iter) {
1139 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1140 "Failed to call Python class's __new__() method: "
1141 "py-cls-addr=%p", py_iter_cls);
1142 goto python_error;
1143 }
1144
1145 /*
1146 * Initialize object:
1147 *
8d8b141d 1148 * py_iter.__init__(config, self_output_port)
4212232c 1149 *
8d8b141d 1150 * through the _init_from_native helper static method.
4212232c
PP
1151 *
1152 * At this point, py_iter._ptr is set, so this initialization
1153 * function has access to self._component (which gives it the
1154 * user Python component object from which the iterator was
1155 * created).
1156 */
8d8b141d
SM
1157 py_config_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(config),
1158 SWIGTYPE_p_bt_self_message_iterator_configuration, 0);
1159 if (!py_config_ptr) {
1160 const char *err = "Failed to create a SWIG pointer object";
1161
1162 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1163 "%s", err);
1164 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1165 self_message_iterator, err);
1166 goto error;
1167 }
1168
4212232c
PP
1169 py_component_port_output_ptr = SWIG_NewPointerObj(
1170 SWIG_as_voidptr(self_component_port_output),
1171 SWIGTYPE_p_bt_self_component_port_output, 0);
1172 if (!py_component_port_output_ptr) {
1173 const char *err = "Failed to create a SWIG pointer object.";
1174
1175 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1176 "%s", err);
1177 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1178 self_message_iterator, err);
1179 goto error;
1180 }
1181
1182 py_init_method_result = PyObject_CallMethod(py_iter,
8d8b141d
SM
1183 "_bt_init_from_native", "OO", py_config_ptr,
1184 py_component_port_output_ptr);
4212232c
PP
1185 if (!py_init_method_result) {
1186 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1187 "User's __init__() method failed:");
1188 goto python_error;
1189 }
1190
1191 /*
1192 * Since the Python code can never instantiate a user-defined
1193 * message iterator class, the native message iterator
1194 * object does NOT belong to a user Python message iterator
1195 * object (borrowed reference). However this Python object is
1196 * owned by this native message iterator object.
1197 *
1198 * In the Python world, the lifetime of the native message
1199 * iterator is managed by a _GenericMessageIterator
1200 * instance:
1201 *
1202 * _GenericMessageIterator instance:
1203 * owns a native bt_message_iterator object (iter)
1204 * owns a _UserMessageIterator instance (py_iter)
1205 * self._ptr is a borrowed reference to the
1206 * native bt_private_connection_private_message_iterator
1207 * object (iter)
1208 */
1209 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1210 py_iter = NULL;
1211 goto end;
1212
1213python_error:
1214 /* Handling of errors that cause a Python exception to be set. */
84e438af 1215 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
4212232c
PP
1216 goto end;
1217
1218error:
1219 /* Handling of errors that don't cause a Python exception to be set. */
1220 status = __BT_FUNC_STATUS_ERROR;
1221
1222end:
1223 BT_ASSERT(!PyErr_Occurred());
1224
1225 Py_XDECREF(py_comp_cls);
1226 Py_XDECREF(py_iter_cls);
1227 Py_XDECREF(py_iter_ptr);
1228 Py_XDECREF(py_component_port_output_ptr);
1229 Py_XDECREF(py_init_method_result);
1230 Py_XDECREF(py_iter);
1231 return status;
1232}
1233
4212232c
PP
1234static
1235void component_class_message_iterator_finalize(
1236 bt_self_message_iterator *message_iterator)
1237{
1238 PyObject *py_message_iter = bt_self_message_iterator_get_data(
1239 message_iterator);
1240 PyObject *py_method_result = NULL;
1241
1242 BT_ASSERT(py_message_iter);
1243
1244 /* Call user's _user_finalize() method */
1245 py_method_result = PyObject_CallMethod(py_message_iter,
1246 "_user_finalize", NULL);
6dbc1938 1247 if (!py_method_result) {
4212232c
PP
1248 bt_self_component *self_comp =
1249 bt_self_message_iterator_borrow_component(
1250 message_iterator);
1251 bt_logging_level log_level = get_self_component_log_level(
1252 self_comp);
1253
6dbc1938
SM
1254 /*
1255 * Ignore any exception raised by the _user_finalize() method
1256 * because it won't change anything at this point: the component
1257 * is being destroyed anyway.
1258 */
981f33dd
SM
1259 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp,
1260 "User's _user_finalize() method raised an exception: ignoring:");
1261 logw_exception_clear(get_self_message_iterator_log_level(
1262 message_iterator));
4212232c
PP
1263 }
1264
4212232c
PP
1265 Py_XDECREF(py_method_result);
1266 Py_DECREF(py_message_iter);
1267}
1268
1269/* Valid for both sources and filters. */
1270
1271static
a3f0c7db 1272bt_message_iterator_class_next_method_status
4212232c
PP
1273component_class_message_iterator_next(
1274 bt_self_message_iterator *message_iterator,
1275 bt_message_array_const msgs, uint64_t capacity,
1276 uint64_t *count)
1277{
a3f0c7db 1278 bt_message_iterator_class_next_method_status status;
4212232c
PP
1279 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1280 PyObject *py_method_result = NULL;
1281
98b15851 1282 BT_ASSERT_DBG(py_message_iter);
4212232c
PP
1283 py_method_result = PyObject_CallMethod(py_message_iter,
1284 "_bt_next_from_native", NULL);
1285 if (!py_method_result) {
84e438af 1286 status = py_exc_to_status_message_iterator_clear(message_iterator);
4212232c
PP
1287 goto end;
1288 }
1289
1290 /*
1291 * The returned object, on success, is an integer object
1292 * (PyLong) containing the address of a native message
1293 * object (which is now ours).
1294 */
1295 msgs[0] = PyLong_AsVoidPtr(py_method_result);
1296 *count = 1;
1297
6dbc1938 1298 /* Overflow errors should never happen. */
98b15851 1299 BT_ASSERT_DBG(!PyErr_Occurred());
6dbc1938 1300
a3f0c7db 1301 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
4212232c
PP
1302
1303end:
1304 Py_XDECREF(py_method_result);
1305 return status;
1306}
1307
1308static
1309bt_component_class_sink_consume_method_status
1310component_class_sink_consume(bt_self_component_sink *self_component_sink)
1311{
1312 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1313 PyObject *py_comp = bt_self_component_get_data(self_component);
1314 PyObject *py_method_result = NULL;
1315 bt_component_class_sink_consume_method_status status;
1316
98b15851 1317 BT_ASSERT_DBG(py_comp);
6dbc1938 1318
4212232c
PP
1319 py_method_result = PyObject_CallMethod(py_comp,
1320 "_user_consume", NULL);
6dbc1938
SM
1321 if (!py_method_result) {
1322 status = py_exc_to_status_component_clear(self_component);
1323 goto end;
1324 }
1325
1326 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
1327
1328end:
4212232c
PP
1329 Py_XDECREF(py_method_result);
1330 return status;
1331}
1332
1333static
1334int component_class_set_help_and_desc(
1335 bt_component_class *component_class,
1336 const char *description, const char *help)
1337{
1338 int ret;
1339
1340 if (description) {
1341 ret = bt_component_class_set_description(component_class, description);
1342 if (ret) {
1343 BT_LOGE("Cannot set component class's description: "
1344 "comp-cls-addr=%p", component_class);
1345 goto end;
1346 }
1347 }
1348
1349 if (help) {
1350 ret = bt_component_class_set_help(component_class, help);
1351 if (ret) {
1352 BT_LOGE("Cannot set component class's help text: "
1353 "comp-cls-addr=%p", component_class);
1354 goto end;
1355 }
1356 }
1357
1358 ret = 0;
1359
1360end:
1361 return ret;
1362}
1363
a3f0c7db
SM
1364static
1365bt_message_iterator_class *create_message_iterator_class(void)
1366{
1367 bt_message_iterator_class *message_iterator_class;
1368 bt_message_iterator_class_set_method_status ret;
1369
1370 message_iterator_class = bt_message_iterator_class_create(
1371 component_class_message_iterator_next);
1372 if (!message_iterator_class) {
1373 BT_LOGE_STR("Cannot create message iterator class.");
1374 goto end;
1375 }
1376
1377 ret = bt_message_iterator_class_set_seek_beginning_methods(
1378 message_iterator_class, component_class_seek_beginning,
1379 component_class_can_seek_beginning);
1380 BT_ASSERT(ret == 0);
1381 ret = bt_message_iterator_class_set_seek_ns_from_origin_methods(
1382 message_iterator_class, component_class_seek_ns_from_origin,
1383 component_class_can_seek_ns_from_origin);
1384 BT_ASSERT(ret == 0);
1385 ret = bt_message_iterator_class_set_initialize_method(
1386 message_iterator_class, component_class_message_iterator_init);
1387 BT_ASSERT(ret == 0);
1388 ret = bt_message_iterator_class_set_finalize_method(
1389 message_iterator_class, component_class_message_iterator_finalize);
1390 BT_ASSERT(ret == 0);
1391
1392end:
1393 return message_iterator_class;
1394}
1395
4212232c
PP
1396static
1397bt_component_class_source *bt_bt2_component_class_source_create(
1398 PyObject *py_cls, const char *name, const char *description,
1399 const char *help)
1400{
a3f0c7db
SM
1401 bt_component_class_source *component_class_source = NULL;
1402 bt_message_iterator_class *message_iterator_class;
4212232c
PP
1403 bt_component_class *component_class;
1404 int ret;
1405
1406 BT_ASSERT(py_cls);
a3f0c7db
SM
1407
1408 message_iterator_class = create_message_iterator_class();
1409 if (!message_iterator_class) {
1410 goto end;
1411 }
1412
4212232c 1413 component_class_source = bt_component_class_source_create(name,
a3f0c7db 1414 message_iterator_class);
4212232c
PP
1415 if (!component_class_source) {
1416 BT_LOGE_STR("Cannot create source component class.");
1417 goto end;
1418 }
1419
1420 component_class = bt_component_class_source_as_component_class(component_class_source);
1421
1422 if (component_class_set_help_and_desc(component_class, description, help)) {
1423 goto end;
1424 }
1425
21a9f056 1426 ret = bt_component_class_source_set_initialize_method(component_class_source, component_class_source_init);
4212232c
PP
1427 BT_ASSERT(ret == 0);
1428 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1429 BT_ASSERT(ret == 0);
4212232c
PP
1430 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1431 component_class_source_output_port_connected);
1432 BT_ASSERT(ret == 0);
1433 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1434 BT_ASSERT(ret == 0);
fd57054b
PP
1435 ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions);
1436 BT_ASSERT(ret == 0);
4212232c
PP
1437 register_cc_ptr_to_py_cls(component_class, py_cls);
1438
1439end:
a3f0c7db 1440 bt_message_iterator_class_put_ref(message_iterator_class);
4212232c
PP
1441 return component_class_source;
1442}
1443
1444static
1445bt_component_class_filter *bt_bt2_component_class_filter_create(
1446 PyObject *py_cls, const char *name, const char *description,
1447 const char *help)
1448{
a3f0c7db
SM
1449 bt_component_class_filter *component_class_filter = NULL;
1450 bt_message_iterator_class *message_iterator_class;
4212232c 1451 bt_component_class *component_class;
4212232c
PP
1452 int ret;
1453
1454 BT_ASSERT(py_cls);
a3f0c7db
SM
1455
1456 message_iterator_class = create_message_iterator_class();
1457 if (!message_iterator_class) {
1458 goto end;
1459 }
1460
4212232c 1461 component_class_filter = bt_component_class_filter_create(name,
a3f0c7db 1462 message_iterator_class);
4212232c
PP
1463 if (!component_class_filter) {
1464 BT_LOGE_STR("Cannot create filter component class.");
1465 goto end;
1466 }
1467
1468 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1469
1470 if (component_class_set_help_and_desc(component_class, description, help)) {
1471 goto end;
1472 }
1473
21a9f056 1474 ret = bt_component_class_filter_set_initialize_method(component_class_filter, component_class_filter_init);
4212232c
PP
1475 BT_ASSERT(ret == 0);
1476 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1477 BT_ASSERT(ret == 0);
4212232c
PP
1478 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1479 component_class_filter_input_port_connected);
1480 BT_ASSERT(ret == 0);
1481 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1482 component_class_filter_output_port_connected);
1483 BT_ASSERT(ret == 0);
1484 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1485 BT_ASSERT(ret == 0);
fd57054b
PP
1486 ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions);
1487 BT_ASSERT(ret == 0);
4212232c
PP
1488 register_cc_ptr_to_py_cls(component_class, py_cls);
1489
1490end:
a3f0c7db 1491 bt_message_iterator_class_put_ref(message_iterator_class);
4212232c
PP
1492 return component_class_filter;
1493}
1494
1495static
1496bt_component_class_sink *bt_bt2_component_class_sink_create(
1497 PyObject *py_cls, const char *name, const char *description,
1498 const char *help)
1499{
1500 bt_component_class_sink *component_class_sink;
1501 bt_component_class *component_class;
1502 int ret;
1503
1504 BT_ASSERT(py_cls);
1505 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1506
1507 if (!component_class_sink) {
1508 BT_LOGE_STR("Cannot create sink component class.");
1509 goto end;
1510 }
1511
1512 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1513
1514 if (component_class_set_help_and_desc(component_class, description, help)) {
1515 goto end;
1516 }
1517
21a9f056 1518 ret = bt_component_class_sink_set_initialize_method(component_class_sink, component_class_sink_init);
4212232c
PP
1519 BT_ASSERT(ret == 0);
1520 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1521 BT_ASSERT(ret == 0);
1522 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1523 component_class_sink_input_port_connected);
1524 BT_ASSERT(ret == 0);
1525 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1526 component_class_sink_graph_is_configured);
1527 BT_ASSERT(ret == 0);
1528 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1529 BT_ASSERT(ret == 0);
fd57054b
PP
1530 ret = bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink, component_class_sink_get_supported_mip_versions);
1531 BT_ASSERT(ret == 0);
4212232c
PP
1532 register_cc_ptr_to_py_cls(component_class, py_cls);
1533
1534end:
1535 return component_class_sink;
1536}
This page took 0.103307 seconds and 4 git commands to generate.