12a4fa0453b5ff559c24b8ce136a2ee17df1384b
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i
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 <babeltrace2/graph/component-class.h>
26 %include <babeltrace2/graph/component-class-const.h>
27 %include <babeltrace2/graph/component-class-source-const.h>
28 %include <babeltrace2/graph/component-class-source.h>
29 %include <babeltrace2/graph/component-class-filter-const.h>
30 %include <babeltrace2/graph/component-class-filter.h>
31 %include <babeltrace2/graph/component-class-sink-const.h>
32 %include <babeltrace2/graph/component-class-sink.h>
33 %include <babeltrace2/graph/self-component-class-source.h>
34 %include <babeltrace2/graph/self-component-class-filter.h>
35 %include <babeltrace2/graph/self-component-class-sink.h>
36
37 %{
38 /*
39 * This hash table associates a BT component class object address to a
40 * user-defined Python class (PyObject *). The keys and values are NOT
41 * owned by this hash table. The Python class objects are owned by the
42 * Python module, which should not be unloaded until it is not possible
43 * to create a user Python component anyway.
44 *
45 * This hash table is written to when a user-defined Python component
46 * class is created by one of the bt_bt2_component_class_*_create()
47 * functions.
48 *
49 * This function is read from when a user calls bt_component_create()
50 * with a component class pointer created by one of the functions above.
51 * In this case, the original Python class needs to be found to
52 * instantiate it and associate the created Python component object with
53 * a BT component object instance.
54 */
55
56 static GHashTable *bt_cc_ptr_to_py_cls;
57
58 static
59 void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
60 PyObject *py_cls)
61 {
62 if (!bt_cc_ptr_to_py_cls) {
63 /*
64 * Lazy-initializing this GHashTable because GLib
65 * might not be initialized yet and it needs to be
66 * before we call g_hash_table_new()
67 */
68 BT_LOGD_STR("Creating native component class to Python component class hash table.");
69 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
70 BT_ASSERT(bt_cc_ptr_to_py_cls);
71 }
72
73 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
74 (gpointer) py_cls);
75 }
76
77 static
78 PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
79 {
80 if (!bt_cc_ptr_to_py_cls) {
81 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
82 "comp-cls-addr=%p", bt_cc);
83 return NULL;
84 }
85
86 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
87 (gconstpointer) bt_cc);
88 }
89
90
91 /*
92 * Useful Python objects.
93 */
94
95 static PyObject *py_mod_bt2 = NULL;
96 static PyObject *py_mod_bt2_exc_error_type = NULL;
97 static PyObject *py_mod_bt2_exc_memory_error = NULL;
98 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
99 static PyObject *py_mod_bt2_exc_stop_type = NULL;
100 static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
101 static PyObject *py_mod_bt2_exc_invalid_object_type = NULL;
102 static PyObject *py_mod_bt2_exc_invalid_params_type = NULL;
103 static PyObject *py_mod_bt2_exc_unsupported_type = NULL;
104
105 static
106 void bt_bt2_cc_init_from_bt2(void)
107 {
108 /*
109 * This is called once the bt2 package is loaded.
110 *
111 * Those modules and functions are needed while the package is
112 * used. Loading them here is safe because we know the bt2
113 * package is imported, and we know that the user cannot use the
114 * code here without importing bt2 first.
115 */
116 py_mod_bt2 = PyImport_ImportModule("bt2");
117 BT_ASSERT(py_mod_bt2);
118 py_mod_bt2_exc_error_type =
119 PyObject_GetAttrString(py_mod_bt2, "_Error");
120 BT_ASSERT(py_mod_bt2_exc_error_type);
121 py_mod_bt2_exc_memory_error =
122 PyObject_GetAttrString(py_mod_bt2, "_MemoryError");
123 BT_ASSERT(py_mod_bt2_exc_memory_error);
124 py_mod_bt2_exc_try_again_type =
125 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
126 BT_ASSERT(py_mod_bt2_exc_try_again_type);
127 py_mod_bt2_exc_stop_type =
128 PyObject_GetAttrString(py_mod_bt2, "Stop");
129 BT_ASSERT(py_mod_bt2_exc_stop_type);
130 py_mod_bt2_exc_invalid_object_type =
131 PyObject_GetAttrString(py_mod_bt2, "InvalidObject");
132 BT_ASSERT(py_mod_bt2_exc_invalid_object_type);
133 py_mod_bt2_exc_invalid_params_type =
134 PyObject_GetAttrString(py_mod_bt2, "InvalidParams");
135 BT_ASSERT(py_mod_bt2_exc_invalid_params_type);
136 py_mod_bt2_exc_unsupported_type =
137 PyObject_GetAttrString(py_mod_bt2, "Unsupported");
138 BT_ASSERT(py_mod_bt2_exc_unsupported_type);
139 }
140
141 static
142 void bt_bt2_cc_exit_handler(void)
143 {
144 /*
145 * This is an exit handler (set by the bt2 package).
146 *
147 * We only give back the references that we took in
148 * bt_bt2_cc_init_from_bt2() here. The global variables continue
149 * to exist for the code of this file, but they are now borrowed
150 * references. If this code is executed, it means that somehow
151 * the modules are still loaded, so it should be safe to use
152 * them even without a strong reference.
153 *
154 * We cannot do this in the library's destructor because it
155 * gets executed once Python is already finalized.
156 */
157 Py_XDECREF(py_mod_bt2);
158 Py_XDECREF(py_mod_bt2_exc_error_type);
159 Py_XDECREF(py_mod_bt2_exc_try_again_type);
160 Py_XDECREF(py_mod_bt2_exc_stop_type);
161 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
162 Py_XDECREF(py_mod_bt2_exc_invalid_object_type);
163 Py_XDECREF(py_mod_bt2_exc_invalid_params_type);
164 }
165
166
167 /* Library destructor */
168
169 __attribute__((destructor))
170 static
171 void native_comp_class_dtor(void) {
172 /* Destroy component class association hash table */
173 if (bt_cc_ptr_to_py_cls) {
174 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
175 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
176 }
177 }
178
179 static
180 void restore_current_thread_error_and_append_exception_chain_recursive(
181 PyObject *py_exc_value,
182 bt_self_component_class *self_component_class,
183 bt_self_component *self_component,
184 bt_self_message_iterator *self_message_iterator,
185 const char *module_name)
186 {
187 PyObject *py_exc_cause_value;
188 PyObject *py_exc_type = NULL;
189 PyObject *py_exc_tb = NULL;
190 GString *gstr = NULL;
191
192 /* If this exception has a cause, handle that one first. */
193 py_exc_cause_value = PyException_GetCause(py_exc_value);
194 if (py_exc_cause_value) {
195 restore_current_thread_error_and_append_exception_chain_recursive(
196 py_exc_cause_value, self_component_class,
197 self_component, self_message_iterator, module_name);
198 }
199
200 /*
201 * If the raised exception is a bt2._Error, restore the wrapped error.
202 */
203 if (PyErr_GivenExceptionMatches(py_exc_value, py_mod_bt2_exc_error_type)) {
204 PyObject *py_error_swig_ptr;
205 const bt_error *error;
206 int ret;
207
208 /*
209 * We never raise a bt2._Error with a cause: it should be the
210 * end of the chain.
211 */
212 BT_ASSERT(!py_exc_cause_value);
213
214 /*
215 * We steal the error object from the exception, to move
216 * it back as the current thread's error.
217 */
218 py_error_swig_ptr = PyObject_GetAttrString(py_exc_value, "_ptr");
219 BT_ASSERT(py_error_swig_ptr);
220
221 ret = PyObject_SetAttrString(py_exc_value, "_ptr", Py_None);
222 BT_ASSERT(ret == 0);
223
224 ret = SWIG_ConvertPtr(py_error_swig_ptr, (void **) &error,
225 SWIGTYPE_p_bt_error, 0);
226 BT_ASSERT(ret == 0);
227
228 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(error);
229
230 Py_DECREF(py_error_swig_ptr);
231 }
232
233 py_exc_type = PyObject_Type(py_exc_value);
234 py_exc_tb = PyException_GetTraceback(py_exc_value);
235
236 gstr = bt_py_common_format_exception(py_exc_type, py_exc_value,
237 py_exc_tb, BT_LOG_OUTPUT_LEVEL, false);
238 if (!gstr) {
239 /* bt_py_common_format_exception has already warned. */
240 goto end;
241 }
242
243 if (self_component_class) {
244 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
245 self_component_class, "%s", gstr->str);
246 } else if (self_component) {
247 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
248 self_component, "%s", gstr->str);
249 } else if (self_message_iterator) {
250 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
251 self_message_iterator, "%s", gstr->str);
252 } else {
253 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
254 module_name, "%s", gstr->str);
255 }
256
257 end:
258 if (gstr) {
259 g_string_free(gstr, TRUE);
260 }
261
262 Py_XDECREF(py_exc_cause_value);
263 Py_XDECREF(py_exc_type);
264 Py_XDECREF(py_exc_tb);
265 }
266
267 /*
268 * If you have the following code:
269 *
270 * try:
271 * try:
272 * something_that_raises_bt2_error()
273 * except bt2._Error as e1:
274 * raise ValueError from e1
275 * except ValueError as e2:
276 * raise TypeError from e2
277 *
278 * We will have the following exception chain:
279 *
280 * TypeError -> ValueError -> bt2._Error
281 *
282 * Where the TypeError is the current exception (obtained from PyErr_Fetch).
283 *
284 * The bt2._Error contains a `struct bt_error *` that used to be the current
285 * thread's error, at the moment the exception was raised.
286 *
287 * This function gets to the bt2._Error and restores the wrapped
288 * `struct bt_error *` as the current thread's error.
289 *
290 * Then, for each exception in the chain, starting with the oldest one, it adds
291 * an error cause to the current thread's error.
292 */
293 static
294 void restore_bt_error_and_append_current_exception_chain(
295 bt_self_component_class *self_component_class,
296 bt_self_component *self_component,
297 bt_self_message_iterator *self_message_iterator,
298 const char *module_name)
299 {
300 BT_ASSERT(PyErr_Occurred());
301
302 /* Used to access and restore the current exception. */
303 PyObject *py_exc_type;
304 PyObject *py_exc_value;
305 PyObject *py_exc_tb;
306
307 /* Fetch and normalize the Python exception. */
308 PyErr_Fetch(&py_exc_type, &py_exc_value, &py_exc_tb);
309 PyErr_NormalizeException(&py_exc_type, &py_exc_value, &py_exc_tb);
310 BT_ASSERT(py_exc_type);
311 BT_ASSERT(py_exc_value);
312 BT_ASSERT(py_exc_tb);
313
314 /*
315 * Set the exception's traceback so it's possible to get it using
316 * PyException_GetTraceback in
317 * restore_current_thread_error_and_append_exception_chain_recursive.
318 */
319 PyException_SetTraceback(py_exc_value, py_exc_tb);
320
321 restore_current_thread_error_and_append_exception_chain_recursive(py_exc_value,
322 self_component_class, self_component, self_message_iterator,
323 module_name);
324
325 PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb);
326 }
327
328 static inline
329 void log_exception_and_maybe_append_error(int log_level,
330 bool append_error,
331 bt_self_component_class *self_component_class,
332 bt_self_component *self_component,
333 bt_self_message_iterator *self_message_iterator,
334 const char *module_name)
335 {
336 GString *gstr;
337
338 BT_ASSERT(PyErr_Occurred());
339 gstr = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL);
340 if (!gstr) {
341 /* bt_py_common_format_current_exception() logs errors */
342 goto end;
343 }
344
345 BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str);
346
347 if (append_error) {
348 restore_bt_error_and_append_current_exception_chain(
349 self_component_class, self_component,
350 self_message_iterator, module_name);
351
352 }
353
354 end:
355 if (gstr) {
356 g_string_free(gstr, TRUE);
357 }
358 }
359
360 static inline
361 void loge_exception(const char *module_name)
362 {
363 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL,
364 NULL, module_name);
365 }
366
367 static
368 void loge_exception_message_iterator(
369 bt_self_message_iterator *self_message_iterator)
370 {
371 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL,
372 self_message_iterator, NULL);
373 }
374
375 static inline
376 void logw_exception(void)
377 {
378 log_exception_and_maybe_append_error(BT_LOG_WARNING, false, NULL, NULL,
379 NULL, NULL);
380 }
381
382 static inline
383 int py_exc_to_status(bt_self_component_class *self_component_class,
384 bt_self_component *self_component,
385 bt_self_message_iterator *self_message_iterator,
386 const char *module_name)
387 {
388 int status = __BT_FUNC_STATUS_OK;
389 PyObject *exc = PyErr_Occurred();
390
391 if (!exc) {
392 goto end;
393 }
394
395 if (PyErr_GivenExceptionMatches(exc,
396 py_mod_bt2_exc_try_again_type)) {
397 status = __BT_FUNC_STATUS_AGAIN;
398 } else if (PyErr_GivenExceptionMatches(exc,
399 py_mod_bt2_exc_stop_type)) {
400 status = __BT_FUNC_STATUS_END;
401 } else if (PyErr_GivenExceptionMatches(exc,
402 py_mod_bt2_exc_invalid_object_type)) {
403 status = __BT_FUNC_STATUS_INVALID_OBJECT;
404 } else if (PyErr_GivenExceptionMatches(exc,
405 py_mod_bt2_exc_invalid_params_type)) {
406 status = __BT_FUNC_STATUS_INVALID_PARAMS;
407 } else if (PyErr_GivenExceptionMatches(exc,
408 py_mod_bt2_exc_unsupported_type)) {
409 status = __BT_FUNC_STATUS_UNSUPPORTED;
410 } else {
411 /* Unknown exception: convert to general error */
412 log_exception_and_maybe_append_error(BT_LOG_WARNING, true,
413 self_component_class, self_component,
414 self_message_iterator, module_name);
415
416 if (PyErr_GivenExceptionMatches(exc,
417 py_mod_bt2_exc_memory_error)) {
418 status = __BT_FUNC_STATUS_MEMORY_ERROR;
419 } else {
420 status = __BT_FUNC_STATUS_ERROR;
421 }
422 }
423
424 end:
425 PyErr_Clear();
426 return status;
427 }
428
429 static
430 int py_exc_to_status_component_class(bt_self_component_class *self_component_class)
431 {
432 return py_exc_to_status(self_component_class, NULL, NULL, NULL);
433 }
434
435 static
436 int py_exc_to_status_component(bt_self_component *self_component)
437 {
438 return py_exc_to_status(NULL, self_component, NULL, NULL);
439 }
440
441 static
442 int py_exc_to_status_message_iterator(
443 bt_self_message_iterator *self_message_iterator)
444 {
445 return py_exc_to_status(NULL, NULL, self_message_iterator, NULL);
446 }
447
448 /* Component class proxy methods (delegate to the attached Python object) */
449
450 static
451 bt_component_class_init_method_status component_class_init(
452 bt_self_component *self_component,
453 void *self_component_v,
454 swig_type_info *self_comp_cls_type_swig_type,
455 const bt_value *params,
456 void *init_method_data)
457 {
458 const bt_component *component = bt_self_component_as_component(self_component);
459 const bt_component_class *component_class = bt_component_borrow_class_const(component);
460 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
461 PyObject *py_cls = NULL;
462 PyObject *py_comp = NULL;
463 PyObject *py_params_ptr = NULL;
464 PyObject *py_comp_ptr = NULL;
465
466 (void) init_method_data;
467
468 BT_ASSERT(self_component);
469 BT_ASSERT(self_component_v);
470 BT_ASSERT(self_comp_cls_type_swig_type);
471
472 /*
473 * Get the user-defined Python class which created this
474 * component's class in the first place (borrowed
475 * reference).
476 */
477 py_cls = lookup_cc_ptr_to_py_cls(component_class);
478 if (!py_cls) {
479 BT_LOGE("Cannot find Python class associated to native component class: "
480 "comp-cls-addr=%p", component_class);
481 goto error;
482 }
483
484 /* Parameters pointer -> SWIG pointer Python object */
485 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
486 SWIGTYPE_p_bt_value, 0);
487 if (!py_params_ptr) {
488 BT_LOGE_STR("Failed to create a SWIG pointer object.");
489 goto error;
490 }
491
492 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
493 self_comp_cls_type_swig_type, 0);
494 if (!py_comp_ptr) {
495 BT_LOGE_STR("Failed to create a SWIG pointer object.");
496 goto error;
497 }
498
499 /*
500 * Do the equivalent of this:
501 *
502 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
503 *
504 * _UserComponentType._bt_init_from_native() calls the Python
505 * component object's __init__() function.
506 */
507 py_comp = PyObject_CallMethod(py_cls,
508 "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
509 if (!py_comp) {
510 BT_LOGW("Failed to call Python class's _bt_init_from_native() method: "
511 "py-cls-addr=%p", py_cls);
512 status = py_exc_to_status_component(self_component);
513 goto end;
514 }
515
516 /*
517 * Our user Python component object is now fully created and
518 * initialized by the user. Since we just created it, this
519 * native component is its only (persistent) owner.
520 */
521 bt_self_component_set_data(self_component, py_comp);
522 py_comp = NULL;
523 goto end;
524
525 error:
526 status = __BT_FUNC_STATUS_ERROR;
527
528 /*
529 * Clear any exception: we're returning a bad status anyway. If
530 * this call originated from Python (creation from a plugin's
531 * component class, for example), then the user gets an
532 * appropriate creation error.
533 */
534 PyErr_Clear();
535
536 end:
537 Py_XDECREF(py_comp);
538 Py_XDECREF(py_params_ptr);
539 Py_XDECREF(py_comp_ptr);
540 return status;
541 }
542
543 /*
544 * Method of bt_component_class_source to initialize a bt_self_component_source
545 * of that class.
546 */
547
548 static
549 bt_component_class_init_method_status component_class_source_init(
550 bt_self_component_source *self_component_source,
551 const bt_value *params, void *init_method_data)
552 {
553 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
554 return component_class_init(
555 self_component,
556 self_component_source,
557 SWIGTYPE_p_bt_self_component_source,
558 params, init_method_data);
559 }
560
561 static
562 bt_component_class_init_method_status component_class_filter_init(
563 bt_self_component_filter *self_component_filter,
564 const bt_value *params, void *init_method_data)
565 {
566 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
567 return component_class_init(
568 self_component,
569 self_component_filter,
570 SWIGTYPE_p_bt_self_component_filter,
571 params, init_method_data);
572 }
573
574 static
575 bt_component_class_init_method_status component_class_sink_init(
576 bt_self_component_sink *self_component_sink,
577 const bt_value *params, void *init_method_data)
578 {
579 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
580 return component_class_init(
581 self_component,
582 self_component_sink,
583 SWIGTYPE_p_bt_self_component_sink,
584 params, init_method_data);
585 }
586
587 static
588 void component_class_finalize(bt_self_component *self_component)
589 {
590 PyObject *py_comp = bt_self_component_get_data(self_component);
591 BT_ASSERT(py_comp);
592
593 /* Call user's _finalize() method */
594 PyObject *py_method_result = PyObject_CallMethod(py_comp,
595 "_finalize", NULL);
596
597 if (PyErr_Occurred()) {
598 BT_LOGW("User component's _finalize() method raised an exception: ignoring:");
599 logw_exception();
600 }
601
602 /*
603 * Ignore any exception raised by the _finalize() method because
604 * it won't change anything at this point: the component is
605 * being destroyed anyway.
606 */
607 PyErr_Clear();
608 Py_XDECREF(py_method_result);
609 Py_DECREF(py_comp);
610 }
611
612 static
613 void component_class_source_finalize(bt_self_component_source *self_component_source)
614 {
615 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
616 component_class_finalize(self_component);
617 }
618
619 static
620 void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
621 {
622 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
623 component_class_finalize(self_component);
624 }
625
626 static
627 void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
628 {
629 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
630 component_class_finalize(self_component);
631 }
632
633 static
634 bt_bool component_class_can_seek_beginning(
635 bt_self_message_iterator *self_message_iterator)
636 {
637 PyObject *py_iter;
638 PyObject *py_result = NULL;
639 bt_bool can_seek_beginning = false;
640
641 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
642 BT_ASSERT(py_iter);
643
644 py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
645
646 BT_ASSERT(!py_result || PyBool_Check(py_result));
647
648 if (py_result) {
649 can_seek_beginning = PyObject_IsTrue(py_result);
650 } else {
651 /*
652 * Once can_seek_beginning can report errors, convert the
653 * exception to a status. For now, log and return false;
654 */
655 loge_exception_message_iterator(self_message_iterator);
656 PyErr_Clear();
657 }
658
659 Py_XDECREF(py_result);
660
661 return can_seek_beginning;
662 }
663
664 static
665 bt_component_class_message_iterator_seek_beginning_method_status
666 component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
667 {
668 PyObject *py_iter;
669 PyObject *py_result;
670 bt_component_class_message_iterator_seek_beginning_method_status status;
671
672 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
673 BT_ASSERT(py_iter);
674 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
675 NULL);
676 BT_ASSERT(!py_result || py_result == Py_None);
677 status = py_exc_to_status_message_iterator(self_message_iterator);
678 Py_XDECREF(py_result);
679 return status;
680 }
681
682 static
683 bt_component_class_port_connected_method_status component_class_port_connected(
684 bt_self_component *self_component,
685 void *self_component_port,
686 swig_type_info *self_component_port_swig_type,
687 bt_port_type self_component_port_type,
688 const void *other_port,
689 swig_type_info *other_port_swig_type)
690 {
691 bt_component_class_port_connected_method_status status;
692 PyObject *py_comp = NULL;
693 PyObject *py_self_port_ptr = NULL;
694 PyObject *py_other_port_ptr = NULL;
695 PyObject *py_method_result = NULL;
696
697 py_comp = bt_self_component_get_data(self_component);
698 BT_ASSERT(py_comp);
699 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
700 self_component_port_swig_type, 0);
701 if (!py_self_port_ptr) {
702 BT_LOGF_STR("Failed to create a SWIG pointer object.");
703 status = __BT_FUNC_STATUS_MEMORY_ERROR;
704 goto end;
705 }
706
707 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
708 other_port_swig_type, 0);
709 if (!py_other_port_ptr) {
710 BT_LOGF_STR("Failed to create a SWIG pointer object.");
711 status = __BT_FUNC_STATUS_MEMORY_ERROR;
712 goto end; }
713
714 py_method_result = PyObject_CallMethod(py_comp,
715 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
716 self_component_port_type, py_other_port_ptr);
717 BT_ASSERT(!py_method_result || py_method_result == Py_None);
718 status = py_exc_to_status_component(self_component);
719
720 end:
721 Py_XDECREF(py_self_port_ptr);
722 Py_XDECREF(py_other_port_ptr);
723 Py_XDECREF(py_method_result);
724 return status;
725 }
726
727 static
728 bt_component_class_port_connected_method_status
729 component_class_source_output_port_connected(
730 bt_self_component_source *self_component_source,
731 bt_self_component_port_output *self_component_port_output,
732 const bt_port_input *other_port_input)
733 {
734 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
735
736 return component_class_port_connected(
737 self_component,
738 self_component_port_output,
739 SWIGTYPE_p_bt_self_component_port_output,
740 BT_PORT_TYPE_OUTPUT,
741 other_port_input,
742 SWIGTYPE_p_bt_port_input);
743 }
744
745 static
746 bt_component_class_port_connected_method_status
747 component_class_filter_input_port_connected(
748 bt_self_component_filter *self_component_filter,
749 bt_self_component_port_input *self_component_port_input,
750 const bt_port_output *other_port_output)
751 {
752 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
753
754 return component_class_port_connected(
755 self_component,
756 self_component_port_input,
757 SWIGTYPE_p_bt_self_component_port_input,
758 BT_PORT_TYPE_INPUT,
759 other_port_output,
760 SWIGTYPE_p_bt_port_output);
761 }
762
763 static
764 bt_component_class_port_connected_method_status
765 component_class_filter_output_port_connected(
766 bt_self_component_filter *self_component_filter,
767 bt_self_component_port_output *self_component_port_output,
768 const bt_port_input *other_port_input)
769 {
770 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
771
772 return component_class_port_connected(
773 self_component,
774 self_component_port_output,
775 SWIGTYPE_p_bt_self_component_port_output,
776 BT_PORT_TYPE_OUTPUT,
777 other_port_input,
778 SWIGTYPE_p_bt_port_input);
779 }
780
781 static
782 bt_component_class_port_connected_method_status
783 component_class_sink_input_port_connected(
784 bt_self_component_sink *self_component_sink,
785 bt_self_component_port_input *self_component_port_input,
786 const bt_port_output *other_port_output)
787 {
788 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
789
790 return component_class_port_connected(
791 self_component,
792 self_component_port_input,
793 SWIGTYPE_p_bt_self_component_port_input,
794 BT_PORT_TYPE_INPUT,
795 other_port_output,
796 SWIGTYPE_p_bt_port_output);
797 }
798
799 static
800 bt_component_class_sink_graph_is_configured_method_status
801 component_class_sink_graph_is_configured(
802 bt_self_component_sink *self_component_sink)
803 {
804 PyObject *py_comp = NULL;
805 PyObject *py_method_result = NULL;
806 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
807 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
808
809 py_comp = bt_self_component_get_data(self_component);
810 py_method_result = PyObject_CallMethod(py_comp,
811 "_bt_graph_is_configured_from_native", NULL);
812 BT_ASSERT(!py_method_result || py_method_result == Py_None);
813 status = py_exc_to_status_component(self_component);
814 Py_XDECREF(py_method_result);
815 return status;
816 }
817
818 static
819 bt_component_class_query_method_status component_class_query(
820 const bt_component_class *component_class,
821 bt_self_component_class *self_component_class,
822 const bt_query_executor *query_executor,
823 const char *object, const bt_value *params,
824 bt_logging_level log_level,
825 const bt_value **result)
826 {
827 PyObject *py_cls = NULL;
828 PyObject *py_params_ptr = NULL;
829 PyObject *py_query_exec_ptr = NULL;
830 PyObject *py_query_func = NULL;
831 PyObject *py_object = NULL;
832 PyObject *py_results_addr = NULL;
833 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
834
835 py_cls = lookup_cc_ptr_to_py_cls(component_class);
836 if (!py_cls) {
837 BT_LOGE("Cannot find Python class associated to native component class: "
838 "comp-cls-addr=%p", component_class);
839 goto error;
840 }
841
842 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
843 SWIGTYPE_p_bt_value, 0);
844 if (!py_params_ptr) {
845 BT_LOGE_STR("Failed to create a SWIG pointer object.");
846 goto error;
847 }
848
849 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
850 SWIGTYPE_p_bt_query_executor, 0);
851 if (!py_query_exec_ptr) {
852 BT_LOGE_STR("Failed to create a SWIG pointer object.");
853 goto error;
854 }
855
856 py_object = SWIG_FromCharPtr(object);
857 if (!py_object) {
858 BT_LOGE_STR("Failed to create a Python string.");
859 goto error;
860 }
861
862 py_results_addr = PyObject_CallMethod(py_cls,
863 "_bt_query_from_native", "(OOOi)", py_query_exec_ptr,
864 py_object, py_params_ptr, (int) log_level);
865 if (!py_results_addr) {
866 BT_LOGW("Failed to call Python class's _bt_query_from_native() method: "
867 "py-cls-addr=%p", py_cls);
868 status = py_exc_to_status_component_class(self_component_class);
869 goto end;
870 }
871
872 /*
873 * The returned object, on success, is an integer object
874 * (PyLong) containing the address of a BT value object (new
875 * reference).
876 */
877 *result = PyLong_AsVoidPtr(py_results_addr);
878 BT_ASSERT(!PyErr_Occurred());
879 BT_ASSERT(*result);
880 goto end;
881
882 error:
883 PyErr_Clear();
884 status = __BT_FUNC_STATUS_ERROR;
885
886 end:
887 Py_XDECREF(py_params_ptr);
888 Py_XDECREF(py_query_exec_ptr);
889 Py_XDECREF(py_query_func);
890 Py_XDECREF(py_object);
891 Py_XDECREF(py_results_addr);
892 return status;
893 }
894
895 static
896 bt_component_class_query_method_status component_class_source_query(
897 bt_self_component_class_source *self_component_class_source,
898 const bt_query_executor *query_executor,
899 const char *object, const bt_value *params,
900 bt_logging_level log_level,
901 const bt_value **result)
902 {
903 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
904 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
905 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
906
907 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
908 }
909
910 static
911 bt_component_class_query_method_status component_class_filter_query(
912 bt_self_component_class_filter *self_component_class_filter,
913 const bt_query_executor *query_executor,
914 const char *object, const bt_value *params,
915 bt_logging_level log_level,
916 const bt_value **result)
917 {
918 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
919 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
920 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
921
922 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
923 }
924
925 static
926 bt_component_class_query_method_status component_class_sink_query(
927 bt_self_component_class_sink *self_component_class_sink,
928 const bt_query_executor *query_executor,
929 const char *object, const bt_value *params,
930 bt_logging_level log_level,
931 const bt_value **result)
932 {
933 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
934 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
935 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
936
937 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
938 }
939
940 static
941 bt_component_class_message_iterator_init_method_status
942 component_class_message_iterator_init(
943 bt_self_message_iterator *self_message_iterator,
944 bt_self_component *self_component,
945 bt_self_component_port_output *self_component_port_output)
946 {
947 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
948 PyObject *py_comp_cls = NULL;
949 PyObject *py_iter_cls = NULL;
950 PyObject *py_iter_ptr = NULL;
951 PyObject *py_component_port_output_ptr = NULL;
952 PyObject *py_init_method_result = NULL;
953 PyObject *py_iter = NULL;
954 PyObject *py_comp;
955
956 py_comp = bt_self_component_get_data(self_component);
957
958 /* Find user's Python message iterator class */
959 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
960 if (!py_comp_cls) {
961 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
962 goto python_error;
963 }
964
965 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
966 if (!py_iter_cls) {
967 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
968 goto python_error;
969 }
970
971 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
972 SWIGTYPE_p_bt_self_message_iterator, 0);
973 if (!py_iter_ptr) {
974 const char *err = "Failed to create a SWIG pointer object.";
975
976 BT_LOGE_STR(err);
977 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
978 self_message_iterator, err);
979 goto error;
980 }
981
982 /*
983 * Create object with borrowed native message iterator
984 * reference:
985 *
986 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
987 */
988 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
989 "(OO)", py_iter_cls, py_iter_ptr);
990 if (!py_iter) {
991 BT_LOGE("Failed to call Python class's __new__() method: "
992 "py-cls-addr=%p", py_iter_cls);
993 goto python_error;
994 }
995
996 /*
997 * Initialize object:
998 *
999 * py_iter.__init__(self_output_port)
1000 *
1001 * through the _init_for_native helper static method.
1002 *
1003 * At this point, py_iter._ptr is set, so this initialization
1004 * function has access to self._component (which gives it the
1005 * user Python component object from which the iterator was
1006 * created).
1007 */
1008 py_component_port_output_ptr = SWIG_NewPointerObj(
1009 SWIG_as_voidptr(self_component_port_output),
1010 SWIGTYPE_p_bt_self_component_port_output, 0);
1011 if (!py_component_port_output_ptr) {
1012 const char *err = "Failed to create a SWIG pointer object.";
1013
1014 BT_LOGE_STR(err);
1015 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1016 self_message_iterator, err);
1017 goto error;
1018 }
1019
1020 py_init_method_result = PyObject_CallMethod(py_iter,
1021 "_bt_init_from_native", "O", py_component_port_output_ptr);
1022 if (!py_init_method_result) {
1023 BT_LOGE_STR("User's __init__() method failed:");
1024 goto python_error;
1025 }
1026
1027 /*
1028 * Since the Python code can never instantiate a user-defined
1029 * message iterator class, the native message iterator
1030 * object does NOT belong to a user Python message iterator
1031 * object (borrowed reference). However this Python object is
1032 * owned by this native message iterator object.
1033 *
1034 * In the Python world, the lifetime of the native message
1035 * iterator is managed by a _GenericMessageIterator
1036 * instance:
1037 *
1038 * _GenericMessageIterator instance:
1039 * owns a native bt_message_iterator object (iter)
1040 * owns a _UserMessageIterator instance (py_iter)
1041 * self._ptr is a borrowed reference to the
1042 * native bt_private_connection_private_message_iterator
1043 * object (iter)
1044 */
1045 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1046 py_iter = NULL;
1047 goto end;
1048
1049 python_error:
1050 /* Handling of errors that cause a Python exception to be set. */
1051 status = py_exc_to_status_message_iterator(self_message_iterator);
1052 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
1053 goto end;
1054
1055 error:
1056 /* Handling of errors that don't cause a Python exception to be set. */
1057 status = __BT_FUNC_STATUS_ERROR;
1058
1059 end:
1060 BT_ASSERT(!PyErr_Occurred());
1061
1062 Py_XDECREF(py_comp_cls);
1063 Py_XDECREF(py_iter_cls);
1064 Py_XDECREF(py_iter_ptr);
1065 Py_XDECREF(py_component_port_output_ptr);
1066 Py_XDECREF(py_init_method_result);
1067 Py_XDECREF(py_iter);
1068 return status;
1069 }
1070
1071 static
1072 bt_component_class_message_iterator_init_method_status
1073 component_class_source_message_iterator_init(
1074 bt_self_message_iterator *self_message_iterator,
1075 bt_self_component_source *self_component_source,
1076 bt_self_component_port_output *self_component_port_output)
1077 {
1078 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
1079
1080 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1081 }
1082
1083 static
1084 bt_component_class_message_iterator_init_method_status
1085 component_class_filter_message_iterator_init(
1086 bt_self_message_iterator *self_message_iterator,
1087 bt_self_component_filter *self_component_filter,
1088 bt_self_component_port_output *self_component_port_output)
1089 {
1090 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
1091
1092 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1093 }
1094
1095 static
1096 void component_class_message_iterator_finalize(
1097 bt_self_message_iterator *message_iterator)
1098 {
1099 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1100 PyObject *py_method_result = NULL;
1101
1102 BT_ASSERT(py_message_iter);
1103
1104 /* Call user's _finalize() method */
1105 py_method_result = PyObject_CallMethod(py_message_iter,
1106 "_finalize", NULL);
1107
1108 if (PyErr_Occurred()) {
1109 BT_LOGW("User's _finalize() method raised an exception: ignoring:");
1110 logw_exception();
1111 }
1112
1113 /*
1114 * Ignore any exception raised by the _finalize() method because
1115 * it won't change anything at this point: the component is
1116 * being destroyed anyway.
1117 */
1118 PyErr_Clear();
1119 Py_XDECREF(py_method_result);
1120 Py_DECREF(py_message_iter);
1121 }
1122
1123 /* Valid for both sources and filters. */
1124
1125 static
1126 bt_component_class_message_iterator_next_method_status
1127 component_class_message_iterator_next(
1128 bt_self_message_iterator *message_iterator,
1129 bt_message_array_const msgs, uint64_t capacity,
1130 uint64_t *count)
1131 {
1132 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
1133 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1134 PyObject *py_method_result = NULL;
1135
1136 BT_ASSERT(py_message_iter);
1137 py_method_result = PyObject_CallMethod(py_message_iter,
1138 "_bt_next_from_native", NULL);
1139 if (!py_method_result) {
1140 status = py_exc_to_status_message_iterator(message_iterator);
1141 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
1142 goto end;
1143 }
1144
1145 /*
1146 * The returned object, on success, is an integer object
1147 * (PyLong) containing the address of a native message
1148 * object (which is now ours).
1149 */
1150 msgs[0] = PyLong_AsVoidPtr(py_method_result);
1151 *count = 1;
1152
1153 /* Clear potential overflow error; should never happen */
1154 BT_ASSERT(!PyErr_Occurred());
1155 goto end;
1156
1157 end:
1158 Py_XDECREF(py_method_result);
1159 return status;
1160 }
1161
1162 static
1163 bt_component_class_sink_consume_method_status
1164 component_class_sink_consume(bt_self_component_sink *self_component_sink)
1165 {
1166 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1167 PyObject *py_comp = bt_self_component_get_data(self_component);
1168 PyObject *py_method_result = NULL;
1169 bt_component_class_sink_consume_method_status status;
1170
1171 BT_ASSERT(py_comp);
1172 py_method_result = PyObject_CallMethod(py_comp,
1173 "_consume", NULL);
1174 status = py_exc_to_status_component(self_component);
1175 if (!py_method_result && status == __BT_FUNC_STATUS_OK) {
1176 /* Pretty sure this should never happen, but just in case */
1177 BT_LOGE("User's _consume() method failed without raising an exception: "
1178 "status=%d", status);
1179 status = __BT_FUNC_STATUS_ERROR;
1180 }
1181
1182 Py_XDECREF(py_method_result);
1183 return status;
1184 }
1185
1186 static
1187 int component_class_set_help_and_desc(
1188 bt_component_class *component_class,
1189 const char *description, const char *help)
1190 {
1191 int ret;
1192
1193 if (description) {
1194 ret = bt_component_class_set_description(component_class, description);
1195 if (ret) {
1196 BT_LOGE("Cannot set component class's description: "
1197 "comp-cls-addr=%p", component_class);
1198 goto end;
1199 }
1200 }
1201
1202 if (help) {
1203 ret = bt_component_class_set_help(component_class, help);
1204 if (ret) {
1205 BT_LOGE("Cannot set component class's help text: "
1206 "comp-cls-addr=%p", component_class);
1207 goto end;
1208 }
1209 }
1210
1211 ret = 0;
1212
1213 end:
1214 return ret;
1215 }
1216
1217 static
1218 bt_component_class_source *bt_bt2_component_class_source_create(
1219 PyObject *py_cls, const char *name, const char *description,
1220 const char *help)
1221 {
1222 bt_component_class_source *component_class_source;
1223 bt_component_class *component_class;
1224 int ret;
1225
1226 BT_ASSERT(py_cls);
1227 component_class_source = bt_component_class_source_create(name,
1228 component_class_message_iterator_next);
1229 if (!component_class_source) {
1230 BT_LOGE_STR("Cannot create source component class.");
1231 goto end;
1232 }
1233
1234 component_class = bt_component_class_source_as_component_class(component_class_source);
1235
1236 if (component_class_set_help_and_desc(component_class, description, help)) {
1237 goto end;
1238 }
1239
1240 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
1241 BT_ASSERT(ret == 0);
1242 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1243 BT_ASSERT(ret == 0);
1244 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
1245 component_class_can_seek_beginning);
1246 BT_ASSERT(ret == 0);
1247 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
1248 component_class_seek_beginning);
1249 BT_ASSERT(ret == 0);
1250 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1251 component_class_source_output_port_connected);
1252 BT_ASSERT(ret == 0);
1253 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1254 BT_ASSERT(ret == 0);
1255 ret = bt_component_class_source_set_message_iterator_init_method(
1256 component_class_source, component_class_source_message_iterator_init);
1257 BT_ASSERT(ret == 0);
1258 ret = bt_component_class_source_set_message_iterator_finalize_method(
1259 component_class_source, component_class_message_iterator_finalize);
1260 BT_ASSERT(ret == 0);
1261 register_cc_ptr_to_py_cls(component_class, py_cls);
1262
1263 end:
1264 return component_class_source;
1265 }
1266
1267 static
1268 bt_component_class_filter *bt_bt2_component_class_filter_create(
1269 PyObject *py_cls, const char *name, const char *description,
1270 const char *help)
1271 {
1272 bt_component_class *component_class;
1273 bt_component_class_filter *component_class_filter;
1274 int ret;
1275
1276 BT_ASSERT(py_cls);
1277 component_class_filter = bt_component_class_filter_create(name,
1278 component_class_message_iterator_next);
1279 if (!component_class_filter) {
1280 BT_LOGE_STR("Cannot create filter component class.");
1281 goto end;
1282 }
1283
1284 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1285
1286 if (component_class_set_help_and_desc(component_class, description, help)) {
1287 goto end;
1288 }
1289
1290 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
1291 BT_ASSERT(ret == 0);
1292 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1293 BT_ASSERT(ret == 0);
1294 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
1295 component_class_can_seek_beginning);
1296 BT_ASSERT(ret == 0);
1297 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
1298 component_class_seek_beginning);
1299 BT_ASSERT(ret == 0);
1300 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1301 component_class_filter_input_port_connected);
1302 BT_ASSERT(ret == 0);
1303 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1304 component_class_filter_output_port_connected);
1305 BT_ASSERT(ret == 0);
1306 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1307 BT_ASSERT(ret == 0);
1308 ret = bt_component_class_filter_set_message_iterator_init_method(
1309 component_class_filter, component_class_filter_message_iterator_init);
1310 BT_ASSERT(ret == 0);
1311 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1312 component_class_filter, component_class_message_iterator_finalize);
1313 BT_ASSERT(ret == 0);
1314 register_cc_ptr_to_py_cls(component_class, py_cls);
1315
1316 end:
1317 return component_class_filter;
1318 }
1319
1320 static
1321 bt_component_class_sink *bt_bt2_component_class_sink_create(
1322 PyObject *py_cls, const char *name, const char *description,
1323 const char *help)
1324 {
1325 bt_component_class_sink *component_class_sink;
1326 bt_component_class *component_class;
1327 int ret;
1328
1329 BT_ASSERT(py_cls);
1330 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1331
1332 if (!component_class_sink) {
1333 BT_LOGE_STR("Cannot create sink component class.");
1334 goto end;
1335 }
1336
1337 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1338
1339 if (component_class_set_help_and_desc(component_class, description, help)) {
1340 goto end;
1341 }
1342
1343 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
1344 BT_ASSERT(ret == 0);
1345 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1346 BT_ASSERT(ret == 0);
1347 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1348 component_class_sink_input_port_connected);
1349 BT_ASSERT(ret == 0);
1350 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1351 component_class_sink_graph_is_configured);
1352 BT_ASSERT(ret == 0);
1353 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1354 BT_ASSERT(ret == 0);
1355 register_cc_ptr_to_py_cls(component_class, py_cls);
1356
1357 end:
1358 return component_class_sink;
1359 }
1360 %}
1361
1362 struct bt_component_class_source *bt_bt2_component_class_source_create(
1363 PyObject *py_cls, const char *name, const char *description,
1364 const char *help);
1365 struct bt_component_class_filter *bt_bt2_component_class_filter_create(
1366 PyObject *py_cls, const char *name, const char *description,
1367 const char *help);
1368 struct bt_component_class_sink *bt_bt2_component_class_sink_create(
1369 PyObject *py_cls, const char *name, const char *description,
1370 const char *help);
1371 void bt_bt2_cc_init_from_bt2(void);
1372 void bt_bt2_cc_exit_handler(void);
This page took 0.104848 seconds and 3 git commands to generate.