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