bt2: free port user data when finalizing components
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i.h
CommitLineData
5fa16feb
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"
b20382e2 26#include "compat/glib.h"
5fa16feb
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
774b7f28
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
5fa16feb
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
5fa16feb
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);
774b7f28 102 bt_cc_ptr_to_py_cls = NULL;
5fa16feb
PP
103 }
104}
105
5fa16feb 106static inline
27ba49f1
SM
107int py_exc_to_status_clear(
108 bt_self_component_class *self_component_class,
5fa16feb
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{
7f3ee93d 113 int status;
5fa16feb
PP
114 PyObject *exc = PyErr_Occurred();
115
116 if (!exc) {
7f3ee93d 117 status = __BT_FUNC_STATUS_OK;
5fa16feb
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);
27ba49f1 149 log_exception_and_maybe_append_cause(BT_LOG_WARNING,
5fa16feb
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
27ba49f1 168int py_exc_to_status_component_class_clear(
5fa16feb
PP
169 bt_self_component_class *self_component_class,
170 int active_log_level)
171{
27ba49f1 172 return py_exc_to_status_clear(self_component_class, NULL, NULL, NULL,
5fa16feb
PP
173 active_log_level);
174}
175
176static
27ba49f1 177int py_exc_to_status_component_clear(bt_self_component *self_component)
5fa16feb 178{
27ba49f1 179 return py_exc_to_status_clear(NULL, self_component, NULL, NULL, -1);
5fa16feb
PP
180}
181
182static
27ba49f1 183int py_exc_to_status_message_iterator_clear(
5fa16feb
PP
184 bt_self_message_iterator *self_message_iterator)
185{
27ba49f1 186 return py_exc_to_status_clear(NULL, NULL, self_message_iterator, NULL, -1);
5fa16feb
PP
187}
188
b20382e2
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
5fa16feb
PP
195/* Component class proxy methods (delegate to the attached Python object) */
196
197static
4175c1d5 198bt_component_class_initialize_method_status component_class_init(
5fa16feb
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);
7f3ee93d 207 bt_component_class_initialize_method_status status;
5fa16feb
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
5fa16feb
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 *
b20382e2
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)
5fa16feb
PP
254 *
255 * _UserComponentType._bt_init_from_native() calls the Python
256 * component object's __init__() function.
b20382e2
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.
5fa16feb
PP
263 */
264 py_comp = PyObject_CallMethod(py_cls,
b20382e2
PP
265 "_bt_init_from_native", "(OOO)", py_comp_ptr, py_params_ptr,
266 init_method_data ? init_method_data : Py_None);
5fa16feb
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);
27ba49f1 271 status = py_exc_to_status_component_clear(self_component);
5fa16feb
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;
7f3ee93d
SM
282
283 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
284
5fa16feb
PP
285 goto end;
286
287error:
7f3ee93d
SM
288 /* This error path is for non-Python errors only. */
289 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
5fa16feb
PP
290
291end:
7f3ee93d 292 BT_ASSERT(!PyErr_Occurred());
5fa16feb
PP
293 Py_XDECREF(py_comp);
294 Py_XDECREF(py_params_ptr);
295 Py_XDECREF(py_comp_ptr);
296 return status;
297}
298
ed68d910
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;
7f3ee93d 313 bt_component_class_get_supported_mip_versions_method_status status;
ed68d910
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);
27ba49f1 346 status = py_exc_to_status_component_class_clear(self_component_class,
ed68d910
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
7f3ee93d
SM
380 status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK;
381
ed68d910
PP
382 goto end;
383
384error:
7f3ee93d
SM
385 /* This error path is for non-Python errors only. */
386 status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR;
ed68d910
PP
387
388end:
7f3ee93d 389 BT_ASSERT(!PyErr_Occurred());
ed68d910
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
5fa16feb
PP
447/*
448 * Method of bt_component_class_source to initialize a bt_self_component_source
449 * of that class.
450 */
451
452static
4175c1d5 453bt_component_class_initialize_method_status component_class_source_init(
5fa16feb 454 bt_self_component_source *self_component_source,
e3250e61 455 bt_self_component_source_configuration *config,
5fa16feb
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
4175c1d5 467bt_component_class_initialize_method_status component_class_filter_init(
5fa16feb 468 bt_self_component_filter *self_component_filter,
e3250e61 469 bt_self_component_filter_configuration *config,
5fa16feb
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
4175c1d5 481bt_component_class_initialize_method_status component_class_sink_init(
5fa16feb 482 bt_self_component_sink *self_component_sink,
e3250e61 483 bt_self_component_sink_configuration *config,
5fa16feb
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);
7f3ee93d
SM
498 PyObject *py_method_result;
499
5fa16feb
PP
500 BT_ASSERT(py_comp);
501
502 /* Call user's _user_finalize() method */
7f3ee93d
SM
503 py_method_result = PyObject_CallMethod(py_comp, "_user_finalize", NULL);
504 if (!py_method_result) {
5fa16feb
PP
505 bt_logging_level log_level = get_self_component_log_level(
506 self_component);
507
7f3ee93d
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 */
c7cc9a2a
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
7f3ee93d 517 goto end;
5fa16feb
PP
518 }
519
7f3ee93d
SM
520 BT_ASSERT(py_method_result == Py_None);
521
522end:
5fa16feb
PP
523 Py_XDECREF(py_method_result);
524 Py_DECREF(py_comp);
525}
526
f002c045
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
5fa16feb
PP
553static
554void component_class_source_finalize(bt_self_component_source *self_component_source)
555{
f002c045
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
5fa16feb 565 component_class_finalize(self_component);
f002c045
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 }
5fa16feb
PP
580}
581
582static
583void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
584{
f002c045
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
5fa16feb 594 component_class_finalize(self_component);
f002c045
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 }
5fa16feb
PP
618}
619
620static
621void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
622{
f002c045
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
5fa16feb 632 component_class_finalize(self_component);
f002c045
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 }
5fa16feb
PP
647}
648
649static
9e8e8b43
SM
650bt_component_class_message_iterator_can_seek_beginning_method_status
651component_class_can_seek_beginning(
652 bt_self_message_iterator *self_message_iterator, bt_bool *can_seek)
5fa16feb
PP
653{
654 PyObject *py_iter;
655 PyObject *py_result = NULL;
9e8e8b43 656 bt_component_class_message_iterator_can_seek_beginning_method_status status;
5fa16feb 657 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
7f3ee93d 658
5fa16feb
PP
659 BT_ASSERT(py_iter);
660
5c836b95
SM
661 py_result = PyObject_CallMethod(py_iter,
662 "_bt_can_seek_beginning_from_native", NULL);
7f3ee93d 663 if (!py_result) {
27ba49f1 664 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
7f3ee93d 665 goto end;
5fa16feb
PP
666 }
667
7f3ee93d
SM
668 BT_ASSERT(PyBool_Check(py_result));
669 *can_seek = PyObject_IsTrue(py_result);
670
671 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
672
673end:
5fa16feb
PP
674 Py_XDECREF(py_result);
675
9e8e8b43 676 return status;
5fa16feb
PP
677}
678
679static
680bt_component_class_message_iterator_seek_beginning_method_status
681component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
682{
683 PyObject *py_iter;
684 PyObject *py_result;
685 bt_component_class_message_iterator_seek_beginning_method_status status;
686
687 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
688 BT_ASSERT(py_iter);
7f3ee93d
SM
689
690 py_result = PyObject_CallMethod(py_iter,
691 "_bt_seek_beginning_from_native",
5fa16feb 692 NULL);
7f3ee93d
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
700 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
701
702end:
5fa16feb 703 Py_XDECREF(py_result);
7f3ee93d 704
5fa16feb
PP
705 return status;
706}
707
24a68958
SM
708static
709bt_component_class_message_iterator_can_seek_ns_from_origin_method_status
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;
716 bt_component_class_message_iterator_can_seek_ns_from_origin_method_status status;
7f3ee93d 717
24a68958
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);
7f3ee93d 723 if (!py_result) {
27ba49f1 724 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
7f3ee93d 725 goto end;
24a68958
SM
726 }
727
7f3ee93d
SM
728 BT_ASSERT(PyBool_Check(py_result));
729 *can_seek = PyObject_IsTrue(py_result);
730
731 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
732
733end:
24a68958
SM
734 Py_XDECREF(py_result);
735
736 return status;
737}
738
739static
740bt_component_class_message_iterator_seek_ns_from_origin_method_status
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;
747 bt_component_class_message_iterator_seek_ns_from_origin_method_status status;
748
749 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
750 BT_ASSERT(py_iter);
7f3ee93d 751
24a68958
SM
752 py_result = PyObject_CallMethod(py_iter,
753 "_bt_seek_ns_from_origin_from_native", "L", ns_from_origin);
7f3ee93d
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
762 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
763
764end:
24a68958 765 Py_XDECREF(py_result);
7f3ee93d 766
24a68958
SM
767 return status;
768}
769
5fa16feb
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;
7f3ee93d
SM
804 goto end;
805 }
5fa16feb
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);
7f3ee93d
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;
5fa16feb
PP
818
819end:
820 Py_XDECREF(py_self_port_ptr);
821 Py_XDECREF(py_other_port_ptr);
822 Py_XDECREF(py_method_result);
7f3ee93d 823
5fa16feb
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;
7f3ee93d 906 bt_component_class_sink_graph_is_configured_method_status status;
5fa16feb
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);
7f3ee93d 910
5fa16feb
PP
911 py_method_result = PyObject_CallMethod(py_comp,
912 "_bt_graph_is_configured_from_native", NULL);
7f3ee93d
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:
5fa16feb
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,
f6e305ce 932 const char *object, const bt_value *params, void *method_data,
5fa16feb
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
f6e305ce
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
5fa16feb
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
f6e305ce
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 */
5fa16feb 995 py_results_addr = PyObject_CallMethod(py_cls,
f6e305ce
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);
5fa16feb 999 if (!py_results_addr) {
27ba49f1 1000 status = py_exc_to_status_component_class_clear(self_component_class,
5fa16feb 1001 log_level);
afe3f8ce
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 }
5fa16feb
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,
f6e305ce 1040 const char *object, const bt_value *params, void *method_data,
5fa16feb
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,
f6e305ce 1048 priv_query_executor, object, params, method_data, result);
5fa16feb
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,
f6e305ce 1055 const char *object, const bt_value *params, void *method_data,
5fa16feb
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,
f6e305ce 1063 priv_query_executor, object, params, method_data, result);
5fa16feb
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,
f6e305ce 1070 const char *object, const bt_value *params, void *method_data,
5fa16feb
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,
f6e305ce 1078 priv_query_executor, object, params, method_data, result);
5fa16feb
PP
1079}
1080
1081static
4175c1d5 1082bt_component_class_message_iterator_initialize_method_status
5fa16feb
PP
1083component_class_message_iterator_init(
1084 bt_self_message_iterator *self_message_iterator,
9415de1c 1085 bt_self_message_iterator_configuration *config,
5fa16feb
PP
1086 bt_self_component *self_component,
1087 bt_self_component_port_output *self_component_port_output)
1088{
4175c1d5 1089 bt_component_class_message_iterator_initialize_method_status status = __BT_FUNC_STATUS_OK;
5fa16feb
PP
1090 PyObject *py_comp_cls = NULL;
1091 PyObject *py_iter_cls = NULL;
1092 PyObject *py_iter_ptr = NULL;
9415de1c 1093 PyObject *py_config_ptr = NULL;
5fa16feb
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 *
9415de1c 1148 * py_iter.__init__(config, self_output_port)
5fa16feb 1149 *
9415de1c 1150 * through the _init_from_native helper static method.
5fa16feb
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 */
9415de1c
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
5fa16feb
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,
9415de1c
SM
1183 "_bt_init_from_native", "OO", py_config_ptr,
1184 py_component_port_output_ptr);
5fa16feb
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. */
27ba49f1 1215 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
5fa16feb
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
1234static
4175c1d5 1235bt_component_class_message_iterator_initialize_method_status
5fa16feb
PP
1236component_class_source_message_iterator_init(
1237 bt_self_message_iterator *self_message_iterator,
9415de1c 1238 bt_self_message_iterator_configuration *config,
5fa16feb
PP
1239 bt_self_component_source *self_component_source,
1240 bt_self_component_port_output *self_component_port_output)
1241{
9415de1c
SM
1242 bt_self_component *self_component =
1243 bt_self_component_source_as_self_component(self_component_source);
5fa16feb 1244
9415de1c
SM
1245 return component_class_message_iterator_init(self_message_iterator,
1246 config, self_component, self_component_port_output);
5fa16feb
PP
1247}
1248
1249static
4175c1d5 1250bt_component_class_message_iterator_initialize_method_status
5fa16feb
PP
1251component_class_filter_message_iterator_init(
1252 bt_self_message_iterator *self_message_iterator,
9415de1c 1253 bt_self_message_iterator_configuration *config,
5fa16feb
PP
1254 bt_self_component_filter *self_component_filter,
1255 bt_self_component_port_output *self_component_port_output)
1256{
9415de1c
SM
1257 bt_self_component *self_component =
1258 bt_self_component_filter_as_self_component(self_component_filter);
5fa16feb 1259
9415de1c
SM
1260 return component_class_message_iterator_init(self_message_iterator,
1261 config, self_component, self_component_port_output);
5fa16feb
PP
1262}
1263
1264static
1265void component_class_message_iterator_finalize(
1266 bt_self_message_iterator *message_iterator)
1267{
1268 PyObject *py_message_iter = bt_self_message_iterator_get_data(
1269 message_iterator);
1270 PyObject *py_method_result = NULL;
1271
1272 BT_ASSERT(py_message_iter);
1273
1274 /* Call user's _user_finalize() method */
1275 py_method_result = PyObject_CallMethod(py_message_iter,
1276 "_user_finalize", NULL);
7f3ee93d 1277 if (!py_method_result) {
5fa16feb
PP
1278 bt_self_component *self_comp =
1279 bt_self_message_iterator_borrow_component(
1280 message_iterator);
1281 bt_logging_level log_level = get_self_component_log_level(
1282 self_comp);
1283
7f3ee93d
SM
1284 /*
1285 * Ignore any exception raised by the _user_finalize() method
1286 * because it won't change anything at this point: the component
1287 * is being destroyed anyway.
1288 */
c7cc9a2a
SM
1289 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp,
1290 "User's _user_finalize() method raised an exception: ignoring:");
1291 logw_exception_clear(get_self_message_iterator_log_level(
1292 message_iterator));
5fa16feb
PP
1293 }
1294
5fa16feb
PP
1295 Py_XDECREF(py_method_result);
1296 Py_DECREF(py_message_iter);
1297}
1298
1299/* Valid for both sources and filters. */
1300
1301static
1302bt_component_class_message_iterator_next_method_status
1303component_class_message_iterator_next(
1304 bt_self_message_iterator *message_iterator,
1305 bt_message_array_const msgs, uint64_t capacity,
1306 uint64_t *count)
1307{
7f3ee93d 1308 bt_component_class_message_iterator_next_method_status status;
5fa16feb
PP
1309 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1310 PyObject *py_method_result = NULL;
1311
ec4a3354 1312 BT_ASSERT_DBG(py_message_iter);
5fa16feb
PP
1313 py_method_result = PyObject_CallMethod(py_message_iter,
1314 "_bt_next_from_native", NULL);
1315 if (!py_method_result) {
27ba49f1 1316 status = py_exc_to_status_message_iterator_clear(message_iterator);
5fa16feb
PP
1317 goto end;
1318 }
1319
1320 /*
1321 * The returned object, on success, is an integer object
1322 * (PyLong) containing the address of a native message
1323 * object (which is now ours).
1324 */
1325 msgs[0] = PyLong_AsVoidPtr(py_method_result);
1326 *count = 1;
1327
7f3ee93d 1328 /* Overflow errors should never happen. */
ec4a3354 1329 BT_ASSERT_DBG(!PyErr_Occurred());
7f3ee93d
SM
1330
1331 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
5fa16feb
PP
1332
1333end:
1334 Py_XDECREF(py_method_result);
1335 return status;
1336}
1337
1338static
1339bt_component_class_sink_consume_method_status
1340component_class_sink_consume(bt_self_component_sink *self_component_sink)
1341{
1342 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1343 PyObject *py_comp = bt_self_component_get_data(self_component);
1344 PyObject *py_method_result = NULL;
1345 bt_component_class_sink_consume_method_status status;
1346
ec4a3354 1347 BT_ASSERT_DBG(py_comp);
7f3ee93d 1348
5fa16feb
PP
1349 py_method_result = PyObject_CallMethod(py_comp,
1350 "_user_consume", NULL);
7f3ee93d
SM
1351 if (!py_method_result) {
1352 status = py_exc_to_status_component_clear(self_component);
1353 goto end;
1354 }
1355
1356 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
1357
1358end:
5fa16feb
PP
1359 Py_XDECREF(py_method_result);
1360 return status;
1361}
1362
1363static
1364int component_class_set_help_and_desc(
1365 bt_component_class *component_class,
1366 const char *description, const char *help)
1367{
1368 int ret;
1369
1370 if (description) {
1371 ret = bt_component_class_set_description(component_class, description);
1372 if (ret) {
1373 BT_LOGE("Cannot set component class's description: "
1374 "comp-cls-addr=%p", component_class);
1375 goto end;
1376 }
1377 }
1378
1379 if (help) {
1380 ret = bt_component_class_set_help(component_class, help);
1381 if (ret) {
1382 BT_LOGE("Cannot set component class's help text: "
1383 "comp-cls-addr=%p", component_class);
1384 goto end;
1385 }
1386 }
1387
1388 ret = 0;
1389
1390end:
1391 return ret;
1392}
1393
1394static
1395bt_component_class_source *bt_bt2_component_class_source_create(
1396 PyObject *py_cls, const char *name, const char *description,
1397 const char *help)
1398{
1399 bt_component_class_source *component_class_source;
1400 bt_component_class *component_class;
1401 int ret;
1402
1403 BT_ASSERT(py_cls);
1404 component_class_source = bt_component_class_source_create(name,
1405 component_class_message_iterator_next);
1406 if (!component_class_source) {
1407 BT_LOGE_STR("Cannot create source component class.");
1408 goto end;
1409 }
1410
1411 component_class = bt_component_class_source_as_component_class(component_class_source);
1412
1413 if (component_class_set_help_and_desc(component_class, description, help)) {
1414 goto end;
1415 }
1416
4175c1d5 1417 ret = bt_component_class_source_set_initialize_method(component_class_source, component_class_source_init);
5fa16feb
PP
1418 BT_ASSERT(ret == 0);
1419 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1420 BT_ASSERT(ret == 0);
7f8e969d
SM
1421 ret = bt_component_class_source_set_message_iterator_seek_beginning_methods(component_class_source,
1422 component_class_seek_beginning, component_class_can_seek_beginning);
1423 ret = bt_component_class_source_set_message_iterator_seek_ns_from_origin_methods(
1424 component_class_source, component_class_seek_ns_from_origin,
1425 component_class_can_seek_ns_from_origin);
5fa16feb
PP
1426 BT_ASSERT(ret == 0);
1427 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1428 component_class_source_output_port_connected);
1429 BT_ASSERT(ret == 0);
1430 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1431 BT_ASSERT(ret == 0);
ed68d910
PP
1432 ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions);
1433 BT_ASSERT(ret == 0);
4175c1d5 1434 ret = bt_component_class_source_set_message_iterator_initialize_method(
5fa16feb
PP
1435 component_class_source, component_class_source_message_iterator_init);
1436 BT_ASSERT(ret == 0);
1437 ret = bt_component_class_source_set_message_iterator_finalize_method(
1438 component_class_source, component_class_message_iterator_finalize);
1439 BT_ASSERT(ret == 0);
1440 register_cc_ptr_to_py_cls(component_class, py_cls);
1441
1442end:
1443 return component_class_source;
1444}
1445
1446static
1447bt_component_class_filter *bt_bt2_component_class_filter_create(
1448 PyObject *py_cls, const char *name, const char *description,
1449 const char *help)
1450{
1451 bt_component_class *component_class;
1452 bt_component_class_filter *component_class_filter;
1453 int ret;
1454
1455 BT_ASSERT(py_cls);
1456 component_class_filter = bt_component_class_filter_create(name,
1457 component_class_message_iterator_next);
1458 if (!component_class_filter) {
1459 BT_LOGE_STR("Cannot create filter component class.");
1460 goto end;
1461 }
1462
1463 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1464
1465 if (component_class_set_help_and_desc(component_class, description, help)) {
1466 goto end;
1467 }
1468
4175c1d5 1469 ret = bt_component_class_filter_set_initialize_method(component_class_filter, component_class_filter_init);
5fa16feb
PP
1470 BT_ASSERT(ret == 0);
1471 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1472 BT_ASSERT(ret == 0);
7f8e969d
SM
1473 ret = bt_component_class_filter_set_message_iterator_seek_beginning_methods(component_class_filter,
1474 component_class_seek_beginning, component_class_can_seek_beginning);
24a68958 1475 BT_ASSERT(ret == 0);
7f8e969d
SM
1476 ret = bt_component_class_filter_set_message_iterator_seek_ns_from_origin_methods(
1477 component_class_filter, component_class_seek_ns_from_origin,
1478 component_class_can_seek_ns_from_origin);
5fa16feb
PP
1479 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1480 component_class_filter_input_port_connected);
1481 BT_ASSERT(ret == 0);
1482 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1483 component_class_filter_output_port_connected);
1484 BT_ASSERT(ret == 0);
1485 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1486 BT_ASSERT(ret == 0);
ed68d910
PP
1487 ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions);
1488 BT_ASSERT(ret == 0);
4175c1d5 1489 ret = bt_component_class_filter_set_message_iterator_initialize_method(
5fa16feb
PP
1490 component_class_filter, component_class_filter_message_iterator_init);
1491 BT_ASSERT(ret == 0);
1492 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1493 component_class_filter, component_class_message_iterator_finalize);
1494 BT_ASSERT(ret == 0);
1495 register_cc_ptr_to_py_cls(component_class, py_cls);
1496
1497end:
1498 return component_class_filter;
1499}
1500
1501static
1502bt_component_class_sink *bt_bt2_component_class_sink_create(
1503 PyObject *py_cls, const char *name, const char *description,
1504 const char *help)
1505{
1506 bt_component_class_sink *component_class_sink;
1507 bt_component_class *component_class;
1508 int ret;
1509
1510 BT_ASSERT(py_cls);
1511 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1512
1513 if (!component_class_sink) {
1514 BT_LOGE_STR("Cannot create sink component class.");
1515 goto end;
1516 }
1517
1518 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1519
1520 if (component_class_set_help_and_desc(component_class, description, help)) {
1521 goto end;
1522 }
1523
4175c1d5 1524 ret = bt_component_class_sink_set_initialize_method(component_class_sink, component_class_sink_init);
5fa16feb
PP
1525 BT_ASSERT(ret == 0);
1526 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1527 BT_ASSERT(ret == 0);
1528 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1529 component_class_sink_input_port_connected);
1530 BT_ASSERT(ret == 0);
1531 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1532 component_class_sink_graph_is_configured);
1533 BT_ASSERT(ret == 0);
1534 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1535 BT_ASSERT(ret == 0);
ed68d910
PP
1536 ret = bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink, component_class_sink_get_supported_mip_versions);
1537 BT_ASSERT(ret == 0);
5fa16feb
PP
1538 register_cc_ptr_to_py_cls(component_class, py_cls);
1539
1540end:
1541 return component_class_sink;
1542}
This page took 0.100574 seconds and 4 git commands to generate.