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