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